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

Last change on this file since 12806 was 12510, checked in by Don-vip, 7 years ago

avoid code duplication

  • Property svn:eol-style set to native
File size: 3.5 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.util.HashMap;
7import java.util.Map;
8
9import javax.xml.xpath.XPath;
10import javax.xml.xpath.XPathConstants;
11import javax.xml.xpath.XPathException;
12import javax.xml.xpath.XPathFactory;
13
14import org.openstreetmap.josm.data.osm.DataSet;
15import org.openstreetmap.josm.gui.progress.ProgressMonitor;
16import org.openstreetmap.josm.tools.XmlParsingException;
17import org.w3c.dom.Document;
18import org.w3c.dom.Node;
19import 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 */
26public 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}
Note: See TracBrowser for help on using the repository browser.