Ticket #14490: patch-14490.patch

File patch-14490.patch, 7.3 KB (added by ToniE, 3 weeks ago)

patch with correct file name

  • src/org/openstreetmap/josm/io/remotecontrol/AddTagsDialog.java

     
    283283     * @since 15316
    284284     */
    285285    public static Map<String, String> parseUrlTagsToKeyValues(String urlSection) {
    286         Map<String, String> tags = TextTagParser.readTagsByRegexp(urlSection, "\\|", "(.*?)=(.*?)", false);
     286        Map<String, String> tags = TextTagParser.readTagsByRegexp(urlSection, "(?<!\\\\)\\|", "(.*?)=(.*?)", false, true);
    287287        return tags == null ? Collections.emptyMap() : tags;
    288288    }
    289289
  • src/org/openstreetmap/josm/tools/TextTagParser.java

     
    4646     * @param splitRegex - text is split into parts with this delimiter
    4747     * @param tagRegex - each part is matched against this regex
    4848     * @param unescapeTextInQuotes - if true, matched tag and value will be analyzed more thoroughly
     49     * @param unescapePipe - if true, then replace all '\|' by '|' (issue #14490)
    4950     * @return map of tags
    5051     */
    51     public static Map<String, String> readTagsByRegexp(String text, String splitRegex, String tagRegex, boolean unescapeTextInQuotes) {
     52    public static Map<String, String> readTagsByRegexp(String text, String splitRegex, String tagRegex, boolean unescapeTextInQuotes, boolean unescapePipe) {
    5253         String[] lines = text.split(splitRegex, -1);
    5354         Pattern p = Pattern.compile(tagRegex);
    5455         Map<String, String> tags = new LinkedHashMap<>();
     
    6566                     v = unescape(v);
    6667                     if (k == null || v == null) return null;
    6768                 }
     69                 if (unescapePipe) {
     70                     k = k.replaceAll("\\\\\\|","|");
     71                     v = v.replaceAll("\\\\\\|","|");
     72                 }
    6873                 tags.put(k, v);
    6974            } else {
    7075                return null;
     
    99104
    100105        // Format
    101106        // tag1\tval1\ntag2\tval2\n
    102         tags = readTagsByRegexp(buf, "[\\r\\n]+", ".*?([a-zA-Z0-9:_]+).*\\t(.*?)", false);
     107        tags = readTagsByRegexp(buf, "[\\r\\n]+", ".*?([a-zA-Z0-9:_]+).*\\t(.*?)", false, false);
    103108        // try "tag\tvalue\n" format
    104109        if (tags != null) return tags;
    105110
     
    108113        // SORRY: "a=b" = c is not supported for now, only first = will be considered
    109114        // a = "b=c" is OK
    110115        // a = b=c  - this method of parsing fails intentionally
    111         tags = readTagsByRegexp(buf, "[\\n\\t\\r]+", "(.*?)=(.*?)", true);
     116        tags = readTagsByRegexp(buf, "[\\n\\t\\r]+", "(.*?)=(.*?)", true, false );
    112117        // try format  t1=v1\n t2=v2\n ...
    113118        if (tags != null) return tags;
    114119
     
    118123        if (bufJson.startsWith("{") && bufJson.endsWith("}"))
    119124            bufJson = bufJson.substring(1, bufJson.length()-1);
    120125        tags = readTagsByRegexp(bufJson, "[\\s]*,[\\s]*",
    121                 "[\\s]*(\\\".*?[^\\\\]\\\")"+"[\\s]*:[\\s]*"+"(\\\".*?[^\\\\]\\\")[\\s]*", true);
     126                "[\\s]*(\\\".*?[^\\\\]\\\")"+"[\\s]*:[\\s]*"+"(\\\".*?[^\\\\]\\\")[\\s]*", true, false);
    122127        if (tags != null) return tags;
    123128
    124129        // Free format
  • test/unit/org/openstreetmap/josm/io/remotecontrol/AddTagsDialogTest.java

     
    2323        assertEquals("Residenzschloss Dresden", strings.get("wikipedia:de"));
    2424        assertEquals("Dresden Castle", strings.get("name:en"));
    2525    }
     26
     27    /**
     28     * Unit test for issue #14490 "Support for escaping pipe character in remote control addtags parameters"
     29     * A single URL section (value of addtags=...) with a single escaped pipe sysmbol
     30     */
     31    @Test
     32    void testParseUrlTagsToKeyValues_OneKeyValuePair_OneEscapedPipe() {
     33        Map<String, String> strings = AddTagsDialog.parseUrlTagsToKeyValues("gtfs:route_id=de:mvv-muenchen:19-210\\|210");
     34        assertEquals(1, strings.size());
     35        assertEquals("de:mvv-muenchen:19-210|210", strings.get("gtfs:route_id"));
     36    }
     37
     38    /**
     39     * Unit test for issue #14490 "Support for escaping pipe character in remote control addtags parameters"
     40     * A single URL section (value of addtags=...) with two escaped pipe sysmbols
     41     */
     42    @Test
     43    void testParseUrlTagsToKeyValues_OneKeyValuePair_TwoEscapedPipe() {
     44        Map<String, String> strings = AddTagsDialog.parseUrlTagsToKeyValues("gtfs:route_id=de:mvv-muenchen:19-210\\|210\\|RegionalBus:1179_3");
     45        assertEquals(1, strings.size());
     46        assertEquals("de:mvv-muenchen:19-210|210|RegionalBus:1179_3", strings.get("gtfs:route_id"));
     47    }
     48    /**
     49     * Unit test for issue #14490 "Support for escaping pipe character in remote control addtags parameters"
     50     * Two URL sections (values of addtags=...) with two escaped pipe sysmbols each
     51     */
     52    @Test
     53    void testParseUrlTagsToKeyValues_TwoKeyValuePairs_FourEscapedPipe() {
     54        Map<String, String> strings = AddTagsDialog.parseUrlTagsToKeyValues("gtfs:route_id=de:mvv-muenchen:19-210\\|210\\|RegionalBus:1179_3|gtfs:trip_id:sample=de:mvv-muenchen:19-210\\|210\\|RegionalBus:1179-1-1-H-0-We#3-320-333");
     55        assertEquals(2, strings.size());
     56        assertEquals("de:mvv-muenchen:19-210|210|RegionalBus:1179_3", strings.get("gtfs:route_id"));
     57        assertEquals("de:mvv-muenchen:19-210|210|RegionalBus:1179-1-1-H-0-We#3-320-333", strings.get("gtfs:trip_id:sample"));
     58    }
     59    /**
     60     * Unit test for issue #14490 "Support for escaping pipe character in remote control addtags parameters"
     61     * Two URL sections (values of addtags=...) with four unescaped pipe sysmbols in the value parts
     62     * Same as the test above, but without escapeing the pipe symbols
     63     * This is how it worked before the patch, even if the pipe symbols would have been escaped
     64     */
     65    @Test
     66    void testParseUrlTagsToKeyValues_PipeInValue_NoEscapedPipe() {
     67        Map<String, String> strings = AddTagsDialog.parseUrlTagsToKeyValues("gtfs:route_id=de:mvv-muenchen:19-210|210|RegionalBus:1179_3|gtfs:trip_id:sample=de:mvv-muenchen:19-210|210|RegionalBus:1179-1-1-H-0-We#3-320-333");
     68        assertEquals(0, strings.size());
     69    }
    2670}
  • test/unit/org/openstreetmap/josm/tools/TextTagParserTest.java

     
    175175    void testStableOrder() {
    176176        List<String> expected = Arrays.asList("foo4", "foo3", "foo2", "foo1");
    177177        ArrayList<String> actual = new ArrayList<>(TextTagParser.readTagsByRegexp(
    178                 "foo4=bar4 foo3=bar3 foo2=bar2 foo1=bar1", " ", "(.*?)=(.*?)", true).keySet());
     178                "foo4=bar4 foo3=bar3 foo2=bar2 foo1=bar1", " ", "(.*?)=(.*?)", true, false).keySet());
    179179        assertEquals(expected, actual);
    180180    }
    181181}