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

Last change on this file since 2646 was 2512, checked in by stoecker, 14 years ago

i18n updated, fixed files to reduce problems when applying patches, fix #4017

File size: 5.7 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.DateUtils;
21import org.w3c.dom.Document;
22import org.w3c.dom.Node;
23import org.w3c.dom.NodeList;
24
25public class OsmServerUserInfoReader extends OsmServerReader {
26
27 public OsmServerUserInfoReader() {
28 setDoAuthenticate(true);
29 }
30
31 @Override
32 public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
33 // not implemented
34 return null;
35 }
36
37 protected String getAttribute(Node node, String name) {
38 return node.getAttributes().getNamedItem(name).getNodeValue();
39 }
40
41 protected UserInfo buildFromXML(Document document) throws OsmDataParsingException{
42 try {
43 XPathFactory factory = XPathFactory.newInstance();
44 XPath xpath = factory.newXPath();
45 UserInfo userInfo = new UserInfo();
46 Node xmlNode = (Node)xpath.compile("/osm/user[1]").evaluate(document, XPathConstants.NODE);
47 if ( xmlNode== null)
48 throw new OsmDataParsingException(tr("XML tag <user> is missing."));
49
50 // -- id
51 String v = getAttribute(xmlNode, "id");
52 if (v == null)
53 throw new OsmDataParsingException(tr("Missing attribute ''{0}'' on XML tag ''{1}''.", "id", "user"));
54 try {
55 userInfo.setId(Long.parseLong(v));
56 } catch(NumberFormatException e) {
57 throw new OsmDataParsingException(tr("Illegal value for attribute ''{0}'' on XML tag ''{1}''. Got {2}.", "id", "user", v));
58 }
59 // -- display name
60 v = getAttribute(xmlNode, "display_name");
61 userInfo.setDisplayName(v);
62 // -- account_created
63 v = getAttribute(xmlNode, "account_created");
64 if (v!=null) {
65 userInfo.setAccountCreated(DateUtils.fromString(v));
66 }
67 // -- description
68 xmlNode = (Node)xpath.compile("/osm/user[1]/description[1]/text()").evaluate(document, XPathConstants.NODE);
69 if (xmlNode != null) {
70 userInfo.setDescription(xmlNode.getNodeValue());
71 }
72 // -- home
73 xmlNode = (Node)xpath.compile("/osm/user[1]/home").evaluate(document, XPathConstants.NODE);
74 if (xmlNode != null) {
75 v = getAttribute(xmlNode, "lat");
76 if (v == null)
77 throw new OsmDataParsingException(tr("Missing attribute ''{0}'' on XML tag ''{1}''.", "lat", "home"));
78 double lat;
79 try {
80 lat = Double.parseDouble(v);
81 } catch(NumberFormatException e) {
82 throw new OsmDataParsingException(tr("Illegal value for attribute ''{0}'' on XML tag ''{1}''. Got {2}.", "lat", "home", v));
83 }
84
85 v = getAttribute(xmlNode, "lon");
86 if (v == null)
87 throw new OsmDataParsingException(tr("Missing attribute ''{0}'' on XML tag ''{1}''.", "lon", "home"));
88 double lon;
89 try {
90 lon = Double.parseDouble(v);
91 } catch(NumberFormatException e) {
92 throw new OsmDataParsingException(tr("Illegal value for attribute ''{0}'' on XML tag ''{1}''. Got {2}.", "lon", "home", v));
93 }
94
95 v = getAttribute(xmlNode, "zoom");
96 if (v == null)
97 throw new OsmDataParsingException(tr("Missing attribute ''{0}'' on XML tag ''{1}''.", "zoom", "home"));
98 int zoom;
99 try {
100 zoom = Integer.parseInt(v);
101 } catch(NumberFormatException e) {
102 throw new OsmDataParsingException(tr("Illegal value for attribute ''{0}'' on XML tag ''{1}''. Got {2}.", "zoom", "home", v));
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").evaluate(document, XPathConstants.NODESET);
110 if (xmlNodeList != null) {
111 List<String> languages = new LinkedList<String>();
112 for (int i=0; i < xmlNodeList.getLength(); i++) {
113 languages.add(xmlNodeList.item(i).getNodeValue());
114 }
115 userInfo.setLanguages(languages);
116 }
117 return userInfo;
118 } catch(XPathException e) {
119 throw new OsmDataParsingException(e);
120 }
121 }
122
123 public UserInfo fetchUserInfo(ProgressMonitor monitor) throws OsmTransferException {
124 try {
125 monitor.beginTask("Reading user info ...");
126 InputStream in = getInputStream("user/details", monitor.createSubTaskMonitor(1, true));
127 return buildFromXML(
128 DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in)
129 );
130 } catch(OsmTransferException e) {
131 throw e;
132 } catch(Exception e) {
133 throw new OsmTransferException(e);
134 } finally {
135 monitor.finishTask();
136 }
137 }
138}
Note: See TracBrowser for help on using the repository browser.