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

Last change on this file since 6316 was 6316, checked in by Don-vip, 11 years ago

Sonar/FindBugs - Loose coupling

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