Changeset 19571 in josm
- Timestamp:
- 2026-05-07T15:11:23+02:00 (2 days ago)
- Location:
- trunk
- Files:
-
- 4 edited
-
src/org/openstreetmap/josm/io/remotecontrol/AddTagsDialog.java (modified) (1 diff)
-
src/org/openstreetmap/josm/tools/TextTagParser.java (modified) (5 diffs)
-
test/unit/org/openstreetmap/josm/io/remotecontrol/AddTagsDialogTest.java (modified) (1 diff)
-
test/unit/org/openstreetmap/josm/tools/TextTagParserTest.java (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/remotecontrol/AddTagsDialog.java
r19101 r19571 284 284 */ 285 285 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); 287 287 return tags == null ? Collections.emptyMap() : tags; 288 288 } -
trunk/src/org/openstreetmap/josm/tools/TextTagParser.java
r19437 r19571 47 47 * @param tagRegex - each part is matched against this regex 48 48 * @param unescapeTextInQuotes - if true, matched tag and value will be analyzed more thoroughly 49 * @param unescapePipe - if true, then replace all '\|' by '|' (issue #14490) 49 50 * @return map of tags 51 * @since 19570 (parameter unescapePipe added) 50 52 */ 51 public static Map<String, String> readTagsByRegexp(String text, String splitRegex, String tagRegex, boolean unescapeTextInQuotes) { 52 String[] lines = text.split(splitRegex, -1); 53 Pattern p = Pattern.compile(tagRegex); 54 Map<String, String> tags = new LinkedHashMap<>(); 55 String k; 56 String v; 57 for (String line: lines) { 53 public static Map<String, String> readTagsByRegexp(String text, String splitRegex, String tagRegex, 54 boolean unescapeTextInQuotes, boolean unescapePipe) { 55 String[] lines = text.split(splitRegex, -1); 56 Pattern p = Pattern.compile(tagRegex); 57 Map<String, String> tags = new LinkedHashMap<>(); 58 String k; 59 String v; 60 for (String line: lines) { 58 61 if (line.trim().isEmpty()) continue; // skip empty lines 59 62 Matcher m = p.matcher(line); … … 66 69 if (k == null || v == null) return null; 67 70 } 71 if (unescapePipe) { 72 k = k.replaceAll("\\\\\\|", "|"); 73 v = v.replaceAll("\\\\\\|", "|"); 74 } 68 75 tags.put(k, v); 69 76 } else { 70 77 return null; 71 78 } 72 }73 if (!tags.isEmpty()) {74 return tags;75 } else {76 return null;77 }79 } 80 if (!tags.isEmpty()) { 81 return tags; 82 } else { 83 return null; 84 } 78 85 } 79 86 … … 100 107 // Format 101 108 // tag1\tval1\ntag2\tval2\n 102 tags = readTagsByRegexp(buf, "[\\r\\n]+", ".*?([a-zA-Z0-9:_]+).*\\t(.*?)", false); 109 tags = readTagsByRegexp(buf, "[\\r\\n]+", ".*?([a-zA-Z0-9:_]+).*\\t(.*?)", false, false); 103 110 // try "tag\tvalue\n" format 104 111 if (tags != null) return tags; … … 109 116 // a = "b=c" is OK 110 117 // a = b=c - this method of parsing fails intentionally 111 tags = readTagsByRegexp(buf, "[\\n\\t\\r]+", "(.*?)=(.*?)", true); 118 tags = readTagsByRegexp(buf, "[\\n\\t\\r]+", "(.*?)=(.*?)", true, false); 112 119 // try format t1=v1\n t2=v2\n ... 113 120 if (tags != null) return tags; … … 119 126 bufJson = bufJson.substring(1, bufJson.length()-1); 120 127 tags = readTagsByRegexp(bufJson, "[\\s]*,[\\s]*", 121 "[\\s]*(\\\".*?[^\\\\]\\\")"+"[\\s]*:[\\s]*"+"(\\\".*?[^\\\\]\\\")[\\s]*", true); 128 "[\\s]*(\\\".*?[^\\\\]\\\")"+"[\\s]*:[\\s]*"+"(\\\".*?[^\\\\]\\\")[\\s]*", true, false); 122 129 if (tags != null) return tags; 123 130 -
trunk/test/unit/org/openstreetmap/josm/io/remotecontrol/AddTagsDialogTest.java
r19519 r19571 24 24 assertEquals("Dresden Castle", strings.get("name:en")); 25 25 } 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 /** 50 * Unit test for issue #14490 "Support for escaping pipe character in remote control addtags parameters" 51 * Two URL sections (values of addtags=...) with two escaped pipe sysmbols each 52 */ 53 @Test 54 void testParseUrlTagsToKeyValues_TwoKeyValuePairs_FourEscapedPipe() { 55 Map<String, String> strings = AddTagsDialog.parseUrlTagsToKeyValues("gtfs:route_id=de:mvv-muenchen:19-210\\|210\\|" 56 + "RegionalBus:1179_3|gtfs:trip_id:sample=de:mvv-muenchen:19-210\\|210\\|RegionalBus:1179-1-1-H-0-We#3-320-333"); 57 assertEquals(2, strings.size()); 58 assertEquals("de:mvv-muenchen:19-210|210|RegionalBus:1179_3", strings.get("gtfs:route_id")); 59 assertEquals("de:mvv-muenchen:19-210|210|RegionalBus:1179-1-1-H-0-We#3-320-333", strings.get("gtfs:trip_id:sample")); 60 } 61 62 /** 63 * Unit test for issue #14490 "Support for escaping pipe character in remote control addtags parameters" 64 * Two URL sections (values of addtags=...) with four unescaped pipe sysmbols in the value parts 65 * Same as the test above, but without escapeing the pipe symbols 66 * This is how it worked before the patch, even if the pipe symbols would have been escaped 67 */ 68 @Test 69 void testParseUrlTagsToKeyValues_PipeInValue_NoEscapedPipe() { 70 Map<String, String> strings = AddTagsDialog.parseUrlTagsToKeyValues("gtfs:route_id=de:mvv-muenchen:19-210|210|" 71 + "RegionalBus:1179_3|gtfs:trip_id:sample=de:mvv-muenchen:19-210|210|RegionalBus:1179-1-1-H-0-We#3-320-333"); 72 assertEquals(0, strings.size()); 73 } 26 74 } -
trunk/test/unit/org/openstreetmap/josm/tools/TextTagParserTest.java
r18037 r19571 176 176 List<String> expected = Arrays.asList("foo4", "foo3", "foo2", "foo1"); 177 177 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()); 179 179 assertEquals(expected, actual); 180 180 }
Note:
See TracChangeset
for help on using the changeset viewer.
