source: josm/trunk/src/org/openstreetmap/josm/io/OsmServerUserPreferencesReader.java@ 12502

Last change on this file since 12502 was 12502, checked in by Don-vip, 8 years ago

see #15057 - add new class to fetch user preferences from OSM API

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