1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.Collections;
|
---|
8 | import java.util.HashMap;
|
---|
9 | import java.util.List;
|
---|
10 | import java.util.Map;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.Main;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * Represents the server capabilities
|
---|
16 | *
|
---|
17 | * Example capabilites document:
|
---|
18 | * <pre>
|
---|
19 | * <osm version="0.6" generator="OpenStreetMap server">
|
---|
20 | * <api>
|
---|
21 | * <version minimum="0.6" maximum="0.6"/>
|
---|
22 | * <area maximum="0.25"/>
|
---|
23 | * <tracepoints per_page="5000"/>
|
---|
24 | * <waynodes maximum="2000"/>
|
---|
25 | * <changesets maximum_elements="50000"/>
|
---|
26 | * <timeout seconds="300"/>
|
---|
27 | * </api>
|
---|
28 | * <policy>
|
---|
29 | * <imagery>
|
---|
30 | * <blacklist regex=".*\.google\.com/.*"/>
|
---|
31 | * <blacklist regex=".*209\.85\.2\d\d.*"/>
|
---|
32 | * <blacklist regex=".*209\.85\.1[3-9]\d.*"/>
|
---|
33 | * <blacklist regex=".*209\.85\.12[89].*"/>
|
---|
34 | * </imagery>
|
---|
35 | * </policy>
|
---|
36 | * </osm>
|
---|
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 | */
|
---|
43 | public 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 | }
|
---|