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.List;
|
---|
10 | import java.util.Map.Entry;
|
---|
11 | import java.util.regex.Matcher;
|
---|
12 | import java.util.regex.Pattern;
|
---|
13 | import java.util.regex.PatternSyntaxException;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.Main;
|
---|
16 | import org.openstreetmap.josm.data.osm.Node;
|
---|
17 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
18 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
19 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
20 | import org.openstreetmap.josm.data.osm.User;
|
---|
21 | import org.openstreetmap.josm.data.osm.Way;
|
---|
22 | import org.openstreetmap.josm.data.osm.visitor.CollectBackReferencesVisitor;
|
---|
23 | import org.openstreetmap.josm.tools.DateUtils;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Implements a google-like search.
|
---|
27 | * @author Imi
|
---|
28 | */
|
---|
29 | public class SearchCompiler {
|
---|
30 |
|
---|
31 | private boolean caseSensitive = false;
|
---|
32 | private boolean regexSearch = false;
|
---|
33 | private String rxErrorMsg = marktr("The regex \"{0}\" had a parse error at offset {1}, full error:\n\n{2}");
|
---|
34 | private PushbackTokenizer tokenizer;
|
---|
35 | private CollectBackReferencesVisitor childBackRefs;
|
---|
36 |
|
---|
37 | public SearchCompiler(boolean caseSensitive, boolean regexSearch, PushbackTokenizer tokenizer) {
|
---|
38 | this.caseSensitive = caseSensitive;
|
---|
39 | this.regexSearch = regexSearch;
|
---|
40 | this.tokenizer = tokenizer;
|
---|
41 | childBackRefs = new CollectBackReferencesVisitor(Main.main.getCurrentDataSet());
|
---|
42 | }
|
---|
43 |
|
---|
44 | abstract public static class Match {
|
---|
45 | abstract public boolean match(OsmPrimitive osm) throws ParseError;
|
---|
46 | }
|
---|
47 |
|
---|
48 | private static class Always extends Match {
|
---|
49 | @Override public boolean match(OsmPrimitive osm) {
|
---|
50 | return true;
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | private static class Not extends Match {
|
---|
55 | private final Match match;
|
---|
56 | public Not(Match match) {this.match = match;}
|
---|
57 | @Override public boolean match(OsmPrimitive osm) throws ParseError {
|
---|
58 | return !match.match(osm);
|
---|
59 | }
|
---|
60 | @Override public String toString() {return "!"+match;}
|
---|
61 | }
|
---|
62 |
|
---|
63 | private static class And extends Match {
|
---|
64 | private Match lhs;
|
---|
65 | private Match rhs;
|
---|
66 | public And(Match lhs, Match rhs) {this.lhs = lhs; this.rhs = rhs;}
|
---|
67 | @Override public boolean match(OsmPrimitive osm) throws ParseError {
|
---|
68 | return lhs.match(osm) && rhs.match(osm);
|
---|
69 | }
|
---|
70 | @Override public String toString() {return lhs+" && "+rhs;}
|
---|
71 | }
|
---|
72 |
|
---|
73 | private static class Or extends Match {
|
---|
74 | private Match lhs;
|
---|
75 | private Match rhs;
|
---|
76 | public Or(Match lhs, Match rhs) {this.lhs = lhs; this.rhs = rhs;}
|
---|
77 | @Override public boolean match(OsmPrimitive osm) throws ParseError {
|
---|
78 | return lhs.match(osm) || rhs.match(osm);
|
---|
79 | }
|
---|
80 | @Override public String toString() {return lhs+" || "+rhs;}
|
---|
81 | }
|
---|
82 |
|
---|
83 | private static class Id extends Match {
|
---|
84 | private long id;
|
---|
85 | public Id(long id) {this.id = id;}
|
---|
86 | @Override public boolean match(OsmPrimitive osm) {
|
---|
87 | return osm.getId() == id;
|
---|
88 | }
|
---|
89 | @Override public String toString() {return "id="+id;}
|
---|
90 | }
|
---|
91 |
|
---|
92 | private class KeyValue extends Match {
|
---|
93 | private String key;
|
---|
94 | private String value;
|
---|
95 | public KeyValue(String key, String value) {this.key = key; this.value = value; }
|
---|
96 | @Override public boolean match(OsmPrimitive osm) throws ParseError {
|
---|
97 |
|
---|
98 | if (regexSearch) {
|
---|
99 | if (!osm.hasKeys())
|
---|
100 | return false;
|
---|
101 |
|
---|
102 | /* The string search will just get a key like
|
---|
103 | * 'highway' and look that up as osm.get(key). But
|
---|
104 | * since we're doing a regex match we'll have to loop
|
---|
105 | * over all the keys to see if they match our regex,
|
---|
106 | * and only then try to match against the value
|
---|
107 | */
|
---|
108 |
|
---|
109 | Pattern searchKey = null;
|
---|
110 | Pattern searchValue = null;
|
---|
111 | int searchFlags = regexFlags();
|
---|
112 |
|
---|
113 | try {
|
---|
114 | searchKey = Pattern.compile(key, searchFlags);
|
---|
115 | searchValue = Pattern.compile(value, searchFlags);
|
---|
116 | } catch (PatternSyntaxException e) {
|
---|
117 | throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
|
---|
118 | }
|
---|
119 |
|
---|
120 | for (Entry<String, String> e : osm.entrySet()) {
|
---|
121 | String k = e.getKey();
|
---|
122 | String v = e.getValue();
|
---|
123 |
|
---|
124 | Matcher matcherKey = searchKey.matcher(k);
|
---|
125 | boolean matchedKey = matcherKey.find();
|
---|
126 |
|
---|
127 | if (matchedKey) {
|
---|
128 | Matcher matcherValue = searchValue.matcher(v);
|
---|
129 | boolean matchedValue = matcherValue.find();
|
---|
130 |
|
---|
131 | if (matchedValue)
|
---|
132 | return true;
|
---|
133 | }
|
---|
134 | }
|
---|
135 | } else {
|
---|
136 | String value = null;
|
---|
137 |
|
---|
138 | if (key.equals("timestamp")) {
|
---|
139 | value = DateUtils.fromDate(osm.getTimestamp());
|
---|
140 | } else {
|
---|
141 | value = osm.get(key);
|
---|
142 | }
|
---|
143 |
|
---|
144 | if (value == null)
|
---|
145 | return false;
|
---|
146 |
|
---|
147 | String v1 = caseSensitive ? value : value.toLowerCase();
|
---|
148 | String v2 = caseSensitive ? this.value : this.value.toLowerCase();
|
---|
149 |
|
---|
150 | // is not Java 1.5
|
---|
151 | //v1 = java.text.Normalizer.normalize(v1, java.text.Normalizer.Form.NFC);
|
---|
152 | //v2 = java.text.Normalizer.normalize(v2, java.text.Normalizer.Form.NFC);
|
---|
153 | return v1.indexOf(v2) != -1;
|
---|
154 | }
|
---|
155 |
|
---|
156 | return false;
|
---|
157 | }
|
---|
158 | @Override public String toString() {return key+"="+value;}
|
---|
159 | }
|
---|
160 |
|
---|
161 | private static class ExactKeyValue extends Match {
|
---|
162 |
|
---|
163 | private enum Mode {
|
---|
164 | ANY, ANY_KEY, ANY_VALUE, EXACT, NONE, MISSING_KEY,
|
---|
165 | ANY_KEY_REGEXP, ANY_VALUE_REGEXP, EXACT_REGEXP, MISSING_KEY_REGEXP;
|
---|
166 | }
|
---|
167 |
|
---|
168 | private final String key;
|
---|
169 | private final String value;
|
---|
170 | private final Pattern keyPattern;
|
---|
171 | private final Pattern valuePattern;
|
---|
172 | private final Mode mode;
|
---|
173 |
|
---|
174 | public ExactKeyValue(boolean regexp, String key, String value) throws ParseError {
|
---|
175 | if (key == "")
|
---|
176 | throw new ParseError(tr("Key cannot be empty when tag operator is used. Sample use: key=value"));
|
---|
177 | this.key = key;
|
---|
178 | this.value = value;
|
---|
179 | if ("".equals(value) && "*".equals(key)) {
|
---|
180 | mode = Mode.NONE;
|
---|
181 | } else if ("".equals(value)) {
|
---|
182 | if (regexp) {
|
---|
183 | mode = Mode.MISSING_KEY_REGEXP;
|
---|
184 | } else {
|
---|
185 | mode = Mode.MISSING_KEY;
|
---|
186 | }
|
---|
187 | } else if ("*".equals(key) && "*".equals(value)) {
|
---|
188 | mode = Mode.ANY;
|
---|
189 | } else if ("*".equals(key)) {
|
---|
190 | if (regexp) {
|
---|
191 | mode = Mode.ANY_KEY_REGEXP;
|
---|
192 | } else {
|
---|
193 | mode = Mode.ANY_KEY;
|
---|
194 | }
|
---|
195 | } else if ("*".equals(value)) {
|
---|
196 | if (regexp) {
|
---|
197 | mode = Mode.ANY_VALUE_REGEXP;
|
---|
198 | } else {
|
---|
199 | mode = Mode.ANY_VALUE;
|
---|
200 | }
|
---|
201 | } else {
|
---|
202 | if (regexp) {
|
---|
203 | mode = Mode.EXACT_REGEXP;
|
---|
204 | } else {
|
---|
205 | mode = Mode.EXACT;
|
---|
206 | }
|
---|
207 | }
|
---|
208 |
|
---|
209 | if (regexp && key.length() > 0 && !key.equals("*")) {
|
---|
210 | keyPattern = Pattern.compile(key);
|
---|
211 | } else {
|
---|
212 | keyPattern = null;
|
---|
213 | }
|
---|
214 | if (regexp && value.length() > 0 && !value.equals("*")) {
|
---|
215 | try {
|
---|
216 | valuePattern = Pattern.compile(value);
|
---|
217 | } catch (PatternSyntaxException e) {
|
---|
218 | throw new ParseError(tr("Pattern Syntax Error: Pattern {0} in {1} is illegal!", e.getPattern(), value));
|
---|
219 | }
|
---|
220 | } else {
|
---|
221 | valuePattern = null;
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | @Override
|
---|
226 | public boolean match(OsmPrimitive osm) throws ParseError {
|
---|
227 |
|
---|
228 | if (!osm.hasKeys())
|
---|
229 | return mode == Mode.NONE;
|
---|
230 |
|
---|
231 | switch (mode) {
|
---|
232 | case NONE:
|
---|
233 | return false;
|
---|
234 | case MISSING_KEY:
|
---|
235 | return osm.get(key) == null;
|
---|
236 | case ANY:
|
---|
237 | return true;
|
---|
238 | case ANY_VALUE:
|
---|
239 | return osm.get(key) != null;
|
---|
240 | case ANY_KEY:
|
---|
241 | for (String v:osm.getKeys().values()) {
|
---|
242 | if (v.equals(value))
|
---|
243 | return true;
|
---|
244 | }
|
---|
245 | return false;
|
---|
246 | case EXACT:
|
---|
247 | return value.equals(osm.get(key));
|
---|
248 | case ANY_KEY_REGEXP:
|
---|
249 | for (String v:osm.getKeys().values()) {
|
---|
250 | if (valuePattern.matcher(v).matches())
|
---|
251 | return true;
|
---|
252 | }
|
---|
253 | return false;
|
---|
254 | case ANY_VALUE_REGEXP:
|
---|
255 | case EXACT_REGEXP:
|
---|
256 | for (Entry<String, String> entry:osm.entrySet()) {
|
---|
257 | if (keyPattern.matcher(entry.getKey()).matches()) {
|
---|
258 | if (mode == Mode.ANY_VALUE_REGEXP
|
---|
259 | || valuePattern.matcher(entry.getValue()).matches())
|
---|
260 | return true;
|
---|
261 | }
|
---|
262 | }
|
---|
263 | return false;
|
---|
264 | case MISSING_KEY_REGEXP:
|
---|
265 | for (String k:osm.keySet()) {
|
---|
266 | if (keyPattern.matcher(k).matches())
|
---|
267 | return false;
|
---|
268 | }
|
---|
269 | return true;
|
---|
270 | }
|
---|
271 | throw new AssertionError("Missed state");
|
---|
272 | }
|
---|
273 |
|
---|
274 | @Override
|
---|
275 | public String toString() {
|
---|
276 | return key + '=' + value;
|
---|
277 | }
|
---|
278 |
|
---|
279 | }
|
---|
280 |
|
---|
281 | private class Any extends Match {
|
---|
282 | private String s;
|
---|
283 | public Any(String s) {this.s = s;}
|
---|
284 | @Override public boolean match(OsmPrimitive osm) throws ParseError {
|
---|
285 | if (!osm.hasKeys())
|
---|
286 | return s.equals("");
|
---|
287 |
|
---|
288 | String search;
|
---|
289 | Pattern searchRegex = null;
|
---|
290 |
|
---|
291 | if (regexSearch) {
|
---|
292 | search = s;
|
---|
293 | int searchFlags = regexFlags();
|
---|
294 |
|
---|
295 | try {
|
---|
296 | searchRegex = Pattern.compile(search, searchFlags);
|
---|
297 | } catch (PatternSyntaxException e) {
|
---|
298 | throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
|
---|
299 | }
|
---|
300 | } else {
|
---|
301 | search = caseSensitive ? s : s.toLowerCase();
|
---|
302 | }
|
---|
303 |
|
---|
304 | // is not Java 1.5
|
---|
305 | //search = java.text.Normalizer.normalize(search, java.text.Normalizer.Form.NFC);
|
---|
306 | for (Entry<String, String> e : osm.entrySet()) {
|
---|
307 | if (regexSearch) {
|
---|
308 | String key = e.getKey();
|
---|
309 | String value = e.getValue();
|
---|
310 |
|
---|
311 | // is not Java 1.5
|
---|
312 | //value = java.text.Normalizer.normalize(value, java.text.Normalizer.Form.NFC);
|
---|
313 |
|
---|
314 | Matcher keyMatcher = searchRegex.matcher(key);
|
---|
315 | Matcher valMatcher = searchRegex.matcher(value);
|
---|
316 |
|
---|
317 | boolean keyMatchFound = keyMatcher.find();
|
---|
318 | boolean valMatchFound = valMatcher.find();
|
---|
319 |
|
---|
320 | if (keyMatchFound || valMatchFound)
|
---|
321 | return true;
|
---|
322 | } else {
|
---|
323 | String key = caseSensitive ? e.getKey() : e.getKey().toLowerCase();
|
---|
324 | String value = caseSensitive ? e.getValue() : e.getValue().toLowerCase();
|
---|
325 |
|
---|
326 | // is not Java 1.5
|
---|
327 | //value = java.text.Normalizer.normalize(value, java.text.Normalizer.Form.NFC);
|
---|
328 |
|
---|
329 | if (key.indexOf(search) != -1 || value.indexOf(search) != -1)
|
---|
330 | return true;
|
---|
331 | }
|
---|
332 | }
|
---|
333 | if (osm.getUser() != null) {
|
---|
334 | String name = osm.getUser().getName();
|
---|
335 | // is not Java 1.5
|
---|
336 | //String name = java.text.Normalizer.normalize(name, java.text.Normalizer.Form.NFC);
|
---|
337 | if (!caseSensitive) {
|
---|
338 | name = name.toLowerCase();
|
---|
339 | }
|
---|
340 | if (name.indexOf(search) != -1)
|
---|
341 | return true;
|
---|
342 | }
|
---|
343 | return false;
|
---|
344 | }
|
---|
345 | @Override public String toString() {return s;}
|
---|
346 | }
|
---|
347 |
|
---|
348 | private static class ExactType extends Match {
|
---|
349 | private final Class<?> type;
|
---|
350 | public ExactType(String type) throws ParseError {
|
---|
351 | if ("node".equals(type)) {
|
---|
352 | this.type = Node.class;
|
---|
353 | } else if ("way".equals(type)) {
|
---|
354 | this.type = Way.class;
|
---|
355 | } else if ("relation".equals(type)) {
|
---|
356 | this.type = Relation.class;
|
---|
357 | } else
|
---|
358 | throw new ParseError(tr("Unknown primitive type: {0}. Allowed values are node, way or relation",
|
---|
359 | type));
|
---|
360 | }
|
---|
361 | @Override public boolean match(OsmPrimitive osm) {
|
---|
362 | return osm.getClass() == type;
|
---|
363 | }
|
---|
364 | @Override public String toString() {return "type="+type;}
|
---|
365 | }
|
---|
366 |
|
---|
367 | private static class UserMatch extends Match {
|
---|
368 | private User user;
|
---|
369 | public UserMatch(String user) {
|
---|
370 | List<User> users = User.getByName(user);
|
---|
371 | if (!users.isEmpty()) {
|
---|
372 | // selecting an arbitrary user
|
---|
373 | this.user = users.get(0);
|
---|
374 | } else {
|
---|
375 | user = null;
|
---|
376 | }
|
---|
377 | }
|
---|
378 | @Override public boolean match(OsmPrimitive osm) {
|
---|
379 | if (osm.getUser() == null && user == null) return true;
|
---|
380 | if (osm.getUser() == null) return false;
|
---|
381 | return osm.getUser().equals(user);
|
---|
382 | }
|
---|
383 | @Override public String toString() {
|
---|
384 | return "user=" + user == null ? "" : user.getName();
|
---|
385 | }
|
---|
386 | }
|
---|
387 |
|
---|
388 | private static class NodeCount extends Match {
|
---|
389 | private int count;
|
---|
390 | public NodeCount(int count) {this.count = count;}
|
---|
391 | @Override public boolean match(OsmPrimitive osm) {
|
---|
392 | return osm instanceof Way && ((Way) osm).getNodesCount() == count;
|
---|
393 | }
|
---|
394 | @Override public String toString() {return "nodes="+count;}
|
---|
395 | }
|
---|
396 |
|
---|
397 | private static class NodeCountRange extends Match {
|
---|
398 | private int minCount;
|
---|
399 | private int maxCount;
|
---|
400 | public NodeCountRange(int minCount, int maxCount) {
|
---|
401 | if(maxCount < minCount) {
|
---|
402 | this.minCount = maxCount;
|
---|
403 | this.maxCount = minCount;
|
---|
404 | } else {
|
---|
405 | this.minCount = minCount;
|
---|
406 | this.maxCount = maxCount;
|
---|
407 | }
|
---|
408 | }
|
---|
409 | @Override public boolean match(OsmPrimitive osm) {
|
---|
410 | if(!(osm instanceof Way)) return false;
|
---|
411 | int size = ((Way)osm).getNodesCount();
|
---|
412 | return (size >= minCount) && (size <= maxCount);
|
---|
413 | }
|
---|
414 | @Override public String toString() {return "nodes="+minCount+"-"+maxCount;}
|
---|
415 | }
|
---|
416 |
|
---|
417 | private static class TagCount extends Match {
|
---|
418 | private int count;
|
---|
419 | public TagCount(int count) {this.count = count;}
|
---|
420 | @Override public boolean match(OsmPrimitive osm) {
|
---|
421 | int size = osm.getKeys().size();
|
---|
422 | return size == count;
|
---|
423 | }
|
---|
424 | @Override public String toString() {return "tags="+count;}
|
---|
425 | }
|
---|
426 |
|
---|
427 | private static class TagCountRange extends Match {
|
---|
428 | private int minCount;
|
---|
429 | private int maxCount;
|
---|
430 | public TagCountRange(int minCount, int maxCount) {
|
---|
431 | if(maxCount < minCount) {
|
---|
432 | this.minCount = maxCount;
|
---|
433 | this.maxCount = minCount;
|
---|
434 | } else {
|
---|
435 | this.minCount = minCount;
|
---|
436 | this.maxCount = maxCount;
|
---|
437 | }
|
---|
438 | }
|
---|
439 | @Override public boolean match(OsmPrimitive osm) {
|
---|
440 | int size = osm.getKeys().size();
|
---|
441 | return (size >= minCount) && (size <= maxCount);
|
---|
442 | }
|
---|
443 | @Override public String toString() {return "tags="+minCount+"-"+maxCount;}
|
---|
444 | }
|
---|
445 |
|
---|
446 | private static class Modified extends Match {
|
---|
447 | @Override public boolean match(OsmPrimitive osm) {
|
---|
448 | return osm.isModified() || osm.isNew();
|
---|
449 | }
|
---|
450 | @Override public String toString() {return "modified";}
|
---|
451 | }
|
---|
452 |
|
---|
453 | private static class Selected extends Match {
|
---|
454 | @Override public boolean match(OsmPrimitive osm) {
|
---|
455 | return Main.main.getCurrentDataSet().isSelected(osm);
|
---|
456 | }
|
---|
457 | @Override public String toString() {return "selected";}
|
---|
458 | }
|
---|
459 |
|
---|
460 | private static class Incomplete extends Match {
|
---|
461 | @Override public boolean match(OsmPrimitive osm) {
|
---|
462 | return osm.incomplete;
|
---|
463 | }
|
---|
464 | @Override public String toString() {return "incomplete";}
|
---|
465 | }
|
---|
466 |
|
---|
467 | private static class Untagged extends Match {
|
---|
468 | @Override public boolean match(OsmPrimitive osm) {
|
---|
469 | return !osm.isTagged();
|
---|
470 | }
|
---|
471 | @Override public String toString() {return "untagged";}
|
---|
472 | }
|
---|
473 |
|
---|
474 | private static class Parent extends Match {
|
---|
475 | private Match child;
|
---|
476 | public Parent(Match m) { child = m; }
|
---|
477 | @Override public boolean match(OsmPrimitive osm) throws ParseError {
|
---|
478 | boolean isParent = false;
|
---|
479 |
|
---|
480 | // "parent" (null) should mean the same as "parent()"
|
---|
481 | // (Always). I.e. match everything
|
---|
482 | if (child == null) {
|
---|
483 | child = new Always();
|
---|
484 | }
|
---|
485 |
|
---|
486 | if (osm instanceof Way) {
|
---|
487 | for (Node n : ((Way)osm).getNodes()) {
|
---|
488 | isParent |= child.match(n);
|
---|
489 | }
|
---|
490 | } else if (osm instanceof Relation) {
|
---|
491 | for (RelationMember member : ((Relation)osm).getMembers()) {
|
---|
492 | if (member.getMember() != null) {
|
---|
493 | // TODO Nullable member will not be allowed
|
---|
494 | isParent |= child.match(member.getMember());
|
---|
495 | }
|
---|
496 | }
|
---|
497 | }
|
---|
498 | return isParent;
|
---|
499 | }
|
---|
500 | @Override public String toString() {return "parent(" + child + ")";}
|
---|
501 | }
|
---|
502 |
|
---|
503 | private static class Child extends Match {
|
---|
504 | private final Match parent;
|
---|
505 | private final CollectBackReferencesVisitor childBackRefs;
|
---|
506 |
|
---|
507 | public Child(Match m, CollectBackReferencesVisitor childBackRefs) {
|
---|
508 | // "child" (null) should mean the same as "child()"
|
---|
509 | // (Always). I.e. match everything
|
---|
510 | if (m == null) {
|
---|
511 | parent = new Always();
|
---|
512 | } else {
|
---|
513 | parent = m;
|
---|
514 | }
|
---|
515 | this.childBackRefs = childBackRefs;
|
---|
516 | }
|
---|
517 |
|
---|
518 | @Override public boolean match(OsmPrimitive osm) throws ParseError {
|
---|
519 | boolean isChild = false;
|
---|
520 | childBackRefs.initialize();
|
---|
521 | osm.visit(childBackRefs);
|
---|
522 | for (OsmPrimitive p : childBackRefs.getData()) {
|
---|
523 | isChild |= parent.match(p);
|
---|
524 | }
|
---|
525 | return isChild;
|
---|
526 | }
|
---|
527 | @Override public String toString() {return "child(" + parent + ")";}
|
---|
528 | }
|
---|
529 |
|
---|
530 | public static class ParseError extends Exception {
|
---|
531 | public ParseError(String msg) {
|
---|
532 | super(msg);
|
---|
533 | }
|
---|
534 | }
|
---|
535 |
|
---|
536 | public static Match compile(String searchStr, boolean caseSensitive, boolean regexSearch)
|
---|
537 | throws ParseError {
|
---|
538 | return new SearchCompiler(caseSensitive, regexSearch,
|
---|
539 | new PushbackTokenizer(
|
---|
540 | new PushbackReader(new StringReader(searchStr))))
|
---|
541 | .parse();
|
---|
542 | }
|
---|
543 |
|
---|
544 | public Match parse() throws ParseError {
|
---|
545 | Match m = parseJuxta();
|
---|
546 | if (!tokenizer.readIfEqual(null))
|
---|
547 | throw new ParseError(tr("Unexpected token: {0}", tokenizer.nextToken()));
|
---|
548 | return m;
|
---|
549 | }
|
---|
550 |
|
---|
551 | private Match parseJuxta() throws ParseError {
|
---|
552 | Match juxta = new Always();
|
---|
553 |
|
---|
554 | Match m;
|
---|
555 | while ((m = parseOr()) != null) {
|
---|
556 | juxta = new And(m, juxta);
|
---|
557 | }
|
---|
558 |
|
---|
559 | return juxta;
|
---|
560 | }
|
---|
561 |
|
---|
562 | private Match parseOr() throws ParseError {
|
---|
563 | Match a = parseNot();
|
---|
564 | if (tokenizer.readIfEqual("|")) {
|
---|
565 | Match b = parseNot();
|
---|
566 | if (a == null || b == null)
|
---|
567 | throw new ParseError(tr("Missing arguments for or."));
|
---|
568 | return new Or(a, b);
|
---|
569 | }
|
---|
570 | return a;
|
---|
571 | }
|
---|
572 |
|
---|
573 | private Match parseNot() throws ParseError {
|
---|
574 | if (tokenizer.readIfEqual("-")) {
|
---|
575 | Match m = parseParens();
|
---|
576 | if (m == null)
|
---|
577 | throw new ParseError(tr("Missing argument for not."));
|
---|
578 | return new Not(m);
|
---|
579 | }
|
---|
580 | return parseParens();
|
---|
581 | }
|
---|
582 |
|
---|
583 | private Match parseParens() throws ParseError {
|
---|
584 | if (tokenizer.readIfEqual("(")) {
|
---|
585 | Match m = parseJuxta();
|
---|
586 | if (!tokenizer.readIfEqual(")"))
|
---|
587 | throw new ParseError(tr("Expected closing parenthesis."));
|
---|
588 | return m;
|
---|
589 | }
|
---|
590 | return parsePat();
|
---|
591 | }
|
---|
592 |
|
---|
593 | private Match parsePat() throws ParseError {
|
---|
594 | String tok = tokenizer.readText();
|
---|
595 |
|
---|
596 | if (tokenizer.readIfEqual(":")) {
|
---|
597 | String tok2 = tokenizer.readText();
|
---|
598 | if (tok == null) {
|
---|
599 | tok = "";
|
---|
600 | }
|
---|
601 | if (tok2 == null) {
|
---|
602 | tok2 = "";
|
---|
603 | }
|
---|
604 | return parseKV(tok, tok2);
|
---|
605 | }
|
---|
606 |
|
---|
607 | if (tokenizer.readIfEqual("=")) {
|
---|
608 | String tok2 = tokenizer.readText();
|
---|
609 | if (tok == null) {
|
---|
610 | tok = "";
|
---|
611 | }
|
---|
612 | if (tok2 == null) {
|
---|
613 | tok2 = "";
|
---|
614 | }
|
---|
615 | return new ExactKeyValue(regexSearch, tok, tok2);
|
---|
616 | }
|
---|
617 |
|
---|
618 | if (tok == null)
|
---|
619 | return null;
|
---|
620 | else if (tok.equals("modified"))
|
---|
621 | return new Modified();
|
---|
622 | else if (tok.equals("incomplete"))
|
---|
623 | return new Incomplete();
|
---|
624 | else if (tok.equals("untagged"))
|
---|
625 | return new Untagged();
|
---|
626 | else if (tok.equals("selected"))
|
---|
627 | return new Selected();
|
---|
628 | else if (tok.equals("child"))
|
---|
629 | return new Child(parseParens(), childBackRefs);
|
---|
630 | else if (tok.equals("parent"))
|
---|
631 | return new Parent(parseParens());
|
---|
632 | else
|
---|
633 | return new Any(tok);
|
---|
634 | }
|
---|
635 |
|
---|
636 | private Match parseKV(String key, String value) throws ParseError {
|
---|
637 | if (key.equals("type"))
|
---|
638 | return new ExactType(value);
|
---|
639 | else if (key.equals("user"))
|
---|
640 | return new UserMatch(value);
|
---|
641 | else if (key.equals("tags"))
|
---|
642 | try {
|
---|
643 | String[] range = value.split("-");
|
---|
644 | if (range.length == 1)
|
---|
645 | return new TagCount(Integer.parseInt(value));
|
---|
646 | else if (range.length == 2)
|
---|
647 | return new TagCountRange(Integer.parseInt(range[0]), Integer.parseInt(range[1]));
|
---|
648 | else
|
---|
649 | throw new ParseError(tr("Wrong number of parameters for tags operator."));
|
---|
650 | } catch (NumberFormatException e) {
|
---|
651 | throw new ParseError(tr("Incorrect value of tags operator: {0}. Tags operator expects number of tags or range, for example tags:1 or tags:2-5", value));
|
---|
652 | }
|
---|
653 | else if (key.equals("nodes")) {
|
---|
654 | try {
|
---|
655 | String[] range = value.split("-");
|
---|
656 | if (range.length == 1)
|
---|
657 | return new NodeCount(Integer.parseInt(value));
|
---|
658 | else if (range.length == 2)
|
---|
659 | return new NodeCountRange(Integer.parseInt(range[0]), Integer.parseInt(range[1]));
|
---|
660 | else
|
---|
661 | throw new ParseError(tr("Wrong number of parameters for nodes operator."));
|
---|
662 | } catch (NumberFormatException e) {
|
---|
663 | throw new ParseError(tr("Incorrect value of nodes operator: {0}. Nodes operator expects number of nodes or range, for example nodes:10-20", value));
|
---|
664 | }
|
---|
665 |
|
---|
666 | } else if (key.equals("id")) {
|
---|
667 | try {
|
---|
668 | return new Id(Long.parseLong(value));
|
---|
669 | } catch (NumberFormatException x) {
|
---|
670 | throw new ParseError(tr("Incorrect value of id operator: {0}. Number is expected.", value));
|
---|
671 | }
|
---|
672 | } else
|
---|
673 | return new KeyValue(key, value);
|
---|
674 | }
|
---|
675 |
|
---|
676 | private int regexFlags() {
|
---|
677 | int searchFlags = 0;
|
---|
678 |
|
---|
679 | // Enables canonical Unicode equivalence so that e.g. the two
|
---|
680 | // forms of "\u00e9gal" and "e\u0301gal" will match.
|
---|
681 | //
|
---|
682 | // It makes sense to match no matter how the character
|
---|
683 | // happened to be constructed.
|
---|
684 | searchFlags |= Pattern.CANON_EQ;
|
---|
685 |
|
---|
686 | // Make "." match any character including newline (/s in Perl)
|
---|
687 | searchFlags |= Pattern.DOTALL;
|
---|
688 |
|
---|
689 | // CASE_INSENSITIVE by itself only matches US-ASCII case
|
---|
690 | // insensitively, but the OSM data is in Unicode. With
|
---|
691 | // UNICODE_CASE casefolding is made Unicode-aware.
|
---|
692 | if (!caseSensitive) {
|
---|
693 | searchFlags |= (Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
|
---|
694 | }
|
---|
695 |
|
---|
696 | return searchFlags;
|
---|
697 | }
|
---|
698 | }
|
---|