Changeset 32130 in osm


Ignore:
Timestamp:
2016-03-27T23:17:02+02:00 (9 years ago)
Author:
donvip
Message:

fix #josm12694 - robustness to invalid KML colors

Location:
applications/editors/josm/plugins/opendata
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/geographic/KmlReader.java

    r32127 r32130  
    99import java.util.List;
    1010import java.util.Map;
     11import java.util.regex.Pattern;
    1112
    1213import javax.xml.stream.FactoryConfigurationError;
     
    3233
    3334    public static final String KML_PLACEMARK   = "Placemark";
    34     public static final String KML_NAME           = "name";
     35    public static final String KML_NAME        = "name";
    3536    public static final String KML_COLOR       = "color";
    3637    public static final String KML_SIMPLE_DATA = "SimpleData";
     
    4344    public static final String KML_COORDINATES = "coordinates";
    4445   
     46    public static Pattern COLOR_PATTERN = Pattern.compile("\\p{XDigit}{8}");
    4547
    4648    private XMLStreamReader parser;
     
    5456        InputStreamReader ir = UTFInputStreamReader.create(in);
    5557        XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(ir);
    56         //XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(in, UTF8);
    5758        return new KmlReader(parser).parseDoc();
    5859    }
     
    9293                if (parser.getLocalName().equals(KML_COLOR)) {
    9394                    String s = parser.getElementText();
    94                     // KML color format is aabbggrr, convert it to OSM (web) format: #rrggbb
    95                     String rgbColor = '#'+s.substring(6,8)+s.substring(4,6)+s.substring(2,4);
    96                     tags.put(KML_COLOR, rgbColor);
     95                    if (COLOR_PATTERN.matcher(s).matches()) {
     96                        // KML color format is aabbggrr, convert it to OSM (web) format: #rrggbb
     97                        tags.put(KML_COLOR, '#'+s.substring(6,8)+s.substring(4,6)+s.substring(2,4));
     98                    }
    9799                } else if (parser.getLocalName().equals(KML_NAME)) {
    98100                    tags.put(KML_NAME, parser.getElementText());
  • applications/editors/josm/plugins/opendata/test/unit/org/openstreetmap/josm/plugins/opendata/core/io/geographic/KmlReaderTest.java

    r31990 r32130  
    22package org.openstreetmap.josm.plugins.opendata.core.io.geographic;
    33
    4 import java.io.File;
    5 import java.io.FileInputStream;
     4import static org.junit.Assert.assertFalse;
     5import static org.junit.Assert.assertTrue;
     6
    67import java.io.IOException;
    78import java.io.InputStream;
     
    2829        JOSMFixture.createUnitTestFixture().init();
    2930    }
     31
     32    /**
     33     * Unit test of {@link KmlReader#COLOR_PATTERN}
     34     */
     35    @Test
     36    public void testColorPattern() {
     37        assertTrue(KmlReader.COLOR_PATTERN.matcher("00112233").matches());
     38        assertTrue(KmlReader.COLOR_PATTERN.matcher("44556677").matches());
     39        assertTrue(KmlReader.COLOR_PATTERN.matcher("8899aabb").matches());
     40        assertTrue(KmlReader.COLOR_PATTERN.matcher("CCDDEEFF").matches());
     41        assertFalse(KmlReader.COLOR_PATTERN.matcher("0011223").matches());
     42        assertFalse(KmlReader.COLOR_PATTERN.matcher("001122330").matches());
     43        assertFalse(KmlReader.COLOR_PATTERN.matcher("gg112233").matches());
     44        assertFalse(KmlReader.COLOR_PATTERN.matcher("red").matches());
     45        assertFalse(KmlReader.COLOR_PATTERN.matcher("yellow").matches());
     46    }
    3047   
     48    /**
     49     * Non-regression test for ticket <a href="https://josm.openstreetmap.de/ticket/12694">#12694</a>
     50     * @throws IOException if an error occurs during reading
     51     */
     52    @Test
     53    public void testTicket12694() throws IOException, XMLStreamException, FactoryConfigurationError {
     54        try (InputStream is = TestUtils.getRegressionDataStream(12694, "Alvinรณpolis_314946.kml")) {
     55            NonRegFunctionalTests.testGeneric("#12694", KmlReader.parseDataSet(is, null));
     56        }
     57    }
     58
    3159    /**
    3260     * Non-regression test for ticket <a href="https://josm.openstreetmap.de/ticket/10214">#10214</a>
     
    3967        }
    4068    }
    41    
     69
    4270    /**
    4371     * Non-regression test for ticket <a href="https://josm.openstreetmap.de/ticket/7714">#7714</a>
Note: See TracChangeset for help on using the changeset viewer.