001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.tools; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005import static org.openstreetmap.josm.tools.I18n.trn; 006 007import java.util.LinkedHashMap; 008import java.util.Map; 009import java.util.Map.Entry; 010import java.util.regex.Matcher; 011import java.util.regex.Pattern; 012 013import org.openstreetmap.josm.spi.preferences.Config; 014 015/** 016 * Class that helps to parse tags from arbitrary text 017 */ 018public final class TextTagParser { 019 020 // properties need JOSM restart to apply, modified rarely enough 021 private static final int MAX_KEY_LENGTH = Config.getPref().getInt("tags.paste.max-key-length", 50); 022 private static final int MAX_KEY_COUNT = Config.getPref().getInt("tags.paste.max-key-count", 30); 023 private static final String KEY_PATTERN = Config.getPref().get("tags.paste.tag-pattern", "[0-9a-zA-Z:_-]*"); 024 private static final int MAX_VALUE_LENGTH = 255; 025 026 private TextTagParser() { 027 // Hide default constructor for utils classes 028 } 029 030 static String unescape(String k) { 031 if (!(k.startsWith("\"") && k.endsWith("\""))) { 032 if (k.contains("=")) { 033 // '=' not in quotes will be treated as an error! 034 return null; 035 } else { 036 return k; 037 } 038 } 039 String text = k.substring(1, k.length()-1); 040 return new TextAnalyzer(text).parseString("\r\t\n"); 041 } 042 043 /** 044 * Try to find tag-value pairs in given text 045 * @param text - text in which tags are looked for 046 * @param splitRegex - text is split into parts with this delimiter 047 * @param tagRegex - each part is matched against this regex 048 * @param unescapeTextInQuotes - if true, matched tag and value will be analyzed more thoroughly 049 * @param unescapePipe - if true, then replace all '\|' by '|' (issue #14490) 050 * @return map of tags 051 * @since 19571 (parameter unescapePipe added) 052 */ 053 public static Map<String, String> readTagsByRegexp(String text, String splitRegex, String tagRegex, 054 boolean unescapeTextInQuotes, boolean unescapePipe) { 055 String[] lines = text.split(splitRegex, -1); 056 Pattern p = Pattern.compile(tagRegex); 057 Map<String, String> tags = new LinkedHashMap<>(); 058 String k; 059 String v; 060 for (String line: lines) { 061 if (line.isBlank()) continue; // skip empty lines 062 Matcher m = p.matcher(line); 063 if (m.matches()) { 064 k = Utils.removeWhiteSpaces(m.group(1)); 065 v = Utils.removeWhiteSpaces(m.group(2)); 066 if (unescapeTextInQuotes) { 067 k = unescape(k); 068 v = unescape(v); 069 if (k == null || v == null) return null; 070 } 071 if (unescapePipe) { 072 k = k.replaceAll("\\\\\\|", "|"); 073 v = v.replaceAll("\\\\\\|", "|"); 074 } 075 tags.put(k, v); 076 } else { 077 return null; 078 } 079 } 080 if (!tags.isEmpty()) { 081 return tags; 082 } else { 083 return null; 084 } 085 } 086 087 /** 088 * Gets a list of tags that are in the given text 089 * @param buf The text to parse 090 * @param callback warning callback 091 * @return The tags or <code>null</code> if the tags are not valid 092 * @since 12683 093 */ 094 public static Map<String, String> getValidatedTagsFromText(String buf, TagWarningCallback callback) { 095 Map<String, String> tags = readTagsFromText(buf); 096 return validateTags(tags, callback) ? tags : null; 097 } 098 099 /** 100 * Apply different methods to extract tag-value pairs from arbitrary text 101 * @param buf buffer 102 * @return null if no format is suitable 103 */ 104 public static Map<String, String> readTagsFromText(String buf) { 105 Map<String, String> tags; 106 107 // Format 108 // tag1\tval1\ntag2\tval2\n 109 tags = readTagsByRegexp(buf, "[\\r\\n]+", ".*?([a-zA-Z0-9:_]+).*\\t(.*?)", false, false); 110 // try "tag\tvalue\n" format 111 if (tags != null) return tags; 112 113 // Format 114 // a=b \n c=d \n "a b"=hello 115 // SORRY: "a=b" = c is not supported for now, only first = will be considered 116 // a = "b=c" is OK 117 // a = b=c - this method of parsing fails intentionally 118 tags = readTagsByRegexp(buf, "[\\n\\t\\r]+", "(.*?)=(.*?)", true, false); 119 // try format t1=v1\n t2=v2\n ... 120 if (tags != null) return tags; 121 122 // JSON-format 123 String bufJson = buf.trim(); 124 // trim { }, if there are any 125 if (bufJson.startsWith("{") && bufJson.endsWith("}")) 126 bufJson = bufJson.substring(1, bufJson.length()-1); 127 tags = readTagsByRegexp(bufJson, "[\\s]*,[\\s]*", 128 "[\\s]*(\\\".*?[^\\\\]\\\")"+"[\\s]*:[\\s]*"+"(\\\".*?[^\\\\]\\\")[\\s]*", true, false); 129 if (tags != null) return tags; 130 131 // Free format 132 // a 1 "b" 2 c=3 d 4 e "5" 133 return new TextAnalyzer(buf).getFreeParsedTags(); 134 } 135 136 /** 137 * Check tags for correctness and display warnings if needed 138 * @param tags - map key → value to check 139 * @param callback warning callback 140 * @return true if the tags should be pasted 141 * @since 12683 142 */ 143 public static boolean validateTags(Map<String, String> tags, TagWarningCallback callback) { 144 int r; 145 int s = tags.size(); 146 if (s > MAX_KEY_COUNT) { 147 // Use trn() even if for english it makes no sense, as s > 30 148 r = callback.warning(trn("There was {0} tag found in the buffer, it is suspicious!", 149 "There were {0} tags found in the buffer, it is suspicious!", s, 150 s), "", "tags.paste.toomanytags"); 151 if (r == 2 || r == 3) return false; if (r == 4) return true; 152 } 153 for (Entry<String, String> entry : tags.entrySet()) { 154 String key = entry.getKey(); 155 String value = entry.getValue(); 156 if (key.length() > MAX_KEY_LENGTH) { 157 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; 159 } 160 if (!key.matches(KEY_PATTERN)) { 161 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; 163 } 164 if (!Utils.checkCodePointCount(value, MAX_VALUE_LENGTH)) { 165 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; 167 } 168 } 169 return true; 170 } 171 172 /** 173 * Called when a problematic tag is encountered. 174 * @since 12683 175 */ 176 @FunctionalInterface 177 public interface TagWarningCallback { 178 /** 179 * Displays a warning about a problematic tag and ask user what to do about it. 180 * @param text Message to display 181 * @param data Tag key and/or value 182 * @param code to use with {@code ExtendedDialog#toggleEnable(String)} 183 * @return 1 to validate and display next warnings if any, 2 to cancel operation, 3 to clear buffer, 4 to paste tags 184 */ 185 int warning(String text, String data, String code); 186 } 187}