Ticket #18258: 18258.4.patch
| File 18258.4.patch, 13.2 KB (added by , 6 years ago) |
|---|
-
src/org/openstreetmap/josm/data/osm/AbstractPrimitive.java
734 734 Arrays.asList( 735 735 "created_by", 736 736 "converted_by", 737 "current_id", 737 738 "geobase:datasetName", 738 739 "geobase:uuid", 739 740 "KSJ2:ADS", -
src/org/openstreetmap/josm/io/OsmReader.java
4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 6 import java.io.InputStream; 7 import java.util.Arrays; 7 8 import java.util.Collection; 9 import java.util.Collections; 10 import java.util.List; 8 11 import java.util.Objects; 9 12 import java.util.Set; 10 13 import java.util.TreeSet; … … 39 42 */ 40 43 public class OsmReader extends AbstractReader { 41 44 45 enum Options {CONVERT_UNKNOWN_TO_TAGS, SAVE_ORIGINAL_ID} 46 42 47 protected XMLStreamReader parser; 43 48 44 protected boolean convertUnknownToTags;49 protected List<Options> options; 45 50 46 51 private static final Set<String> COMMON_XML_ATTRIBUTES = new TreeSet<>(); 47 52 … … 64 69 * @see #parseDataSet(InputStream, ProgressMonitor) 65 70 */ 66 71 protected OsmReader() { 67 this( false);72 this((Options) null); 68 73 } 69 74 70 75 /** … … 73 78 * 74 79 * @see #parseDataSet(InputStream, ProgressMonitor) 75 80 * @since 15470 81 * @deprecated Use {@link OsmReader#OsmReader} with {@link OsmReader.Options#CONVERT_UNKNOWN_TO_TAGS} 76 82 */ 83 @Deprecated 77 84 protected OsmReader(boolean convertUnknownToTags) { 78 85 // Restricts visibility 79 this .convertUnknownToTags = convertUnknownToTags;86 this(convertUnknownToTags ? Options.CONVERT_UNKNOWN_TO_TAGS : (Options) null); 80 87 } 81 88 89 90 /** 91 * constructor (for private and subclasses use only) 92 * @param options The options to use when reading data 93 * 94 * @see #parseDataSet(InputStream, ProgressMonitor) 95 * @since xxx 96 */ 97 protected OsmReader(Options... options) { 98 // Restricts visibility 99 this.options = options == null ? Collections.emptyList() : Arrays.asList(options); 100 } 101 82 102 protected void setParser(XMLStreamReader parser) { 83 103 this.parser = parser; 84 104 } … … 425 445 parseAction(current, parser.getAttributeValue(null, "action")); 426 446 parseChangeset(current, parser.getAttributeValue(null, "changeset")); 427 447 428 if (convertUnknownToTags) { 448 if (options.contains(Options.SAVE_ORIGINAL_ID)) { 449 parseTag(current, "current_id", Long.toString(getLong("id"))); 450 } 451 if (options.contains(Options.CONVERT_UNKNOWN_TO_TAGS)) { 429 452 for (int i = 0; i < parser.getAttributeCount(); i++) { 430 453 if (!COMMON_XML_ATTRIBUTES.contains(parser.getAttributeLocalName(i))) { 431 454 parseTag(current, parser.getAttributeLocalName(i), parser.getAttributeValue(i)); … … 496 519 * @throws IllegalArgumentException if source is null 497 520 */ 498 521 public static DataSet parseDataSet(InputStream source, ProgressMonitor progressMonitor) throws IllegalDataException { 499 return parseDataSet(source, progressMonitor, false);522 return parseDataSet(source, progressMonitor, (Options) null); 500 523 } 501 524 502 525 /** … … 510 533 * @throws IllegalDataException if an error was found while parsing the data from the source 511 534 * @throws IllegalArgumentException if source is null 512 535 * @since 15470 536 * @deprecated Use {@link OsmReader#parseDataSet} with {@link OsmReader.Options} 513 537 */ 538 @Deprecated 514 539 public static DataSet parseDataSet(InputStream source, ProgressMonitor progressMonitor, boolean convertUnknownToTags) 515 540 throws IllegalDataException { 516 return new OsmReader(convertUnknownToTags).doParseDataSet(source, progressMonitor);541 return parseDataSet(source, progressMonitor, Options.CONVERT_UNKNOWN_TO_TAGS); 517 542 } 543 544 /** 545 * Parse the given input source and return the dataset. 546 * 547 * @param source the source input stream. Must not be null. 548 * @param progressMonitor the progress monitor. If null, {@link NullProgressMonitor#INSTANCE} is assumed 549 * @param options The options to use when parsing the dataset 550 * 551 * @return the dataset with the parsed data 552 * @throws IllegalDataException if an error was found while parsing the data from the source 553 * @throws IllegalArgumentException if source is null 554 * @since xxx 555 */ 556 public static DataSet parseDataSet(InputStream source, ProgressMonitor progressMonitor, Options... options) 557 throws IllegalDataException { 558 return new OsmReader(options).doParseDataSet(source, progressMonitor); 559 } 518 560 } -
test/unit/org/openstreetmap/josm/io/OsmReaderTest.java
6 6 import static org.junit.Assert.assertNull; 7 7 import static org.junit.Assert.assertTrue; 8 8 import static org.junit.Assert.fail; 9 import static org.junit.jupiter.api.Assertions.assertNotEquals; 9 10 10 11 import java.io.ByteArrayInputStream; 11 12 import java.io.InputStream; … … 22 23 import org.openstreetmap.josm.data.osm.Way; 23 24 import org.openstreetmap.josm.gui.progress.NullProgressMonitor; 24 25 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 26 import org.openstreetmap.josm.io.OsmReader.Options; 25 27 import org.openstreetmap.josm.testutils.JOSMTestRules; 26 28 27 29 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; … … 82 84 private static void testUnknown(String osm, boolean parseUnknownAttributes) throws Exception { 83 85 try (InputStream in = new ByteArrayInputStream( 84 86 ("<?xml version='1.0' encoding='UTF-8'?>" + osm).getBytes(StandardCharsets.UTF_8))) { 85 assertTrue(OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE, parseUnknownAttributes).allPrimitives()87 assertTrue(OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE, Options.CONVERT_UNKNOWN_TO_TAGS).allPrimitives() 86 88 .isEmpty()); 87 89 } 90 testUnknown(osm, parseUnknownAttributes, true); 91 testUnknown(osm, parseUnknownAttributes, true); 88 92 } 89 93 94 private static void testUnknown(String osm, boolean parseUnknownAttributes, boolean keepOriginalId) 95 throws Exception { 96 try (InputStream in = new ByteArrayInputStream( 97 ("<?xml version='1.0' encoding='UTF-8'?>" + osm).getBytes(StandardCharsets.UTF_8))) { 98 assertTrue(OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE, Options.CONVERT_UNKNOWN_TO_TAGS, Options.SAVE_ORIGINAL_ID) 99 .allPrimitives().isEmpty()); 100 } 101 } 102 90 103 /** 91 104 * Unit test of {@link OsmReader#parseUnknown} - root case. 92 105 * @throws Exception if any error occurs … … 147 160 private static DataSet testValidData(String osm, boolean parseUnknownAttributes) throws Exception { 148 161 try (InputStream in = new ByteArrayInputStream( 149 162 ("<?xml version='1.0' encoding='UTF-8'?>" + osm).getBytes(StandardCharsets.UTF_8))) { 150 return OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE, parseUnknownAttributes);163 return OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE, Options.CONVERT_UNKNOWN_TO_TAGS); 151 164 } 152 165 } 153 166 154 167 /** 168 * Test valid data. 169 * 170 * @param osm OSM data without XML prefix 171 * @param parseUnknownAttributes if true, attempt to parse unknown xml 172 * attributes 173 * @param keepOriginalId if true, keep the original id of the object (if 174 * a negative id) 175 * @return parsed data set 176 * @throws Exception if any error occurs 177 */ 178 private static DataSet testValidData(String osm, boolean parseUnknownAttributes, boolean keepOriginalId) 179 throws Exception { 180 try (InputStream in = new ByteArrayInputStream( 181 ("<?xml version='1.0' encoding='UTF-8'?>" + osm).getBytes(StandardCharsets.UTF_8))) { 182 return OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE, Options.CONVERT_UNKNOWN_TO_TAGS, Options.SAVE_ORIGINAL_ID); 183 } 184 } 185 186 /** 155 187 * Test invalid data. 156 188 * @param osm OSM data without XML prefix 157 189 * @param expectedError expected error message … … 182 214 throws Exception { 183 215 try (InputStream in = new ByteArrayInputStream( 184 216 ("<?xml version='1.0' encoding='UTF-8'?>" + osm).getBytes(StandardCharsets.UTF_8))) { 185 OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE, parseUnknownAttributes);217 OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE, Options.CONVERT_UNKNOWN_TO_TAGS); 186 218 fail("should throw exception"); 187 219 } catch (IllegalDataException e) { 188 220 assertEquals(expectedError, e.getMessage()); … … 326 358 testValidData(gdprChangeset); 327 359 testValidData(gdprChangeset, true); 328 360 testValidData(gdprChangeset, false); 361 testValidData(gdprChangeset, false, true); 362 testValidData(gdprChangeset, true, false); 363 testValidData(gdprChangeset, false, false); 364 testValidData(gdprChangeset, true, true); 329 365 } 330 366 331 367 /** … … 405 441 "<meta osm_base=\"2018-08-30T12:46:02Z\" areas=\"2018-08-30T12:40:02Z\"/>\r\n" + 406 442 "<remark>runtime error: Query ran out of memory in \"query\" at line 5.</remark>\r\n" + 407 443 "</osm>"; 408 for (DataSet ds : Arrays.asList(testValidData(query), testValidData(query, true), testValidData(query, false))) { 444 for (DataSet ds : Arrays.asList(testValidData(query), testValidData(query, true), testValidData(query, false), 445 testValidData(query, true, false), testValidData(query, false, false), testValidData(query, true, true), 446 testValidData(query, false, true))) { 409 447 assertEquals("runtime error: Query ran out of memory in \"query\" at line 5.", ds.getRemark()); 410 448 } 411 449 } … … 420 458 + "<node id='1' version='1' visible='true' changeset='82' randomkey='randomvalue'></node>" + "</osm>"; 421 459 DataSet ds = testValidData(testData); 422 460 assertEquals(0, ds.getNodes().iterator().next().getKeys().size()); 461 assertEquals(1, ds.getNodes().iterator().next().getUniqueId()); 423 462 424 ds = testValidData(testData, true); 425 Node firstNode = ds.getNodes().iterator().next(); 426 assertEquals(1, firstNode.getKeys().size()); 427 assertEquals("randomvalue", firstNode.get("randomkey")); 463 for (DataSet ds1 : Arrays.asList(testValidData(testData, true), testValidData(testData, true, false), 464 testValidData(testData, true, true))) { 465 ds = ds1; 466 Node firstNode = ds.getNodes().iterator().next(); 467 assertEquals(1, firstNode.getKeys().size()); 468 assertEquals("randomvalue", firstNode.get("randomkey")); 469 assertEquals(1, ds.getNodes().iterator().next().getUniqueId()); 470 } 428 471 429 ds = testValidData(testData, false); 430 assertEquals(0, ds.getNodes().iterator().next().getKeys().size()); 472 for (DataSet ds1 : Arrays.asList(testValidData(testData, false), testValidData(testData, false, false), 473 testValidData(testData, false, true))) { 474 ds = ds1; 475 assertEquals(0, ds.getNodes().iterator().next().getKeys().size()); 476 assertEquals(1, ds.getNodes().iterator().next().getUniqueId()); 477 } 431 478 } 479 480 /** 481 * Test reading a file with negative ids in osm primitives 482 * 483 * @throws Exception if any error occurs 484 */ 485 @Test 486 public void testNegativeIds() throws Exception { 487 String testData = "<osm version=\"0.6\" generator=\"fake generator\">" 488 + "<node id='-1' version='1' visible='true' changeset='82' randomkey='randomvalue'></node>" + "</osm>"; 489 DataSet ds = testValidData(testData); 490 assertNotEquals(-1L, ds.getNodes().iterator().next().getUniqueId()); 491 492 for (DataSet ds1 : Arrays.asList(testValidData(testData, true), testValidData(testData, true, false), 493 testValidData(testData, true, false))) { 494 ds = ds1; 495 assertNotEquals(-1L, ds.getNodes().iterator().next().getUniqueId()); 496 } 497 498 for (DataSet ds1 : Arrays.asList(testValidData(testData, true, true), testValidData(testData, false, true))) { 499 ds = ds1; 500 assertEquals(-1L, ds.getNodes().iterator().next().getUniqueId()); // if we set the id to the original id 501 assertEquals("-1", ds.getNodes().iterator().next().get("current_id")); // if we just add a tag 502 } 503 } 432 504 }
