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

Last change on this file since 4067 was 4063, checked in by bastiK, 13 years ago

see #6267, check for null

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