001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.io; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.io.IOException; 007import java.io.InputStream; 008import java.io.InputStreamReader; 009import java.text.MessageFormat; 010import java.time.DateTimeException; 011import java.util.ArrayList; 012import java.util.Collection; 013import java.util.HashMap; 014import java.util.List; 015import java.util.Map; 016import java.util.Map.Entry; 017import java.util.OptionalLong; 018import java.util.function.Consumer; 019 020import org.openstreetmap.josm.data.Bounds; 021import org.openstreetmap.josm.data.DataSource; 022import org.openstreetmap.josm.data.coor.LatLon; 023import org.openstreetmap.josm.data.osm.AbstractPrimitive; 024import org.openstreetmap.josm.data.osm.Changeset; 025import org.openstreetmap.josm.data.osm.DataSet; 026import org.openstreetmap.josm.data.osm.DownloadPolicy; 027import org.openstreetmap.josm.data.osm.Node; 028import org.openstreetmap.josm.data.osm.NodeData; 029import org.openstreetmap.josm.data.osm.OsmPrimitive; 030import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 031import org.openstreetmap.josm.data.osm.PrimitiveData; 032import org.openstreetmap.josm.data.osm.PrimitiveId; 033import org.openstreetmap.josm.data.osm.Relation; 034import org.openstreetmap.josm.data.osm.RelationData; 035import org.openstreetmap.josm.data.osm.RelationMember; 036import org.openstreetmap.josm.data.osm.RelationMemberData; 037import org.openstreetmap.josm.data.osm.SimplePrimitiveId; 038import org.openstreetmap.josm.data.osm.Tagged; 039import org.openstreetmap.josm.data.osm.UploadPolicy; 040import org.openstreetmap.josm.data.osm.User; 041import org.openstreetmap.josm.data.osm.Way; 042import org.openstreetmap.josm.data.osm.WayData; 043import org.openstreetmap.josm.gui.progress.NullProgressMonitor; 044import org.openstreetmap.josm.gui.progress.ProgressMonitor; 045import org.openstreetmap.josm.gui.util.LruCache; 046import org.openstreetmap.josm.tools.CheckParameterUtil; 047import org.openstreetmap.josm.tools.Logging; 048import org.openstreetmap.josm.tools.UncheckedParseException; 049import org.openstreetmap.josm.tools.Utils; 050import org.openstreetmap.josm.tools.date.DateUtils; 051 052/** 053 * Abstract Reader, allowing other implementations than OsmReader (PbfReader in PBF plugin for example) 054 * @author Vincent 055 * @since 4490 056 */ 057public abstract class AbstractReader { 058 059 /** Used by plugins to register themselves as data postprocessors. */ 060 private static volatile List<OsmServerReadPostprocessor> postprocessors; 061 062 protected boolean cancel; 063 064 /** 065 * Register a new postprocessor. 066 * @param pp postprocessor 067 * @see #deregisterPostprocessor 068 * @since 14119 (moved from OsmReader) 069 */ 070 public static void registerPostprocessor(OsmServerReadPostprocessor pp) { 071 if (postprocessors == null) { 072 postprocessors = new ArrayList<>(); 073 } 074 postprocessors.add(pp); 075 } 076 077 /** 078 * Deregister a postprocessor previously registered with {@link #registerPostprocessor}. 079 * @param pp postprocessor 080 * @see #registerPostprocessor 081 * @since 14119 (moved from OsmReader) 082 */ 083 public static void deregisterPostprocessor(OsmServerReadPostprocessor pp) { 084 if (postprocessors != null) { 085 postprocessors.remove(pp); 086 } 087 } 088 089 /** 090 * A lookup table to avoid calling {@link String#intern()} unnecessarily. 091 */ 092 private final Map<String, String> tagMap = new HashMap<>(); 093 094 /** 095 * The dataset to add parsed objects to. 096 */ 097 protected DataSet ds = new DataSet(); 098 099 protected Changeset uploadChangeset; 100 101 /** the map from external ids to read OsmPrimitives. External ids are 102 * longs too, but in contrast to internal ids negative values are used 103 * to identify primitives unknown to the OSM server 104 */ 105 protected final Map<PrimitiveId, OsmPrimitive> externalIdMap = new HashMap<>(); 106 107 /** 108 * Data structure for the remaining way objects 109 */ 110 protected final Map<Long, Collection<Long>> ways = new HashMap<>(); 111 112 /** 113 * Data structure for relation objects 114 */ 115 protected final Map<Long, Collection<RelationMemberData>> relations = new HashMap<>(); 116 117 /** 118 * Replies the parsed data set 119 * 120 * @return the parsed data set 121 */ 122 public DataSet getDataSet() { 123 return ds; 124 } 125 126 /** 127 * Iterate over registered postprocessors and give them each a chance to modify the dataset we have just loaded. 128 * @param progressMonitor Progress monitor 129 */ 130 protected void callPostProcessors(ProgressMonitor progressMonitor) { 131 if (postprocessors != null) { 132 for (OsmServerReadPostprocessor pp : postprocessors) { 133 pp.postprocessDataSet(getDataSet(), progressMonitor); 134 } 135 } 136 } 137 138 /** 139 * Processes the parsed nodes after parsing. Just adds them to 140 * the dataset 141 * 142 */ 143 protected void processNodesAfterParsing() { 144 for (OsmPrimitive primitive: externalIdMap.values()) { 145 if (primitive instanceof Node) { 146 this.ds.addPrimitive(primitive); 147 } 148 } 149 } 150 151 /** 152 * Processes the ways after parsing. Rebuilds the list of nodes of each way and 153 * adds the way to the dataset 154 * 155 * @throws IllegalDataException if a data integrity problem is detected 156 */ 157 protected void processWaysAfterParsing() throws IllegalDataException { 158 for (Entry<Long, Collection<Long>> entry : ways.entrySet()) { 159 Long externalWayId = entry.getKey(); 160 Way w = (Way) externalIdMap.get(new SimplePrimitiveId(externalWayId, OsmPrimitiveType.WAY)); 161 List<Node> wayNodes = new ArrayList<>(); 162 for (long id : entry.getValue()) { 163 Node n = (Node) externalIdMap.get(new SimplePrimitiveId(id, OsmPrimitiveType.NODE)); 164 if (n == null) { 165 if (id <= 0) 166 throw new IllegalDataException( 167 tr("Way with external ID ''{0}'' includes missing node with external ID ''{1}''.", 168 Long.toString(externalWayId), 169 Long.toString(id))); 170 // create an incomplete node if necessary 171 n = (Node) ds.getPrimitiveById(id, OsmPrimitiveType.NODE); 172 if (n == null) { 173 n = new Node(id); 174 ds.addPrimitive(n); 175 } 176 } 177 if (n.isDeleted()) { 178 Logging.info(tr("Deleted node {0} is part of way {1}", Long.toString(id), Long.toString(w.getId()))); 179 } else { 180 wayNodes.add(n); 181 } 182 } 183 w.setNodes(wayNodes); 184 if (w.hasIncompleteNodes()) { 185 Logging.info(tr("Way {0} with {1} nodes is incomplete because at least one node was missing in the loaded data.", 186 Long.toString(externalWayId), w.getNodesCount())); 187 } 188 ds.addPrimitive(w); 189 } 190 } 191 192 /** 193 * Completes the parsed relations with its members. 194 * 195 * @throws IllegalDataException if a data integrity problem is detected, i.e. if a 196 * relation member refers to a local primitive which wasn't available in the data 197 */ 198 protected void processRelationsAfterParsing() throws IllegalDataException { 199 200 // First add all relations to make sure that when relation reference other relation, the referenced will be already in dataset 201 for (Long externalRelationId : relations.keySet()) { 202 Relation relation = (Relation) externalIdMap.get( 203 new SimplePrimitiveId(externalRelationId, OsmPrimitiveType.RELATION) 204 ); 205 ds.addPrimitive(relation); 206 } 207 208 for (Entry<Long, Collection<RelationMemberData>> entry : relations.entrySet()) { 209 Long externalRelationId = entry.getKey(); 210 Relation relation = (Relation) externalIdMap.get( 211 new SimplePrimitiveId(externalRelationId, OsmPrimitiveType.RELATION) 212 ); 213 List<RelationMember> relationMembers = new ArrayList<>(); 214 for (RelationMemberData rm : entry.getValue()) { 215 // lookup the member from the map of already created primitives 216 OsmPrimitive primitive = externalIdMap.get(new SimplePrimitiveId(rm.getMemberId(), rm.getMemberType())); 217 218 if (primitive == null) { 219 if (rm.getMemberId() <= 0) 220 // relation member refers to a primitive with a negative id which was not 221 // found in the data. This is always a data integrity problem and we abort 222 // with an exception 223 // 224 throw new IllegalDataException( 225 tr("Relation with external id ''{0}'' refers to a missing primitive with external id ''{1}''.", 226 Long.toString(externalRelationId), 227 Long.toString(rm.getMemberId()))); 228 229 // member refers to OSM primitive which was not present in the parsed data 230 // -> create a new incomplete primitive and add it to the dataset 231 // 232 primitive = ds.getPrimitiveById(rm.getMemberId(), rm.getMemberType()); 233 if (primitive == null) { 234 switch (rm.getMemberType()) { 235 case NODE: 236 primitive = new Node(rm.getMemberId()); break; 237 case WAY: 238 primitive = new Way(rm.getMemberId()); break; 239 case RELATION: 240 primitive = new Relation(rm.getMemberId()); break; 241 default: throw new AssertionError(); // can't happen 242 } 243 244 ds.addPrimitive(primitive); 245 externalIdMap.put(new SimplePrimitiveId(rm.getMemberId(), rm.getMemberType()), primitive); 246 } 247 } 248 if (primitive.isDeleted()) { 249 Logging.info(tr("Deleted member {0} is used by relation {1}", 250 Long.toString(primitive.getId()), Long.toString(relation.getId()))); 251 } else { 252 relationMembers.add(new RelationMember(rm.getRole(), primitive)); 253 } 254 } 255 relation.setMembers(relationMembers); 256 } 257 } 258 259 protected void processChangesetAfterParsing() { 260 if (uploadChangeset != null) { 261 for (Map.Entry<String, String> e : uploadChangeset.getKeys().entrySet()) { 262 ds.addChangeSetTag(e.getKey(), e.getValue()); 263 } 264 } 265 } 266 267 protected final void prepareDataSet() throws IllegalDataException { 268 ds.beginUpdate(); 269 try { 270 processNodesAfterParsing(); 271 processWaysAfterParsing(); 272 processRelationsAfterParsing(); 273 processChangesetAfterParsing(); 274 } finally { 275 ds.endUpdate(); 276 } 277 } 278 279 protected abstract DataSet doParseDataSet(InputStream source, ProgressMonitor progressMonitor) throws IllegalDataException; 280 281 /** 282 * An interface for reading binary data 283 * @since 18695 284 */ 285 @FunctionalInterface 286 protected interface BinaryParserWorker { 287 /** 288 * Effectively parses the file, depending on the binary format (PBF, etc.) 289 * @param ir input stream reader 290 * @throws IllegalDataException in case of invalid data 291 * @throws IOException in case of I/O error 292 */ 293 void accept(InputStream ir) throws IllegalDataException, IOException; 294 } 295 296 @FunctionalInterface 297 protected interface ParserWorker { 298 /** 299 * Effectively parses the file, depending on the format (XML, JSON, etc.) 300 * @param ir input stream reader 301 * @throws IllegalDataException in case of invalid data 302 * @throws IOException in case of I/O error 303 */ 304 void accept(InputStreamReader ir) throws IllegalDataException, IOException; 305 } 306 307 protected final DataSet doParseDataSet(InputStream source, ProgressMonitor progressMonitor, BinaryParserWorker parserWorker) 308 throws IllegalDataException { 309 return this.doParseDataSet(source, progressMonitor, (Object) parserWorker); 310 } 311 312 protected final DataSet doParseDataSet(InputStream source, ProgressMonitor progressMonitor, ParserWorker parserWorker) 313 throws IllegalDataException { 314 return this.doParseDataSet(source, progressMonitor, (Object) parserWorker); 315 } 316 317 private DataSet doParseDataSet(InputStream source, ProgressMonitor progressMonitor, Object parserWorker) 318 throws IllegalDataException { 319 if (progressMonitor == null) { 320 progressMonitor = NullProgressMonitor.INSTANCE; 321 } 322 ProgressMonitor.CancelListener cancelListener = () -> cancel = true; 323 progressMonitor.addCancelListener(cancelListener); 324 CheckParameterUtil.ensureParameterNotNull(source, "source"); 325 try { 326 progressMonitor.beginTask(tr("Prepare OSM data..."), 4); // read, prepare, post-process, render 327 progressMonitor.indeterminateSubTask(tr("Parsing OSM data...")); 328 329 if (parserWorker instanceof ParserWorker) { 330 try (InputStreamReader ir = UTFInputStreamReader.create(source)) { 331 ((ParserWorker) parserWorker).accept(ir); 332 } 333 } else if (parserWorker instanceof BinaryParserWorker) { 334 ((BinaryParserWorker) parserWorker).accept(source); 335 } else { 336 throw new IllegalArgumentException("Unknown parser worker type: " + parserWorker.getClass()); 337 } 338 progressMonitor.worked(1); 339 340 boolean readOnly = getDataSet().isLocked(); 341 342 progressMonitor.indeterminateSubTask(tr("Preparing data set...")); 343 if (readOnly) { 344 getDataSet().unlock(); 345 } 346 prepareDataSet(); 347 if (readOnly) { 348 getDataSet().lock(); 349 } 350 progressMonitor.worked(1); 351 progressMonitor.indeterminateSubTask(tr("Post-processing data set...")); 352 // iterate over registered postprocessors and give them each a chance 353 // to modify the dataset we have just loaded. 354 callPostProcessors(progressMonitor); 355 progressMonitor.worked(1); 356 progressMonitor.indeterminateSubTask(tr("Rendering data set...")); 357 // Make sure postprocessors did not change the read-only state 358 if (readOnly && !getDataSet().isLocked()) { 359 getDataSet().lock(); 360 } 361 return getDataSet(); 362 } catch (IllegalDataException e) { 363 throw e; 364 } catch (IOException e) { 365 throw new IllegalDataException(e); 366 } finally { 367 for (OsmPrimitiveType dataType : OsmPrimitiveType.dataValues()) { 368 OptionalLong minId = externalIdMap.entrySet().parallelStream() 369 .filter(e -> e.getKey().getType() == dataType) 370 .mapToLong(e -> e.getValue().getUniqueId()).min(); 371 synchronized (dataType.getDataClass()) { 372 if (minId.isPresent() && minId.getAsLong() < dataType.getIdGenerator().currentUniqueId()) { 373 dataType.getIdGenerator().advanceUniqueId(minId.getAsLong()); 374 } 375 } 376 } 377 this.tagMap.clear(); 378 progressMonitor.finishTask(); 379 progressMonitor.removeCancelListener(cancelListener); 380 } 381 } 382 383 protected final long getLong(String name, String value) throws IllegalDataException { 384 if (value == null) { 385 throw new IllegalDataException(tr("Missing required attribute ''{0}''.", name)); 386 } 387 try { 388 return Long.parseLong(value); 389 } catch (NumberFormatException e) { 390 throw new IllegalDataException(tr("Illegal long value for attribute ''{0}''. Got ''{1}''.", name, value), e); 391 } 392 } 393 394 protected final void parseVersion(String version) throws IllegalDataException { 395 validateVersion(version); 396 ds.setVersion(version); 397 } 398 399 private static void validateVersion(String version) throws IllegalDataException { 400 if (version == null) { 401 throw new IllegalDataException(tr("Missing mandatory attribute ''{0}''.", "version")); 402 } 403 if (!"0.6".equals(version)) { 404 throw new IllegalDataException(tr("Unsupported version: {0}", version)); 405 } 406 } 407 408 protected final void parseDownloadPolicy(String key, String downloadPolicy) throws IllegalDataException { 409 parsePolicy(key, downloadPolicy, policy -> ds.setDownloadPolicy(DownloadPolicy.of(policy))); 410 } 411 412 protected final void parseUploadPolicy(String key, String uploadPolicy) throws IllegalDataException { 413 parsePolicy(key, uploadPolicy, policy -> ds.setUploadPolicy(UploadPolicy.of(policy))); 414 } 415 416 private static void parsePolicy(String key, String policy, Consumer<String> consumer) throws IllegalDataException { 417 if (policy != null) { 418 try { 419 consumer.accept(policy); 420 } catch (IllegalArgumentException e) { 421 throw new IllegalDataException(MessageFormat.format( 422 "Illegal value for attribute ''{0}''. Got ''{1}''.", key, policy), e); 423 } 424 } 425 } 426 427 protected final void parseLocked(String locked) { 428 if ("true".equalsIgnoreCase(locked)) { 429 ds.lock(); 430 } 431 } 432 433 protected final void parseBounds(String generator, String minlon, String minlat, String maxlon, String maxlat, String origin) 434 throws IllegalDataException { 435 if (minlon != null && maxlon != null && minlat != null && maxlat != null) { 436 if (origin == null) { 437 origin = generator; 438 } 439 Bounds bounds = new Bounds( 440 Double.parseDouble(minlat), Double.parseDouble(minlon), 441 Double.parseDouble(maxlat), Double.parseDouble(maxlon)); 442 if (bounds.isOutOfTheWorld()) { 443 Bounds copy = new Bounds(bounds); 444 bounds.normalize(); 445 Logging.info("Bbox " + copy + " is out of the world, normalized to " + bounds); 446 } 447 ds.addDataSource(new DataSource(bounds, origin)); 448 } else { 449 throw new IllegalDataException(tr("Missing mandatory attributes on element ''bounds''. " + 450 "Got minlon=''{0}'',minlat=''{1}'',maxlon=''{2}'',maxlat=''{3}'', origin=''{4}''.", 451 minlon, minlat, maxlon, maxlat, origin 452 )); 453 } 454 } 455 456 protected final void parseId(PrimitiveData current, long id) throws IllegalDataException { 457 current.setId(id); 458 if (current.getUniqueId() == 0) { 459 throw new IllegalDataException(tr("Illegal object with ID=0.")); 460 } 461 } 462 463 private final Map<String, Integer> timestampCache = new LruCache<>(30); 464 465 protected final void parseTimestamp(PrimitiveData current, String time) { 466 if (Utils.isEmpty(time)) { 467 return; 468 } 469 try { 470 int timestamp = timestampCache.computeIfAbsent(time, t -> (int) DateUtils.parseInstant(t).getEpochSecond()); 471 current.setRawTimestamp(timestamp); 472 } catch (UncheckedParseException | DateTimeException e) { 473 Logging.error(e); 474 } 475 } 476 477 private static User createUser(String uid, String name) throws IllegalDataException { 478 if (uid == null) { 479 if (name == null) 480 return null; 481 return User.createLocalUser(name); 482 } 483 try { 484 return User.createOsmUser(Long.parseLong(uid), name); 485 } catch (NumberFormatException e) { 486 throw new IllegalDataException(MessageFormat.format("Illegal value for attribute ''uid''. Got ''{0}''.", uid), e); 487 } 488 } 489 490 protected final void parseUser(PrimitiveData current, String user, long uid) { 491 current.setUser(User.createOsmUser(uid, user)); 492 } 493 494 protected final void parseUser(PrimitiveData current, String user, String uid) throws IllegalDataException { 495 current.setUser(createUser(uid, user)); 496 } 497 498 protected final void parseVisible(PrimitiveData current, String visible) { 499 if (visible != null) { 500 current.setVisible(Boolean.parseBoolean(visible)); 501 } 502 } 503 504 protected final void parseVersion(PrimitiveData current, String versionString) throws IllegalDataException { 505 int version = 0; 506 if (versionString != null) { 507 try { 508 version = Integer.parseInt(versionString); 509 } catch (NumberFormatException e) { 510 throw new IllegalDataException( 511 tr("Illegal value for attribute ''version'' on OSM primitive with ID {0}. Got {1}.", 512 Long.toString(current.getUniqueId()), versionString), e); 513 } 514 parseVersion(current, version); 515 } else { 516 // version expected for OSM primitives with an id assigned by the server (id > 0), since API 0.6 517 if (!current.isNew() && ds.getVersion() != null && "0.6".equals(ds.getVersion())) { 518 throw new IllegalDataException( 519 tr("Missing attribute ''version'' on OSM primitive with ID {0}.", Long.toString(current.getUniqueId()))); 520 } 521 } 522 } 523 524 protected final void parseVersion(PrimitiveData current, int version) throws IllegalDataException { 525 if (ds.getVersion().equals("0.6")) { 526 if (version <= 0 && !current.isNew()) { 527 throw new IllegalDataException( 528 tr("Illegal value for attribute ''version'' on OSM primitive with ID {0}. Got {1}.", 529 Long.toString(current.getUniqueId()), version)); 530 } else if (version < 0 && current.isNew()) { 531 Logging.warn(tr("Normalizing value of attribute ''version'' of element {0} to {2}, API version is ''{3}''. Got {1}.", 532 current.getUniqueId(), version, 0, "0.6")); 533 version = 0; 534 } 535 } else { // should not happen. API version has been checked before 536 throw new IllegalDataException(tr("Unknown or unsupported API version. Got {0}.", ds.getVersion())); 537 } 538 current.setVersion(version); 539 } 540 541 protected final void parseAction(PrimitiveData current, String action) { 542 if (action == null) { 543 // do nothing 544 } else if ("delete".equals(action)) { 545 current.setDeleted(true); 546 current.setModified(current.isVisible()); 547 } else if ("modify".equals(action)) { 548 current.setModified(true); 549 } 550 } 551 552 private static void handleIllegalChangeset(PrimitiveData current, IllegalArgumentException e, Object v) 553 throws IllegalDataException { 554 Logging.debug(e.getMessage()); 555 if (current.isNew()) { 556 // for a new primitive we just log a warning 557 Logging.info(tr("Illegal value for attribute ''changeset'' on new object {1}. Got {0}. Resetting to 0.", 558 v, current.getUniqueId())); 559 current.setChangesetId(0); 560 } else { 561 // for an existing primitive this is a problem 562 throw new IllegalDataException(tr("Illegal value for attribute ''changeset''. Got {0}.", v), e); 563 } 564 } 565 566 protected final void parseChangeset(PrimitiveData current, String v) throws IllegalDataException { 567 if (v == null) { 568 current.setChangesetId(0); 569 } else { 570 try { 571 parseChangeset(current, Integer.parseInt(v)); 572 } catch (NumberFormatException e) { 573 handleIllegalChangeset(current, e, v); 574 } 575 } 576 } 577 578 protected final void parseChangeset(PrimitiveData current, int v) throws IllegalDataException { 579 try { 580 current.setChangesetId(v); 581 } catch (IllegalArgumentException e) { 582 handleIllegalChangeset(current, e, v); 583 } catch (IllegalStateException e) { 584 // thrown for positive changeset id on new primitives 585 Logging.debug(e); 586 Logging.info(e.getMessage()); 587 current.setChangesetId(0); 588 } 589 if (current.getChangesetId() <= 0) { 590 if (current.isNew()) { 591 // for a new primitive we just log a warning 592 Logging.info(tr("Illegal value for attribute ''changeset'' on new object {1}. Got {0}. Resetting to 0.", 593 v, current.getUniqueId())); 594 current.setChangesetId(0); 595 } else if (current.getChangesetId() < 0) { 596 // for an existing primitive this is a problem only for negative ids (GDPR extracts are set to 0) 597 throw new IllegalDataException(tr("Illegal value for attribute ''changeset''. Got {0}.", v)); 598 } 599 } 600 } 601 602 protected final void parseTag(Tagged t, String key, String value) throws IllegalDataException { 603 if (key == null || value == null) { 604 throw new IllegalDataException(tr("Missing key or value attribute in tag.")); 605 } else if (Utils.isStripEmpty(key) && t instanceof AbstractPrimitive) { 606 // #14199: Empty keys as ignored by AbstractPrimitive#put, but it causes problems to fix existing data 607 // Drop the tag on import, but flag the primitive as modified 608 ((AbstractPrimitive) t).setModified(true); 609 } else { 610 t.put(this.tagMap.computeIfAbsent(key, Utils::intern), this.tagMap.computeIfAbsent(value, Utils::intern)); 611 } 612 } 613 614 @FunctionalInterface 615 protected interface CommonReader { 616 /** 617 * Reads the common primitive attributes and sets them in {@code pd} 618 * @param pd primitive data to update 619 * @throws IllegalDataException in case of invalid data 620 */ 621 void accept(PrimitiveData pd) throws IllegalDataException; 622 } 623 624 @FunctionalInterface 625 protected interface NodeReader { 626 /** 627 * Reads the node tags. 628 * @param n node 629 * @throws IllegalDataException in case of invalid data 630 */ 631 void accept(NodeData n) throws IllegalDataException; 632 } 633 634 @FunctionalInterface 635 protected interface WayReader { 636 /** 637 * Reads the way nodes and tags. 638 * @param w way 639 * @param nodeIds collection of resulting node ids 640 * @throws IllegalDataException in case of invalid data 641 */ 642 void accept(WayData w, Collection<Long> nodeIds) throws IllegalDataException; 643 } 644 645 @FunctionalInterface 646 protected interface RelationReader { 647 /** 648 * Reads the relation members and tags. 649 * @param r relation 650 * @param members collection of resulting members 651 * @throws IllegalDataException in case of invalid data 652 */ 653 void accept(RelationData r, Collection<RelationMemberData> members) throws IllegalDataException; 654 } 655 656 private static boolean areLatLonDefined(String lat, String lon) { 657 return lat != null && lon != null; 658 } 659 660 private static boolean areLatLonDefined(double lat, double lon) { 661 return !Double.isNaN(lat) && !Double.isNaN(lon); 662 } 663 664 protected OsmPrimitive buildPrimitive(PrimitiveData pd) { 665 OsmPrimitive p; 666 if (pd.getUniqueId() < pd.getIdGenerator().currentUniqueId()) { 667 p = pd.getType().newInstance(pd.getUniqueId(), true); 668 pd.getIdGenerator().advanceUniqueId(pd.getUniqueId()); 669 } else { 670 p = pd.getType().newVersionedInstance(pd.getId(), pd.getVersion()); 671 } 672 p.setVisible(pd.isVisible()); 673 p.load(pd); 674 externalIdMap.put(pd.getPrimitiveId(), p); 675 return p; 676 } 677 678 private Node addNode(NodeData nd, NodeReader nodeReader) throws IllegalDataException { 679 nodeReader.accept(nd); 680 return (Node) buildPrimitive(nd); 681 } 682 683 protected final Node parseNode(double lat, double lon, CommonReader commonReader, NodeReader nodeReader) 684 throws IllegalDataException { 685 NodeData nd = new NodeData(0); 686 LatLon ll = null; 687 if (areLatLonDefined(lat, lon)) { 688 try { 689 ll = new LatLon(lat, lon); 690 nd.setCoor(ll); 691 } catch (NumberFormatException e) { 692 Logging.trace(e); 693 } 694 } 695 commonReader.accept(nd); 696 if (areLatLonDefined(lat, lon) && (ll == null || !ll.isValid())) { 697 throw new IllegalDataException(tr("Illegal value for attributes ''lat'', ''lon'' on node with ID {0}. Got ''{1}'', ''{2}''.", 698 Long.toString(nd.getId()), lat, lon)); 699 } 700 return addNode(nd, nodeReader); 701 } 702 703 protected final Node parseNode(String lat, String lon, CommonReader commonReader, NodeReader nodeReader) 704 throws IllegalDataException { 705 NodeData nd = new NodeData(0); 706 LatLon ll = null; 707 if (areLatLonDefined(lat, lon)) { 708 try { 709 ll = new LatLon(Double.parseDouble(lat), Double.parseDouble(lon)); 710 nd.setCoor(ll); 711 } catch (NumberFormatException e) { 712 Logging.trace(e); 713 } 714 } 715 commonReader.accept(nd); 716 if (areLatLonDefined(lat, lon) && (ll == null || !ll.isValid())) { 717 throw new IllegalDataException(tr("Illegal value for attributes ''lat'', ''lon'' on node with ID {0}. Got ''{1}'', ''{2}''.", 718 Long.toString(nd.getId()), lat, lon)); 719 } 720 return addNode(nd, nodeReader); 721 } 722 723 protected final Way parseWay(CommonReader commonReader, WayReader wayReader) throws IllegalDataException { 724 WayData wd = new WayData(0); 725 commonReader.accept(wd); 726 727 Collection<Long> nodeIds = new ArrayList<>(); 728 wayReader.accept(wd, nodeIds); 729 if (wd.isDeleted() && !nodeIds.isEmpty()) { 730 Logging.info(tr("Deleted way {0} contains nodes", Long.toString(wd.getUniqueId()))); 731 nodeIds = new ArrayList<>(); 732 } 733 ways.put(wd.getUniqueId(), nodeIds); 734 return (Way) buildPrimitive(wd); 735 } 736 737 protected final Relation parseRelation(CommonReader commonReader, RelationReader relationReader) throws IllegalDataException { 738 RelationData rd = new RelationData(0); 739 commonReader.accept(rd); 740 741 Collection<RelationMemberData> members = new ArrayList<>(); 742 relationReader.accept(rd, members); 743 if (rd.isDeleted() && !members.isEmpty()) { 744 Logging.info(tr("Deleted relation {0} contains members", Long.toString(rd.getUniqueId()))); 745 members = new ArrayList<>(); 746 } 747 relations.put(rd.getUniqueId(), members); 748 return (Relation) buildPrimitive(rd); 749 } 750 751 protected final RelationMemberData parseRelationMember(RelationData r, String ref, String type, String role) throws IllegalDataException { 752 if (ref == null) { 753 throw new IllegalDataException(tr("Missing attribute ''ref'' on member in relation {0}.", 754 Long.toString(r.getUniqueId()))); 755 } 756 try { 757 return parseRelationMember(r, Long.parseLong(ref), type, role); 758 } catch (NumberFormatException e) { 759 throw new IllegalDataException(tr("Illegal value for attribute ''ref'' on member in relation {0}. Got {1}", 760 Long.toString(r.getUniqueId()), ref), e); 761 } 762 } 763 764 protected final RelationMemberData parseRelationMember(RelationData r, long id, String type, String role) throws IllegalDataException { 765 if (id == 0) { 766 throw new IllegalDataException(tr("Incomplete <member> specification with ref=0")); 767 } 768 if (type == null) { 769 throw new IllegalDataException(tr("Missing attribute ''type'' on member {0} in relation {1}.", 770 Long.toString(id), Long.toString(r.getUniqueId()))); 771 } 772 try { 773 return new RelationMemberData(role, OsmPrimitiveType.fromApiTypeName(type), id); 774 } catch (IllegalArgumentException e) { 775 throw new IllegalDataException(tr("Illegal value for attribute ''type'' on member {0} in relation {1}. Got {2}.", 776 Long.toString(id), Long.toString(r.getUniqueId()), type), e); 777 } 778 } 779}