[7937] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.tools;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
| 6 |
|
---|
| 7 | import java.awt.GridBagLayout;
|
---|
| 8 | import java.util.Arrays;
|
---|
| 9 | import java.util.HashMap;
|
---|
| 10 | import java.util.Map;
|
---|
| 11 | import java.util.Map.Entry;
|
---|
| 12 | import java.util.regex.Matcher;
|
---|
| 13 | import java.util.regex.Pattern;
|
---|
| 14 |
|
---|
| 15 | import javax.swing.JLabel;
|
---|
| 16 | import javax.swing.JOptionPane;
|
---|
| 17 | import javax.swing.JPanel;
|
---|
| 18 |
|
---|
| 19 | import org.openstreetmap.josm.Main;
|
---|
| 20 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
[10604] | 21 | import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
|
---|
[7937] | 22 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
| 23 | import org.openstreetmap.josm.gui.widgets.UrlLabel;
|
---|
| 24 | import org.openstreetmap.josm.io.XmlWriter;
|
---|
| 25 | import org.openstreetmap.josm.tools.LanguageInfo.LocaleType;
|
---|
| 26 |
|
---|
| 27 | /**
|
---|
| 28 | * Class that helps to parse tags from arbitrary text
|
---|
| 29 | */
|
---|
| 30 | public final class TextTagParser {
|
---|
| 31 |
|
---|
| 32 | // properties need JOSM restart to apply, modified rarely enough
|
---|
[8468] | 33 | private static final int MAX_KEY_LENGTH = Main.pref.getInteger("tags.paste.max-key-length", 50);
|
---|
| 34 | private static final int MAX_KEY_COUNT = Main.pref.getInteger("tags.paste.max-key-count", 30);
|
---|
| 35 | private static final String KEY_PATTERN = Main.pref.get("tags.paste.tag-pattern", "[0-9a-zA-Z:_]*");
|
---|
| 36 | private static final int MAX_VALUE_LENGTH = 255;
|
---|
[7937] | 37 |
|
---|
| 38 | private TextTagParser() {
|
---|
| 39 | // Hide default constructor for utils classes
|
---|
| 40 | }
|
---|
| 41 |
|
---|
[12382] | 42 | /**
|
---|
| 43 | * A helper class that analyzes the text and attempts to parse tags from it
|
---|
| 44 | */
|
---|
[7937] | 45 | public static class TextAnalyzer {
|
---|
[8840] | 46 | private boolean quotesStarted;
|
---|
| 47 | private boolean esc;
|
---|
[9078] | 48 | private final StringBuilder s = new StringBuilder(200);
|
---|
[8285] | 49 | private int pos;
|
---|
[9078] | 50 | private final String data;
|
---|
| 51 | private final int n;
|
---|
[7937] | 52 |
|
---|
[12382] | 53 | /**
|
---|
| 54 | * Create a new {@link TextAnalyzer}
|
---|
| 55 | * @param text The text to parse
|
---|
| 56 | */
|
---|
[7937] | 57 | public TextAnalyzer(String text) {
|
---|
| 58 | pos = 0;
|
---|
| 59 | data = text;
|
---|
| 60 | n = data.length();
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | /**
|
---|
| 64 | * Read tags from "Free format"
|
---|
[8928] | 65 | * @return map of tags
|
---|
[7937] | 66 | */
|
---|
[8928] | 67 | private Map<String, String> getFreeParsedTags() {
|
---|
[7937] | 68 | String k, v;
|
---|
| 69 | Map<String, String> tags = new HashMap<>();
|
---|
| 70 |
|
---|
| 71 | while (true) {
|
---|
| 72 | skipEmpty();
|
---|
[8513] | 73 | if (pos == n) {
|
---|
| 74 | break;
|
---|
| 75 | }
|
---|
[7937] | 76 | k = parseString("\n\r\t= ");
|
---|
[8513] | 77 | if (pos == n) {
|
---|
| 78 | tags.clear();
|
---|
| 79 | break;
|
---|
| 80 | }
|
---|
[7937] | 81 | skipSign();
|
---|
[8513] | 82 | if (pos == n) {
|
---|
| 83 | tags.clear();
|
---|
| 84 | break;
|
---|
| 85 | }
|
---|
[7937] | 86 | v = parseString("\n\r\t ");
|
---|
| 87 | tags.put(k, v);
|
---|
| 88 | }
|
---|
| 89 | return tags;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | private String parseString(String stopChars) {
|
---|
| 93 | char[] stop = stopChars.toCharArray();
|
---|
| 94 | Arrays.sort(stop);
|
---|
| 95 | char c;
|
---|
| 96 | while (pos < n) {
|
---|
| 97 | c = data.charAt(pos);
|
---|
| 98 | if (esc) {
|
---|
| 99 | esc = false;
|
---|
| 100 | s.append(c); // \" \\
|
---|
| 101 | } else if (c == '\\') {
|
---|
| 102 | esc = true;
|
---|
| 103 | } else if (c == '\"' && !quotesStarted) { // opening "
|
---|
[8461] | 104 | if (!s.toString().trim().isEmpty()) { // we had ||some text"||
|
---|
[7937] | 105 | s.append(c); // just add ", not open
|
---|
| 106 | } else {
|
---|
| 107 | s.delete(0, s.length()); // forget that empty characthers and start reading "....
|
---|
| 108 | quotesStarted = true;
|
---|
| 109 | }
|
---|
| 110 | } else if (c == '\"' && quotesStarted) { // closing "
|
---|
| 111 | quotesStarted = false;
|
---|
| 112 | pos++;
|
---|
| 113 | break;
|
---|
[8510] | 114 | } else if (!quotesStarted && (Arrays.binarySearch(stop, c) >= 0)) {
|
---|
[7937] | 115 | // stop-symbol found
|
---|
| 116 | pos++;
|
---|
| 117 | break;
|
---|
| 118 | } else {
|
---|
| 119 | // skip non-printable characters
|
---|
[8510] | 120 | if (c >= 32) s.append(c);
|
---|
[7937] | 121 | }
|
---|
| 122 | pos++;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | String res = s.toString();
|
---|
| 126 | s.delete(0, s.length());
|
---|
| 127 | return res.trim();
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | private void skipSign() {
|
---|
| 131 | char c;
|
---|
| 132 | boolean signFound = false;
|
---|
| 133 | while (pos < n) {
|
---|
| 134 | c = data.charAt(pos);
|
---|
[10378] | 135 | if (c == '\t' || c == '\n' || c == ' ') {
|
---|
[7937] | 136 | pos++;
|
---|
[8510] | 137 | } else if (c == '=') {
|
---|
[7937] | 138 | if (signFound) break; // a = =qwerty means "a"="=qwerty"
|
---|
| 139 | signFound = true;
|
---|
| 140 | pos++;
|
---|
| 141 | } else {
|
---|
| 142 | break;
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | private void skipEmpty() {
|
---|
| 148 | char c;
|
---|
| 149 | while (pos < n) {
|
---|
| 150 | c = data.charAt(pos);
|
---|
[8443] | 151 | if (c == '\t' || c == '\n' || c == '\r' || c == ' ') {
|
---|
[7937] | 152 | pos++;
|
---|
| 153 | } else {
|
---|
| 154 | break;
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
| 159 |
|
---|
[10763] | 160 | static String unescape(String k) {
|
---|
[8443] | 161 | if (!(k.startsWith("\"") && k.endsWith("\""))) {
|
---|
[7937] | 162 | if (k.contains("=")) {
|
---|
| 163 | // '=' not in quotes will be treated as an error!
|
---|
| 164 | return null;
|
---|
| 165 | } else {
|
---|
| 166 | return k;
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
[8510] | 169 | String text = k.substring(1, k.length()-1);
|
---|
[7937] | 170 | return (new TextAnalyzer(text)).parseString("\r\t\n");
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | /**
|
---|
| 174 | * Try to find tag-value pairs in given text
|
---|
| 175 | * @param text - text in which tags are looked for
|
---|
| 176 | * @param splitRegex - text is splitted into parts with this delimiter
|
---|
| 177 | * @param tagRegex - each part is matched against this regex
|
---|
| 178 | * @param unescapeTextInQuotes - if true, matched tag and value will be analyzed more thoroughly
|
---|
[8928] | 179 | * @return map of tags
|
---|
[7937] | 180 | */
|
---|
| 181 | public static Map<String, String> readTagsByRegexp(String text, String splitRegex, String tagRegex, boolean unescapeTextInQuotes) {
|
---|
| 182 | String[] lines = text.split(splitRegex);
|
---|
| 183 | Pattern p = Pattern.compile(tagRegex);
|
---|
| 184 | Map<String, String> tags = new HashMap<>();
|
---|
[10308] | 185 | String k;
|
---|
| 186 | String v;
|
---|
[10378] | 187 | for (String line: lines) {
|
---|
[7937] | 188 | if (line.trim().isEmpty()) continue; // skip empty lines
|
---|
| 189 | Matcher m = p.matcher(line);
|
---|
| 190 | if (m.matches()) {
|
---|
[8510] | 191 | k = m.group(1).trim();
|
---|
| 192 | v = m.group(2).trim();
|
---|
[7937] | 193 | if (unescapeTextInQuotes) {
|
---|
| 194 | k = unescape(k);
|
---|
| 195 | v = unescape(v);
|
---|
[8510] | 196 | if (k == null || v == null) return null;
|
---|
[7937] | 197 | }
|
---|
[8510] | 198 | tags.put(k, v);
|
---|
[7937] | 199 | } else {
|
---|
| 200 | return null;
|
---|
| 201 | }
|
---|
| 202 | }
|
---|
| 203 | if (!tags.isEmpty()) {
|
---|
| 204 | return tags;
|
---|
[10378] | 205 | } else {
|
---|
[7937] | 206 | return null;
|
---|
| 207 | }
|
---|
| 208 | }
|
---|
| 209 |
|
---|
[12382] | 210 | /**
|
---|
| 211 | * Gets a list of tags that are in the given text
|
---|
| 212 | * @param buf The text to parse
|
---|
| 213 | * @return The tags or <code>null</code> if the tags are not valid
|
---|
| 214 | */
|
---|
[8510] | 215 | public static Map<String, String> getValidatedTagsFromText(String buf) {
|
---|
| 216 | Map<String, String> tags = readTagsFromText(buf);
|
---|
[7937] | 217 | return validateTags(tags) ? tags : null;
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | /**
|
---|
| 221 | * Apply different methods to extract tag-value pairs from arbitrary text
|
---|
[8470] | 222 | * @param buf buffer
|
---|
[7937] | 223 | * @return null if no format is suitable
|
---|
| 224 | */
|
---|
[8510] | 225 | public static Map<String, String> readTagsFromText(String buf) {
|
---|
| 226 | Map<String, String> tags;
|
---|
[7937] | 227 |
|
---|
| 228 | // Format
|
---|
| 229 | // tag1\tval1\ntag2\tval2\n
|
---|
[8482] | 230 | tags = readTagsByRegexp(buf, "[\\r\\n]+", ".*?([a-zA-Z0-9:_]+).*\\t(.*?)", false);
|
---|
[9073] | 231 | // try "tag\tvalue\n" format
|
---|
[8510] | 232 | if (tags != null) return tags;
|
---|
[7937] | 233 |
|
---|
| 234 | // Format
|
---|
| 235 | // a=b \n c=d \n "a b"=hello
|
---|
| 236 | // SORRY: "a=b" = c is not supported fror now, only first = will be considered
|
---|
| 237 | // a = "b=c" is OK
|
---|
| 238 | // a = b=c - this method of parsing fails intentionally
|
---|
| 239 | tags = readTagsByRegexp(buf, "[\\n\\t\\r]+", "(.*?)=(.*?)", true);
|
---|
[9073] | 240 | // try format t1=v1\n t2=v2\n ...
|
---|
[8510] | 241 | if (tags != null) return tags;
|
---|
[7937] | 242 |
|
---|
| 243 | // JSON-format
|
---|
| 244 | String bufJson = buf.trim();
|
---|
| 245 | // trim { }, if there are any
|
---|
[8443] | 246 | if (bufJson.startsWith("{") && bufJson.endsWith("}"))
|
---|
| 247 | bufJson = bufJson.substring(1, bufJson.length()-1);
|
---|
[7937] | 248 | tags = readTagsByRegexp(bufJson, "[\\s]*,[\\s]*",
|
---|
| 249 | "[\\s]*(\\\".*?[^\\\\]\\\")"+"[\\s]*:[\\s]*"+"(\\\".*?[^\\\\]\\\")[\\s]*", true);
|
---|
[8510] | 250 | if (tags != null) return tags;
|
---|
[7937] | 251 |
|
---|
| 252 | // Free format
|
---|
| 253 | // a 1 "b" 2 c=3 d 4 e "5"
|
---|
[8375] | 254 | return new TextAnalyzer(buf).getFreeParsedTags();
|
---|
[7937] | 255 | }
|
---|
| 256 |
|
---|
| 257 | /**
|
---|
| 258 | * Check tags for correctness and display warnings if needed
|
---|
| 259 | * @param tags - map key->value to check
|
---|
| 260 | * @return true if the tags should be pasted
|
---|
| 261 | */
|
---|
| 262 | public static boolean validateTags(Map<String, String> tags) {
|
---|
| 263 | int r;
|
---|
| 264 | int s = tags.size();
|
---|
| 265 | if (s > MAX_KEY_COUNT) {
|
---|
| 266 | // Use trn() even if for english it makes no sense, as s > 30
|
---|
[8510] | 267 | r = warning(trn("There was {0} tag found in the buffer, it is suspicious!",
|
---|
[7937] | 268 | "There were {0} tags found in the buffer, it is suspicious!", s,
|
---|
| 269 | s), "", "tags.paste.toomanytags");
|
---|
[8510] | 270 | if (r == 2 || r == 3) return false; if (r == 4) return true;
|
---|
[7937] | 271 | }
|
---|
| 272 | for (Entry<String, String> entry : tags.entrySet()) {
|
---|
| 273 | String key = entry.getKey();
|
---|
| 274 | String value = entry.getValue();
|
---|
| 275 | if (key.length() > MAX_KEY_LENGTH) {
|
---|
[8846] | 276 | r = warning(tr("Key is too long (max {0} characters):", MAX_KEY_LENGTH), key+'='+value, "tags.paste.keytoolong");
|
---|
[8510] | 277 | if (r == 2 || r == 3) return false; if (r == 4) return true;
|
---|
[7937] | 278 | }
|
---|
| 279 | if (!key.matches(KEY_PATTERN)) {
|
---|
| 280 | r = warning(tr("Suspicious characters in key:"), key, "tags.paste.keydoesnotmatch");
|
---|
[8510] | 281 | if (r == 2 || r == 3) return false; if (r == 4) return true;
|
---|
[7937] | 282 | }
|
---|
| 283 | if (value.length() > MAX_VALUE_LENGTH) {
|
---|
| 284 | r = warning(tr("Value is too long (max {0} characters):", MAX_VALUE_LENGTH), value, "tags.paste.valuetoolong");
|
---|
[8510] | 285 | if (r == 2 || r == 3) return false; if (r == 4) return true;
|
---|
[7937] | 286 | }
|
---|
| 287 | }
|
---|
| 288 | return true;
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 | private static int warning(String text, String data, String code) {
|
---|
| 292 | ExtendedDialog ed = new ExtendedDialog(
|
---|
| 293 | Main.parent,
|
---|
| 294 | tr("Do you want to paste these tags?"),
|
---|
[12279] | 295 | tr("Ok"), tr("Cancel"), tr("Clear buffer"), tr("Ignore warnings"));
|
---|
| 296 | ed.setButtonIcons("ok", "cancel", "dialogs/delete", "pastetags");
|
---|
[8510] | 297 | ed.setContent("<html><b>"+text + "</b><br/><br/><div width=\"300px\">"+XmlWriter.encode(data, true)+"</html>");
|
---|
[7937] | 298 | ed.setDefaultButton(2);
|
---|
| 299 | ed.setCancelButton(2);
|
---|
| 300 | ed.setIcon(JOptionPane.WARNING_MESSAGE);
|
---|
| 301 | ed.toggleEnable(code);
|
---|
| 302 | ed.showDialog();
|
---|
| 303 | int r = ed.getValue();
|
---|
[8510] | 304 | if (r == 0) r = 2;
|
---|
[7937] | 305 | // clean clipboard if user asked
|
---|
[10604] | 306 | if (r == 3) ClipboardUtils.copyString("");
|
---|
[7937] | 307 | return r;
|
---|
| 308 | }
|
---|
| 309 |
|
---|
| 310 | /**
|
---|
| 311 | * Shows message that the buffer can not be pasted, allowing user to clean the buffer
|
---|
| 312 | * @param helpTopic the help topic of the parent action
|
---|
| 313 | * TODO: Replace by proper HelpAwareOptionPane instead of self-made help link
|
---|
| 314 | */
|
---|
| 315 | public static void showBadBufferMessage(String helpTopic) {
|
---|
| 316 | String msg = tr("<html><p> Sorry, it is impossible to paste tags from buffer. It does not contain any JOSM object"
|
---|
| 317 | + " or suitable text. </p></html>");
|
---|
| 318 | JPanel p = new JPanel(new GridBagLayout());
|
---|
[8510] | 319 | p.add(new JLabel(msg), GBC.eop());
|
---|
[7937] | 320 | String helpUrl = HelpUtil.getHelpTopicUrl(HelpUtil.buildAbsoluteHelpTopic(helpTopic, LocaleType.DEFAULT));
|
---|
| 321 | if (helpUrl != null) {
|
---|
| 322 | p.add(new UrlLabel(helpUrl), GBC.eop());
|
---|
| 323 | }
|
---|
| 324 |
|
---|
| 325 | ExtendedDialog ed = new ExtendedDialog(
|
---|
| 326 | Main.parent,
|
---|
| 327 | tr("Warning"),
|
---|
[12279] | 328 | tr("Ok"), tr("Clear buffer"))
|
---|
| 329 | .setButtonIcons("ok", "dialogs/delete")
|
---|
| 330 | .setContent(p)
|
---|
| 331 | .setDefaultButton(1)
|
---|
| 332 | .setCancelButton(1)
|
---|
| 333 | .setIcon(JOptionPane.WARNING_MESSAGE)
|
---|
| 334 | .toggleEnable("tags.paste.cleanbadbuffer");
|
---|
[7937] | 335 |
|
---|
| 336 | ed.showDialog();
|
---|
| 337 |
|
---|
| 338 | // clean clipboard if user asked
|
---|
[12279] | 339 | if (ed.getValue() == 2) ClipboardUtils.copyString("");
|
---|
[7937] | 340 | }
|
---|
| 341 | }
|
---|