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

Last change on this file since 8895 was 8846, checked in by Don-vip, 9 years ago

sonar - fb-contrib - minor performance improvements:

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