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.util.HashMap;
|
---|
7 | import java.util.Map;
|
---|
8 |
|
---|
9 | import javax.xml.xpath.XPath;
|
---|
10 | import javax.xml.xpath.XPathConstants;
|
---|
11 | import javax.xml.xpath.XPathException;
|
---|
12 | import javax.xml.xpath.XPathFactory;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
15 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
16 | import org.openstreetmap.josm.tools.XmlParsingException;
|
---|
17 | import org.w3c.dom.Document;
|
---|
18 | import org.w3c.dom.Node;
|
---|
19 | import org.w3c.dom.NodeList;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Download and parse preferences of the logged in user (OSM API v0.6 "/user/preferences").
|
---|
23 | * @see <a href="https://wiki.openstreetmap.org/wiki/API_v0.6#Preferences_of_the_logged-in_user">/user/preferences</a>
|
---|
24 | * @since 12502
|
---|
25 | */
|
---|
26 | public class OsmServerUserPreferencesReader extends OsmServerReader {
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Parses the given XML data and returns the associated user preferences.
|
---|
30 | * @param document The XML contents
|
---|
31 | * @return The user preferences
|
---|
32 | * @throws XmlParsingException if parsing goes wrong
|
---|
33 | */
|
---|
34 | public static Map<String, String> buildFromXML(Document document) throws XmlParsingException {
|
---|
35 | try {
|
---|
36 | XPath xpath = XPathFactory.newInstance().newXPath();
|
---|
37 | Map<String, String> result = new HashMap<>();
|
---|
38 |
|
---|
39 | // -- preferences
|
---|
40 | NodeList xmlNodeList = (NodeList) xpath.compile("/osm/preferences/preference").evaluate(document, XPathConstants.NODESET);
|
---|
41 | if (xmlNodeList != null) {
|
---|
42 | for (int i = 0; i < xmlNodeList.getLength(); i++) {
|
---|
43 | Node xmlNode = xmlNodeList.item(i);
|
---|
44 | String k = getAttribute(xmlNode, "k");
|
---|
45 | if (k == null)
|
---|
46 | throw new XmlParsingException(tr("Missing attribute ''{0}'' on XML tag ''{1}''.", "k", "preference"));
|
---|
47 | String v = getAttribute(xmlNode, "v");
|
---|
48 | if (v == null)
|
---|
49 | throw new XmlParsingException(tr("Missing attribute ''{0}'' on XML tag ''{1}''.", "v", "preference"));
|
---|
50 | result.put(k, v);
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | return result;
|
---|
55 | } catch (XPathException e) {
|
---|
56 | throw new XmlParsingException(e);
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Constructs a new {@code OsmServerUserInfoReader}.
|
---|
62 | */
|
---|
63 | public OsmServerUserPreferencesReader() {
|
---|
64 | setDoAuthenticate(true);
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
69 | // not implemented
|
---|
70 | return null;
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Fetches user preferences, without explicit reason.
|
---|
75 | * @param monitor The progress monitor
|
---|
76 | * @return The user preferences
|
---|
77 | * @throws OsmTransferException if something goes wrong
|
---|
78 | */
|
---|
79 | public Map<String, String> fetchUserPreferences(ProgressMonitor monitor) throws OsmTransferException {
|
---|
80 | return fetchUserPreferences(monitor, null);
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Fetches user info, with an explicit reason.
|
---|
85 | * @param monitor The progress monitor
|
---|
86 | * @param reason The reason to show on console. Can be {@code null} if no reason is given
|
---|
87 | * @return The user info
|
---|
88 | * @throws OsmTransferException if something goes wrong
|
---|
89 | */
|
---|
90 | public Map<String, String> fetchUserPreferences(ProgressMonitor monitor, String reason) throws OsmTransferException {
|
---|
91 | return fetchData("user/preferences", tr("Reading user preferences ..."),
|
---|
92 | OsmServerUserPreferencesReader::buildFromXML, monitor, reason);
|
---|
93 | }
|
---|
94 | }
|
---|