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

Last change on this file since 17356 was 16436, checked in by simon04, 4 years ago

see #19251 - Java 8: use Stream

  • Property svn:eol-style set to native
File size: 7.3 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.time.DateTimeException;
7import java.util.List;
8import java.util.stream.Collectors;
9import java.util.stream.IntStream;
10
11import javax.xml.xpath.XPath;
12import javax.xml.xpath.XPathConstants;
13import javax.xml.xpath.XPathException;
14import javax.xml.xpath.XPathFactory;
15
16import org.openstreetmap.josm.data.coor.LatLon;
17import org.openstreetmap.josm.data.osm.DataSet;
18import org.openstreetmap.josm.data.osm.UserInfo;
19import org.openstreetmap.josm.gui.progress.ProgressMonitor;
20import org.openstreetmap.josm.tools.UncheckedParseException;
21import org.openstreetmap.josm.tools.XmlParsingException;
22import org.openstreetmap.josm.tools.date.DateUtils;
23import org.w3c.dom.Document;
24import org.w3c.dom.Node;
25import org.w3c.dom.NodeList;
26
27/**
28 * Download and parse info of the logged in user (OSM API v0.6 "/user/details").
29 * @see <a href="https://wiki.openstreetmap.org/wiki/API_v0.6#Details_of_the_logged-in_user">/user/details</a>
30 */
31public class OsmServerUserInfoReader extends OsmServerReader {
32
33 /**
34 * Parses the given XML data and returns the associated user info.
35 * @param document The XML contents
36 * @return The user info
37 * @throws XmlParsingException if parsing goes wrong
38 */
39 public static UserInfo buildFromXML(Document document) throws XmlParsingException {
40 try {
41 XPathFactory factory = XPathFactory.newInstance();
42 XPath xpath = factory.newXPath();
43 UserInfo userInfo = new UserInfo();
44 Node xmlNode = (Node) xpath.compile("/osm/user[1]").evaluate(document, XPathConstants.NODE);
45 if (xmlNode == null)
46 throw new XmlParsingException(tr("XML tag <user> is missing."));
47
48 // -- id
49 String v = getAttribute(xmlNode, "id");
50 if (v == null)
51 throw new XmlParsingException(tr("Missing attribute ''{0}'' on XML tag ''{1}''.", "id", "user"));
52 try {
53 userInfo.setId(Integer.parseInt(v));
54 } catch (NumberFormatException e) {
55 throw new XmlParsingException(tr("Illegal value for attribute ''{0}'' on XML tag ''{1}''. Got {2}.", "id", "user", v), e);
56 }
57 // -- display name
58 v = getAttribute(xmlNode, "display_name");
59 userInfo.setDisplayName(v);
60 // -- account_created
61 v = getAttribute(xmlNode, "account_created");
62 if (v != null) {
63 userInfo.setAccountCreated(DateUtils.fromString(v));
64 }
65 // -- description
66 xmlNode = (Node) xpath.compile("/osm/user[1]/description[1]/text()").evaluate(document, XPathConstants.NODE);
67 if (xmlNode != null) {
68 userInfo.setDescription(xmlNode.getNodeValue());
69 }
70 // -- home
71 xmlNode = (Node) xpath.compile("/osm/user[1]/home").evaluate(document, XPathConstants.NODE);
72 if (xmlNode != null) {
73 v = getAttribute(xmlNode, "lat");
74 if (v == null)
75 throw new XmlParsingException(tr("Missing attribute ''{0}'' on XML tag ''{1}''.", "lat", "home"));
76 double lat;
77 try {
78 lat = Double.parseDouble(v);
79 } catch (NumberFormatException e) {
80 throw new XmlParsingException(tr("Illegal value for attribute ''{0}'' on XML tag ''{1}''. Got {2}.",
81 "lat", "home", v), e);
82 }
83
84 v = getAttribute(xmlNode, "lon");
85 if (v == null)
86 throw new XmlParsingException(tr("Missing attribute ''{0}'' on XML tag ''{1}''.", "lon", "home"));
87 double lon;
88 try {
89 lon = Double.parseDouble(v);
90 } catch (NumberFormatException e) {
91 throw new XmlParsingException(tr("Illegal value for attribute ''{0}'' on XML tag ''{1}''. Got {2}.",
92 "lon", "home", v), e);
93 }
94
95 v = getAttribute(xmlNode, "zoom");
96 if (v == null)
97 throw new XmlParsingException(tr("Missing attribute ''{0}'' on XML tag ''{1}''.", "zoom", "home"));
98 int zoom;
99 try {
100 zoom = Integer.parseInt(v);
101 } catch (NumberFormatException e) {
102 throw new XmlParsingException(tr("Illegal value for attribute ''{0}'' on XML tag ''{1}''. Got {2}.",
103 "zoom", "home", v), e);
104 }
105 userInfo.setHome(new LatLon(lat, lon));
106 userInfo.setHomeZoom(zoom);
107 }
108
109 // -- language list
110 NodeList xmlNodeList = (NodeList) xpath.compile("/osm/user[1]/languages[1]/lang/text()").evaluate(document, XPathConstants.NODESET);
111 if (xmlNodeList != null) {
112 List<String> languages = IntStream.range(0, xmlNodeList.getLength())
113 .mapToObj(i -> xmlNodeList.item(i).getNodeValue())
114 .collect(Collectors.toList());
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 | UncheckedParseException | DateTimeException 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 return fetchData("user/details", tr("Reading user info ..."),
171 OsmServerUserInfoReader::buildFromXML, monitor, reason);
172 }
173}
Note: See TracBrowser for help on using the repository browser.