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

Last change on this file since 8337 was 7509, checked in by stoecker, 10 years ago

remove tabs

  • Property svn:eol-style set to native
File size: 7.4 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));
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}.", "lat", "home", v));
80 }
81
82 v = getAttribute(xmlNode, "lon");
83 if (v == null)
84 throw new XmlParsingException(tr("Missing attribute ''{0}'' on XML tag ''{1}''.", "lon", "home"));
85 double lon;
86 try {
87 lon = Double.parseDouble(v);
88 } catch(NumberFormatException e) {
89 throw new XmlParsingException(tr("Illegal value for attribute ''{0}'' on XML tag ''{1}''. Got {2}.", "lon", "home", v));
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}.", "zoom", "home", v));
100 }
101 userInfo.setHome(new LatLon(lat,lon));
102 userInfo.setHomeZoom(zoom);
103 }
104
105 // -- language list
106 NodeList xmlNodeList = (NodeList)xpath.compile("/osm/user[1]/languages[1]/lang/text()").evaluate(document, XPathConstants.NODESET);
107 if (xmlNodeList != null) {
108 List<String> languages = new LinkedList<>();
109 for (int i=0; i < xmlNodeList.getLength(); i++) {
110 languages.add(xmlNodeList.item(i).getNodeValue());
111 }
112 userInfo.setLanguages(languages);
113 }
114
115 // -- messages
116 xmlNode = (Node)xpath.compile("/osm/user[1]/messages/received").evaluate(document, XPathConstants.NODE);
117 if (xmlNode != null) {
118 v = getAttribute(xmlNode, "unread");
119 if (v == null)
120 throw new XmlParsingException(tr("Missing attribute ''{0}'' on XML tag ''{1}''.", "unread", "received"));
121 try {
122 userInfo.setUnreadMessages(Integer.parseInt(v));
123 } catch(NumberFormatException e) {
124 throw new XmlParsingException(tr("Illegal value for attribute ''{0}'' on XML tag ''{1}''. Got {2}.", "unread", "received", v), e);
125 }
126 }
127
128 return userInfo;
129 } catch(XPathException e) {
130 throw new XmlParsingException(e);
131 }
132 }
133
134 /**
135 * Constructs a new {@code OsmServerUserInfoReader}.
136 */
137 public OsmServerUserInfoReader() {
138 setDoAuthenticate(true);
139 }
140
141 @Override
142 public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
143 // not implemented
144 return null;
145 }
146
147 /**
148 * Fetches user info, without explicit reason.
149 * @param monitor The progress monitor
150 * @return The user info
151 * @throws OsmTransferException if something goes wrong
152 */
153 public UserInfo fetchUserInfo(ProgressMonitor monitor) throws OsmTransferException {
154 return fetchUserInfo(monitor, null);
155 }
156
157 /**
158 * Fetches user info, with an explicit reason.
159 * @param monitor The progress monitor
160 * @param reason The reason to show on console. Can be {@code null} if no reason is given
161 * @return The user info
162 * @throws OsmTransferException if something goes wrong
163 * @since 6695
164 */
165 public UserInfo fetchUserInfo(ProgressMonitor monitor, String reason) throws OsmTransferException {
166 try {
167 monitor.beginTask("");
168 monitor.indeterminateSubTask(tr("Reading user info ..."));
169 try (InputStream in = getInputStream("user/details", monitor.createSubTaskMonitor(1, true), reason)) {
170 return buildFromXML(
171 DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in)
172 );
173 }
174 } catch(OsmTransferException e) {
175 throw e;
176 } catch(Exception e) {
177 throw new OsmTransferException(e);
178 } finally {
179 monitor.finishTask();
180 }
181 }
182}
Note: See TracBrowser for help on using the repository browser.