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