Changeset 16641 in josm


Ignore:
Timestamp:
2020-06-14T19:35:04+02:00 (4 weeks ago)
Author:
simon04
Message:

fix #18258 - OsmReader: Allow end user to know what the original id of a feature was (patch by taylor.smock, modified)

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/AbstractPrimitive.java

    r15820 r16641  
    735735                            "created_by",
    736736                            "converted_by",
     737                            "current_id",
    737738                            "geobase:datasetName",
    738739                            "geobase:uuid",
  • trunk/src/org/openstreetmap/josm/io/OsmReader.java

    r15470 r16641  
    55
    66import java.io.InputStream;
     7import java.util.Arrays;
    78import java.util.Collection;
     9import java.util.Collections;
    810import java.util.Objects;
    911import java.util.Set;
     
    4042public class OsmReader extends AbstractReader {
    4143
     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 XML attributes to a tag for the object.
     47     * @since 16641
     48     */
     49    public enum Options {
     50        /**
     51         * Convert unknown XML attributes to tags
     52         */
     53        CONVERT_UNKNOWN_TO_TAGS,
     54        /**
     55         * Save the original id of an object (currently stored in `current_id`)
     56         */
     57        SAVE_ORIGINAL_ID
     58    }
     59
    4260    protected XMLStreamReader parser;
    4361
    44     protected boolean convertUnknownToTags;
     62    /** The {@link OsmReader.Options} to use when parsing the xml data */
     63    protected final Collection<Options> options;
    4564
    4665    private static final Set<String> COMMON_XML_ATTRIBUTES = new TreeSet<>();
     
    6584     */
    6685    protected OsmReader() {
    67         this(false);
     86        this((Options) null);
    6887    }
    6988
    7089    /**
    7190     * constructor (for private and subclasses use only)
    72      * @param convertUnknownToTags if true, keep unknown xml attributes as tags
     91     * @param options The options to use when reading data
    7392     *
    7493     * @see #parseDataSet(InputStream, ProgressMonitor)
    75      * @since 15470
    76      */
    77     protected OsmReader(boolean convertUnknownToTags) {
     94     * @since 16641
     95     */
     96    protected OsmReader(Options... options) {
    7897        // Restricts visibility
    79         this.convertUnknownToTags = convertUnknownToTags;
     98        this.options = options == null ? Collections.emptyList() : Arrays.asList(options);
    8099    }
    81100
     
    426445            parseChangeset(current, parser.getAttributeValue(null, "changeset"));
    427446
    428             if (convertUnknownToTags) {
     447            if (options.contains(Options.SAVE_ORIGINAL_ID)) {
     448                parseTag(current, "current_id", Long.toString(getLong("id")));
     449            }
     450            if (options.contains(Options.CONVERT_UNKNOWN_TO_TAGS)) {
    429451                for (int i = 0; i < parser.getAttributeCount(); i++) {
    430452                    if (!COMMON_XML_ATTRIBUTES.contains(parser.getAttributeLocalName(i))) {
     
    497519     */
    498520    public static DataSet parseDataSet(InputStream source, ProgressMonitor progressMonitor) throws IllegalDataException {
    499         return parseDataSet(source, progressMonitor, false);
     521        return parseDataSet(source, progressMonitor, (Options) null);
    500522    }
    501523
     
    505527     * @param source the source input stream. Must not be null.
    506528     * @param progressMonitor the progress monitor. If null, {@link NullProgressMonitor#INSTANCE} is assumed
    507      * @param convertUnknownToTags true if unknown xml attributes should be kept as tags
     529     * @param options The options to use when parsing the dataset
    508530     *
    509531     * @return the dataset with the parsed data
    510532     * @throws IllegalDataException if an error was found while parsing the data from the source
    511533     * @throws IllegalArgumentException if source is null
    512      * @since 15470
    513      */
    514     public static DataSet parseDataSet(InputStream source, ProgressMonitor progressMonitor, boolean convertUnknownToTags)
     534     * @since 16641
     535     */
     536    public static DataSet parseDataSet(InputStream source, ProgressMonitor progressMonitor, Options... options)
    515537            throws IllegalDataException {
    516         return new OsmReader(convertUnknownToTags).doParseDataSet(source, progressMonitor);
     538        return new OsmReader(options).doParseDataSet(source, progressMonitor);
    517539    }
    518540}
  • trunk/test/unit/org/openstreetmap/josm/io/OsmReaderTest.java

    r15470 r16641  
    2323import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
    2424import org.openstreetmap.josm.gui.progress.ProgressMonitor;
     25import org.openstreetmap.josm.io.OsmReader.Options;
    2526import org.openstreetmap.josm.testutils.JOSMTestRules;
    2627
     
    8384        try (InputStream in = new ByteArrayInputStream(
    8485                ("<?xml version='1.0' encoding='UTF-8'?>" + osm).getBytes(StandardCharsets.UTF_8))) {
    85             assertTrue(OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE, parseUnknownAttributes).allPrimitives()
     86            assertTrue(OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE, Options.CONVERT_UNKNOWN_TO_TAGS).allPrimitives()
    8687                    .isEmpty());
     88        }
     89        testUnknown(osm, parseUnknownAttributes, true);
     90        testUnknown(osm, parseUnknownAttributes, true);
     91    }
     92
     93    private static void testUnknown(String osm, boolean parseUnknownAttributes, boolean keepOriginalId)
     94            throws Exception {
     95        try (InputStream in = new ByteArrayInputStream(
     96                ("<?xml version='1.0' encoding='UTF-8'?>" + osm).getBytes(StandardCharsets.UTF_8))) {
     97            assertTrue(OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE, Options.CONVERT_UNKNOWN_TO_TAGS, Options.SAVE_ORIGINAL_ID)
     98                    .allPrimitives().isEmpty());
    8799        }
    88100    }
     
    141153     * Test valid data.
    142154     * @param osm OSM data without XML prefix
    143      * @param parseUnknownAttributes if true, attempt to parse unknown xml attributes
     155     * @param options The options to use to parse the data
    144156     * @return parsed data set
    145157     * @throws Exception if any error occurs
    146158     */
    147     private static DataSet testValidData(String osm, boolean parseUnknownAttributes) throws Exception {
    148         try (InputStream in = new ByteArrayInputStream(
    149                 ("<?xml version='1.0' encoding='UTF-8'?>" + osm).getBytes(StandardCharsets.UTF_8))) {
    150             return OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE, parseUnknownAttributes);
     159    private static DataSet testValidData(String osm, Options... options) throws Exception {
     160        try (InputStream in = new ByteArrayInputStream(
     161                ("<?xml version='1.0' encoding='UTF-8'?>" + osm).getBytes(StandardCharsets.UTF_8))) {
     162            return OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE, options);
    151163        }
    152164    }
     
    183195        try (InputStream in = new ByteArrayInputStream(
    184196                ("<?xml version='1.0' encoding='UTF-8'?>" + osm).getBytes(StandardCharsets.UTF_8))) {
    185             OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE, parseUnknownAttributes);
     197            OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE, Options.CONVERT_UNKNOWN_TO_TAGS);
    186198            fail("should throw exception");
    187199        } catch (IllegalDataException e) {
     
    325337        String gdprChangeset = "<osm version='0.6'><node id='1' version='1' changeset='0'/></osm>";
    326338        testValidData(gdprChangeset);
    327         testValidData(gdprChangeset, true);
    328         testValidData(gdprChangeset, false);
     339        testValidData(gdprChangeset, Options.CONVERT_UNKNOWN_TO_TAGS);
     340        testValidData(gdprChangeset, (Options) null);
     341        testValidData(gdprChangeset, Options.SAVE_ORIGINAL_ID);
     342        testValidData(gdprChangeset, Options.values());
    329343    }
    330344
     
    406420                "<remark>runtime error: Query ran out of memory in \"query\" at line 5.</remark>\r\n" +
    407421                "</osm>";
    408         for (DataSet ds : Arrays.asList(testValidData(query), testValidData(query, true), testValidData(query, false))) {
     422        for (DataSet ds : Arrays.asList(testValidData(query), testValidData(query, Options.CONVERT_UNKNOWN_TO_TAGS), testValidData(query, (Options) null),
     423                testValidData(query, Options.SAVE_ORIGINAL_ID), testValidData(query, Options.values()))) {
    409424            assertEquals("runtime error: Query ran out of memory in \"query\" at line 5.", ds.getRemark());
    410425        }
     
    421436        DataSet ds = testValidData(testData);
    422437        assertEquals(0, ds.getNodes().iterator().next().getKeys().size());
    423 
    424         ds = testValidData(testData, true);
     438        assertEquals(1, ds.getNodes().iterator().next().getUniqueId());
     439
     440        ds = testValidData(testData, Options.CONVERT_UNKNOWN_TO_TAGS);
    425441        Node firstNode = ds.getNodes().iterator().next();
    426442        assertEquals(1, firstNode.getKeys().size());
    427443        assertEquals("randomvalue", firstNode.get("randomkey"));
    428 
    429         ds = testValidData(testData, false);
     444        assertEquals(1, ds.getNodes().iterator().next().getUniqueId());
     445
     446
     447        ds = testValidData(testData, (Options) null);
    430448        assertEquals(0, ds.getNodes().iterator().next().getKeys().size());
     449        assertEquals(1, ds.getNodes().iterator().next().getUniqueId());
    431450    }
    432451}
Note: See TracChangeset for help on using the changeset viewer.