source: josm/trunk/src/org/openstreetmap/josm/tools/TextTagParser.java@ 12411

Last change on this file since 12411 was 12382, checked in by michael2402, 7 years ago

More documentation for the tools package

  • Property svn:eol-style set to native
File size: 12.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5import static org.openstreetmap.josm.tools.I18n.trn;
6
7import java.awt.GridBagLayout;
8import java.util.Arrays;
9import java.util.HashMap;
10import java.util.Map;
11import java.util.Map.Entry;
12import java.util.regex.Matcher;
13import java.util.regex.Pattern;
14
15import javax.swing.JLabel;
16import javax.swing.JOptionPane;
17import javax.swing.JPanel;
18
19import org.openstreetmap.josm.Main;
20import org.openstreetmap.josm.gui.ExtendedDialog;
21import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
22import org.openstreetmap.josm.gui.help.HelpUtil;
23import org.openstreetmap.josm.gui.widgets.UrlLabel;
24import org.openstreetmap.josm.io.XmlWriter;
25import org.openstreetmap.josm.tools.LanguageInfo.LocaleType;
26
27/**
28 * Class that helps to parse tags from arbitrary text
29 */
30public final class TextTagParser {
31
32 // properties need JOSM restart to apply, modified rarely enough
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;
37
38 private TextTagParser() {
39 // Hide default constructor for utils classes
40 }
41
42 /**
43 * A helper class that analyzes the text and attempts to parse tags from it
44 */
45 public static class TextAnalyzer {
46 private boolean quotesStarted;
47 private boolean esc;
48 private final StringBuilder s = new StringBuilder(200);
49 private int pos;
50 private final String data;
51 private final int n;
52
53 /**
54 * Create a new {@link TextAnalyzer}
55 * @param text The text to parse
56 */
57 public TextAnalyzer(String text) {
58 pos = 0;
59 data = text;
60 n = data.length();
61 }
62
63 /**
64 * Read tags from "Free format"
65 * @return map of tags
66 */
67 private Map<String, String> getFreeParsedTags() {
68 String k, v;
69 Map<String, String> tags = new HashMap<>();
70
71 while (true) {
72 skipEmpty();
73 if (pos == n) {
74 break;
75 }
76 k = parseString("\n\r\t= ");
77 if (pos == n) {
78 tags.clear();
79 break;
80 }
81 skipSign();
82 if (pos == n) {
83 tags.clear();
84 break;
85 }
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 "
104 if (!s.toString().trim().isEmpty()) { // we had ||some text"||
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;
114 } else if (!quotesStarted && (Arrays.binarySearch(stop, c) >= 0)) {
115 // stop-symbol found
116 pos++;
117 break;
118 } else {
119 // skip non-printable characters
120 if (c >= 32) s.append(c);
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);
135 if (c == '\t' || c == '\n' || c == ' ') {
136 pos++;
137 } else if (c == '=') {
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);
151 if (c == '\t' || c == '\n' || c == '\r' || c == ' ') {
152 pos++;
153 } else {
154 break;
155 }
156 }
157 }
158 }
159
160 static String unescape(String k) {
161 if (!(k.startsWith("\"") && k.endsWith("\""))) {
162 if (k.contains("=")) {
163 // '=' not in quotes will be treated as an error!
164 return null;
165 } else {
166 return k;
167 }
168 }
169 String text = k.substring(1, k.length()-1);
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
179 * @return map of tags
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<>();
185 String k;
186 String v;
187 for (String line: lines) {
188 if (line.trim().isEmpty()) continue; // skip empty lines
189 Matcher m = p.matcher(line);
190 if (m.matches()) {
191 k = m.group(1).trim();
192 v = m.group(2).trim();
193 if (unescapeTextInQuotes) {
194 k = unescape(k);
195 v = unescape(v);
196 if (k == null || v == null) return null;
197 }
198 tags.put(k, v);
199 } else {
200 return null;
201 }
202 }
203 if (!tags.isEmpty()) {
204 return tags;
205 } else {
206 return null;
207 }
208 }
209
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 */
215 public static Map<String, String> getValidatedTagsFromText(String buf) {
216 Map<String, String> tags = readTagsFromText(buf);
217 return validateTags(tags) ? tags : null;
218 }
219
220 /**
221 * Apply different methods to extract tag-value pairs from arbitrary text
222 * @param buf buffer
223 * @return null if no format is suitable
224 */
225 public static Map<String, String> readTagsFromText(String buf) {
226 Map<String, String> tags;
227
228 // Format
229 // tag1\tval1\ntag2\tval2\n
230 tags = readTagsByRegexp(buf, "[\\r\\n]+", ".*?([a-zA-Z0-9:_]+).*\\t(.*?)", false);
231 // try "tag\tvalue\n" format
232 if (tags != null) return tags;
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);
240 // try format t1=v1\n t2=v2\n ...
241 if (tags != null) return tags;
242
243 // JSON-format
244 String bufJson = buf.trim();
245 // trim { }, if there are any
246 if (bufJson.startsWith("{") && bufJson.endsWith("}"))
247 bufJson = bufJson.substring(1, bufJson.length()-1);
248 tags = readTagsByRegexp(bufJson, "[\\s]*,[\\s]*",
249 "[\\s]*(\\\".*?[^\\\\]\\\")"+"[\\s]*:[\\s]*"+"(\\\".*?[^\\\\]\\\")[\\s]*", true);
250 if (tags != null) return tags;
251
252 // Free format
253 // a 1 "b" 2 c=3 d 4 e "5"
254 return new TextAnalyzer(buf).getFreeParsedTags();
255 }
256
257 /**
258 * Check tags for correctness and display warnings if needed
259 * @param tags - map key-&gt;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
267 r = warning(trn("There was {0} tag found in the buffer, it is suspicious!",
268 "There were {0} tags found in the buffer, it is suspicious!", s,
269 s), "", "tags.paste.toomanytags");
270 if (r == 2 || r == 3) return false; if (r == 4) return true;
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) {
276 r = warning(tr("Key is too long (max {0} characters):", MAX_KEY_LENGTH), key+'='+value, "tags.paste.keytoolong");
277 if (r == 2 || r == 3) return false; if (r == 4) return true;
278 }
279 if (!key.matches(KEY_PATTERN)) {
280 r = warning(tr("Suspicious characters in key:"), key, "tags.paste.keydoesnotmatch");
281 if (r == 2 || r == 3) return false; if (r == 4) return true;
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");
285 if (r == 2 || r == 3) return false; if (r == 4) return true;
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?"),
295 tr("Ok"), tr("Cancel"), tr("Clear buffer"), tr("Ignore warnings"));
296 ed.setButtonIcons("ok", "cancel", "dialogs/delete", "pastetags");
297 ed.setContent("<html><b>"+text + "</b><br/><br/><div width=\"300px\">"+XmlWriter.encode(data, true)+"</html>");
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();
304 if (r == 0) r = 2;
305 // clean clipboard if user asked
306 if (r == 3) ClipboardUtils.copyString("");
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());
319 p.add(new JLabel(msg), GBC.eop());
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"),
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");
335
336 ed.showDialog();
337
338 // clean clipboard if user asked
339 if (ed.getValue() == 2) ClipboardUtils.copyString("");
340 }
341}
Note: See TracBrowser for help on using the repository browser.