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

Last change on this file since 2115 was 2115, checked in by Gubaer, 15 years ago

new: reading open changesets from the server
new: reading user info from the server
new: any open changeset can be used when uploading
new: generic dialog for closing changesets
fixed #3427: JOSM can't keep many changesets open at once
fixed #3408: Allow continuing opened changeset even after restarting JOSM
fixed #3476: Default selection in upload dialog should be different for unclosed changesets. (Upload dialog now looks different)

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 @Override
28 public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
29 // not implemented
30 return null;
31 }
32
33 protected String getAttribute(Node node, String name) {
34 return node.getAttributes().getNamedItem(name).getNodeValue();
35 }
36
37 protected 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(Long.parseLong(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").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 return userInfo;
114 } catch(XPathException e) {
115 throw new OsmDataParsingException(e);
116 }
117 }
118
119 public UserInfo fetchUserInfo(ProgressMonitor monitor) throws OsmTransferException {
120 try {
121 monitor.beginTask("Reading user info ...");
122 InputStream in = getInputStream("user/details", monitor.createSubTaskMonitor(1, true));
123 return buildFromXML(
124 DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in)
125 );
126 } catch(OsmTransferException e) {
127 throw e;
128 } catch(Exception e) {
129 throw new OsmTransferException(e);
130 } finally {
131 monitor.finishTask();
132 }
133 }
134}
Note: See TracBrowser for help on using the repository browser.