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

Last change on this file since 7448 was 7012, checked in by Don-vip, 10 years ago

see #8465 - use String switch/case where applicable

  • Property svn:eol-style set to native
File size: 5.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.util.ArrayList;
7import java.util.Collections;
8import java.util.HashMap;
9import java.util.List;
10import java.util.Map;
11
12import org.openstreetmap.josm.Main;
13
14/**
15 * Represents the server capabilities
16 *
17 * Example capabilites document:
18 * <pre>
19 * &lt;osm version="0.6" generator="OpenStreetMap server"&gt;
20 * &lt;api&gt;
21 * &lt;version minimum="0.6" maximum="0.6"/&gt;
22 * &lt;area maximum="0.25"/&gt;
23 * &lt;tracepoints per_page="5000"/&gt;
24 * &lt;waynodes maximum="2000"/&gt;
25 * &lt;changesets maximum_elements="50000"/&gt;
26 * &lt;timeout seconds="300"/&gt;
27 * &lt;/api&gt;
28 * &lt;policy&gt;
29 * &lt;imagery&gt;
30 * &lt;blacklist regex=".*\.google\.com/.*"/&gt;
31 * &lt;blacklist regex=".*209\.85\.2\d\d.*"/&gt;
32 * &lt;blacklist regex=".*209\.85\.1[3-9]\d.*"/&gt;
33 * &lt;blacklist regex=".*209\.85\.12[89].*"/&gt;
34 * &lt;/imagery&gt;
35 * &lt;/policy&gt;
36 * &lt;/osm&gt;
37 * </pre>
38 * This class is used in conjunction with a very primitive parser
39 * and simply stuffs the each tag and its attributes into a hash
40 * of hashes, with the exception of the "blacklist" tag which gets
41 * a list of its own. The DOM hierarchy is disregarded.
42 */
43public class Capabilities {
44
45 private Map<String, HashMap<String,String>> capabilities;
46 private List<String> imageryBlacklist;
47
48 /**
49 * Constructs new {@code Capabilities}.
50 */
51 public Capabilities() {
52 clear();
53 }
54
55 public boolean isDefined(String element, String attribute) {
56 if (! capabilities.containsKey(element)) return false;
57 HashMap<String, String> e = capabilities.get(element);
58 if (e == null) return false;
59 return (e.get(attribute) != null);
60 }
61
62 public String get(String element, String attribute ) {
63 if (! capabilities.containsKey(element)) return null;
64 HashMap<String, String> e = capabilities.get(element);
65 if (e == null) return null;
66 return e.get(attribute);
67 }
68
69 /**
70 * returns the value of configuration item in the capabilities as
71 * double value
72 *
73 * @param element the name of the element
74 * @param attribute the name of the attribute
75 * @return the value; null, if the respective configuration item doesn't exist
76 * @throws NumberFormatException if the value is not a valid double
77 */
78 public Double getDouble(String element, String attribute) throws NumberFormatException {
79 String s = get(element, attribute);
80 if (s == null) return null;
81 return Double.parseDouble(s);
82 }
83
84 public Long getLong(String element, String attribute) {
85 String s = get(element, attribute);
86 if (s == null) return null;
87 return Long.parseLong(s);
88 }
89
90 public void put(String element, String attribute, String value) {
91 if ("blacklist".equals(element)) {
92 if ("regex".equals(attribute)) {
93 imageryBlacklist.add(value);
94 }
95 } else {
96 if (! capabilities.containsKey(element)) {
97 HashMap<String,String> h = new HashMap<>();
98 capabilities.put(element, h);
99 }
100 HashMap<String, String> e = capabilities.get(element);
101 e.put(attribute, value);
102 }
103 }
104
105 public final void clear() {
106 capabilities = new HashMap<>();
107 imageryBlacklist = new ArrayList<>();
108 }
109
110 public boolean supportsVersion(String version) {
111 return get("version", "minimum").compareTo(version) <= 0
112 && get("version", "maximum").compareTo(version) >= 0;
113 }
114
115 /**
116 * Returns the max number of objects in a changeset. -1 if either the capabilities
117 * don't include this parameter or if the parameter value is illegal (not a number,
118 * a negative number)
119 *
120 * @return the max number of objects in a changeset
121 */
122 public int getMaxChangesetSize() {
123 String v = get("changesets", "maximum_elements");
124 if (v == null) return -1;
125 try {
126 int n = Integer.parseInt(v);
127 if (n <= 0) {
128 Main.warn(tr("Illegal value of attribute ''{0}'' of element ''{1}'' in server capabilities. Got ''{2}''", "changesets", "maximum_elements", n ));
129 return -1;
130 }
131 return n;
132 } catch (NumberFormatException e) {
133 Main.warn(tr("Illegal value of attribute ''{0}'' of element ''{1}'' in server capabilities. Got ''{2}''", "changesets", "maximum_elements", v ));
134 return -1;
135 }
136 }
137
138 /**
139 * checks if the given URL is blacklisted by one of the of the
140 * regular expressions.
141 */
142
143 public boolean isOnImageryBlacklist(String url)
144 {
145 if (url != null && imageryBlacklist != null) {
146 for (String blacklistRegex : imageryBlacklist) {
147 if (url.matches(blacklistRegex))
148 return true;
149 }
150 }
151 return false;
152 }
153
154 /**
155 * returns the full list of blacklist regular expressions.
156 */
157 public List<String> getImageryBlacklist()
158 {
159 return Collections.unmodifiableList(imageryBlacklist);
160 }
161}
Note: See TracBrowser for help on using the repository browser.