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

Last change on this file since 7206 was 7005, checked in by Don-vip, 10 years ago

see #8465 - use diamond operator where applicable

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.help.HelpUtil;
22import org.openstreetmap.josm.gui.widgets.UrlLabel;
23import org.openstreetmap.josm.io.XmlWriter;
24import org.openstreetmap.josm.tools.LanguageInfo.LocaleType;
25
26/**
27 * Class that helps to parse tags from arbitrary text
28 */
29public final class TextTagParser {
30
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;
36
37 private TextTagParser() {
38 // Hide default constructor for utils classes
39 }
40
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 }
54
55 /**
56 * Read tags from "Free format"
57 */
58 Map<String, String> getFreeParsedTags() {
59 String k, v;
60 Map<String, String> tags = new HashMap<>();
61
62 while (true) {
63 skipEmpty();
64 if (pos == n) { break; }
65 k = parseString("\n\r\t= ");
66 if (pos == n) { tags.clear(); break; }
67 skipSign();
68 if (pos == n) { tags.clear(); break; }
69 v = parseString("\n\r\t ");
70 tags.put(k, v);
71 }
72 return tags;
73 }
74
75 private String parseString(String stopChars) {
76 char[] stop = stopChars.toCharArray();
77 Arrays.sort(stop);
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;
97 } else if (!quotesStarted && (Arrays.binarySearch(stop, c)>=0)) {
98 // stop-symbol found
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 }
112
113 private void skipSign() {
114 char c;
115 boolean signFound = false;
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
143 protected static String unescape(String k) {
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);
153 return (new TextAnalyzer(text)).parseString("\r\t\n");
154 }
155
156 /**
157 * Try to find tag-value pairs in given text
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) {
164 String[] lines = text.split(splitRegex);
165 Pattern p = Pattern.compile(tagRegex);
166 Map<String, String> tags = new HashMap<>();
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;
177 }
178 tags.put(k,v);
179 } else {
180 return null;
181 }
182 }
183 if (!tags.isEmpty()) {
184 return tags;
185 } else {
186 return null;
187 }
188 }
189
190 public static Map<String,String> getValidatedTagsFromText(String buf) {
191 Map<String,String> tags = readTagsFromText(buf);
192 return validateTags(tags) ? tags : null;
193 }
194
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 */
200
201 public static Map<String,String> readTagsFromText(String buf) {
202 Map<String,String> tags;
203
204 // Format
205 // tag1\tval1\ntag2\tval2\n
206 tags = readTagsByRegexp(buf, "[\\r\\n]+", "(.*?)\\t(.*?)", false);
207 // try "tag\tvalue\n" format
208 if (tags!=null) return tags;
209
210 // Format
211 // a=b \n c=d \n "a b"=hello
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;
218
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);
223 tags = readTagsByRegexp(bufJson, "[\\s]*,[\\s]*",
224 "[\\s]*(\\\".*?[^\\\\]\\\")"+"[\\s]*:[\\s]*"+"(\\\".*?[^\\\\]\\\")[\\s]*", true);
225 if (tags!=null) return tags;
226
227 // Free format
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
236 * @param tags - map key-&gt;value to check
237 * @return true if the tags should be pasted
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,
246 s), "", "tags.paste.toomanytags");
247 if (r==2 || r==3) return false; if (r==4) return true;
248 }
249 for (Entry<String, String> entry : tags.entrySet()) {
250 String key = entry.getKey();
251 String value = entry.getValue();
252 if (key.length() > MAX_KEY_LENGTH) {
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;
255 }
256 if (!key.matches(KEY_PATTERN)) {
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;
259 }
260 if (value.length() > MAX_VALUE_LENGTH) {
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;
263 }
264 }
265 return true;
266 }
267
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?"),
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"});
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();
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;
285 }
286
287 /**
288 * Shows message that the buffer can not be pasted, allowing user to clean the buffer
289 * @param helpTopic the help topic of the parent action
290 * TODO: Replace by proper HelpAwareOptionPane instead of self-made help link
291 */
292 public static void showBadBufferMessage(String helpTopic) {
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());
297 String helpUrl = HelpUtil.getHelpTopicUrl(HelpUtil.buildAbsoluteHelpTopic(helpTopic, LocaleType.DEFAULT));
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")});
306
307 ed.setButtonIcons(new String[]{"ok.png", "dialogs/delete.png"});
308
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 }
320}
Note: See TracBrowser for help on using the repository browser.