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