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