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