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

Last change on this file since 10101 was 8510, checked in by Don-vip, 9 years ago

checkstyle: enable relevant whitespace checks and fix them

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