1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.InputStream;
|
---|
7 | import java.util.LinkedList;
|
---|
8 | import java.util.List;
|
---|
9 |
|
---|
10 | import javax.xml.parsers.DocumentBuilderFactory;
|
---|
11 | import javax.xml.xpath.XPath;
|
---|
12 | import javax.xml.xpath.XPathConstants;
|
---|
13 | import javax.xml.xpath.XPathException;
|
---|
14 | import javax.xml.xpath.XPathFactory;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
17 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
18 | import org.openstreetmap.josm.data.osm.UserInfo;
|
---|
19 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
20 | import org.openstreetmap.josm.tools.XmlParsingException;
|
---|
21 | import org.openstreetmap.josm.tools.date.DateUtils;
|
---|
22 | import org.w3c.dom.Document;
|
---|
23 | import org.w3c.dom.Node;
|
---|
24 | import org.w3c.dom.NodeList;
|
---|
25 |
|
---|
26 | public class OsmServerUserInfoReader extends OsmServerReader {
|
---|
27 |
|
---|
28 | protected static String getAttribute(Node node, String name) {
|
---|
29 | return node.getAttributes().getNamedItem(name).getNodeValue();
|
---|
30 | }
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Parses the given XML data and returns the associated user info.
|
---|
34 | * @param document The XML contents
|
---|
35 | * @return The user info
|
---|
36 | * @throws XmlParsingException if parsing goes wrong
|
---|
37 | */
|
---|
38 | public static UserInfo buildFromXML(Document document) throws XmlParsingException {
|
---|
39 | try {
|
---|
40 | XPathFactory factory = XPathFactory.newInstance();
|
---|
41 | XPath xpath = factory.newXPath();
|
---|
42 | UserInfo userInfo = new UserInfo();
|
---|
43 | Node xmlNode = (Node) xpath.compile("/osm/user[1]").evaluate(document, XPathConstants.NODE);
|
---|
44 | if (xmlNode == null)
|
---|
45 | throw new XmlParsingException(tr("XML tag <user> is missing."));
|
---|
46 |
|
---|
47 | // -- id
|
---|
48 | String v = getAttribute(xmlNode, "id");
|
---|
49 | if (v == null)
|
---|
50 | throw new XmlParsingException(tr("Missing attribute ''{0}'' on XML tag ''{1}''.", "id", "user"));
|
---|
51 | try {
|
---|
52 | userInfo.setId(Integer.parseInt(v));
|
---|
53 | } catch (NumberFormatException e) {
|
---|
54 | throw new XmlParsingException(tr("Illegal value for attribute ''{0}'' on XML tag ''{1}''. Got {2}.", "id", "user", v), e);
|
---|
55 | }
|
---|
56 | // -- display name
|
---|
57 | v = getAttribute(xmlNode, "display_name");
|
---|
58 | userInfo.setDisplayName(v);
|
---|
59 | // -- account_created
|
---|
60 | v = getAttribute(xmlNode, "account_created");
|
---|
61 | if (v != null) {
|
---|
62 | userInfo.setAccountCreated(DateUtils.fromString(v));
|
---|
63 | }
|
---|
64 | // -- description
|
---|
65 | xmlNode = (Node) xpath.compile("/osm/user[1]/description[1]/text()").evaluate(document, XPathConstants.NODE);
|
---|
66 | if (xmlNode != null) {
|
---|
67 | userInfo.setDescription(xmlNode.getNodeValue());
|
---|
68 | }
|
---|
69 | // -- home
|
---|
70 | xmlNode = (Node) xpath.compile("/osm/user[1]/home").evaluate(document, XPathConstants.NODE);
|
---|
71 | if (xmlNode != null) {
|
---|
72 | v = getAttribute(xmlNode, "lat");
|
---|
73 | if (v == null)
|
---|
74 | throw new XmlParsingException(tr("Missing attribute ''{0}'' on XML tag ''{1}''.", "lat", "home"));
|
---|
75 | double lat;
|
---|
76 | try {
|
---|
77 | lat = Double.parseDouble(v);
|
---|
78 | } catch (NumberFormatException e) {
|
---|
79 | throw new XmlParsingException(tr("Illegal value for attribute ''{0}'' on XML tag ''{1}''. Got {2}.",
|
---|
80 | "lat", "home", v), e);
|
---|
81 | }
|
---|
82 |
|
---|
83 | v = getAttribute(xmlNode, "lon");
|
---|
84 | if (v == null)
|
---|
85 | throw new XmlParsingException(tr("Missing attribute ''{0}'' on XML tag ''{1}''.", "lon", "home"));
|
---|
86 | double lon;
|
---|
87 | try {
|
---|
88 | lon = Double.parseDouble(v);
|
---|
89 | } catch (NumberFormatException e) {
|
---|
90 | throw new XmlParsingException(tr("Illegal value for attribute ''{0}'' on XML tag ''{1}''. Got {2}.",
|
---|
91 | "lon", "home", v), e);
|
---|
92 | }
|
---|
93 |
|
---|
94 | v = getAttribute(xmlNode, "zoom");
|
---|
95 | if (v == null)
|
---|
96 | throw new XmlParsingException(tr("Missing attribute ''{0}'' on XML tag ''{1}''.", "zoom", "home"));
|
---|
97 | int zoom;
|
---|
98 | try {
|
---|
99 | zoom = Integer.parseInt(v);
|
---|
100 | } catch (NumberFormatException e) {
|
---|
101 | throw new XmlParsingException(tr("Illegal value for attribute ''{0}'' on XML tag ''{1}''. Got {2}.",
|
---|
102 | "zoom", "home", v), e);
|
---|
103 | }
|
---|
104 | userInfo.setHome(new LatLon(lat, lon));
|
---|
105 | userInfo.setHomeZoom(zoom);
|
---|
106 | }
|
---|
107 |
|
---|
108 | // -- language list
|
---|
109 | NodeList xmlNodeList = (NodeList) xpath.compile("/osm/user[1]/languages[1]/lang/text()").evaluate(document, XPathConstants.NODESET);
|
---|
110 | if (xmlNodeList != null) {
|
---|
111 | List<String> languages = new LinkedList<>();
|
---|
112 | for (int i = 0; i < xmlNodeList.getLength(); i++) {
|
---|
113 | languages.add(xmlNodeList.item(i).getNodeValue());
|
---|
114 | }
|
---|
115 | userInfo.setLanguages(languages);
|
---|
116 | }
|
---|
117 |
|
---|
118 | // -- messages
|
---|
119 | xmlNode = (Node) xpath.compile("/osm/user[1]/messages/received").evaluate(document, XPathConstants.NODE);
|
---|
120 | if (xmlNode != null) {
|
---|
121 | v = getAttribute(xmlNode, "unread");
|
---|
122 | if (v == null)
|
---|
123 | throw new XmlParsingException(tr("Missing attribute ''{0}'' on XML tag ''{1}''.", "unread", "received"));
|
---|
124 | try {
|
---|
125 | userInfo.setUnreadMessages(Integer.parseInt(v));
|
---|
126 | } catch (NumberFormatException e) {
|
---|
127 | throw new XmlParsingException(
|
---|
128 | tr("Illegal value for attribute ''{0}'' on XML tag ''{1}''. Got {2}.", "unread", "received", v), e);
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | return userInfo;
|
---|
133 | } catch (XPathException e) {
|
---|
134 | throw new XmlParsingException(e);
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Constructs a new {@code OsmServerUserInfoReader}.
|
---|
140 | */
|
---|
141 | public OsmServerUserInfoReader() {
|
---|
142 | setDoAuthenticate(true);
|
---|
143 | }
|
---|
144 |
|
---|
145 | @Override
|
---|
146 | public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
147 | // not implemented
|
---|
148 | return null;
|
---|
149 | }
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Fetches user info, without explicit reason.
|
---|
153 | * @param monitor The progress monitor
|
---|
154 | * @return The user info
|
---|
155 | * @throws OsmTransferException if something goes wrong
|
---|
156 | */
|
---|
157 | public UserInfo fetchUserInfo(ProgressMonitor monitor) throws OsmTransferException {
|
---|
158 | return fetchUserInfo(monitor, null);
|
---|
159 | }
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Fetches user info, with an explicit reason.
|
---|
163 | * @param monitor The progress monitor
|
---|
164 | * @param reason The reason to show on console. Can be {@code null} if no reason is given
|
---|
165 | * @return The user info
|
---|
166 | * @throws OsmTransferException if something goes wrong
|
---|
167 | * @since 6695
|
---|
168 | */
|
---|
169 | public UserInfo fetchUserInfo(ProgressMonitor monitor, String reason) throws OsmTransferException {
|
---|
170 | try {
|
---|
171 | monitor.beginTask("");
|
---|
172 | monitor.indeterminateSubTask(tr("Reading user info ..."));
|
---|
173 | try (InputStream in = getInputStream("user/details", monitor.createSubTaskMonitor(1, true), reason)) {
|
---|
174 | return buildFromXML(
|
---|
175 | DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in)
|
---|
176 | );
|
---|
177 | }
|
---|
178 | } catch (OsmTransferException e) {
|
---|
179 | throw e;
|
---|
180 | } catch (Exception e) {
|
---|
181 | throw new OsmTransferException(e);
|
---|
182 | } finally {
|
---|
183 | monitor.finishTask();
|
---|
184 | }
|
---|
185 | }
|
---|
186 | }
|
---|