1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.actions.search;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.io.PushbackReader;
|
---|
8 | import java.io.StringReader;
|
---|
9 | import java.util.Map.Entry;
|
---|
10 | import java.util.regex.Matcher;
|
---|
11 | import java.util.regex.Pattern;
|
---|
12 | import java.util.regex.PatternSyntaxException;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 | import org.openstreetmap.josm.data.osm.Node;
|
---|
16 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
17 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
18 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
19 | import org.openstreetmap.josm.data.osm.User;
|
---|
20 | import org.openstreetmap.josm.data.osm.Way;
|
---|
21 | import org.openstreetmap.josm.data.osm.visitor.CollectBackReferencesVisitor;
|
---|
22 | import org.openstreetmap.josm.tools.DateUtils;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Implements a google-like search.
|
---|
26 | * @author Imi
|
---|
27 | */
|
---|
28 | public class SearchCompiler {
|
---|
29 |
|
---|
30 | private boolean caseSensitive = false;
|
---|
31 | private boolean regexSearch = false;
|
---|
32 | private String rxErrorMsg = marktr("The regex \"{0}\" had a parse error at offset {1}, full error:\n\n{2}");
|
---|
33 | private PushbackTokenizer tokenizer;
|
---|
34 |
|
---|
35 | public SearchCompiler(boolean caseSensitive, boolean regexSearch, PushbackTokenizer tokenizer) {
|
---|
36 | this.caseSensitive = caseSensitive;
|
---|
37 | this.regexSearch = regexSearch;
|
---|
38 | this.tokenizer = tokenizer;
|
---|
39 | }
|
---|
40 |
|
---|
41 | abstract public static class Match {
|
---|
42 | abstract public boolean match(OsmPrimitive osm) throws ParseError;
|
---|
43 | }
|
---|
44 |
|
---|
45 | private static class Always extends Match {
|
---|
46 | @Override public boolean match(OsmPrimitive osm) {
|
---|
47 | return true;
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | private static class Not extends Match {
|
---|
52 | private final Match match;
|
---|
53 | public Not(Match match) {this.match = match;}
|
---|
54 | @Override public boolean match(OsmPrimitive osm) throws ParseError {
|
---|
55 | return !match.match(osm);
|
---|
56 | }
|
---|
57 | @Override public String toString() {return "!"+match;}
|
---|
58 | }
|
---|
59 |
|
---|
60 | private static class And extends Match {
|
---|
61 | private Match lhs;
|
---|
62 | private Match rhs;
|
---|
63 | public And(Match lhs, Match rhs) {this.lhs = lhs; this.rhs = rhs;}
|
---|
64 | @Override public boolean match(OsmPrimitive osm) throws ParseError {
|
---|
65 | return lhs.match(osm) && rhs.match(osm);
|
---|
66 | }
|
---|
67 | @Override public String toString() {return lhs+" && "+rhs;}
|
---|
68 | }
|
---|
69 |
|
---|
70 | private static class Or extends Match {
|
---|
71 | private Match lhs;
|
---|
72 | private Match rhs;
|
---|
73 | public Or(Match lhs, Match rhs) {this.lhs = lhs; this.rhs = rhs;}
|
---|
74 | @Override public boolean match(OsmPrimitive osm) throws ParseError {
|
---|
75 | return lhs.match(osm) || rhs.match(osm);
|
---|
76 | }
|
---|
77 | @Override public String toString() {return lhs+" || "+rhs;}
|
---|
78 | }
|
---|
79 |
|
---|
80 | private static class Id extends Match {
|
---|
81 | private long id;
|
---|
82 | public Id(long id) {this.id = id;}
|
---|
83 | @Override public boolean match(OsmPrimitive osm) {
|
---|
84 | return osm.id == id;
|
---|
85 | }
|
---|
86 | @Override public String toString() {return "id="+id;}
|
---|
87 | }
|
---|
88 |
|
---|
89 | private class KeyValue extends Match {
|
---|
90 | private String key;
|
---|
91 | private String value;
|
---|
92 | public KeyValue(String key, String value) {this.key = key; this.value = value; }
|
---|
93 | @Override public boolean match(OsmPrimitive osm) throws ParseError {
|
---|
94 |
|
---|
95 | if (regexSearch) {
|
---|
96 | if (osm.keys == null)
|
---|
97 | return false;
|
---|
98 |
|
---|
99 | /* The string search will just get a key like
|
---|
100 | * 'highway' and look that up as osm.get(key). But
|
---|
101 | * since we're doing a regex match we'll have to loop
|
---|
102 | * over all the keys to see if they match our regex,
|
---|
103 | * and only then try to match against the value
|
---|
104 | */
|
---|
105 |
|
---|
106 | Pattern searchKey = null;
|
---|
107 | Pattern searchValue = null;
|
---|
108 |
|
---|
109 | if (caseSensitive) {
|
---|
110 | try {
|
---|
111 | searchKey = Pattern.compile(key);
|
---|
112 | } catch (PatternSyntaxException e) {
|
---|
113 | throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
|
---|
114 | }
|
---|
115 | try {
|
---|
116 | searchValue = Pattern.compile(value);
|
---|
117 | } catch (PatternSyntaxException e) {
|
---|
118 | throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
|
---|
119 | }
|
---|
120 | } else {
|
---|
121 | try {
|
---|
122 | searchKey = Pattern.compile(key, Pattern.CASE_INSENSITIVE);
|
---|
123 | } catch (PatternSyntaxException e) {
|
---|
124 | throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
|
---|
125 | }
|
---|
126 | try {
|
---|
127 | searchValue = Pattern.compile(value, Pattern.CASE_INSENSITIVE);
|
---|
128 | } catch (PatternSyntaxException e) {
|
---|
129 | throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | for (Entry<String, String> e : osm.keys.entrySet()) {
|
---|
134 | String k = e.getKey();
|
---|
135 | String v = e.getValue();
|
---|
136 |
|
---|
137 | Matcher matcherKey = searchKey.matcher(k);
|
---|
138 | boolean matchedKey = matcherKey.find();
|
---|
139 |
|
---|
140 | if (matchedKey) {
|
---|
141 | Matcher matcherValue = searchValue.matcher(v);
|
---|
142 | boolean matchedValue = matcherValue.find();
|
---|
143 |
|
---|
144 | if (matchedValue)
|
---|
145 | return true;
|
---|
146 | }
|
---|
147 | }
|
---|
148 | } else {
|
---|
149 | String value = null;
|
---|
150 |
|
---|
151 | if (key.equals("timestamp"))
|
---|
152 | value = DateUtils.fromDate(osm.getTimestamp());
|
---|
153 | else
|
---|
154 | value = osm.get(key);
|
---|
155 |
|
---|
156 | if (value == null)
|
---|
157 | return false;
|
---|
158 |
|
---|
159 | String v1 = caseSensitive ? value : value.toLowerCase();
|
---|
160 | String v2 = caseSensitive ? this.value : this.value.toLowerCase();
|
---|
161 |
|
---|
162 | // is not Java 1.5
|
---|
163 | //v1 = java.text.Normalizer.normalize(v1, java.text.Normalizer.Form.NFC);
|
---|
164 | //v2 = java.text.Normalizer.normalize(v2, java.text.Normalizer.Form.NFC);
|
---|
165 | return v1.indexOf(v2) != -1;
|
---|
166 | }
|
---|
167 |
|
---|
168 | return false;
|
---|
169 | }
|
---|
170 | @Override public String toString() {return key+"="+value;}
|
---|
171 | }
|
---|
172 |
|
---|
173 | private class Any extends Match {
|
---|
174 | private String s;
|
---|
175 | public Any(String s) {this.s = s;}
|
---|
176 | @Override public boolean match(OsmPrimitive osm) throws ParseError {
|
---|
177 | if (osm.keys == null)
|
---|
178 | return s.equals("");
|
---|
179 |
|
---|
180 | String search;
|
---|
181 | Pattern searchRegex = null;
|
---|
182 |
|
---|
183 | if (regexSearch) {
|
---|
184 | search = s;
|
---|
185 | if (caseSensitive) {
|
---|
186 | try {
|
---|
187 | searchRegex = Pattern.compile(search);
|
---|
188 | } catch (PatternSyntaxException e) {
|
---|
189 | throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
|
---|
190 | }
|
---|
191 | } else {
|
---|
192 | try {
|
---|
193 | searchRegex = Pattern.compile(search, Pattern.CASE_INSENSITIVE);
|
---|
194 | } catch (PatternSyntaxException e) {
|
---|
195 | throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
|
---|
196 | }
|
---|
197 | }
|
---|
198 | } else {
|
---|
199 | search = caseSensitive ? s : s.toLowerCase();
|
---|
200 | }
|
---|
201 |
|
---|
202 | // is not Java 1.5
|
---|
203 | //search = java.text.Normalizer.normalize(search, java.text.Normalizer.Form.NFC);
|
---|
204 | for (Entry<String, String> e : osm.keys.entrySet()) {
|
---|
205 | if (regexSearch) {
|
---|
206 | String key = e.getKey();
|
---|
207 | String value = e.getValue();
|
---|
208 |
|
---|
209 | // is not Java 1.5
|
---|
210 | //value = java.text.Normalizer.normalize(value, java.text.Normalizer.Form.NFC);
|
---|
211 |
|
---|
212 | Matcher keyMatcher = searchRegex.matcher(key);
|
---|
213 | Matcher valMatcher = searchRegex.matcher(value);
|
---|
214 |
|
---|
215 | boolean keyMatchFound = keyMatcher.find();
|
---|
216 | boolean valMatchFound = valMatcher.find();
|
---|
217 |
|
---|
218 | if (keyMatchFound || valMatchFound)
|
---|
219 | return true;
|
---|
220 | } else {
|
---|
221 | String key = caseSensitive ? e.getKey() : e.getKey().toLowerCase();
|
---|
222 | String value = caseSensitive ? e.getValue() : e.getValue().toLowerCase();
|
---|
223 |
|
---|
224 | // is not Java 1.5
|
---|
225 | //value = java.text.Normalizer.normalize(value, java.text.Normalizer.Form.NFC);
|
---|
226 |
|
---|
227 | if (key.indexOf(search) != -1 || value.indexOf(search) != -1)
|
---|
228 | return true;
|
---|
229 | }
|
---|
230 | }
|
---|
231 | if (osm.user != null) {
|
---|
232 | String name = osm.user.name;
|
---|
233 | // is not Java 1.5
|
---|
234 | //String name = java.text.Normalizer.normalize(name, java.text.Normalizer.Form.NFC);
|
---|
235 | if (!caseSensitive)
|
---|
236 | name = name.toLowerCase();
|
---|
237 | if (name.indexOf(search) != -1)
|
---|
238 | return true;
|
---|
239 | }
|
---|
240 | return false;
|
---|
241 | }
|
---|
242 | @Override public String toString() {return s;}
|
---|
243 | }
|
---|
244 |
|
---|
245 | private static class ExactType extends Match {
|
---|
246 | private String type;
|
---|
247 | public ExactType(String type) {this.type = type;}
|
---|
248 | @Override public boolean match(OsmPrimitive osm) {
|
---|
249 | if (osm instanceof Node)
|
---|
250 | return type.equals("node");
|
---|
251 | if (osm instanceof Way)
|
---|
252 | return type.equals("way");
|
---|
253 | if (osm instanceof Relation)
|
---|
254 | return type.equals("relation");
|
---|
255 | throw new IllegalStateException("unknown class "+osm.getClass());
|
---|
256 | }
|
---|
257 | @Override public String toString() {return "type="+type;}
|
---|
258 | }
|
---|
259 |
|
---|
260 | private static class UserMatch extends Match {
|
---|
261 | private User user;
|
---|
262 | public UserMatch(String user) { this.user = User.get(user); }
|
---|
263 | @Override public boolean match(OsmPrimitive osm) {
|
---|
264 | return osm.user == user;
|
---|
265 | }
|
---|
266 | @Override public String toString() { return "user=" + user.name; }
|
---|
267 | }
|
---|
268 |
|
---|
269 | private static class NodeCount extends Match {
|
---|
270 | private int count;
|
---|
271 | public NodeCount(int count) {this.count = count;}
|
---|
272 | @Override public boolean match(OsmPrimitive osm) {
|
---|
273 | return osm instanceof Way && ((Way) osm).nodes.size() == count;
|
---|
274 | }
|
---|
275 | @Override public String toString() {return "nodes="+count;}
|
---|
276 | }
|
---|
277 |
|
---|
278 | private static class NodeCountRange extends Match {
|
---|
279 | private int minCount;
|
---|
280 | private int maxCount;
|
---|
281 | public NodeCountRange(int minCount, int maxCount) {
|
---|
282 | if(maxCount < minCount) {
|
---|
283 | this.minCount = maxCount;
|
---|
284 | this.maxCount = minCount;
|
---|
285 | } else {
|
---|
286 | this.minCount = minCount;
|
---|
287 | this.maxCount = maxCount;
|
---|
288 | }
|
---|
289 | }
|
---|
290 | @Override public boolean match(OsmPrimitive osm) {
|
---|
291 | if(!(osm instanceof Way)) return false;
|
---|
292 | int size = ((Way)osm).nodes.size();
|
---|
293 | return (size >= minCount) && (size <= maxCount);
|
---|
294 | }
|
---|
295 | @Override public String toString() {return "nodes="+minCount+"-"+maxCount;}
|
---|
296 | }
|
---|
297 |
|
---|
298 | private static class Modified extends Match {
|
---|
299 | @Override public boolean match(OsmPrimitive osm) {
|
---|
300 | return osm.modified || osm.id == 0;
|
---|
301 | }
|
---|
302 | @Override public String toString() {return "modified";}
|
---|
303 | }
|
---|
304 |
|
---|
305 | private static class Selected extends Match {
|
---|
306 | @Override public boolean match(OsmPrimitive osm) {
|
---|
307 | return osm.selected;
|
---|
308 | }
|
---|
309 | @Override public String toString() {return "selected";}
|
---|
310 | }
|
---|
311 |
|
---|
312 | private static class Incomplete extends Match {
|
---|
313 | @Override public boolean match(OsmPrimitive osm) {
|
---|
314 | return osm.incomplete;
|
---|
315 | }
|
---|
316 | @Override public String toString() {return "incomplete";}
|
---|
317 | }
|
---|
318 |
|
---|
319 | private static class Untagged extends Match {
|
---|
320 | @Override public boolean match(OsmPrimitive osm) {
|
---|
321 | return !osm.isTagged();
|
---|
322 | }
|
---|
323 | @Override public String toString() {return "untagged";}
|
---|
324 | }
|
---|
325 |
|
---|
326 | private static class Parent extends Match {
|
---|
327 | private Match child;
|
---|
328 | public Parent(Match m) { child = m; }
|
---|
329 | @Override public boolean match(OsmPrimitive osm) throws ParseError {
|
---|
330 | boolean isParent = false;
|
---|
331 |
|
---|
332 | // "parent" (null) should mean the same as "parent()"
|
---|
333 | // (Always). I.e. match everything
|
---|
334 | if (child == null)
|
---|
335 | child = new Always();
|
---|
336 |
|
---|
337 | if (osm instanceof Way) {
|
---|
338 | for (Node n : ((Way)osm).nodes)
|
---|
339 | isParent |= child.match(n);
|
---|
340 | } else if (osm instanceof Relation) {
|
---|
341 | for (RelationMember member : ((Relation)osm).members) {
|
---|
342 | if (member.member != null)
|
---|
343 | isParent |= child.match(member.member);
|
---|
344 | }
|
---|
345 | }
|
---|
346 | return isParent;
|
---|
347 | }
|
---|
348 | @Override public String toString() {return "parent(" + child + ")";}
|
---|
349 | }
|
---|
350 |
|
---|
351 | private static class Child extends Match {
|
---|
352 | private Match parent;
|
---|
353 | public Child(Match m) { parent = m; }
|
---|
354 | @Override public boolean match(OsmPrimitive osm) throws ParseError {
|
---|
355 | // "child" (null) should mean the same as "child()"
|
---|
356 | // (Always). I.e. match everything
|
---|
357 | if (parent == null)
|
---|
358 | parent = new Always();
|
---|
359 |
|
---|
360 | boolean isChild = false;
|
---|
361 | CollectBackReferencesVisitor backRefs = new CollectBackReferencesVisitor(Main.ds);
|
---|
362 | osm.visit(backRefs);
|
---|
363 | for (OsmPrimitive p : backRefs.data) {
|
---|
364 | isChild |= parent.match(p);
|
---|
365 | }
|
---|
366 | return isChild;
|
---|
367 | }
|
---|
368 | @Override public String toString() {return "child(" + parent + ")";}
|
---|
369 | }
|
---|
370 |
|
---|
371 | public static class ParseError extends Exception {
|
---|
372 | public ParseError(String msg) {
|
---|
373 | super(msg);
|
---|
374 | }
|
---|
375 | }
|
---|
376 |
|
---|
377 | public static Match compile(String searchStr, boolean caseSensitive, boolean regexSearch)
|
---|
378 | throws ParseError {
|
---|
379 | return new SearchCompiler(caseSensitive, regexSearch,
|
---|
380 | new PushbackTokenizer(
|
---|
381 | new PushbackReader(new StringReader(searchStr))))
|
---|
382 | .parse();
|
---|
383 | }
|
---|
384 |
|
---|
385 | public Match parse() throws ParseError {
|
---|
386 | Match m = parseJuxta();
|
---|
387 | if (!tokenizer.readIfEqual(null)) {
|
---|
388 | throw new ParseError("Unexpected token: " + tokenizer.nextToken());
|
---|
389 | }
|
---|
390 | return m;
|
---|
391 | }
|
---|
392 |
|
---|
393 | private Match parseJuxta() throws ParseError {
|
---|
394 | Match juxta = new Always();
|
---|
395 |
|
---|
396 | Match m;
|
---|
397 | while ((m = parseOr()) != null) {
|
---|
398 | juxta = new And(m, juxta);
|
---|
399 | }
|
---|
400 |
|
---|
401 | return juxta;
|
---|
402 | }
|
---|
403 |
|
---|
404 | private Match parseOr() throws ParseError {
|
---|
405 | Match a = parseNot();
|
---|
406 | if (tokenizer.readIfEqual("|")) {
|
---|
407 | Match b = parseNot();
|
---|
408 | if (a == null || b == null) {
|
---|
409 | throw new ParseError(tr("Missing arguments for or."));
|
---|
410 | }
|
---|
411 | return new Or(a, b);
|
---|
412 | }
|
---|
413 | return a;
|
---|
414 | }
|
---|
415 |
|
---|
416 | private Match parseNot() throws ParseError {
|
---|
417 | if (tokenizer.readIfEqual("-")) {
|
---|
418 | Match m = parseParens();
|
---|
419 | if (m == null) {
|
---|
420 | throw new ParseError(tr("Missing argument for not."));
|
---|
421 | }
|
---|
422 | return new Not(m);
|
---|
423 | }
|
---|
424 | return parseParens();
|
---|
425 | }
|
---|
426 |
|
---|
427 | private Match parseParens() throws ParseError {
|
---|
428 | if (tokenizer.readIfEqual("(")) {
|
---|
429 | Match m = parseJuxta();
|
---|
430 | if (!tokenizer.readIfEqual(")")) {
|
---|
431 | throw new ParseError(tr("Expected closing parenthesis."));
|
---|
432 | }
|
---|
433 | return m;
|
---|
434 | }
|
---|
435 | return parsePat();
|
---|
436 | }
|
---|
437 |
|
---|
438 | private Match parsePat() throws ParseError {
|
---|
439 | String tok = tokenizer.readText();
|
---|
440 |
|
---|
441 | if (tokenizer.readIfEqual(":")) {
|
---|
442 | String tok2 = tokenizer.readText();
|
---|
443 | if (tok == null) tok = "";
|
---|
444 | if (tok2 == null) tok2 = "";
|
---|
445 | return parseKV(tok, tok2);
|
---|
446 | }
|
---|
447 |
|
---|
448 | if (tok == null) {
|
---|
449 | return null;
|
---|
450 | } else if (tok.equals("modified")) {
|
---|
451 | return new Modified();
|
---|
452 | } else if (tok.equals("incomplete")) {
|
---|
453 | return new Incomplete();
|
---|
454 | } else if (tok.equals("untagged")) {
|
---|
455 | return new Untagged();
|
---|
456 | } else if (tok.equals("selected")) {
|
---|
457 | return new Selected();
|
---|
458 | } else if (tok.equals("child")) {
|
---|
459 | return new Child(parseParens());
|
---|
460 | } else if (tok.equals("parent")) {
|
---|
461 | return new Parent(parseParens());
|
---|
462 | } else {
|
---|
463 | return new Any(tok);
|
---|
464 | }
|
---|
465 | }
|
---|
466 |
|
---|
467 | private Match parseKV(String key, String value) {
|
---|
468 | if (key.equals("type")) {
|
---|
469 | return new ExactType(value);
|
---|
470 | } else if (key.equals("user")) {
|
---|
471 | return new UserMatch(value);
|
---|
472 | } else if (key.equals("nodes")) {
|
---|
473 | try {
|
---|
474 | return new NodeCount(Integer.parseInt(value));
|
---|
475 | } catch(Exception x) {}
|
---|
476 |
|
---|
477 | try {
|
---|
478 | String[] range = value.split("-", 2);
|
---|
479 | return new NodeCountRange(Integer.parseInt(range[0]), Integer.parseInt(range[1]));
|
---|
480 | } catch(Exception x) {}
|
---|
481 |
|
---|
482 | return new NodeCount(0);
|
---|
483 | } else if (key.equals("id")) {
|
---|
484 | try {
|
---|
485 | return new Id(Long.parseLong(value));
|
---|
486 | } catch (NumberFormatException x) {
|
---|
487 | return new Id(0);
|
---|
488 | }
|
---|
489 | } else {
|
---|
490 | return new KeyValue(key, value);
|
---|
491 | }
|
---|
492 | }
|
---|
493 | }
|
---|