source: josm/trunk/src/org/openstreetmap/josm/io/OsmReader.java@ 7012

Last change on this file since 7012 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: 25.9 KB
Line 
1// License: GPL. See LICENSE file for details.
2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.InputStream;
7import java.io.InputStreamReader;
8import java.text.MessageFormat;
9import java.util.ArrayList;
10import java.util.Collection;
11import java.util.List;
12import java.util.regex.Matcher;
13import java.util.regex.Pattern;
14
15import javax.xml.stream.Location;
16import javax.xml.stream.XMLInputFactory;
17import javax.xml.stream.XMLStreamConstants;
18import javax.xml.stream.XMLStreamException;
19import javax.xml.stream.XMLStreamReader;
20
21import org.openstreetmap.josm.Main;
22import org.openstreetmap.josm.data.Bounds;
23import org.openstreetmap.josm.data.coor.LatLon;
24import org.openstreetmap.josm.data.osm.Changeset;
25import org.openstreetmap.josm.data.osm.DataSet;
26import org.openstreetmap.josm.data.osm.DataSource;
27import org.openstreetmap.josm.data.osm.Node;
28import org.openstreetmap.josm.data.osm.NodeData;
29import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
30import org.openstreetmap.josm.data.osm.PrimitiveData;
31import org.openstreetmap.josm.data.osm.Relation;
32import org.openstreetmap.josm.data.osm.RelationData;
33import org.openstreetmap.josm.data.osm.RelationMemberData;
34import org.openstreetmap.josm.data.osm.Tagged;
35import org.openstreetmap.josm.data.osm.User;
36import org.openstreetmap.josm.data.osm.Way;
37import org.openstreetmap.josm.data.osm.WayData;
38import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
39import org.openstreetmap.josm.gui.progress.ProgressMonitor;
40import org.openstreetmap.josm.tools.CheckParameterUtil;
41import org.openstreetmap.josm.tools.DateUtils;
42
43/**
44 * Parser for the Osm Api. Read from an input stream and construct a dataset out of it.
45 *
46 * For each xml element, there is a dedicated method.
47 * The XMLStreamReader cursor points to the start of the element, when the method is
48 * entered, and it must point to the end of the same element, when it is exited.
49 */
50public class OsmReader extends AbstractReader {
51
52 protected XMLStreamReader parser;
53
54 protected boolean cancel;
55
56 /** Used by plugins to register themselves as data postprocessors. */
57 public static List<OsmServerReadPostprocessor> postprocessors;
58
59 /** register a new postprocessor */
60 public static void registerPostprocessor(OsmServerReadPostprocessor pp) {
61 if (postprocessors == null) {
62 postprocessors = new ArrayList<>();
63 }
64 postprocessors.add(pp);
65 }
66
67 /** deregister a postprocessor previously registered with registerPostprocessor */
68 public static void deregisterPostprocessor(OsmServerReadPostprocessor pp) {
69 if (postprocessors != null) {
70 postprocessors.remove(pp);
71 }
72 }
73
74 /**
75 * constructor (for private and subclasses use only)
76 *
77 * @see #parseDataSet(InputStream, ProgressMonitor)
78 */
79 protected OsmReader() {
80 }
81
82 protected void setParser(XMLStreamReader parser) {
83 this.parser = parser;
84 }
85
86 protected void throwException(String msg, Throwable th) throws XMLStreamException {
87 throw new OsmParsingException(msg, parser.getLocation(), th);
88 }
89
90 protected void throwException(String msg) throws XMLStreamException {
91 throw new OsmParsingException(msg, parser.getLocation());
92 }
93
94 protected void parse() throws XMLStreamException {
95 int event = parser.getEventType();
96 while (true) {
97 if (event == XMLStreamConstants.START_ELEMENT) {
98 parseRoot();
99 } else if (event == XMLStreamConstants.END_ELEMENT)
100 return;
101 if (parser.hasNext()) {
102 event = parser.next();
103 } else {
104 break;
105 }
106 }
107 parser.close();
108 }
109
110 protected void parseRoot() throws XMLStreamException {
111 if ("osm".equals(parser.getLocalName())) {
112 parseOsm();
113 } else {
114 parseUnknown();
115 }
116 }
117
118 private void parseOsm() throws XMLStreamException {
119 String v = parser.getAttributeValue(null, "version");
120 if (v == null) {
121 throwException(tr("Missing mandatory attribute ''{0}''.", "version"));
122 }
123 if (!("0.5".equals(v) || "0.6".equals(v))) {
124 throwException(tr("Unsupported version: {0}", v));
125 }
126 ds.setVersion(v);
127 String upload = parser.getAttributeValue(null, "upload");
128 if (upload != null) {
129 ds.setUploadDiscouraged(!Boolean.parseBoolean(upload));
130 }
131 String generator = parser.getAttributeValue(null, "generator");
132 Long uploadChangesetId = null;
133 if (parser.getAttributeValue(null, "upload-changeset") != null) {
134 uploadChangesetId = getLong("upload-changeset");
135 }
136 while (true) {
137 int event = parser.next();
138
139 if (cancel) {
140 cancel = false;
141 throw new OsmParsingCanceledException(tr("Reading was canceled"), parser.getLocation());
142 }
143
144 if (event == XMLStreamConstants.START_ELEMENT) {
145 switch (parser.getLocalName()) {
146 case "bounds":
147 parseBounds(generator);
148 break;
149 case "node":
150 parseNode();
151 break;
152 case "way":
153 parseWay();
154 break;
155 case "relation":
156 parseRelation();
157 break;
158 case "changeset":
159 parseChangeset(uploadChangesetId);
160 break;
161 default:
162 parseUnknown();
163 }
164 } else if (event == XMLStreamConstants.END_ELEMENT)
165 return;
166 }
167 }
168
169 private void parseBounds(String generator) throws XMLStreamException {
170 String minlon = parser.getAttributeValue(null, "minlon");
171 String minlat = parser.getAttributeValue(null, "minlat");
172 String maxlon = parser.getAttributeValue(null, "maxlon");
173 String maxlat = parser.getAttributeValue(null, "maxlat");
174 String origin = parser.getAttributeValue(null, "origin");
175 if (minlon != null && maxlon != null && minlat != null && maxlat != null) {
176 if (origin == null) {
177 origin = generator;
178 }
179 Bounds bounds = new Bounds(
180 Double.parseDouble(minlat), Double.parseDouble(minlon),
181 Double.parseDouble(maxlat), Double.parseDouble(maxlon));
182 if (bounds.isOutOfTheWorld()) {
183 Bounds copy = new Bounds(bounds);
184 bounds.normalize();
185 Main.info("Bbox " + copy + " is out of the world, normalized to " + bounds);
186 }
187 DataSource src = new DataSource(bounds, origin);
188 ds.dataSources.add(src);
189 } else {
190 throwException(tr(
191 "Missing mandatory attributes on element ''bounds''. Got minlon=''{0}'',minlat=''{1}'',maxlon=''{3}'',maxlat=''{4}'', origin=''{5}''.",
192 minlon, minlat, maxlon, maxlat, origin
193 ));
194 }
195 jumpToEnd();
196 }
197
198 protected Node parseNode() throws XMLStreamException {
199 NodeData nd = new NodeData();
200 String lat = parser.getAttributeValue(null, "lat");
201 String lon = parser.getAttributeValue(null, "lon");
202 if (lat != null && lon != null) {
203 nd.setCoor(new LatLon(Double.parseDouble(lat), Double.parseDouble(lon)));
204 }
205 readCommon(nd);
206 Node n = new Node(nd.getId(), nd.getVersion());
207 n.setVisible(nd.isVisible());
208 n.load(nd);
209 externalIdMap.put(nd.getPrimitiveId(), n);
210 while (true) {
211 int event = parser.next();
212 if (event == XMLStreamConstants.START_ELEMENT) {
213 if ("tag".equals(parser.getLocalName())) {
214 parseTag(n);
215 } else {
216 parseUnknown();
217 }
218 } else if (event == XMLStreamConstants.END_ELEMENT)
219 return n;
220 }
221 }
222
223 protected Way parseWay() throws XMLStreamException {
224 WayData wd = new WayData();
225 readCommon(wd);
226 Way w = new Way(wd.getId(), wd.getVersion());
227 w.setVisible(wd.isVisible());
228 w.load(wd);
229 externalIdMap.put(wd.getPrimitiveId(), w);
230
231 Collection<Long> nodeIds = new ArrayList<>();
232 while (true) {
233 int event = parser.next();
234 if (event == XMLStreamConstants.START_ELEMENT) {
235 switch (parser.getLocalName()) {
236 case "nd":
237 nodeIds.add(parseWayNode(w));
238 break;
239 case "tag":
240 parseTag(w);
241 break;
242 default:
243 parseUnknown();
244 }
245 } else if (event == XMLStreamConstants.END_ELEMENT) {
246 break;
247 }
248 }
249 if (w.isDeleted() && !nodeIds.isEmpty()) {
250 Main.info(tr("Deleted way {0} contains nodes", w.getUniqueId()));
251 nodeIds = new ArrayList<>();
252 }
253 ways.put(wd.getUniqueId(), nodeIds);
254 return w;
255 }
256
257 private long parseWayNode(Way w) throws XMLStreamException {
258 if (parser.getAttributeValue(null, "ref") == null) {
259 throwException(
260 tr("Missing mandatory attribute ''{0}'' on <nd> of way {1}.", "ref", w.getUniqueId())
261 );
262 }
263 long id = getLong("ref");
264 if (id == 0) {
265 throwException(
266 tr("Illegal value of attribute ''ref'' of element <nd>. Got {0}.", id)
267 );
268 }
269 jumpToEnd();
270 return id;
271 }
272
273 protected Relation parseRelation() throws XMLStreamException {
274 RelationData rd = new RelationData();
275 readCommon(rd);
276 Relation r = new Relation(rd.getId(), rd.getVersion());
277 r.setVisible(rd.isVisible());
278 r.load(rd);
279 externalIdMap.put(rd.getPrimitiveId(), r);
280
281 Collection<RelationMemberData> members = new ArrayList<>();
282 while (true) {
283 int event = parser.next();
284 if (event == XMLStreamConstants.START_ELEMENT) {
285 switch (parser.getLocalName()) {
286 case "member":
287 members.add(parseRelationMember(r));
288 break;
289 case "tag":
290 parseTag(r);
291 break;
292 default:
293 parseUnknown();
294 }
295 } else if (event == XMLStreamConstants.END_ELEMENT) {
296 break;
297 }
298 }
299 if (r.isDeleted() && !members.isEmpty()) {
300 Main.info(tr("Deleted relation {0} contains members", r.getUniqueId()));
301 members = new ArrayList<>();
302 }
303 relations.put(rd.getUniqueId(), members);
304 return r;
305 }
306
307 private RelationMemberData parseRelationMember(Relation r) throws XMLStreamException {
308 String role = null;
309 OsmPrimitiveType type = null;
310 long id = 0;
311 String value = parser.getAttributeValue(null, "ref");
312 if (value == null) {
313 throwException(tr("Missing attribute ''ref'' on member in relation {0}.",r.getUniqueId()));
314 }
315 try {
316 id = Long.parseLong(value);
317 } catch(NumberFormatException e) {
318 throwException(tr("Illegal value for attribute ''ref'' on member in relation {0}. Got {1}", Long.toString(r.getUniqueId()),value), e);
319 }
320 value = parser.getAttributeValue(null, "type");
321 if (value == null) {
322 throwException(tr("Missing attribute ''type'' on member {0} in relation {1}.", Long.toString(id), Long.toString(r.getUniqueId())));
323 }
324 try {
325 type = OsmPrimitiveType.fromApiTypeName(value);
326 } catch(IllegalArgumentException e) {
327 throwException(tr("Illegal value for attribute ''type'' on member {0} in relation {1}. Got {2}.", Long.toString(id), Long.toString(r.getUniqueId()), value), e);
328 }
329 value = parser.getAttributeValue(null, "role");
330 role = value;
331
332 if (id == 0) {
333 throwException(tr("Incomplete <member> specification with ref=0"));
334 }
335 jumpToEnd();
336 return new RelationMemberData(role, type, id);
337 }
338
339 private void parseChangeset(Long uploadChangesetId) throws XMLStreamException {
340
341 Long id = null;
342 if (parser.getAttributeValue(null, "id") != null) {
343 id = getLong("id");
344 }
345 // Read changeset info if neither upload-changeset nor id are set, or if they are both set to the same value
346 if (id == uploadChangesetId || (id != null && id.equals(uploadChangesetId))) {
347 uploadChangeset = new Changeset(id != null ? id.intValue() : 0);
348 while (true) {
349 int event = parser.next();
350 if (event == XMLStreamConstants.START_ELEMENT) {
351 if ("tag".equals(parser.getLocalName())) {
352 parseTag(uploadChangeset);
353 } else {
354 parseUnknown();
355 }
356 } else if (event == XMLStreamConstants.END_ELEMENT)
357 return;
358 }
359 } else {
360 jumpToEnd(false);
361 }
362 }
363
364 private void parseTag(Tagged t) throws XMLStreamException {
365 String key = parser.getAttributeValue(null, "k");
366 String value = parser.getAttributeValue(null, "v");
367 if (key == null || value == null) {
368 throwException(tr("Missing key or value attribute in tag."));
369 }
370 t.put(key.intern(), value.intern());
371 jumpToEnd();
372 }
373
374 protected void parseUnknown(boolean printWarning) throws XMLStreamException {
375 if (printWarning) {
376 Main.info(tr("Undefined element ''{0}'' found in input stream. Skipping.", parser.getLocalName()));
377 }
378 while (true) {
379 int event = parser.next();
380 if (event == XMLStreamConstants.START_ELEMENT) {
381 parseUnknown(false); /* no more warning for inner elements */
382 } else if (event == XMLStreamConstants.END_ELEMENT)
383 return;
384 }
385 }
386
387 protected void parseUnknown() throws XMLStreamException {
388 parseUnknown(true);
389 }
390
391 /**
392 * When cursor is at the start of an element, moves it to the end tag of that element.
393 * Nested content is skipped.
394 *
395 * This is basically the same code as parseUnknown(), except for the warnings, which
396 * are displayed for inner elements and not at top level.
397 */
398 private void jumpToEnd(boolean printWarning) throws XMLStreamException {
399 while (true) {
400 int event = parser.next();
401 if (event == XMLStreamConstants.START_ELEMENT) {
402 parseUnknown(printWarning);
403 } else if (event == XMLStreamConstants.END_ELEMENT)
404 return;
405 }
406 }
407
408 private void jumpToEnd() throws XMLStreamException {
409 jumpToEnd(true);
410 }
411
412 private User createUser(String uid, String name) throws XMLStreamException {
413 if (uid == null) {
414 if (name == null)
415 return null;
416 return User.createLocalUser(name);
417 }
418 try {
419 long id = Long.parseLong(uid);
420 return User.createOsmUser(id, name);
421 } catch(NumberFormatException e) {
422 throwException(MessageFormat.format("Illegal value for attribute ''uid''. Got ''{0}''.", uid), e);
423 }
424 return null;
425 }
426
427 /**
428 * Read out the common attributes and put them into current OsmPrimitive.
429 */
430 private void readCommon(PrimitiveData current) throws XMLStreamException {
431 current.setId(getLong("id"));
432 if (current.getUniqueId() == 0) {
433 throwException(tr("Illegal object with ID=0."));
434 }
435
436 String time = parser.getAttributeValue(null, "timestamp");
437 if (time != null && time.length() != 0) {
438 current.setTimestamp(DateUtils.fromString(time));
439 }
440
441 // user attribute added in 0.4 API
442 String user = parser.getAttributeValue(null, "user");
443 // uid attribute added in 0.6 API
444 String uid = parser.getAttributeValue(null, "uid");
445 current.setUser(createUser(uid, user));
446
447 // visible attribute added in 0.4 API
448 String visible = parser.getAttributeValue(null, "visible");
449 if (visible != null) {
450 current.setVisible(Boolean.parseBoolean(visible));
451 }
452
453 String versionString = parser.getAttributeValue(null, "version");
454 int version = 0;
455 if (versionString != null) {
456 try {
457 version = Integer.parseInt(versionString);
458 } catch(NumberFormatException e) {
459 throwException(tr("Illegal value for attribute ''version'' on OSM primitive with ID {0}. Got {1}.", Long.toString(current.getUniqueId()), versionString), e);
460 }
461 switch (ds.getVersion()) {
462 case "0.6":
463 if (version <= 0 && !current.isNew()) {
464 throwException(tr("Illegal value for attribute ''version'' on OSM primitive with ID {0}. Got {1}.", Long.toString(current.getUniqueId()), versionString));
465 } else if (version < 0 && current.isNew()) {
466 Main.warn(tr("Normalizing value of attribute ''version'' of element {0} to {2}, API version is ''{3}''. Got {1}.", current.getUniqueId(), version, 0, "0.6"));
467 version = 0;
468 }
469 break;
470 case "0.5":
471 if (version <= 0 && !current.isNew()) {
472 Main.warn(tr("Normalizing value of attribute ''version'' of element {0} to {2}, API version is ''{3}''. Got {1}.", current.getUniqueId(), version, 1, "0.5"));
473 version = 1;
474 } else if (version < 0 && current.isNew()) {
475 Main.warn(tr("Normalizing value of attribute ''version'' of element {0} to {2}, API version is ''{3}''. Got {1}.", current.getUniqueId(), version, 0, "0.5"));
476 version = 0;
477 }
478 break;
479 default:
480 // should not happen. API version has been checked before
481 throwException(tr("Unknown or unsupported API version. Got {0}.", ds.getVersion()));
482 }
483 } else {
484 // version expected for OSM primitives with an id assigned by the server (id > 0), since API 0.6
485 //
486 if (!current.isNew() && ds.getVersion() != null && "0.6".equals(ds.getVersion())) {
487 throwException(tr("Missing attribute ''version'' on OSM primitive with ID {0}.", Long.toString(current.getUniqueId())));
488 } else if (!current.isNew() && ds.getVersion() != null && "0.5".equals(ds.getVersion())) {
489 // default version in 0.5 files for existing primitives
490 Main.warn(tr("Normalizing value of attribute ''version'' of element {0} to {2}, API version is ''{3}''. Got {1}.", current.getUniqueId(), version, 1, "0.5"));
491 version= 1;
492 } else if (current.isNew() && ds.getVersion() != null && "0.5".equals(ds.getVersion())) {
493 // default version in 0.5 files for new primitives, no warning necessary. This is (was) legal in API 0.5
494 version= 0;
495 }
496 }
497 current.setVersion(version);
498
499 String action = parser.getAttributeValue(null, "action");
500 if (action == null) {
501 // do nothing
502 } else if ("delete".equals(action)) {
503 current.setDeleted(true);
504 current.setModified(current.isVisible());
505 } else if ("modify".equals(action)) {
506 current.setModified(true);
507 }
508
509 String v = parser.getAttributeValue(null, "changeset");
510 if (v == null) {
511 current.setChangesetId(0);
512 } else {
513 try {
514 current.setChangesetId(Integer.parseInt(v));
515 } catch(NumberFormatException e) {
516 if (current.isNew()) {
517 // for a new primitive we just log a warning
518 Main.info(tr("Illegal value for attribute ''changeset'' on new object {1}. Got {0}. Resetting to 0.", v, current.getUniqueId()));
519 current.setChangesetId(0);
520 } else {
521 // for an existing primitive this is a problem
522 throwException(tr("Illegal value for attribute ''changeset''. Got {0}.", v), e);
523 }
524 }
525 if (current.getChangesetId() <=0) {
526 if (current.isNew()) {
527 // for a new primitive we just log a warning
528 Main.info(tr("Illegal value for attribute ''changeset'' on new object {1}. Got {0}. Resetting to 0.", v, current.getUniqueId()));
529 current.setChangesetId(0);
530 } else {
531 // for an existing primitive this is a problem
532 throwException(tr("Illegal value for attribute ''changeset''. Got {0}.", v));
533 }
534 }
535 }
536 }
537
538 private long getLong(String name) throws XMLStreamException {
539 String value = parser.getAttributeValue(null, name);
540 if (value == null) {
541 throwException(tr("Missing required attribute ''{0}''.",name));
542 }
543 try {
544 return Long.parseLong(value);
545 } catch(NumberFormatException e) {
546 throwException(tr("Illegal long value for attribute ''{0}''. Got ''{1}''.",name, value), e);
547 }
548 return 0; // should not happen
549 }
550
551 private static class OsmParsingException extends XMLStreamException {
552
553 public OsmParsingException(String msg, Location location) {
554 super(msg); /* cannot use super(msg, location) because it messes with the message preventing localization */
555 this.location = location;
556 }
557
558 public OsmParsingException(String msg, Location location, Throwable th) {
559 super(msg, th);
560 this.location = location;
561 }
562
563 @Override
564 public String getMessage() {
565 String msg = super.getMessage();
566 if (msg == null) {
567 msg = getClass().getName();
568 }
569 if (getLocation() == null)
570 return msg;
571 msg = msg + " " + tr("(at line {0}, column {1})", getLocation().getLineNumber(), getLocation().getColumnNumber());
572 return msg;
573 }
574 }
575
576 /**
577 * Exception thrown after user cancelation.
578 */
579 private static final class OsmParsingCanceledException extends OsmParsingException implements ImportCancelException {
580 /**
581 * Constructs a new {@code OsmParsingCanceledException}.
582 * @param msg The error message
583 * @param location The parser location
584 */
585 public OsmParsingCanceledException(String msg, Location location) {
586 super(msg, location);
587 }
588 }
589
590 protected DataSet doParseDataSet(InputStream source, ProgressMonitor progressMonitor) throws IllegalDataException {
591 if (progressMonitor == null) {
592 progressMonitor = NullProgressMonitor.INSTANCE;
593 }
594 ProgressMonitor.CancelListener cancelListener = new ProgressMonitor.CancelListener() {
595 @Override public void operationCanceled() {
596 cancel = true;
597 }
598 };
599 progressMonitor.addCancelListener(cancelListener);
600 CheckParameterUtil.ensureParameterNotNull(source, "source");
601 try {
602 progressMonitor.beginTask(tr("Prepare OSM data...", 2));
603 progressMonitor.indeterminateSubTask(tr("Parsing OSM data..."));
604
605 InputStreamReader ir = UTFInputStreamReader.create(source);
606 XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(ir);
607 setParser(parser);
608 parse();
609 progressMonitor.worked(1);
610
611 progressMonitor.indeterminateSubTask(tr("Preparing data set..."));
612 prepareDataSet();
613 progressMonitor.worked(1);
614
615 // iterate over registered postprocessors and give them each a chance
616 // to modify the dataset we have just loaded.
617 if (postprocessors != null) {
618 for (OsmServerReadPostprocessor pp : postprocessors) {
619 pp.postprocessDataSet(getDataSet(), progressMonitor);
620 }
621 }
622 return getDataSet();
623 } catch(IllegalDataException e) {
624 throw e;
625 } catch(OsmParsingException e) {
626 throw new IllegalDataException(e.getMessage(), e);
627 } catch(XMLStreamException e) {
628 String msg = e.getMessage();
629 Pattern p = Pattern.compile("Message: (.+)");
630 Matcher m = p.matcher(msg);
631 if (m.find()) {
632 msg = m.group(1);
633 }
634 if (e.getLocation() != null)
635 throw new IllegalDataException(tr("Line {0} column {1}: ", e.getLocation().getLineNumber(), e.getLocation().getColumnNumber()) + msg, e);
636 else
637 throw new IllegalDataException(msg, e);
638 } catch(Exception e) {
639 throw new IllegalDataException(e);
640 } finally {
641 progressMonitor.finishTask();
642 progressMonitor.removeCancelListener(cancelListener);
643 }
644 }
645
646 /**
647 * Parse the given input source and return the dataset.
648 *
649 * @param source the source input stream. Must not be null.
650 * @param progressMonitor the progress monitor. If null, {@link NullProgressMonitor#INSTANCE} is assumed
651 *
652 * @return the dataset with the parsed data
653 * @throws IllegalDataException thrown if the an error was found while parsing the data from the source
654 * @throws IllegalArgumentException thrown if source is null
655 */
656 public static DataSet parseDataSet(InputStream source, ProgressMonitor progressMonitor) throws IllegalDataException {
657 return new OsmReader().doParseDataSet(source, progressMonitor);
658 }
659}
Note: See TracBrowser for help on using the repository browser.