Changeset 15470 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2019-10-22T23:54:14+02:00 (5 years ago)
Author:
Don-vip
Message:

fix #18249 - Allow unknown xml attributes to be added as tags (patch by taylor.smock)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/OsmReader.java

    r14266 r15470  
    77import java.util.Collection;
    88import java.util.Objects;
     9import java.util.Set;
     10import java.util.TreeSet;
    911import java.util.regex.Matcher;
    1012import java.util.regex.Pattern;
     
    4042    protected XMLStreamReader parser;
    4143
     44    protected boolean convertUnknownToTags;
     45
     46    private static final Set<String> COMMON_XML_ATTRIBUTES = new TreeSet<>();
     47
     48    static {
     49        COMMON_XML_ATTRIBUTES.add("id");
     50        COMMON_XML_ATTRIBUTES.add("timestamp");
     51        COMMON_XML_ATTRIBUTES.add("user");
     52        COMMON_XML_ATTRIBUTES.add("uid");
     53        COMMON_XML_ATTRIBUTES.add("visible");
     54        COMMON_XML_ATTRIBUTES.add("version");
     55        COMMON_XML_ATTRIBUTES.add("action");
     56        COMMON_XML_ATTRIBUTES.add("changeset");
     57        COMMON_XML_ATTRIBUTES.add("lat");
     58        COMMON_XML_ATTRIBUTES.add("lon");
     59    }
     60
    4261    /**
    4362     * constructor (for private and subclasses use only)
     
    4665     */
    4766    protected OsmReader() {
     67        this(false);
     68    }
     69
     70    /**
     71     * constructor (for private and subclasses use only)
     72     * @param convertUnknownToTags if true, keep unknown xml attributes as tags
     73     *
     74     * @see #parseDataSet(InputStream, ProgressMonitor)
     75     * @since 15470
     76     */
     77    protected OsmReader(boolean convertUnknownToTags) {
    4878        // Restricts visibility
     79        this.convertUnknownToTags = convertUnknownToTags;
    4980    }
    5081
     
    394425            parseAction(current, parser.getAttributeValue(null, "action"));
    395426            parseChangeset(current, parser.getAttributeValue(null, "changeset"));
     427
     428            if (convertUnknownToTags) {
     429                for (int i = 0; i < parser.getAttributeCount(); i++) {
     430                    if (!COMMON_XML_ATTRIBUTES.contains(parser.getAttributeLocalName(i))) {
     431                        parseTag(current, parser.getAttributeLocalName(i), parser.getAttributeValue(i));
     432                    }
     433                }
     434            }
    396435        } catch (UncheckedParseException | XMLStreamException e) {
    397436            throw new IllegalDataException(e);
     
    458497     */
    459498    public static DataSet parseDataSet(InputStream source, ProgressMonitor progressMonitor) throws IllegalDataException {
    460         return new OsmReader().doParseDataSet(source, progressMonitor);
     499        return parseDataSet(source, progressMonitor, false);
     500    }
     501
     502    /**
     503     * Parse the given input source and return the dataset.
     504     *
     505     * @param source the source input stream. Must not be null.
     506     * @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
     508     *
     509     * @return the dataset with the parsed data
     510     * @throws IllegalDataException if an error was found while parsing the data from the source
     511     * @throws IllegalArgumentException if source is null
     512     * @since 15470
     513     */
     514    public static DataSet parseDataSet(InputStream source, ProgressMonitor progressMonitor, boolean convertUnknownToTags)
     515            throws IllegalDataException {
     516        return new OsmReader(convertUnknownToTags).doParseDataSet(source, progressMonitor);
    461517    }
    462518}
Note: See TracChangeset for help on using the changeset viewer.