### Eclipse Workspace Patch 1.0
#P josm
diff --git src/org/openstreetmap/josm/tools/TextTagParser.java src/org/openstreetmap/josm/tools/TextTagParser.java
index d1b278c..9194122 100644
|
|
|
|
| 36 | 36 | return k; |
| 37 | 37 | } |
| 38 | 38 | } |
| 39 | | String text = k.substring(1, k.length()-1); |
| | 39 | final String text = k.substring(1, k.length()-1); |
| 40 | 40 | return new TextAnalyzer(text).parseString("\r\t\n"); |
| 41 | 41 | } |
| 42 | 42 | |
| … |
… |
|
| 52 | 52 | */ |
| 53 | 53 | public static Map<String, String> readTagsByRegexp(String text, String splitRegex, String tagRegex, |
| 54 | 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<>(); |
| | 55 | final String[] lines = text.split(splitRegex, -1); |
| | 56 | final Pattern p = Pattern.compile(tagRegex); |
| | 57 | final Map<String, String> tags = new LinkedHashMap<>(); |
| 58 | 58 | String k; |
| 59 | 59 | String v; |
| 60 | 60 | for (String line: lines) { |
| 61 | | if (line.isBlank()) continue; // skip empty lines |
| 62 | | Matcher m = p.matcher(line); |
| | 61 | if (line.isBlank()) { |
| | 62 | continue; // skip empty lines |
| | 63 | } |
| | 64 | final Matcher m = p.matcher(line); |
| 63 | 65 | if (m.matches()) { |
| 64 | 66 | k = Utils.removeWhiteSpaces(m.group(1)); |
| 65 | 67 | v = Utils.removeWhiteSpaces(m.group(2)); |
| 66 | 68 | if (unescapeTextInQuotes) { |
| 67 | 69 | k = unescape(k); |
| 68 | 70 | v = unescape(v); |
| 69 | | if (k == null || v == null) return null; |
| | 71 | if (k == null || v == null) { |
| | 72 | return null; |
| | 73 | } |
| 70 | 74 | } |
| 71 | 75 | if (unescapePipe) { |
| 72 | | k = k.replaceAll("\\\\\\|", "|"); |
| 73 | | v = v.replaceAll("\\\\\\|", "|"); |
| | 76 | k = k.replace("\\|", "|"); |
| | 77 | v = v.replace("\\|", "|"); |
| 74 | 78 | } |
| 75 | 79 | tags.put(k, v); |
| 76 | 80 | } else { |
| … |
… |
|
| 92 | 96 | * @since 12683 |
| 93 | 97 | */ |
| 94 | 98 | public static Map<String, String> getValidatedTagsFromText(String buf, TagWarningCallback callback) { |
| 95 | | Map<String, String> tags = readTagsFromText(buf); |
| | 99 | final Map<String, String> tags = readTagsFromText(buf); |
| 96 | 100 | return validateTags(tags, callback) ? tags : null; |
| 97 | 101 | } |
| 98 | 102 | |
| … |
… |
|
| 108 | 112 | // tag1\tval1\ntag2\tval2\n |
| 109 | 113 | tags = readTagsByRegexp(buf, "[\\r\\n]+", ".*?([a-zA-Z0-9:_]+).*\\t(.*?)", false, false); |
| 110 | 114 | // try "tag\tvalue\n" format |
| 111 | | if (tags != null) return tags; |
| | 115 | if (tags != null) { |
| | 116 | return tags; |
| | 117 | } |
| 112 | 118 | |
| 113 | 119 | // Format |
| 114 | 120 | // a=b \n c=d \n "a b"=hello |
| … |
… |
|
| 117 | 123 | // a = b=c - this method of parsing fails intentionally |
| 118 | 124 | tags = readTagsByRegexp(buf, "[\\n\\t\\r]+", "(.*?)=(.*?)", true, false); |
| 119 | 125 | // try format t1=v1\n t2=v2\n ... |
| 120 | | if (tags != null) return tags; |
| | 126 | if (tags != null) { |
| | 127 | return tags; |
| | 128 | } |
| 121 | 129 | |
| 122 | 130 | // JSON-format |
| 123 | 131 | String bufJson = buf.trim(); |
| 124 | 132 | // trim { }, if there are any |
| 125 | | if (bufJson.startsWith("{") && bufJson.endsWith("}")) |
| | 133 | if (bufJson.startsWith("{") && bufJson.endsWith("}")) { |
| 126 | 134 | bufJson = bufJson.substring(1, bufJson.length()-1); |
| | 135 | } |
| 127 | 136 | tags = readTagsByRegexp(bufJson, "[\\s]*,[\\s]*", |
| 128 | 137 | "[\\s]*(\\\".*?[^\\\\]\\\")"+"[\\s]*:[\\s]*"+"(\\\".*?[^\\\\]\\\")[\\s]*", true, false); |
| 129 | | if (tags != null) return tags; |
| | 138 | if (tags != null) { |
| | 139 | return tags; |
| | 140 | } |
| 130 | 141 | |
| 131 | 142 | // Free format |
| 132 | 143 | // a 1 "b" 2 c=3 d 4 e "5" |
| … |
… |
|
| 142 | 153 | */ |
| 143 | 154 | public static boolean validateTags(Map<String, String> tags, TagWarningCallback callback) { |
| 144 | 155 | int r; |
| 145 | | int s = tags.size(); |
| | 156 | final int s = tags.size(); |
| 146 | 157 | if (s > MAX_KEY_COUNT) { |
| 147 | 158 | // Use trn() even if for english it makes no sense, as s > 30 |
| 148 | 159 | r = callback.warning(trn("There was {0} tag found in the buffer, it is suspicious!", |
| 149 | 160 | "There were {0} tags found in the buffer, it is suspicious!", s, |
| 150 | 161 | s), "", "tags.paste.toomanytags"); |
| 151 | | if (r == 2 || r == 3) return false; if (r == 4) return true; |
| | 162 | if (r == 2 || r == 3) { |
| | 163 | return false; |
| | 164 | } else if (r == 4) { |
| | 165 | return true; |
| | 166 | } |
| 152 | 167 | } |
| 153 | 168 | for (Entry<String, String> entry : tags.entrySet()) { |
| 154 | | String key = entry.getKey(); |
| 155 | | String value = entry.getValue(); |
| | 169 | final String key = entry.getKey(); |
| | 170 | final String value = entry.getValue(); |
| 156 | 171 | if (key.length() > MAX_KEY_LENGTH) { |
| 157 | 172 | r = callback.warning(tr("Key is too long (max {0} characters):", MAX_KEY_LENGTH), key+'='+value, "tags.paste.keytoolong"); |
| 158 | | if (r == 2 || r == 3) return false; if (r == 4) return true; |
| | 173 | if (r == 2 || r == 3) { |
| | 174 | return false; |
| | 175 | } else if (r == 4) { |
| | 176 | return true; |
| | 177 | } |
| 159 | 178 | } |
| 160 | 179 | if (!key.matches(KEY_PATTERN)) { |
| 161 | 180 | r = callback.warning(tr("Suspicious characters in key:"), key, "tags.paste.keydoesnotmatch"); |
| 162 | | if (r == 2 || r == 3) return false; if (r == 4) return true; |
| | 181 | if (r == 2 || r == 3) { |
| | 182 | return false; |
| | 183 | } else if (r == 4) { |
| | 184 | return true; |
| | 185 | } |
| 163 | 186 | } |
| 164 | 187 | if (!Utils.checkCodePointCount(value, MAX_VALUE_LENGTH)) { |
| 165 | 188 | r = callback.warning(tr("Value is too long (max {0} characters):", MAX_VALUE_LENGTH), value, ""); |
| 166 | | if (r == 2 || r == 3) return false; if (r == 4) return true; |
| | 189 | if (r == 2 || r == 3) { |
| | 190 | return false; |
| | 191 | } else if (r == 4) { |
| | 192 | return true; |
| | 193 | } |
| 167 | 194 | } |
| 168 | 195 | } |
| 169 | 196 | return true; |