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

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

remove extra whitespaces

  • Property svn:eol-style set to native
File size: 7.5 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(tr("Illegal value for attribute ''{0}'' on XML tag ''{1}''. Got {2}.", "unread", "received", v), e);
128 }
129 }
130
131 return userInfo;
132 } catch(XPathException e) {
133 throw new XmlParsingException(e);
134 }
135 }
136
137 /**
138 * Constructs a new {@code OsmServerUserInfoReader}.
139 */
140 public OsmServerUserInfoReader() {
141 setDoAuthenticate(true);
142 }
143
144 @Override
145 public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
146 // not implemented
147 return null;
148 }
149
150 /**
151 * Fetches user info, without explicit reason.
152 * @param monitor The progress monitor
153 * @return The user info
154 * @throws OsmTransferException if something goes wrong
155 */
156 public UserInfo fetchUserInfo(ProgressMonitor monitor) throws OsmTransferException {
157 return fetchUserInfo(monitor, null);
158 }
159
160 /**
161 * Fetches user info, with an explicit reason.
162 * @param monitor The progress monitor
163 * @param reason The reason to show on console. Can be {@code null} if no reason is given
164 * @return The user info
165 * @throws OsmTransferException if something goes wrong
166 * @since 6695
167 */
168 public UserInfo fetchUserInfo(ProgressMonitor monitor, String reason) throws OsmTransferException {
169 try {
170 monitor.beginTask("");
171 monitor.indeterminateSubTask(tr("Reading user info ..."));
172 try (InputStream in = getInputStream("user/details", monitor.createSubTaskMonitor(1, true), reason)) {
173 return buildFromXML(
174 DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in)
175 );
176 }
177 } catch(OsmTransferException e) {
178 throw e;
179 } catch(Exception e) {
180 throw new OsmTransferException(e);
181 } finally {
182 monitor.finishTask();
183 }
184 }
185}
Note: See TracBrowser for help on using the repository browser.