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

Last change on this file since 5915 was 5915, checked in by stoecker, 11 years ago

use 3 step wiki loading fallback, cleanup handling of language fallbacks

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