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