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

Last change on this file since 12846 was 12510, checked in by Don-vip, 7 years ago

avoid code duplication

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