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

Last change on this file since 10171 was 8540, checked in by Don-vip, 9 years ago

fix remaining checkstyle issues

  • Property svn:eol-style set to native
File size: 7.6 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.InputStream;
7import java.util.LinkedList;
8import java.util.List;
9
10import javax.xml.parsers.DocumentBuilderFactory;
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.XmlParsingException;
21import org.openstreetmap.josm.tools.date.DateUtils;
22import org.w3c.dom.Document;
23import org.w3c.dom.Node;
24import org.w3c.dom.NodeList;
25
26public 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}
Note: See TracBrowser for help on using the repository browser.