Ticket #18258: 18258.5.patch
| File 18258.5.patch, 12.9 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; 8 10 import java.util.Objects; 9 11 import java.util.Set; 10 12 import java.util.TreeSet; … … 39 41 */ 40 42 public class OsmReader extends AbstractReader { 41 43 44 /** 45 * Options are used to change how the xml data is parsed. 46 * For example, {@link Options#CONVERT_UNKNOWN_TO_TAGS} is used to convert unknown 47 * XML attributes to a tag for the object. 48 */ 49 public enum Options { 50 /** Convert unknown XML attributes to tags */ 51 CONVERT_UNKNOWN_TO_TAGS, 52 /** Save the original id of an object (currently stored in `current_id`) */ 53 SAVE_ORIGINAL_ID 54 } 55 42 56 protected XMLStreamReader parser; 43 57 44 protected boolean convertUnknownToTags; 58 /** The {@link OsmReader.Options} to use when parsing the xml data */ 59 protected Collection<Options> options; 45 60 46 61 private static final Set<String> COMMON_XML_ATTRIBUTES = new TreeSet<>(); 47 62 … … 64 79 * @see #parseDataSet(InputStream, ProgressMonitor) 65 80 */ 66 81 protected OsmReader() { 67 this( false);82 this((Options) null); 68 83 } 69 84 70 85 /** 71 86 * constructor (for private and subclasses use only) 72 * @param convertUnknownToTags if true, keep unknown xml attributes as tags87 * @param options The options to use when reading data 73 88 * 74 89 * @see #parseDataSet(InputStream, ProgressMonitor) 75 * @since 1547090 * @since xxx 76 91 */ 77 protected OsmReader( boolean convertUnknownToTags) {92 protected OsmReader(Options... options) { 78 93 // Restricts visibility 79 this. convertUnknownToTags = convertUnknownToTags;94 this.options = options == null ? Collections.emptyList() : Arrays.asList(options); 80 95 } 81 96 82 97 protected void setParser(XMLStreamReader parser) { … … 425 440 parseAction(current, parser.getAttributeValue(null, "action")); 426 441 parseChangeset(current, parser.getAttributeValue(null, "changeset")); 427 442 428 if (convertUnknownToTags) { 443 if (options.contains(Options.SAVE_ORIGINAL_ID)) { 444 parseTag(current, "current_id", Long.toString(getLong("id"))); 445 } 446 if (options.contains(Options.CONVERT_UNKNOWN_TO_TAGS)) { 429 447 for (int i = 0; i < parser.getAttributeCount(); i++) { 430 448 if (!COMMON_XML_ATTRIBUTES.contains(parser.getAttributeLocalName(i))) { 431 449 parseTag(current, parser.getAttributeLocalName(i), parser.getAttributeValue(i)); … … 496 514 * @throws IllegalArgumentException if source is null 497 515 */ 498 516 public static DataSet parseDataSet(InputStream source, ProgressMonitor progressMonitor) throws IllegalDataException { 499 return parseDataSet(source, progressMonitor, false);517 return parseDataSet(source, progressMonitor, (Options) null); 500 518 } 501 519 502 520 /** … … 504 522 * 505 523 * @param source the source input stream. Must not be null. 506 524 * @param progressMonitor the progress monitor. If null, {@link NullProgressMonitor#INSTANCE} is assumed 507 * @param convertUnknownToTags true if unknown xml attributes should be kept as tags525 * @param options The options to use when parsing the dataset 508 526 * 509 527 * @return the dataset with the parsed data 510 528 * @throws IllegalDataException if an error was found while parsing the data from the source 511 529 * @throws IllegalArgumentException if source is null 512 * @since 15470530 * @since xxx 513 531 */ 514 public static DataSet parseDataSet(InputStream source, ProgressMonitor progressMonitor, boolean convertUnknownToTags)532 public static DataSet parseDataSet(InputStream source, ProgressMonitor progressMonitor, Options... options) 515 533 throws IllegalDataException { 516 return new OsmReader( convertUnknownToTags).doParseDataSet(source, progressMonitor);534 return new OsmReader(options).doParseDataSet(source, progressMonitor); 517 535 } 518 536 } -
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 }
