source: josm/trunk/src/org/openstreetmap/josm/io/Capabilities.java@ 11575

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

capabilities: update javadoc to new default values, code cleanup

  • Property svn:eol-style set to native
File size: 9.3 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.util.ArrayList;
8import java.util.Collections;
9import java.util.HashMap;
10import java.util.List;
11import java.util.Map;
12
13import javax.xml.parsers.ParserConfigurationException;
14
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.tools.Utils;
17import org.xml.sax.Attributes;
18import org.xml.sax.InputSource;
19import org.xml.sax.SAXException;
20import org.xml.sax.helpers.DefaultHandler;
21
22/**
23 * Represents the OSM API server capabilities.
24 *
25 * Example capabilites document:
26 * <pre>
27 * &lt;osm version="0.6" generator="OpenStreetMap server"&gt;
28 * &lt;api&gt;
29 * &lt;version minimum="0.6" maximum="0.6"/&gt;
30 * &lt;area maximum="0.25"/&gt;
31 * &lt;tracepoints per_page="5000"/&gt;
32 * &lt;waynodes maximum="2000"/&gt;
33 * &lt;changesets maximum_elements="10000"/&gt;
34 * &lt;timeout seconds="300"/&gt;
35 * &lt;status database="online" api="online" gpx="online"/&gt;
36 * &lt;/api&gt;
37 * &lt;policy&gt;
38 * &lt;imagery&gt;
39 * &lt;blacklist regex=".*\.google\.com/.*"/&gt;
40 * &lt;blacklist regex=".*209\.85\.2\d\d.*"/&gt;
41 * &lt;blacklist regex=".*209\.85\.1[3-9]\d.*"/&gt;
42 * &lt;blacklist regex=".*209\.85\.12[89].*"/&gt;
43 * &lt;/imagery&gt;
44 * &lt;/policy&gt;
45 * &lt;/osm&gt;
46 * </pre>
47 * This class is used in conjunction with a very primitive parser
48 * and simply stuffs the each tag and its attributes into a hash
49 * of hashes, with the exception of the "blacklist" tag which gets
50 * a list of its own. The DOM hierarchy is disregarded.
51 */
52public class Capabilities {
53
54 private final Map<String, Map<String, String>> capabilities;
55 private final List<String> imageryBlacklist;
56
57 /**
58 * Constructs new {@code Capabilities}.
59 */
60 public Capabilities() {
61 capabilities = new HashMap<>();
62 imageryBlacklist = new ArrayList<>();
63 }
64
65 /**
66 * Determines if given element and attribute are defined.
67 *
68 * @param element the name of the element
69 * @param attribute the name of the attribute
70 * @return {@code true} if defined, {@code false} otherwise
71 */
72 public boolean isDefined(String element, String attribute) {
73 if (!capabilities.containsKey(element)) return false;
74 Map<String, String> e = capabilities.get(element);
75 if (e == null) return false;
76 return e.get(attribute) != null;
77 }
78
79 /**
80 * Returns the value of configuration item in the capabilities as string value.
81 *
82 * @param element the name of the element
83 * @param attribute the name of the attribute
84 * @return the value; {@code null}, if the respective configuration item does not exist
85 */
86 public String get(String element, String attribute) {
87 if (!capabilities.containsKey(element)) return null;
88 Map<String, String> e = capabilities.get(element);
89 if (e == null) return null;
90 return e.get(attribute);
91 }
92
93 /**
94 * Returns the value of configuration item in the capabilities as double value.
95 *
96 * @param element the name of the element
97 * @param attribute the name of the attribute
98 * @return the value; {@code null}, if the respective configuration item does not exist
99 * @throws NumberFormatException if the value is not a valid double
100 */
101 public Double getDouble(String element, String attribute) {
102 String s = get(element, attribute);
103 if (s == null) return null;
104 return Double.valueOf(s);
105 }
106
107 /**
108 * Returns the value of configuration item in the capabilities as long value.
109 *
110 * @param element the name of the element
111 * @param attribute the name of the attribute
112 * @return the value; {@code null}, if the respective configuration item does not exist
113 * @throws NumberFormatException if the value is not a valid long
114 */
115 public Long getLong(String element, String attribute) {
116 String s = get(element, attribute);
117 if (s == null) return null;
118 return Long.valueOf(s);
119 }
120
121 /**
122 * Adds a new configuration item.
123 *
124 * @param element the name of the element
125 * @param attribute the name of the attribute
126 * @param value the value as string
127 */
128 public void put(String element, String attribute, String value) {
129 if ("blacklist".equals(element)) {
130 if ("regex".equals(attribute)) {
131 imageryBlacklist.add(value);
132 }
133 } else {
134 if (!capabilities.containsKey(element)) {
135 capabilities.put(element, new HashMap<>());
136 }
137 capabilities.get(element).put(attribute, value);
138 }
139 }
140
141 /**
142 * Clears the API capabilities.
143 */
144 public final void clear() {
145 capabilities.clear();
146 imageryBlacklist.clear();
147 }
148
149 /**
150 * Determines if a given API version is supported.
151 * @param version The API version to check
152 * @return {@code true} is version is between the minimum supported version and the maximum one, {@code false} otherwise
153 */
154 public boolean supportsVersion(String version) {
155 return get("version", "minimum").compareTo(version) <= 0
156 && get("version", "maximum").compareTo(version) >= 0;
157 }
158
159 private static void warnIllegalValue(String attr, String elem, Object val) {
160 Main.warn(tr("Illegal value of attribute ''{0}'' of element ''{1}'' in server capabilities. Got ''{2}''", attr, elem, val));
161 }
162
163 /**
164 * Returns the max number of objects in a changeset. -1 if either the capabilities
165 * don't include this parameter or if the parameter value is illegal (not a number,
166 * a negative number)
167 *
168 * @return the max number of objects in a changeset
169 */
170 public int getMaxChangesetSize() {
171 String v = get("changesets", "maximum_elements");
172 if (v != null) {
173 try {
174 int n = Integer.parseInt(v);
175 if (n <= 0) {
176 warnIllegalValue("changesets", "maximum_elements", n);
177 } else {
178 return n;
179 }
180 } catch (NumberFormatException e) {
181 warnIllegalValue("changesets", "maximum_elements", v);
182 }
183 }
184 return -1;
185 }
186
187 /**
188 * Returns the max number of nodes in a way. -1 if either the capabilities
189 * don't include this parameter or if the parameter value is illegal (not a number,
190 * a negative number)
191 *
192 * @return the max number of nodes in a way
193 */
194 public long getMaxWayNodes() {
195 String v = get("waynodes", "maximum");
196 if (v != null) {
197 try {
198 long n = Long.parseLong(v);
199 if (n <= 0) {
200 warnIllegalValue("waynodes", "maximum", n);
201 } else {
202 return n;
203 }
204 } catch (NumberFormatException e) {
205 warnIllegalValue("waynodes", "maximum", v);
206 }
207 }
208 return -1;
209 }
210
211 /**
212 * Checks if the given URL is blacklisted by one of the of the regular expressions.
213 * @param url Imagery URL to check
214 * @return {@code true} if URL is blacklisted, {@code false} otherwise
215 */
216 public boolean isOnImageryBlacklist(String url) {
217 if (url != null && imageryBlacklist != null) {
218 for (String blacklistRegex : imageryBlacklist) {
219 if (url.matches(blacklistRegex))
220 return true;
221 }
222 }
223 return false;
224 }
225
226 /**
227 * Returns the full list of imagery blacklist regular expressions.
228 * @return full list of imagery blacklist regular expressions
229 */
230 public List<String> getImageryBlacklist() {
231 return Collections.unmodifiableList(imageryBlacklist);
232 }
233
234 /**
235 * A parser for the "capabilities" response XML.
236 * @since 7473
237 */
238 public static final class CapabilitiesParser extends DefaultHandler {
239
240 private Capabilities capabilities;
241
242 @Override
243 public void startDocument() {
244 capabilities = new Capabilities();
245 }
246
247 @Override
248 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) {
249 for (int i = 0; i < atts.getLength(); i++) {
250 capabilities.put(qName, atts.getQName(i), atts.getValue(i));
251 }
252 }
253
254 /**
255 * Returns the read capabilities.
256 * @return the read capabilities
257 */
258 public Capabilities getCapabilities() {
259 return capabilities;
260 }
261
262 /**
263 * Parses and returns capabilities from the given input source.
264 *
265 * @param inputSource The input source to read capabilities from
266 * @return the capabilities
267 * @throws SAXException if any SAX errors occur during processing
268 * @throws IOException if any I/O errors occur
269 * @throws ParserConfigurationException if a parser cannot be created
270 */
271 public static Capabilities parse(InputSource inputSource) throws SAXException, IOException, ParserConfigurationException {
272 CapabilitiesParser parser = new CapabilitiesParser();
273 Utils.parseSafeSAX(inputSource, parser);
274 return parser.getCapabilities();
275 }
276 }
277}
Note: See TracBrowser for help on using the repository browser.