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

Last change on this file since 12192 was 10763, checked in by Don-vip, 8 years ago

sonar - squid:S2156 - "final" classes should not have "protected" members

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