1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.imagery;
|
---|
3 |
|
---|
4 | import java.io.IOException;
|
---|
5 | import java.io.InputStream;
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.Arrays;
|
---|
8 | import java.util.HashMap;
|
---|
9 | import java.util.List;
|
---|
10 | import java.util.Map;
|
---|
11 | import java.util.Objects;
|
---|
12 | import java.util.Stack;
|
---|
13 |
|
---|
14 | import javax.xml.parsers.ParserConfigurationException;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.Main;
|
---|
17 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
18 | import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryBounds;
|
---|
19 | import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
|
---|
20 | import org.openstreetmap.josm.data.imagery.Shape;
|
---|
21 | import org.openstreetmap.josm.io.CachedFile;
|
---|
22 | import org.openstreetmap.josm.io.UTFInputStreamReader;
|
---|
23 | import org.openstreetmap.josm.tools.LanguageInfo;
|
---|
24 | import org.openstreetmap.josm.tools.Utils;
|
---|
25 | import org.xml.sax.Attributes;
|
---|
26 | import org.xml.sax.InputSource;
|
---|
27 | import org.xml.sax.SAXException;
|
---|
28 | import org.xml.sax.helpers.DefaultHandler;
|
---|
29 |
|
---|
30 | public class ImageryReader {
|
---|
31 |
|
---|
32 | private String source;
|
---|
33 |
|
---|
34 | private enum State {
|
---|
35 | INIT, // initial state, should always be at the bottom of the stack
|
---|
36 | IMAGERY, // inside the imagery element
|
---|
37 | ENTRY, // inside an entry
|
---|
38 | ENTRY_ATTRIBUTE, // note we are inside an entry attribute to collect the character data
|
---|
39 | PROJECTIONS,
|
---|
40 | CODE,
|
---|
41 | BOUNDS,
|
---|
42 | SHAPE,
|
---|
43 | NO_TILE,
|
---|
44 | UNKNOWN, // element is not recognized in the current context
|
---|
45 | }
|
---|
46 |
|
---|
47 | public ImageryReader(String source) {
|
---|
48 | this.source = source;
|
---|
49 | }
|
---|
50 |
|
---|
51 | public List<ImageryInfo> parse() throws SAXException, IOException {
|
---|
52 | Parser parser = new Parser();
|
---|
53 | try {
|
---|
54 | try (InputStream in = new CachedFile(source)
|
---|
55 | .setMaxAge(1*CachedFile.DAYS)
|
---|
56 | .setCachingStrategy(CachedFile.CachingStrategy.IfModifiedSince)
|
---|
57 | .getInputStream()) {
|
---|
58 | InputSource is = new InputSource(UTFInputStreamReader.create(in));
|
---|
59 | Utils.parseSafeSAX(is, parser);
|
---|
60 | return parser.entries;
|
---|
61 | }
|
---|
62 | } catch (SAXException e) {
|
---|
63 | throw e;
|
---|
64 | } catch (ParserConfigurationException e) {
|
---|
65 | Main.error(e); // broken SAXException chaining
|
---|
66 | throw new SAXException(e);
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | private static class Parser extends DefaultHandler {
|
---|
71 | private StringBuffer accumulator = new StringBuffer();
|
---|
72 |
|
---|
73 | private Stack<State> states;
|
---|
74 |
|
---|
75 | private List<ImageryInfo> entries;
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Skip the current entry because it has mandatory attributes
|
---|
79 | * that this version of JOSM cannot process.
|
---|
80 | */
|
---|
81 | private boolean skipEntry;
|
---|
82 |
|
---|
83 | private ImageryInfo entry;
|
---|
84 | private ImageryBounds bounds;
|
---|
85 | private Shape shape;
|
---|
86 | // language of last element, does only work for simple ENTRY_ATTRIBUTE's
|
---|
87 | private String lang;
|
---|
88 | private List<String> projections;
|
---|
89 | private Map<String, String> noTileHeaders;
|
---|
90 |
|
---|
91 | @Override
|
---|
92 | public void startDocument() {
|
---|
93 | accumulator = new StringBuffer();
|
---|
94 | skipEntry = false;
|
---|
95 | states = new Stack<>();
|
---|
96 | states.push(State.INIT);
|
---|
97 | entries = new ArrayList<>();
|
---|
98 | entry = null;
|
---|
99 | bounds = null;
|
---|
100 | projections = null;
|
---|
101 | noTileHeaders = null;
|
---|
102 | }
|
---|
103 |
|
---|
104 | @Override
|
---|
105 | public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
|
---|
106 | accumulator.setLength(0);
|
---|
107 | State newState = null;
|
---|
108 | switch (states.peek()) {
|
---|
109 | case INIT:
|
---|
110 | if ("imagery".equals(qName)) {
|
---|
111 | newState = State.IMAGERY;
|
---|
112 | }
|
---|
113 | break;
|
---|
114 | case IMAGERY:
|
---|
115 | if ("entry".equals(qName)) {
|
---|
116 | entry = new ImageryInfo();
|
---|
117 | skipEntry = false;
|
---|
118 | newState = State.ENTRY;
|
---|
119 | }
|
---|
120 | break;
|
---|
121 | case ENTRY:
|
---|
122 | if (Arrays.asList(new String[] {
|
---|
123 | "name",
|
---|
124 | "id",
|
---|
125 | "type",
|
---|
126 | "description",
|
---|
127 | "default",
|
---|
128 | "url",
|
---|
129 | "eula",
|
---|
130 | "min-zoom",
|
---|
131 | "max-zoom",
|
---|
132 | "attribution-text",
|
---|
133 | "attribution-url",
|
---|
134 | "logo-image",
|
---|
135 | "logo-url",
|
---|
136 | "terms-of-use-text",
|
---|
137 | "terms-of-use-url",
|
---|
138 | "country-code",
|
---|
139 | "icon",
|
---|
140 | "tile-size",
|
---|
141 | }).contains(qName)) {
|
---|
142 | newState = State.ENTRY_ATTRIBUTE;
|
---|
143 | lang = atts.getValue("lang");
|
---|
144 | } else if ("bounds".equals(qName)) {
|
---|
145 | try {
|
---|
146 | bounds = new ImageryBounds(
|
---|
147 | atts.getValue("min-lat") + "," +
|
---|
148 | atts.getValue("min-lon") + "," +
|
---|
149 | atts.getValue("max-lat") + "," +
|
---|
150 | atts.getValue("max-lon"), ",");
|
---|
151 | } catch (IllegalArgumentException e) {
|
---|
152 | break;
|
---|
153 | }
|
---|
154 | newState = State.BOUNDS;
|
---|
155 | } else if ("projections".equals(qName)) {
|
---|
156 | projections = new ArrayList<>();
|
---|
157 | newState = State.PROJECTIONS;
|
---|
158 | } else if ("no-tile-header".equals(qName)) {
|
---|
159 | noTileHeaders = new HashMap<>();
|
---|
160 | noTileHeaders.put(atts.getValue("name"), atts.getValue("value"));
|
---|
161 | newState = State.NO_TILE;
|
---|
162 | }
|
---|
163 | break;
|
---|
164 | case BOUNDS:
|
---|
165 | if ("shape".equals(qName)) {
|
---|
166 | shape = new Shape();
|
---|
167 | newState = State.SHAPE;
|
---|
168 | }
|
---|
169 | break;
|
---|
170 | case SHAPE:
|
---|
171 | if ("point".equals(qName)) {
|
---|
172 | try {
|
---|
173 | shape.addPoint(atts.getValue("lat"), atts.getValue("lon"));
|
---|
174 | } catch (IllegalArgumentException e) {
|
---|
175 | break;
|
---|
176 | }
|
---|
177 | }
|
---|
178 | break;
|
---|
179 | case PROJECTIONS:
|
---|
180 | if ("code".equals(qName)) {
|
---|
181 | newState = State.CODE;
|
---|
182 | }
|
---|
183 | break;
|
---|
184 | }
|
---|
185 | /**
|
---|
186 | * Did not recognize the element, so the new state is UNKNOWN.
|
---|
187 | * This includes the case where we are already inside an unknown
|
---|
188 | * element, i.e. we do not try to understand the inner content
|
---|
189 | * of an unknown element, but wait till it's over.
|
---|
190 | */
|
---|
191 | if (newState == null) {
|
---|
192 | newState = State.UNKNOWN;
|
---|
193 | }
|
---|
194 | states.push(newState);
|
---|
195 | if (newState == State.UNKNOWN && "true".equals(atts.getValue("mandatory"))) {
|
---|
196 | skipEntry = true;
|
---|
197 | }
|
---|
198 | return;
|
---|
199 | }
|
---|
200 |
|
---|
201 | @Override
|
---|
202 | public void characters(char[] ch, int start, int length) {
|
---|
203 | accumulator.append(ch, start, length);
|
---|
204 | }
|
---|
205 |
|
---|
206 | @Override
|
---|
207 | public void endElement(String namespaceURI, String qName, String rqName) {
|
---|
208 | switch (states.pop()) {
|
---|
209 | case INIT:
|
---|
210 | throw new RuntimeException("parsing error: more closing than opening elements");
|
---|
211 | case ENTRY:
|
---|
212 | if ("entry".equals(qName)) {
|
---|
213 | if (!skipEntry) {
|
---|
214 | entries.add(entry);
|
---|
215 | }
|
---|
216 | entry = null;
|
---|
217 | }
|
---|
218 | break;
|
---|
219 | case ENTRY_ATTRIBUTE:
|
---|
220 | switch(qName) {
|
---|
221 | case "name":
|
---|
222 | entry.setName(lang == null ? LanguageInfo.getJOSMLocaleCode(null) : lang, accumulator.toString());
|
---|
223 | break;
|
---|
224 | case "description":
|
---|
225 | entry.setDescription(lang, accumulator.toString());
|
---|
226 | break;
|
---|
227 | case "id":
|
---|
228 | entry.setId(accumulator.toString());
|
---|
229 | break;
|
---|
230 | case "type":
|
---|
231 | boolean found = false;
|
---|
232 | for (ImageryType type : ImageryType.values()) {
|
---|
233 | if (Objects.equals(accumulator.toString(), type.getTypeString())) {
|
---|
234 | entry.setImageryType(type);
|
---|
235 | found = true;
|
---|
236 | break;
|
---|
237 | }
|
---|
238 | }
|
---|
239 | if (!found) {
|
---|
240 | skipEntry = true;
|
---|
241 | }
|
---|
242 | break;
|
---|
243 | case "default":
|
---|
244 | switch (accumulator.toString()) {
|
---|
245 | case "true":
|
---|
246 | entry.setDefaultEntry(true);
|
---|
247 | break;
|
---|
248 | case "false":
|
---|
249 | entry.setDefaultEntry(false);
|
---|
250 | break;
|
---|
251 | default:
|
---|
252 | skipEntry = true;
|
---|
253 | }
|
---|
254 | break;
|
---|
255 | case "url":
|
---|
256 | entry.setUrl(accumulator.toString());
|
---|
257 | break;
|
---|
258 | case "eula":
|
---|
259 | entry.setEulaAcceptanceRequired(accumulator.toString());
|
---|
260 | break;
|
---|
261 | case "min-zoom":
|
---|
262 | case "max-zoom":
|
---|
263 | Integer val = null;
|
---|
264 | try {
|
---|
265 | val = Integer.valueOf(accumulator.toString());
|
---|
266 | } catch(NumberFormatException e) {
|
---|
267 | val = null;
|
---|
268 | }
|
---|
269 | if (val == null) {
|
---|
270 | skipEntry = true;
|
---|
271 | } else {
|
---|
272 | if ("min-zoom".equals(qName)) {
|
---|
273 | entry.setDefaultMinZoom(val);
|
---|
274 | } else {
|
---|
275 | entry.setDefaultMaxZoom(val);
|
---|
276 | }
|
---|
277 | }
|
---|
278 | break;
|
---|
279 | case "attribution-text":
|
---|
280 | entry.setAttributionText(accumulator.toString());
|
---|
281 | break;
|
---|
282 | case "attribution-url":
|
---|
283 | entry.setAttributionLinkURL(accumulator.toString());
|
---|
284 | break;
|
---|
285 | case "logo-image":
|
---|
286 | entry.setAttributionImage(accumulator.toString());
|
---|
287 | break;
|
---|
288 | case "logo-url":
|
---|
289 | entry.setAttributionImageURL(accumulator.toString());
|
---|
290 | break;
|
---|
291 | case "terms-of-use-text":
|
---|
292 | entry.setTermsOfUseText(accumulator.toString());
|
---|
293 | break;
|
---|
294 | case "terms-of-use-url":
|
---|
295 | entry.setTermsOfUseURL(accumulator.toString());
|
---|
296 | break;
|
---|
297 | case "country-code":
|
---|
298 | entry.setCountryCode(accumulator.toString());
|
---|
299 | break;
|
---|
300 | case "icon":
|
---|
301 | entry.setIcon(accumulator.toString());
|
---|
302 | break;
|
---|
303 | case "tile-size":
|
---|
304 | Integer tileSize = null;
|
---|
305 | try {
|
---|
306 | tileSize = Integer.valueOf(accumulator.toString());
|
---|
307 | } catch(NumberFormatException e) {
|
---|
308 | tileSize = null;
|
---|
309 | }
|
---|
310 | if (tileSize == null) {
|
---|
311 | skipEntry = true;
|
---|
312 | } else {
|
---|
313 | entry.setTileSize(tileSize.intValue());
|
---|
314 | }
|
---|
315 | break;
|
---|
316 | }
|
---|
317 | break;
|
---|
318 | case BOUNDS:
|
---|
319 | entry.setBounds(bounds);
|
---|
320 | bounds = null;
|
---|
321 | break;
|
---|
322 | case SHAPE:
|
---|
323 | bounds.addShape(shape);
|
---|
324 | shape = null;
|
---|
325 | break;
|
---|
326 | case CODE:
|
---|
327 | projections.add(accumulator.toString());
|
---|
328 | break;
|
---|
329 | case PROJECTIONS:
|
---|
330 | entry.setServerProjections(projections);
|
---|
331 | projections = null;
|
---|
332 | break;
|
---|
333 | case NO_TILE:
|
---|
334 | entry.setNoTileHeaders(noTileHeaders);
|
---|
335 | noTileHeaders = null;
|
---|
336 | break;
|
---|
337 |
|
---|
338 | }
|
---|
339 | }
|
---|
340 | }
|
---|
341 | }
|
---|