Ticket #18249: 18249.patch

File 18249.patch, 4.5 KB (added by taylor.smock, 5 years ago)

Allow unknown xml attributes to be added as tags, if requested. May have some slowdowns (since it now has a for loop with a switch).

  • src/org/openstreetmap/josm/io/OsmReader.java

     
    3939
    4040    protected XMLStreamReader parser;
    4141
     42    protected boolean convertUnknownToTags;
     43
    4244    /**
    4345     * constructor (for private and subclasses use only)
    4446     *
     
    4547     * @see #parseDataSet(InputStream, ProgressMonitor)
    4648     */
    4749    protected OsmReader() {
     50        this(false);
     51    }
     52
     53    /**
     54     * constructor (for private and subclasses use only)
     55     * @param convertUnknownToTags if true, keep unknown xml attributes as tags
     56     *
     57     * @see #parseDataSet(InputStream, ProgressMonitor)
     58     * @since xxx
     59     */
     60    protected OsmReader(boolean convertUnknownToTags) {
    4861        // Restricts visibility
     62        this.convertUnknownToTags = convertUnknownToTags;
    4963    }
    5064
    5165    protected void setParser(XMLStreamReader parser) {
     
    386400     */
    387401    private void readCommon(PrimitiveData current) throws IllegalDataException {
    388402        try {
    389             parseId(current, getLong("id"));
    390             parseTimestamp(current, parser.getAttributeValue(null, "timestamp"));
    391             parseUser(current, parser.getAttributeValue(null, "user"), parser.getAttributeValue(null, "uid"));
    392             parseVisible(current, parser.getAttributeValue(null, "visible"));
    393             parseVersion(current, parser.getAttributeValue(null, "version"));
    394             parseAction(current, parser.getAttributeValue(null, "action"));
    395             parseChangeset(current, parser.getAttributeValue(null, "changeset"));
     403            for (int i = 0; i < parser.getAttributeCount(); i++) {
     404                switch (parser.getAttributeLocalName(i)) {
     405                case "id":
     406                    parseId(current, getLong("id"));
     407                    break;
     408                case "timestamp":
     409                    parseTimestamp(current, parser.getAttributeValue(null, "timestamp"));
     410                    break;
     411                case "user":
     412                case "uid":
     413                    parseUser(current, parser.getAttributeValue(null, "user"), parser.getAttributeValue(null, "uid"));
     414                    break;
     415                case "visible":
     416                    parseVisible(current, parser.getAttributeValue(null, "visible"));
     417                    break;
     418                case "version":
     419                    parseVersion(current, parser.getAttributeValue(null, "version"));
     420                    break;
     421                case "action":
     422                    parseAction(current, parser.getAttributeValue(null, "action"));
     423                    break;
     424                case "changeset":
     425                    parseChangeset(current, parser.getAttributeValue(null, "changeset"));
     426                    break;
     427                case "lat":
     428                case "lon":
     429                    break;
     430                default:
     431                    if (convertUnknownToTags) {
     432                        parseTag(current, parser.getAttributeLocalName(i), parser.getAttributeValue(i));
     433                    }
     434                }
     435            }
    396436        } catch (UncheckedParseException | XMLStreamException e) {
    397437            throw new IllegalDataException(e);
    398438        }
     
    457497     * @throws IllegalArgumentException if source is null
    458498     */
    459499    public static DataSet parseDataSet(InputStream source, ProgressMonitor progressMonitor) throws IllegalDataException {
    460         return new OsmReader().doParseDataSet(source, progressMonitor);
     500        return parseDataSet(source, progressMonitor, false);
    461501    }
     502
     503    /**
     504     * Parse the given input source and return the dataset.
     505     *
     506     * @param source the source input stream. Must not be null.
     507     * @param progressMonitor the progress monitor. If null, {@link NullProgressMonitor#INSTANCE} is assumed
     508     * @param convertUnknownToTags true if unknown xml attributes should be kept as tags
     509     *
     510     * @return the dataset with the parsed data
     511     * @throws IllegalDataException if an error was found while parsing the data from the source
     512     * @throws IllegalArgumentException if source is null
     513     * @since xxx
     514     */
     515    public static DataSet parseDataSet(InputStream source, ProgressMonitor progressMonitor, boolean convertUnknownToTags)
     516            throws IllegalDataException {
     517        return new OsmReader(convertUnknownToTags).doParseDataSet(source, progressMonitor);
     518    }
    462519}