source: josm/trunk/src/org/openstreetmap/josm/io/OsmServerUserInfoReader.java@ 12470

Last change on this file since 12470 was 12470, checked in by bastiK, 7 years ago

see #14794 - javadoc

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