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

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

fix copyright/license headers globally

  • Property svn:eol-style set to native
File size: 25.0 KB
RevLine 
[8378]1// License: GPL. For details, see LICENSE file.
[283]2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.InputStream;
[4413]7import java.io.InputStreamReader;
[2852]8import java.text.MessageFormat;
[362]9import java.util.ArrayList;
[283]10import java.util.Collection;
[6316]11import java.util.List;
[4413]12import java.util.regex.Matcher;
13import java.util.regex.Pattern;
[283]14
[4530]15import javax.xml.stream.Location;
[4413]16import javax.xml.stream.XMLInputFactory;
17import javax.xml.stream.XMLStreamConstants;
18import javax.xml.stream.XMLStreamException;
[4530]19import javax.xml.stream.XMLStreamReader;
[319]20
[6248]21import org.openstreetmap.josm.Main;
[286]22import org.openstreetmap.josm.data.Bounds;
[7575]23import org.openstreetmap.josm.data.DataSource;
[283]24import org.openstreetmap.josm.data.coor.LatLon;
[4414]25import org.openstreetmap.josm.data.osm.Changeset;
[283]26import org.openstreetmap.josm.data.osm.DataSet;
[582]27import org.openstreetmap.josm.data.osm.Node;
[2932]28import org.openstreetmap.josm.data.osm.NodeData;
[2444]29import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
[2932]30import org.openstreetmap.josm.data.osm.PrimitiveData;
[343]31import org.openstreetmap.josm.data.osm.Relation;
[2932]32import org.openstreetmap.josm.data.osm.RelationData;
[4490]33import org.openstreetmap.josm.data.osm.RelationMemberData;
[4414]34import org.openstreetmap.josm.data.osm.Tagged;
[283]35import org.openstreetmap.josm.data.osm.User;
36import org.openstreetmap.josm.data.osm.Way;
[2932]37import org.openstreetmap.josm.data.osm.WayData;
[2604]38import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
[1811]39import org.openstreetmap.josm.gui.progress.ProgressMonitor;
[2852]40import org.openstreetmap.josm.tools.CheckParameterUtil;
[7299]41import org.openstreetmap.josm.tools.date.DateUtils;
[283]42
43/**
[2115]44 * Parser for the Osm Api. Read from an input stream and construct a dataset out of it.
[283]45 *
[4413]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.
[283]49 */
[4490]50public class OsmReader extends AbstractReader {
[4413]51
[4530]52 protected XMLStreamReader parser;
[4413]53
[5859]54 protected boolean cancel;
55
[4645]56 /** Used by plugins to register themselves as data postprocessors. */
[8126]57 private static volatile List<OsmServerReadPostprocessor> postprocessors;
[4645]58
59 /** register a new postprocessor */
60 public static void registerPostprocessor(OsmServerReadPostprocessor pp) {
61 if (postprocessors == null) {
[7005]62 postprocessors = new ArrayList<>();
[4645]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
[1772]74 /**
[4530]75 * constructor (for private and subclasses use only)
[1811]76 *
[5881]77 * @see #parseDataSet(InputStream, ProgressMonitor)
[1772]78 */
[4530]79 protected OsmReader() {
[1772]80 }
81
[4530]82 protected void setParser(XMLStreamReader parser) {
[4413]83 this.parser = parser;
[1690]84 }
[1353]85
[6621]86 protected void throwException(String msg, Throwable th) throws XMLStreamException {
87 throw new OsmParsingException(msg, parser.getLocation(), th);
88 }
89
[4413]90 protected void throwException(String msg) throws XMLStreamException {
91 throw new OsmParsingException(msg, parser.getLocation());
92 }
[1677]93
[4530]94 protected void parse() throws XMLStreamException {
[4413]95 int event = parser.getEventType();
96 while (true) {
97 if (event == XMLStreamConstants.START_ELEMENT) {
[4530]98 parseRoot();
[4645]99 } else if (event == XMLStreamConstants.END_ELEMENT)
[4413]100 return;
101 if (parser.hasNext()) {
102 event = parser.next();
103 } else {
104 break;
105 }
106 }
107 parser.close();
108 }
[4645]109
[4530]110 protected void parseRoot() throws XMLStreamException {
[7012]111 if ("osm".equals(parser.getLocalName())) {
[4530]112 parseOsm();
113 } else {
114 parseUnknown();
115 }
116 }
[2070]117
[4413]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"));
[2070]122 }
[7013]123 if (!"0.6".equals(v)) {
[4413]124 throwException(tr("Unsupported version: {0}", v));
125 }
[4414]126 ds.setVersion(v);
[5025]127 String upload = parser.getAttributeValue(null, "upload");
128 if (upload != null) {
129 ds.setUploadDiscouraged(!Boolean.parseBoolean(upload));
130 }
[4413]131 String generator = parser.getAttributeValue(null, "generator");
[4414]132 Long uploadChangesetId = null;
133 if (parser.getAttributeValue(null, "upload-changeset") != null) {
134 uploadChangesetId = getLong("upload-changeset");
135 }
[4413]136 while (true) {
137 int event = parser.next();
[6070]138
[5859]139 if (cancel) {
140 cancel = false;
[6621]141 throw new OsmParsingCanceledException(tr("Reading was canceled"), parser.getLocation());
[5859]142 }
[6070]143
[4413]144 if (event == XMLStreamConstants.START_ELEMENT) {
[7012]145 switch (parser.getLocalName()) {
146 case "bounds":
[4413]147 parseBounds(generator);
[7012]148 break;
149 case "node":
[4413]150 parseNode();
[7012]151 break;
152 case "way":
[4413]153 parseWay();
[7012]154 break;
155 case "relation":
[4413]156 parseRelation();
[7012]157 break;
158 case "changeset":
[4414]159 parseChangeset(uploadChangesetId);
[7012]160 break;
161 default:
[4530]162 parseUnknown();
[4413]163 }
[4645]164 } else if (event == XMLStreamConstants.END_ELEMENT)
[4413]165 return;
166 }
167 }
[2070]168
[4413]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();
[6248]185 Main.info("Bbox " + copy + " is out of the world, normalized to " + bounds);
[4413]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 ));
[2070]194 }
[4413]195 jumpToEnd();
196 }
[1169]197
[4532]198 protected Node parseNode() throws XMLStreamException {
[4413]199 NodeData nd = new NodeData();
[5326]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 }
[4413]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) {
[7012]213 if ("tag".equals(parser.getLocalName())) {
[4413]214 parseTag(n);
215 } else {
[4530]216 parseUnknown();
[4413]217 }
[4645]218 } else if (event == XMLStreamConstants.END_ELEMENT)
[4532]219 return n;
[4413]220 }
221 }
[3129]222
[4532]223 protected Way parseWay() throws XMLStreamException {
[4413]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);
[1690]230
[7005]231 Collection<Long> nodeIds = new ArrayList<>();
[4413]232 while (true) {
233 int event = parser.next();
234 if (event == XMLStreamConstants.START_ELEMENT) {
[7012]235 switch (parser.getLocalName()) {
236 case "nd":
[4413]237 nodeIds.add(parseWayNode(w));
[7012]238 break;
239 case "tag":
[4413]240 parseTag(w);
[7012]241 break;
242 default:
[4530]243 parseUnknown();
[4413]244 }
245 } else if (event == XMLStreamConstants.END_ELEMENT) {
246 break;
247 }
248 }
[6093]249 if (w.isDeleted() && !nodeIds.isEmpty()) {
[6248]250 Main.info(tr("Deleted way {0} contains nodes", w.getUniqueId()));
[7005]251 nodeIds = new ArrayList<>();
[4413]252 }
253 ways.put(wd.getUniqueId(), nodeIds);
[4532]254 return w;
[4413]255 }
[1690]256
[4413]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 }
[1169]272
[4532]273 protected Relation parseRelation() throws XMLStreamException {
[4413]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);
[343]280
[7005]281 Collection<RelationMemberData> members = new ArrayList<>();
[4413]282 while (true) {
283 int event = parser.next();
284 if (event == XMLStreamConstants.START_ELEMENT) {
[7012]285 switch (parser.getLocalName()) {
286 case "member":
[4413]287 members.add(parseRelationMember(r));
[7012]288 break;
289 case "tag":
[4413]290 parseTag(r);
[7012]291 break;
292 default:
[4530]293 parseUnknown();
[4413]294 }
295 } else if (event == XMLStreamConstants.END_ELEMENT) {
296 break;
297 }
298 }
[6093]299 if (r.isDeleted() && !members.isEmpty()) {
[6248]300 Main.info(tr("Deleted relation {0} contains members", r.getUniqueId()));
[7005]301 members = new ArrayList<>();
[4413]302 }
303 relations.put(rd.getUniqueId(), members);
[4532]304 return r;
[4413]305 }
[283]306
[4413]307 private RelationMemberData parseRelationMember(Relation r) throws XMLStreamException {
[4490]308 String role = null;
309 OsmPrimitiveType type = null;
310 long id = 0;
[4413]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 {
[4490]316 id = Long.parseLong(value);
[4413]317 } catch(NumberFormatException e) {
[6621]318 throwException(tr("Illegal value for attribute ''ref'' on member in relation {0}. Got {1}", Long.toString(r.getUniqueId()),value), e);
[4413]319 }
320 value = parser.getAttributeValue(null, "type");
321 if (value == null) {
[4490]322 throwException(tr("Missing attribute ''type'' on member {0} in relation {1}.", Long.toString(id), Long.toString(r.getUniqueId())));
[4413]323 }
324 try {
[4490]325 type = OsmPrimitiveType.fromApiTypeName(value);
[4413]326 } catch(IllegalArgumentException e) {
[6621]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);
[4413]328 }
329 value = parser.getAttributeValue(null, "role");
[4490]330 role = value;
[1677]331
[4490]332 if (id == 0) {
[4413]333 throwException(tr("Incomplete <member> specification with ref=0"));
334 }
335 jumpToEnd();
[4490]336 return new RelationMemberData(role, type, id);
[4413]337 }
[1169]338
[4414]339 private void parseChangeset(Long uploadChangesetId) throws XMLStreamException {
340
[5996]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);
[4414]348 while (true) {
349 int event = parser.next();
350 if (event == XMLStreamConstants.START_ELEMENT) {
[7012]351 if ("tag".equals(parser.getLocalName())) {
[4414]352 parseTag(uploadChangeset);
353 } else {
[4530]354 parseUnknown();
[4414]355 }
[4645]356 } else if (event == XMLStreamConstants.END_ELEMENT)
[4414]357 return;
358 }
359 } else {
360 jumpToEnd(false);
361 }
362 }
363
364 private void parseTag(Tagged t) throws XMLStreamException {
[4413]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 }
[4414]370 t.put(key.intern(), value.intern());
[4413]371 jumpToEnd();
372 }
[1169]373
[4530]374 protected void parseUnknown(boolean printWarning) throws XMLStreamException {
[4414]375 if (printWarning) {
[6248]376 Main.info(tr("Undefined element ''{0}'' found in input stream. Skipping.", parser.getLocalName()));
[4414]377 }
[4413]378 while (true) {
379 int event = parser.next();
380 if (event == XMLStreamConstants.START_ELEMENT) {
[4530]381 parseUnknown(false); /* no more warning for inner elements */
[4645]382 } else if (event == XMLStreamConstants.END_ELEMENT)
[4413]383 return;
384 }
385 }
[1169]386
[4530]387 protected void parseUnknown() throws XMLStreamException {
388 parseUnknown(true);
[4413]389 }
390
[4414]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 *
[4856]395 * This is basically the same code as parseUnknown(), except for the warnings, which
[4414]396 * are displayed for inner elements and not at top level.
397 */
398 private void jumpToEnd(boolean printWarning) throws XMLStreamException {
[4413]399 while (true) {
400 int event = parser.next();
401 if (event == XMLStreamConstants.START_ELEMENT) {
[4530]402 parseUnknown(printWarning);
[4645]403 } else if (event == XMLStreamConstants.END_ELEMENT)
[4413]404 return;
[1690]405 }
[4413]406 }
[283]407
[4414]408 private void jumpToEnd() throws XMLStreamException {
409 jumpToEnd(true);
410 }
411
[4413]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);
[1690]417 }
[4413]418 try {
419 long id = Long.parseLong(uid);
420 return User.createOsmUser(id, name);
421 } catch(NumberFormatException e) {
[6621]422 throwException(MessageFormat.format("Illegal value for attribute ''uid''. Got ''{0}''.", uid), e);
[4413]423 }
424 return null;
425 }
[283]426
[4413]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."));
[1690]434 }
[1677]435
[4413]436 String time = parser.getAttributeValue(null, "timestamp");
437 if (time != null && time.length() != 0) {
438 current.setTimestamp(DateUtils.fromString(time));
439 }
[1169]440
[4413]441 String user = parser.getAttributeValue(null, "user");
442 String uid = parser.getAttributeValue(null, "uid");
443 current.setUser(createUser(uid, user));
[2070]444
[4413]445 String visible = parser.getAttributeValue(null, "visible");
446 if (visible != null) {
447 current.setVisible(Boolean.parseBoolean(visible));
448 }
449
450 String versionString = parser.getAttributeValue(null, "version");
451 int version = 0;
452 if (versionString != null) {
453 try {
454 version = Integer.parseInt(versionString);
455 } catch(NumberFormatException e) {
[7869]456 throwException(tr("Illegal value for attribute ''version'' on OSM primitive with ID {0}. Got {1}.",
457 Long.toString(current.getUniqueId()), versionString), e);
[1690]458 }
[7012]459 switch (ds.getVersion()) {
460 case "0.6":
[6936]461 if (version <= 0 && !current.isNew()) {
[7869]462 throwException(tr("Illegal value for attribute ''version'' on OSM primitive with ID {0}. Got {1}.",
463 Long.toString(current.getUniqueId()), versionString));
[6936]464 } else if (version < 0 && current.isNew()) {
[7869]465 Main.warn(tr("Normalizing value of attribute ''version'' of element {0} to {2}, API version is ''{3}''. Got {1}.",
466 current.getUniqueId(), version, 0, "0.6"));
[4413]467 version = 0;
[2070]468 }
[7012]469 break;
470 default:
[4413]471 // should not happen. API version has been checked before
472 throwException(tr("Unknown or unsupported API version. Got {0}.", ds.getVersion()));
[2070]473 }
[4413]474 } else {
475 // version expected for OSM primitives with an id assigned by the server (id > 0), since API 0.6
[7012]476 if (!current.isNew() && ds.getVersion() != null && "0.6".equals(ds.getVersion())) {
[4413]477 throwException(tr("Missing attribute ''version'' on OSM primitive with ID {0}.", Long.toString(current.getUniqueId())));
[2070]478 }
[4413]479 }
480 current.setVersion(version);
[2604]481
[4413]482 String action = parser.getAttributeValue(null, "action");
483 if (action == null) {
484 // do nothing
[7012]485 } else if ("delete".equals(action)) {
[4413]486 current.setDeleted(true);
487 current.setModified(current.isVisible());
[7012]488 } else if ("modify".equals(action)) {
[4413]489 current.setModified(true);
490 }
491
492 String v = parser.getAttributeValue(null, "changeset");
493 if (v == null) {
494 current.setChangesetId(0);
495 } else {
496 try {
497 current.setChangesetId(Integer.parseInt(v));
[7081]498 } catch (IllegalArgumentException e) {
499 Main.debug(e.getMessage());
[6936]500 if (current.isNew()) {
[4413]501 // for a new primitive we just log a warning
[6248]502 Main.info(tr("Illegal value for attribute ''changeset'' on new object {1}. Got {0}. Resetting to 0.", v, current.getUniqueId()));
[4413]503 current.setChangesetId(0);
504 } else {
505 // for an existing primitive this is a problem
[6621]506 throwException(tr("Illegal value for attribute ''changeset''. Got {0}.", v), e);
[2604]507 }
[7081]508 } catch (IllegalStateException e) {
509 // thrown for positive changeset id on new primitives
510 Main.info(e.getMessage());
511 current.setChangesetId(0);
[4413]512 }
[7081]513 if (current.getChangesetId() <= 0) {
[6936]514 if (current.isNew()) {
[4413]515 // for a new primitive we just log a warning
[6248]516 Main.info(tr("Illegal value for attribute ''changeset'' on new object {1}. Got {0}. Resetting to 0.", v, current.getUniqueId()));
[4413]517 current.setChangesetId(0);
518 } else {
519 // for an existing primitive this is a problem
520 throwException(tr("Illegal value for attribute ''changeset''. Got {0}.", v));
[2604]521 }
522 }
[1690]523 }
[4413]524 }
[283]525
[4413]526 private long getLong(String name) throws XMLStreamException {
527 String value = parser.getAttributeValue(null, name);
528 if (value == null) {
529 throwException(tr("Missing required attribute ''{0}''.",name));
530 }
531 try {
532 return Long.parseLong(value);
533 } catch(NumberFormatException e) {
[6621]534 throwException(tr("Illegal long value for attribute ''{0}''. Got ''{1}''.",name, value), e);
[4413]535 }
536 return 0; // should not happen
537 }
538
539 private static class OsmParsingException extends XMLStreamException {
540
541 public OsmParsingException(String msg, Location location) {
542 super(msg); /* cannot use super(msg, location) because it messes with the message preventing localization */
543 this.location = location;
544 }
545
546 public OsmParsingException(String msg, Location location, Throwable th) {
547 super(msg, th);
548 this.location = location;
549 }
550
551 @Override
552 public String getMessage() {
553 String msg = super.getMessage();
554 if (msg == null) {
555 msg = getClass().getName();
[2070]556 }
[4413]557 if (getLocation() == null)
558 return msg;
[7869]559 msg += " " + tr("(at line {0}, column {1})", getLocation().getLineNumber(), getLocation().getColumnNumber());
560 int offset = getLocation().getCharacterOffset();
561 if (offset > -1) {
562 msg += ". "+ tr("{0} bytes have been read", offset);
563 }
[4413]564 return msg;
565 }
566 }
567
[6621]568 /**
569 * Exception thrown after user cancelation.
570 */
571 private static final class OsmParsingCanceledException extends OsmParsingException implements ImportCancelException {
572 /**
573 * Constructs a new {@code OsmParsingCanceledException}.
574 * @param msg The error message
575 * @param location The parser location
576 */
577 public OsmParsingCanceledException(String msg, Location location) {
578 super(msg, location);
579 }
580 }
581
[4530]582 protected DataSet doParseDataSet(InputStream source, ProgressMonitor progressMonitor) throws IllegalDataException {
[2604]583 if (progressMonitor == null) {
584 progressMonitor = NullProgressMonitor.INSTANCE;
585 }
[5859]586 ProgressMonitor.CancelListener cancelListener = new ProgressMonitor.CancelListener() {
587 @Override public void operationCanceled() {
588 cancel = true;
589 }
590 };
591 progressMonitor.addCancelListener(cancelListener);
[2852]592 CheckParameterUtil.ensureParameterNotNull(source, "source");
[1690]593 try {
[2070]594 progressMonitor.beginTask(tr("Prepare OSM data...", 2));
[2751]595 progressMonitor.indeterminateSubTask(tr("Parsing OSM data..."));
[3372]596
[7033]597 try (InputStreamReader ir = UTFInputStreamReader.create(source)) {
598 XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(ir);
599 setParser(parser);
600 parse();
601 }
[2070]602 progressMonitor.worked(1);
[1353]603
[2751]604 progressMonitor.indeterminateSubTask(tr("Preparing data set..."));
[4530]605 prepareDataSet();
[1811]606 progressMonitor.worked(1);
[4645]607
608 // iterate over registered postprocessors and give them each a chance
609 // to modify the dataset we have just loaded.
610 if (postprocessors != null) {
611 for (OsmServerReadPostprocessor pp : postprocessors) {
612 pp.postprocessDataSet(getDataSet(), progressMonitor);
613 }
614 }
[4530]615 return getDataSet();
[2070]616 } catch(IllegalDataException e) {
617 throw e;
[4413]618 } catch(OsmParsingException e) {
[2070]619 throw new IllegalDataException(e.getMessage(), e);
[4413]620 } catch(XMLStreamException e) {
621 String msg = e.getMessage();
622 Pattern p = Pattern.compile("Message: (.+)");
623 Matcher m = p.matcher(msg);
624 if (m.find()) {
625 msg = m.group(1);
626 }
[4645]627 if (e.getLocation() != null)
[4413]628 throw new IllegalDataException(tr("Line {0} column {1}: ", e.getLocation().getLineNumber(), e.getLocation().getColumnNumber()) + msg, e);
[4645]629 else
[4413]630 throw new IllegalDataException(msg, e);
[2070]631 } catch(Exception e) {
632 throw new IllegalDataException(e);
[1811]633 } finally {
634 progressMonitor.finishTask();
[5859]635 progressMonitor.removeCancelListener(cancelListener);
[1811]636 }
[1690]637 }
[4645]638
[4530]639 /**
640 * Parse the given input source and return the dataset.
641 *
642 * @param source the source input stream. Must not be null.
[5266]643 * @param progressMonitor the progress monitor. If null, {@link NullProgressMonitor#INSTANCE} is assumed
[4530]644 *
645 * @return the dataset with the parsed data
[8291]646 * @throws IllegalDataException if the an error was found while parsing the data from the source
647 * @throws IllegalArgumentException if source is null
[4530]648 */
649 public static DataSet parseDataSet(InputStream source, ProgressMonitor progressMonitor) throws IllegalDataException {
650 return new OsmReader().doParseDataSet(source, progressMonitor);
651 }
[283]652}
Note: See TracBrowser for help on using the repository browser.