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