1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm.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.text.Normalizer;
|
---|
10 | import java.util.ArrayList;
|
---|
11 | import java.util.Arrays;
|
---|
12 | import java.util.Collection;
|
---|
13 | import java.util.Collections;
|
---|
14 | import java.util.HashMap;
|
---|
15 | import java.util.List;
|
---|
16 | import java.util.Locale;
|
---|
17 | import java.util.Map;
|
---|
18 | import java.util.Optional;
|
---|
19 | import java.util.function.Predicate;
|
---|
20 | import java.util.regex.Matcher;
|
---|
21 | import java.util.regex.Pattern;
|
---|
22 | import java.util.regex.PatternSyntaxException;
|
---|
23 | import java.util.stream.Collectors;
|
---|
24 |
|
---|
25 | import org.openstreetmap.josm.Main;
|
---|
26 | import org.openstreetmap.josm.actions.search.SearchAction;
|
---|
27 | import org.openstreetmap.josm.data.Bounds;
|
---|
28 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
29 | import org.openstreetmap.josm.data.osm.Node;
|
---|
30 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
31 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
32 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
33 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
34 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
35 | import org.openstreetmap.josm.data.osm.Tagged;
|
---|
36 | import org.openstreetmap.josm.data.osm.Way;
|
---|
37 | import org.openstreetmap.josm.data.osm.search.PushbackTokenizer.Range;
|
---|
38 | import org.openstreetmap.josm.data.osm.search.PushbackTokenizer.Token;
|
---|
39 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
40 | import org.openstreetmap.josm.gui.mappaint.Environment;
|
---|
41 | import org.openstreetmap.josm.gui.mappaint.mapcss.Selector;
|
---|
42 | import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.MapCSSParser;
|
---|
43 | import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.ParseException;
|
---|
44 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset;
|
---|
45 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetMenu;
|
---|
46 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetSeparator;
|
---|
47 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets;
|
---|
48 | import org.openstreetmap.josm.tools.AlphanumComparator;
|
---|
49 | import org.openstreetmap.josm.tools.Geometry;
|
---|
50 | import org.openstreetmap.josm.tools.Logging;
|
---|
51 | import org.openstreetmap.josm.tools.UncheckedParseException;
|
---|
52 | import org.openstreetmap.josm.tools.Utils;
|
---|
53 | import org.openstreetmap.josm.tools.date.DateUtils;
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Implements a google-like search.
|
---|
57 | * <br>
|
---|
58 | * Grammar:
|
---|
59 | * <pre>
|
---|
60 | * expression =
|
---|
61 | * fact | expression
|
---|
62 | * fact expression
|
---|
63 | * fact
|
---|
64 | *
|
---|
65 | * fact =
|
---|
66 | * ( expression )
|
---|
67 | * -fact
|
---|
68 | * term?
|
---|
69 | * term=term
|
---|
70 | * term:term
|
---|
71 | * term
|
---|
72 | * </pre>
|
---|
73 | *
|
---|
74 | * @author Imi
|
---|
75 | * @since 12656 (moved from actions.search package)
|
---|
76 | */
|
---|
77 | public class SearchCompiler {
|
---|
78 |
|
---|
79 | private final boolean caseSensitive;
|
---|
80 | private final boolean regexSearch;
|
---|
81 | private static String rxErrorMsg = marktr("The regex \"{0}\" had a parse error at offset {1}, full error:\n\n{2}");
|
---|
82 | private static String rxErrorMsgNoPos = marktr("The regex \"{0}\" had a parse error, full error:\n\n{1}");
|
---|
83 | private final PushbackTokenizer tokenizer;
|
---|
84 | private static Map<String, SimpleMatchFactory> simpleMatchFactoryMap = new HashMap<>();
|
---|
85 | private static Map<String, UnaryMatchFactory> unaryMatchFactoryMap = new HashMap<>();
|
---|
86 | private static Map<String, BinaryMatchFactory> binaryMatchFactoryMap = new HashMap<>();
|
---|
87 |
|
---|
88 | public SearchCompiler(boolean caseSensitive, boolean regexSearch, PushbackTokenizer tokenizer) {
|
---|
89 | this.caseSensitive = caseSensitive;
|
---|
90 | this.regexSearch = regexSearch;
|
---|
91 | this.tokenizer = tokenizer;
|
---|
92 |
|
---|
93 | // register core match factories at first instance, so plugins should never be able to generate a NPE
|
---|
94 | if (simpleMatchFactoryMap.isEmpty()) {
|
---|
95 | addMatchFactory(new CoreSimpleMatchFactory());
|
---|
96 | }
|
---|
97 | if (unaryMatchFactoryMap.isEmpty()) {
|
---|
98 | addMatchFactory(new CoreUnaryMatchFactory());
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Add (register) MatchFactory with SearchCompiler
|
---|
104 | * @param factory match factory
|
---|
105 | */
|
---|
106 | public static void addMatchFactory(MatchFactory factory) {
|
---|
107 | for (String keyword : factory.getKeywords()) {
|
---|
108 | final MatchFactory existing;
|
---|
109 | if (factory instanceof SimpleMatchFactory) {
|
---|
110 | existing = simpleMatchFactoryMap.put(keyword, (SimpleMatchFactory) factory);
|
---|
111 | } else if (factory instanceof UnaryMatchFactory) {
|
---|
112 | existing = unaryMatchFactoryMap.put(keyword, (UnaryMatchFactory) factory);
|
---|
113 | } else if (factory instanceof BinaryMatchFactory) {
|
---|
114 | existing = binaryMatchFactoryMap.put(keyword, (BinaryMatchFactory) factory);
|
---|
115 | } else
|
---|
116 | throw new AssertionError("Unknown match factory");
|
---|
117 | if (existing != null) {
|
---|
118 | Logging.warn("SearchCompiler: for key ''{0}'', overriding match factory ''{1}'' with ''{2}''", keyword, existing, factory);
|
---|
119 | }
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | public class CoreSimpleMatchFactory implements SimpleMatchFactory {
|
---|
124 | private final Collection<String> keywords = Arrays.asList("id", "version", "type", "user", "role",
|
---|
125 | "changeset", "nodes", "ways", "tags", "areasize", "waylength", "modified", "deleted", "selected",
|
---|
126 | "incomplete", "untagged", "closed", "new", "indownloadedarea",
|
---|
127 | "allindownloadedarea", "inview", "allinview", "timestamp", "nth", "nth%", "hasRole", "preset");
|
---|
128 |
|
---|
129 | @Override
|
---|
130 | public Match get(String keyword, PushbackTokenizer tokenizer) throws SearchParseError {
|
---|
131 | switch(keyword) {
|
---|
132 | case "modified":
|
---|
133 | return new Modified();
|
---|
134 | case "deleted":
|
---|
135 | return new Deleted();
|
---|
136 | case "selected":
|
---|
137 | return new Selected();
|
---|
138 | case "incomplete":
|
---|
139 | return new Incomplete();
|
---|
140 | case "untagged":
|
---|
141 | return new Untagged();
|
---|
142 | case "closed":
|
---|
143 | return new Closed();
|
---|
144 | case "new":
|
---|
145 | return new New();
|
---|
146 | case "indownloadedarea":
|
---|
147 | return new InDataSourceArea(false);
|
---|
148 | case "allindownloadedarea":
|
---|
149 | return new InDataSourceArea(true);
|
---|
150 | case "inview":
|
---|
151 | return new InView(false);
|
---|
152 | case "allinview":
|
---|
153 | return new InView(true);
|
---|
154 | default:
|
---|
155 | if (tokenizer != null) {
|
---|
156 | switch (keyword) {
|
---|
157 | case "id":
|
---|
158 | return new Id(tokenizer);
|
---|
159 | case "version":
|
---|
160 | return new Version(tokenizer);
|
---|
161 | case "type":
|
---|
162 | return new ExactType(tokenizer.readTextOrNumber());
|
---|
163 | case "preset":
|
---|
164 | return new Preset(tokenizer.readTextOrNumber());
|
---|
165 | case "user":
|
---|
166 | return new UserMatch(tokenizer.readTextOrNumber());
|
---|
167 | case "role":
|
---|
168 | return new RoleMatch(tokenizer.readTextOrNumber());
|
---|
169 | case "changeset":
|
---|
170 | return new ChangesetId(tokenizer);
|
---|
171 | case "nodes":
|
---|
172 | return new NodeCountRange(tokenizer);
|
---|
173 | case "ways":
|
---|
174 | return new WayCountRange(tokenizer);
|
---|
175 | case "tags":
|
---|
176 | return new TagCountRange(tokenizer);
|
---|
177 | case "areasize":
|
---|
178 | return new AreaSize(tokenizer);
|
---|
179 | case "waylength":
|
---|
180 | return new WayLength(tokenizer);
|
---|
181 | case "nth":
|
---|
182 | return new Nth(tokenizer, false);
|
---|
183 | case "nth%":
|
---|
184 | return new Nth(tokenizer, true);
|
---|
185 | case "hasRole":
|
---|
186 | return new HasRole(tokenizer);
|
---|
187 | case "timestamp":
|
---|
188 | // add leading/trailing space in order to get expected split (e.g. "a--" => {"a", ""})
|
---|
189 | String rangeS = ' ' + tokenizer.readTextOrNumber() + ' ';
|
---|
190 | String[] rangeA = rangeS.split("/");
|
---|
191 | if (rangeA.length == 1) {
|
---|
192 | return new KeyValue(keyword, rangeS.trim(), regexSearch, caseSensitive);
|
---|
193 | } else if (rangeA.length == 2) {
|
---|
194 | String rangeA1 = rangeA[0].trim();
|
---|
195 | String rangeA2 = rangeA[1].trim();
|
---|
196 | final long minDate;
|
---|
197 | final long maxDate;
|
---|
198 | try {
|
---|
199 | // if min timestap is empty: use lowest possible date
|
---|
200 | minDate = DateUtils.fromString(rangeA1.isEmpty() ? "1980" : rangeA1).getTime();
|
---|
201 | } catch (UncheckedParseException ex) {
|
---|
202 | throw new SearchParseError(tr("Cannot parse timestamp ''{0}''", rangeA1), ex);
|
---|
203 | }
|
---|
204 | try {
|
---|
205 | // if max timestamp is empty: use "now"
|
---|
206 | maxDate = rangeA2.isEmpty() ? System.currentTimeMillis() : DateUtils.fromString(rangeA2).getTime();
|
---|
207 | } catch (UncheckedParseException ex) {
|
---|
208 | throw new SearchParseError(tr("Cannot parse timestamp ''{0}''", rangeA2), ex);
|
---|
209 | }
|
---|
210 | return new TimestampRange(minDate, maxDate);
|
---|
211 | } else {
|
---|
212 | throw new SearchParseError("<html>" + tr("Expecting {0} after {1}", "<i>min</i>/<i>max</i>", "<i>timestamp</i>"));
|
---|
213 | }
|
---|
214 | }
|
---|
215 | } else {
|
---|
216 | throw new SearchParseError("<html>" + tr("Expecting {0} after {1}", "<code>:</code>", "<i>" + keyword + "</i>"));
|
---|
217 | }
|
---|
218 | }
|
---|
219 | throw new IllegalStateException("Not expecting keyword " + keyword);
|
---|
220 | }
|
---|
221 |
|
---|
222 | @Override
|
---|
223 | public Collection<String> getKeywords() {
|
---|
224 | return keywords;
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 | public static class CoreUnaryMatchFactory implements UnaryMatchFactory {
|
---|
229 | private static Collection<String> keywords = Arrays.asList("parent", "child");
|
---|
230 |
|
---|
231 | @Override
|
---|
232 | public UnaryMatch get(String keyword, Match matchOperand, PushbackTokenizer tokenizer) {
|
---|
233 | if ("parent".equals(keyword))
|
---|
234 | return new Parent(matchOperand);
|
---|
235 | else if ("child".equals(keyword))
|
---|
236 | return new Child(matchOperand);
|
---|
237 | return null;
|
---|
238 | }
|
---|
239 |
|
---|
240 | @Override
|
---|
241 | public Collection<String> getKeywords() {
|
---|
242 | return keywords;
|
---|
243 | }
|
---|
244 | }
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Classes implementing this interface can provide Match operators.
|
---|
248 | * @since 10600 (functional interface)
|
---|
249 | */
|
---|
250 | @FunctionalInterface
|
---|
251 | private interface MatchFactory {
|
---|
252 | Collection<String> getKeywords();
|
---|
253 | }
|
---|
254 |
|
---|
255 | public interface SimpleMatchFactory extends MatchFactory {
|
---|
256 | Match get(String keyword, PushbackTokenizer tokenizer) throws SearchParseError;
|
---|
257 | }
|
---|
258 |
|
---|
259 | public interface UnaryMatchFactory extends MatchFactory {
|
---|
260 | UnaryMatch get(String keyword, Match matchOperand, PushbackTokenizer tokenizer) throws SearchParseError;
|
---|
261 | }
|
---|
262 |
|
---|
263 | public interface BinaryMatchFactory extends MatchFactory {
|
---|
264 | AbstractBinaryMatch get(String keyword, Match lhs, Match rhs, PushbackTokenizer tokenizer) throws SearchParseError;
|
---|
265 | }
|
---|
266 |
|
---|
267 | /**
|
---|
268 | * Base class for all search criteria. If the criterion only depends on an object's tags,
|
---|
269 | * inherit from {@link org.openstreetmap.josm.data.osm.search.SearchCompiler.TaggedMatch}.
|
---|
270 | */
|
---|
271 | public abstract static class Match implements Predicate<OsmPrimitive> {
|
---|
272 |
|
---|
273 | /**
|
---|
274 | * Tests whether the primitive matches this criterion.
|
---|
275 | * @param osm the primitive to test
|
---|
276 | * @return true if the primitive matches this criterion
|
---|
277 | */
|
---|
278 | public abstract boolean match(OsmPrimitive osm);
|
---|
279 |
|
---|
280 | /**
|
---|
281 | * Tests whether the tagged object matches this criterion.
|
---|
282 | * @param tagged the tagged object to test
|
---|
283 | * @return true if the tagged object matches this criterion
|
---|
284 | */
|
---|
285 | public boolean match(Tagged tagged) {
|
---|
286 | return false;
|
---|
287 | }
|
---|
288 |
|
---|
289 | @Override
|
---|
290 | public final boolean test(OsmPrimitive object) {
|
---|
291 | return match(object);
|
---|
292 | }
|
---|
293 | }
|
---|
294 |
|
---|
295 | public abstract static class TaggedMatch extends Match {
|
---|
296 |
|
---|
297 | @Override
|
---|
298 | public abstract boolean match(Tagged tags);
|
---|
299 |
|
---|
300 | @Override
|
---|
301 | public final boolean match(OsmPrimitive osm) {
|
---|
302 | return match((Tagged) osm);
|
---|
303 | }
|
---|
304 | }
|
---|
305 |
|
---|
306 | /**
|
---|
307 | * A unary search operator which may take data parameters.
|
---|
308 | */
|
---|
309 | public abstract static class UnaryMatch extends Match {
|
---|
310 |
|
---|
311 | protected final Match match;
|
---|
312 |
|
---|
313 | public UnaryMatch(Match match) {
|
---|
314 | if (match == null) {
|
---|
315 | // "operator" (null) should mean the same as "operator()"
|
---|
316 | // (Always). I.e. match everything
|
---|
317 | this.match = Always.INSTANCE;
|
---|
318 | } else {
|
---|
319 | this.match = match;
|
---|
320 | }
|
---|
321 | }
|
---|
322 |
|
---|
323 | public Match getOperand() {
|
---|
324 | return match;
|
---|
325 | }
|
---|
326 | }
|
---|
327 |
|
---|
328 | /**
|
---|
329 | * A binary search operator which may take data parameters.
|
---|
330 | */
|
---|
331 | public abstract static class AbstractBinaryMatch extends Match {
|
---|
332 |
|
---|
333 | protected final Match lhs;
|
---|
334 | protected final Match rhs;
|
---|
335 |
|
---|
336 | /**
|
---|
337 | * Constructs a new {@code BinaryMatch}.
|
---|
338 | * @param lhs Left hand side
|
---|
339 | * @param rhs Right hand side
|
---|
340 | */
|
---|
341 | public AbstractBinaryMatch(Match lhs, Match rhs) {
|
---|
342 | this.lhs = lhs;
|
---|
343 | this.rhs = rhs;
|
---|
344 | }
|
---|
345 |
|
---|
346 | /**
|
---|
347 | * Returns left hand side.
|
---|
348 | * @return left hand side
|
---|
349 | */
|
---|
350 | public final Match getLhs() {
|
---|
351 | return lhs;
|
---|
352 | }
|
---|
353 |
|
---|
354 | /**
|
---|
355 | * Returns right hand side.
|
---|
356 | * @return right hand side
|
---|
357 | */
|
---|
358 | public final Match getRhs() {
|
---|
359 | return rhs;
|
---|
360 | }
|
---|
361 |
|
---|
362 | protected static String parenthesis(Match m) {
|
---|
363 | return '(' + m.toString() + ')';
|
---|
364 | }
|
---|
365 | }
|
---|
366 |
|
---|
367 | /**
|
---|
368 | * Matches every OsmPrimitive.
|
---|
369 | */
|
---|
370 | public static class Always extends TaggedMatch {
|
---|
371 | /** The unique instance/ */
|
---|
372 | public static final Always INSTANCE = new Always();
|
---|
373 | @Override
|
---|
374 | public boolean match(Tagged osm) {
|
---|
375 | return true;
|
---|
376 | }
|
---|
377 | }
|
---|
378 |
|
---|
379 | /**
|
---|
380 | * Never matches any OsmPrimitive.
|
---|
381 | */
|
---|
382 | public static class Never extends TaggedMatch {
|
---|
383 | /** The unique instance/ */
|
---|
384 | public static final Never INSTANCE = new Never();
|
---|
385 | @Override
|
---|
386 | public boolean match(Tagged osm) {
|
---|
387 | return false;
|
---|
388 | }
|
---|
389 | }
|
---|
390 |
|
---|
391 | /**
|
---|
392 | * Inverts the match.
|
---|
393 | */
|
---|
394 | public static class Not extends UnaryMatch {
|
---|
395 | public Not(Match match) {
|
---|
396 | super(match);
|
---|
397 | }
|
---|
398 |
|
---|
399 | @Override
|
---|
400 | public boolean match(OsmPrimitive osm) {
|
---|
401 | return !match.match(osm);
|
---|
402 | }
|
---|
403 |
|
---|
404 | @Override
|
---|
405 | public boolean match(Tagged osm) {
|
---|
406 | return !match.match(osm);
|
---|
407 | }
|
---|
408 |
|
---|
409 | @Override
|
---|
410 | public String toString() {
|
---|
411 | return '!' + match.toString();
|
---|
412 | }
|
---|
413 |
|
---|
414 | public Match getMatch() {
|
---|
415 | return match;
|
---|
416 | }
|
---|
417 | }
|
---|
418 |
|
---|
419 | /**
|
---|
420 | * Matches if the value of the corresponding key is ''yes'', ''true'', ''1'' or ''on''.
|
---|
421 | */
|
---|
422 | private static class BooleanMatch extends TaggedMatch {
|
---|
423 | private final String key;
|
---|
424 | private final boolean defaultValue;
|
---|
425 |
|
---|
426 | BooleanMatch(String key, boolean defaultValue) {
|
---|
427 | this.key = key;
|
---|
428 | this.defaultValue = defaultValue;
|
---|
429 | }
|
---|
430 |
|
---|
431 | @Override
|
---|
432 | public boolean match(Tagged osm) {
|
---|
433 | return Optional.ofNullable(OsmUtils.getOsmBoolean(osm.get(key))).orElse(defaultValue);
|
---|
434 | }
|
---|
435 |
|
---|
436 | @Override
|
---|
437 | public String toString() {
|
---|
438 | return key + '?';
|
---|
439 | }
|
---|
440 | }
|
---|
441 |
|
---|
442 | /**
|
---|
443 | * Matches if both left and right expressions match.
|
---|
444 | */
|
---|
445 | public static class And extends AbstractBinaryMatch {
|
---|
446 | /**
|
---|
447 | * Constructs a new {@code And} match.
|
---|
448 | * @param lhs left hand side
|
---|
449 | * @param rhs right hand side
|
---|
450 | */
|
---|
451 | public And(Match lhs, Match rhs) {
|
---|
452 | super(lhs, rhs);
|
---|
453 | }
|
---|
454 |
|
---|
455 | @Override
|
---|
456 | public boolean match(OsmPrimitive osm) {
|
---|
457 | return lhs.match(osm) && rhs.match(osm);
|
---|
458 | }
|
---|
459 |
|
---|
460 | @Override
|
---|
461 | public boolean match(Tagged osm) {
|
---|
462 | return lhs.match(osm) && rhs.match(osm);
|
---|
463 | }
|
---|
464 |
|
---|
465 | @Override
|
---|
466 | public String toString() {
|
---|
467 | return (lhs instanceof AbstractBinaryMatch && !(lhs instanceof And) ? parenthesis(lhs) : lhs) + " && "
|
---|
468 | + (rhs instanceof AbstractBinaryMatch && !(rhs instanceof And) ? parenthesis(rhs) : rhs);
|
---|
469 | }
|
---|
470 | }
|
---|
471 |
|
---|
472 | /**
|
---|
473 | * Matches if the left OR the right expression match.
|
---|
474 | */
|
---|
475 | public static class Or extends AbstractBinaryMatch {
|
---|
476 | /**
|
---|
477 | * Constructs a new {@code Or} match.
|
---|
478 | * @param lhs left hand side
|
---|
479 | * @param rhs right hand side
|
---|
480 | */
|
---|
481 | public Or(Match lhs, Match rhs) {
|
---|
482 | super(lhs, rhs);
|
---|
483 | }
|
---|
484 |
|
---|
485 | @Override
|
---|
486 | public boolean match(OsmPrimitive osm) {
|
---|
487 | return lhs.match(osm) || rhs.match(osm);
|
---|
488 | }
|
---|
489 |
|
---|
490 | @Override
|
---|
491 | public boolean match(Tagged osm) {
|
---|
492 | return lhs.match(osm) || rhs.match(osm);
|
---|
493 | }
|
---|
494 |
|
---|
495 | @Override
|
---|
496 | public String toString() {
|
---|
497 | return (lhs instanceof AbstractBinaryMatch && !(lhs instanceof Or) ? parenthesis(lhs) : lhs) + " || "
|
---|
498 | + (rhs instanceof AbstractBinaryMatch && !(rhs instanceof Or) ? parenthesis(rhs) : rhs);
|
---|
499 | }
|
---|
500 | }
|
---|
501 |
|
---|
502 | /**
|
---|
503 | * Matches if the left OR the right expression match, but not both.
|
---|
504 | */
|
---|
505 | public static class Xor extends AbstractBinaryMatch {
|
---|
506 | /**
|
---|
507 | * Constructs a new {@code Xor} match.
|
---|
508 | * @param lhs left hand side
|
---|
509 | * @param rhs right hand side
|
---|
510 | */
|
---|
511 | public Xor(Match lhs, Match rhs) {
|
---|
512 | super(lhs, rhs);
|
---|
513 | }
|
---|
514 |
|
---|
515 | @Override
|
---|
516 | public boolean match(OsmPrimitive osm) {
|
---|
517 | return lhs.match(osm) ^ rhs.match(osm);
|
---|
518 | }
|
---|
519 |
|
---|
520 | @Override
|
---|
521 | public boolean match(Tagged osm) {
|
---|
522 | return lhs.match(osm) ^ rhs.match(osm);
|
---|
523 | }
|
---|
524 |
|
---|
525 | @Override
|
---|
526 | public String toString() {
|
---|
527 | return (lhs instanceof AbstractBinaryMatch && !(lhs instanceof Xor) ? parenthesis(lhs) : lhs) + " ^ "
|
---|
528 | + (rhs instanceof AbstractBinaryMatch && !(rhs instanceof Xor) ? parenthesis(rhs) : rhs);
|
---|
529 | }
|
---|
530 | }
|
---|
531 |
|
---|
532 | /**
|
---|
533 | * Matches objects with ID in the given range.
|
---|
534 | */
|
---|
535 | private static class Id extends RangeMatch {
|
---|
536 | Id(Range range) {
|
---|
537 | super(range);
|
---|
538 | }
|
---|
539 |
|
---|
540 | Id(PushbackTokenizer tokenizer) throws SearchParseError {
|
---|
541 | this(tokenizer.readRange(tr("Range of primitive ids expected")));
|
---|
542 | }
|
---|
543 |
|
---|
544 | @Override
|
---|
545 | protected Long getNumber(OsmPrimitive osm) {
|
---|
546 | return osm.isNew() ? 0 : osm.getUniqueId();
|
---|
547 | }
|
---|
548 |
|
---|
549 | @Override
|
---|
550 | protected String getString() {
|
---|
551 | return "id";
|
---|
552 | }
|
---|
553 | }
|
---|
554 |
|
---|
555 | /**
|
---|
556 | * Matches objects with a changeset ID in the given range.
|
---|
557 | */
|
---|
558 | private static class ChangesetId extends RangeMatch {
|
---|
559 | ChangesetId(Range range) {
|
---|
560 | super(range);
|
---|
561 | }
|
---|
562 |
|
---|
563 | ChangesetId(PushbackTokenizer tokenizer) throws SearchParseError {
|
---|
564 | this(tokenizer.readRange(tr("Range of changeset ids expected")));
|
---|
565 | }
|
---|
566 |
|
---|
567 | @Override
|
---|
568 | protected Long getNumber(OsmPrimitive osm) {
|
---|
569 | return (long) osm.getChangesetId();
|
---|
570 | }
|
---|
571 |
|
---|
572 | @Override
|
---|
573 | protected String getString() {
|
---|
574 | return "changeset";
|
---|
575 | }
|
---|
576 | }
|
---|
577 |
|
---|
578 | /**
|
---|
579 | * Matches objects with a version number in the given range.
|
---|
580 | */
|
---|
581 | private static class Version extends RangeMatch {
|
---|
582 | Version(Range range) {
|
---|
583 | super(range);
|
---|
584 | }
|
---|
585 |
|
---|
586 | Version(PushbackTokenizer tokenizer) throws SearchParseError {
|
---|
587 | this(tokenizer.readRange(tr("Range of versions expected")));
|
---|
588 | }
|
---|
589 |
|
---|
590 | @Override
|
---|
591 | protected Long getNumber(OsmPrimitive osm) {
|
---|
592 | return (long) osm.getVersion();
|
---|
593 | }
|
---|
594 |
|
---|
595 | @Override
|
---|
596 | protected String getString() {
|
---|
597 | return "version";
|
---|
598 | }
|
---|
599 | }
|
---|
600 |
|
---|
601 | /**
|
---|
602 | * Matches objects with the given key-value pair.
|
---|
603 | */
|
---|
604 | private static class KeyValue extends TaggedMatch {
|
---|
605 | private final String key;
|
---|
606 | private final Pattern keyPattern;
|
---|
607 | private final String value;
|
---|
608 | private final Pattern valuePattern;
|
---|
609 | private final boolean caseSensitive;
|
---|
610 |
|
---|
611 | KeyValue(String key, String value, boolean regexSearch, boolean caseSensitive) throws SearchParseError {
|
---|
612 | this.caseSensitive = caseSensitive;
|
---|
613 | if (regexSearch) {
|
---|
614 | int searchFlags = regexFlags(caseSensitive);
|
---|
615 |
|
---|
616 | try {
|
---|
617 | this.keyPattern = Pattern.compile(key, searchFlags);
|
---|
618 | } catch (PatternSyntaxException e) {
|
---|
619 | throw new SearchParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()), e);
|
---|
620 | } catch (IllegalArgumentException e) {
|
---|
621 | throw new SearchParseError(tr(rxErrorMsgNoPos, key, e.getMessage()), e);
|
---|
622 | }
|
---|
623 | try {
|
---|
624 | this.valuePattern = Pattern.compile(value, searchFlags);
|
---|
625 | } catch (PatternSyntaxException e) {
|
---|
626 | throw new SearchParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()), e);
|
---|
627 | } catch (IllegalArgumentException | StringIndexOutOfBoundsException e) {
|
---|
628 | throw new SearchParseError(tr(rxErrorMsgNoPos, value, e.getMessage()), e);
|
---|
629 | }
|
---|
630 | this.key = key;
|
---|
631 | this.value = value;
|
---|
632 |
|
---|
633 | } else {
|
---|
634 | this.key = key;
|
---|
635 | this.value = value;
|
---|
636 | this.keyPattern = null;
|
---|
637 | this.valuePattern = null;
|
---|
638 | }
|
---|
639 | }
|
---|
640 |
|
---|
641 | @Override
|
---|
642 | public boolean match(Tagged osm) {
|
---|
643 |
|
---|
644 | if (keyPattern != null) {
|
---|
645 | if (!osm.hasKeys())
|
---|
646 | return false;
|
---|
647 |
|
---|
648 | /* The string search will just get a key like
|
---|
649 | * 'highway' and look that up as osm.get(key). But
|
---|
650 | * since we're doing a regex match we'll have to loop
|
---|
651 | * over all the keys to see if they match our regex,
|
---|
652 | * and only then try to match against the value
|
---|
653 | */
|
---|
654 |
|
---|
655 | for (String k: osm.keySet()) {
|
---|
656 | String v = osm.get(k);
|
---|
657 |
|
---|
658 | Matcher matcherKey = keyPattern.matcher(k);
|
---|
659 | boolean matchedKey = matcherKey.find();
|
---|
660 |
|
---|
661 | if (matchedKey) {
|
---|
662 | Matcher matcherValue = valuePattern.matcher(v);
|
---|
663 | boolean matchedValue = matcherValue.find();
|
---|
664 |
|
---|
665 | if (matchedValue)
|
---|
666 | return true;
|
---|
667 | }
|
---|
668 | }
|
---|
669 | } else {
|
---|
670 | String mv;
|
---|
671 |
|
---|
672 | if ("timestamp".equals(key) && osm instanceof OsmPrimitive) {
|
---|
673 | mv = DateUtils.fromTimestamp(((OsmPrimitive) osm).getRawTimestamp());
|
---|
674 | } else {
|
---|
675 | mv = osm.get(key);
|
---|
676 | if (!caseSensitive && mv == null) {
|
---|
677 | for (String k: osm.keySet()) {
|
---|
678 | if (key.equalsIgnoreCase(k)) {
|
---|
679 | mv = osm.get(k);
|
---|
680 | break;
|
---|
681 | }
|
---|
682 | }
|
---|
683 | }
|
---|
684 | }
|
---|
685 |
|
---|
686 | if (mv == null)
|
---|
687 | return false;
|
---|
688 |
|
---|
689 | String v1 = caseSensitive ? mv : mv.toLowerCase(Locale.ENGLISH);
|
---|
690 | String v2 = caseSensitive ? value : value.toLowerCase(Locale.ENGLISH);
|
---|
691 |
|
---|
692 | v1 = Normalizer.normalize(v1, Normalizer.Form.NFC);
|
---|
693 | v2 = Normalizer.normalize(v2, Normalizer.Form.NFC);
|
---|
694 | return v1.indexOf(v2) != -1;
|
---|
695 | }
|
---|
696 |
|
---|
697 | return false;
|
---|
698 | }
|
---|
699 |
|
---|
700 | @Override
|
---|
701 | public String toString() {
|
---|
702 | return key + '=' + value;
|
---|
703 | }
|
---|
704 | }
|
---|
705 |
|
---|
706 | public static class ValueComparison extends TaggedMatch {
|
---|
707 | private final String key;
|
---|
708 | private final String referenceValue;
|
---|
709 | private final Double referenceNumber;
|
---|
710 | private final int compareMode;
|
---|
711 | private static final Pattern ISO8601 = Pattern.compile("\\d+-\\d+-\\d+");
|
---|
712 |
|
---|
713 | public ValueComparison(String key, String referenceValue, int compareMode) {
|
---|
714 | this.key = key;
|
---|
715 | this.referenceValue = referenceValue;
|
---|
716 | Double v = null;
|
---|
717 | try {
|
---|
718 | if (referenceValue != null) {
|
---|
719 | v = Double.valueOf(referenceValue);
|
---|
720 | }
|
---|
721 | } catch (NumberFormatException ignore) {
|
---|
722 | Logging.trace(ignore);
|
---|
723 | }
|
---|
724 | this.referenceNumber = v;
|
---|
725 | this.compareMode = compareMode;
|
---|
726 | }
|
---|
727 |
|
---|
728 | @Override
|
---|
729 | public boolean match(Tagged osm) {
|
---|
730 | final String currentValue = osm.get(key);
|
---|
731 | final int compareResult;
|
---|
732 | if (currentValue == null) {
|
---|
733 | return false;
|
---|
734 | } else if (ISO8601.matcher(currentValue).matches() || ISO8601.matcher(referenceValue).matches()) {
|
---|
735 | compareResult = currentValue.compareTo(referenceValue);
|
---|
736 | } else if (referenceNumber != null) {
|
---|
737 | try {
|
---|
738 | compareResult = Double.compare(Double.parseDouble(currentValue), referenceNumber);
|
---|
739 | } catch (NumberFormatException ignore) {
|
---|
740 | return false;
|
---|
741 | }
|
---|
742 | } else {
|
---|
743 | compareResult = AlphanumComparator.getInstance().compare(currentValue, referenceValue);
|
---|
744 | }
|
---|
745 | return compareMode < 0 ? compareResult < 0 : compareMode > 0 ? compareResult > 0 : compareResult == 0;
|
---|
746 | }
|
---|
747 |
|
---|
748 | @Override
|
---|
749 | public String toString() {
|
---|
750 | return key + (compareMode == -1 ? "<" : compareMode == +1 ? ">" : "") + referenceValue;
|
---|
751 | }
|
---|
752 | }
|
---|
753 |
|
---|
754 | /**
|
---|
755 | * Matches objects with the exact given key-value pair.
|
---|
756 | */
|
---|
757 | public static class ExactKeyValue extends TaggedMatch {
|
---|
758 |
|
---|
759 | enum Mode {
|
---|
760 | ANY, ANY_KEY, ANY_VALUE, EXACT, NONE, MISSING_KEY,
|
---|
761 | ANY_KEY_REGEXP, ANY_VALUE_REGEXP, EXACT_REGEXP, MISSING_KEY_REGEXP;
|
---|
762 | }
|
---|
763 |
|
---|
764 | private final String key;
|
---|
765 | private final String value;
|
---|
766 | private final Pattern keyPattern;
|
---|
767 | private final Pattern valuePattern;
|
---|
768 | private final Mode mode;
|
---|
769 |
|
---|
770 | /**
|
---|
771 | * Constructs a new {@code ExactKeyValue}.
|
---|
772 | * @param regexp regular expression
|
---|
773 | * @param key key
|
---|
774 | * @param value value
|
---|
775 | * @throws SearchParseError if a parse error occurs
|
---|
776 | */
|
---|
777 | public ExactKeyValue(boolean regexp, String key, String value) throws SearchParseError {
|
---|
778 | if ("".equals(key))
|
---|
779 | throw new SearchParseError(tr("Key cannot be empty when tag operator is used. Sample use: key=value"));
|
---|
780 | this.key = key;
|
---|
781 | this.value = value == null ? "" : value;
|
---|
782 | if ("".equals(this.value) && "*".equals(key)) {
|
---|
783 | mode = Mode.NONE;
|
---|
784 | } else if ("".equals(this.value)) {
|
---|
785 | if (regexp) {
|
---|
786 | mode = Mode.MISSING_KEY_REGEXP;
|
---|
787 | } else {
|
---|
788 | mode = Mode.MISSING_KEY;
|
---|
789 | }
|
---|
790 | } else if ("*".equals(key) && "*".equals(this.value)) {
|
---|
791 | mode = Mode.ANY;
|
---|
792 | } else if ("*".equals(key)) {
|
---|
793 | if (regexp) {
|
---|
794 | mode = Mode.ANY_KEY_REGEXP;
|
---|
795 | } else {
|
---|
796 | mode = Mode.ANY_KEY;
|
---|
797 | }
|
---|
798 | } else if ("*".equals(this.value)) {
|
---|
799 | if (regexp) {
|
---|
800 | mode = Mode.ANY_VALUE_REGEXP;
|
---|
801 | } else {
|
---|
802 | mode = Mode.ANY_VALUE;
|
---|
803 | }
|
---|
804 | } else {
|
---|
805 | if (regexp) {
|
---|
806 | mode = Mode.EXACT_REGEXP;
|
---|
807 | } else {
|
---|
808 | mode = Mode.EXACT;
|
---|
809 | }
|
---|
810 | }
|
---|
811 |
|
---|
812 | if (regexp && !key.isEmpty() && !"*".equals(key)) {
|
---|
813 | try {
|
---|
814 | keyPattern = Pattern.compile(key, regexFlags(false));
|
---|
815 | } catch (PatternSyntaxException e) {
|
---|
816 | throw new SearchParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()), e);
|
---|
817 | } catch (IllegalArgumentException e) {
|
---|
818 | throw new SearchParseError(tr(rxErrorMsgNoPos, key, e.getMessage()), e);
|
---|
819 | }
|
---|
820 | } else {
|
---|
821 | keyPattern = null;
|
---|
822 | }
|
---|
823 | if (regexp && !this.value.isEmpty() && !"*".equals(this.value)) {
|
---|
824 | try {
|
---|
825 | valuePattern = Pattern.compile(this.value, regexFlags(false));
|
---|
826 | } catch (PatternSyntaxException e) {
|
---|
827 | throw new SearchParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()), e);
|
---|
828 | } catch (IllegalArgumentException e) {
|
---|
829 | throw new SearchParseError(tr(rxErrorMsgNoPos, value, e.getMessage()), e);
|
---|
830 | }
|
---|
831 | } else {
|
---|
832 | valuePattern = null;
|
---|
833 | }
|
---|
834 | }
|
---|
835 |
|
---|
836 | @Override
|
---|
837 | public boolean match(Tagged osm) {
|
---|
838 |
|
---|
839 | if (!osm.hasKeys())
|
---|
840 | return mode == Mode.NONE;
|
---|
841 |
|
---|
842 | switch (mode) {
|
---|
843 | case NONE:
|
---|
844 | return false;
|
---|
845 | case MISSING_KEY:
|
---|
846 | return osm.get(key) == null;
|
---|
847 | case ANY:
|
---|
848 | return true;
|
---|
849 | case ANY_VALUE:
|
---|
850 | return osm.get(key) != null;
|
---|
851 | case ANY_KEY:
|
---|
852 | for (String v:osm.getKeys().values()) {
|
---|
853 | if (v.equals(value))
|
---|
854 | return true;
|
---|
855 | }
|
---|
856 | return false;
|
---|
857 | case EXACT:
|
---|
858 | return value.equals(osm.get(key));
|
---|
859 | case ANY_KEY_REGEXP:
|
---|
860 | for (String v:osm.getKeys().values()) {
|
---|
861 | if (valuePattern.matcher(v).matches())
|
---|
862 | return true;
|
---|
863 | }
|
---|
864 | return false;
|
---|
865 | case ANY_VALUE_REGEXP:
|
---|
866 | case EXACT_REGEXP:
|
---|
867 | for (String k : osm.keySet()) {
|
---|
868 | if (keyPattern.matcher(k).matches()
|
---|
869 | && (mode == Mode.ANY_VALUE_REGEXP || valuePattern.matcher(osm.get(k)).matches()))
|
---|
870 | return true;
|
---|
871 | }
|
---|
872 | return false;
|
---|
873 | case MISSING_KEY_REGEXP:
|
---|
874 | for (String k:osm.keySet()) {
|
---|
875 | if (keyPattern.matcher(k).matches())
|
---|
876 | return false;
|
---|
877 | }
|
---|
878 | return true;
|
---|
879 | }
|
---|
880 | throw new AssertionError("Missed state");
|
---|
881 | }
|
---|
882 |
|
---|
883 | @Override
|
---|
884 | public String toString() {
|
---|
885 | return key + '=' + value;
|
---|
886 | }
|
---|
887 | }
|
---|
888 |
|
---|
889 | /**
|
---|
890 | * Match a string in any tags (key or value), with optional regex and case insensitivity.
|
---|
891 | */
|
---|
892 | private static class Any extends TaggedMatch {
|
---|
893 | private final String search;
|
---|
894 | private final Pattern searchRegex;
|
---|
895 | private final boolean caseSensitive;
|
---|
896 |
|
---|
897 | Any(String s, boolean regexSearch, boolean caseSensitive) throws SearchParseError {
|
---|
898 | s = Normalizer.normalize(s, Normalizer.Form.NFC);
|
---|
899 | this.caseSensitive = caseSensitive;
|
---|
900 | if (regexSearch) {
|
---|
901 | try {
|
---|
902 | this.searchRegex = Pattern.compile(s, regexFlags(caseSensitive));
|
---|
903 | } catch (PatternSyntaxException e) {
|
---|
904 | throw new SearchParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()), e);
|
---|
905 | } catch (IllegalArgumentException | StringIndexOutOfBoundsException e) {
|
---|
906 | // StringIndexOutOfBoundsException catched because of https://bugs.openjdk.java.net/browse/JI-9044959
|
---|
907 | // See #13870: To remove after we switch to a version of Java which resolves this bug
|
---|
908 | throw new SearchParseError(tr(rxErrorMsgNoPos, s, e.getMessage()), e);
|
---|
909 | }
|
---|
910 | this.search = s;
|
---|
911 | } else if (caseSensitive) {
|
---|
912 | this.search = s;
|
---|
913 | this.searchRegex = null;
|
---|
914 | } else {
|
---|
915 | this.search = s.toLowerCase(Locale.ENGLISH);
|
---|
916 | this.searchRegex = null;
|
---|
917 | }
|
---|
918 | }
|
---|
919 |
|
---|
920 | @Override
|
---|
921 | public boolean match(Tagged osm) {
|
---|
922 | if (!osm.hasKeys())
|
---|
923 | return search.isEmpty();
|
---|
924 |
|
---|
925 | for (String key: osm.keySet()) {
|
---|
926 | String value = osm.get(key);
|
---|
927 | if (searchRegex != null) {
|
---|
928 |
|
---|
929 | value = Normalizer.normalize(value, Normalizer.Form.NFC);
|
---|
930 |
|
---|
931 | Matcher keyMatcher = searchRegex.matcher(key);
|
---|
932 | Matcher valMatcher = searchRegex.matcher(value);
|
---|
933 |
|
---|
934 | boolean keyMatchFound = keyMatcher.find();
|
---|
935 | boolean valMatchFound = valMatcher.find();
|
---|
936 |
|
---|
937 | if (keyMatchFound || valMatchFound)
|
---|
938 | return true;
|
---|
939 | } else {
|
---|
940 | if (!caseSensitive) {
|
---|
941 | key = key.toLowerCase(Locale.ENGLISH);
|
---|
942 | value = value.toLowerCase(Locale.ENGLISH);
|
---|
943 | }
|
---|
944 |
|
---|
945 | value = Normalizer.normalize(value, Normalizer.Form.NFC);
|
---|
946 |
|
---|
947 | if (key.indexOf(search) != -1 || value.indexOf(search) != -1)
|
---|
948 | return true;
|
---|
949 | }
|
---|
950 | }
|
---|
951 | return false;
|
---|
952 | }
|
---|
953 |
|
---|
954 | @Override
|
---|
955 | public String toString() {
|
---|
956 | return search;
|
---|
957 | }
|
---|
958 | }
|
---|
959 |
|
---|
960 | private static class ExactType extends Match {
|
---|
961 | private final OsmPrimitiveType type;
|
---|
962 |
|
---|
963 | ExactType(String type) throws SearchParseError {
|
---|
964 | this.type = OsmPrimitiveType.from(type);
|
---|
965 | if (this.type == null)
|
---|
966 | throw new SearchParseError(tr("Unknown primitive type: {0}. Allowed values are node, way or relation", type));
|
---|
967 | }
|
---|
968 |
|
---|
969 | @Override
|
---|
970 | public boolean match(OsmPrimitive osm) {
|
---|
971 | return type.equals(osm.getType());
|
---|
972 | }
|
---|
973 |
|
---|
974 | @Override
|
---|
975 | public String toString() {
|
---|
976 | return "type=" + type;
|
---|
977 | }
|
---|
978 | }
|
---|
979 |
|
---|
980 | /**
|
---|
981 | * Matches objects last changed by the given username.
|
---|
982 | */
|
---|
983 | private static class UserMatch extends Match {
|
---|
984 | private String user;
|
---|
985 |
|
---|
986 | UserMatch(String user) {
|
---|
987 | if ("anonymous".equals(user)) {
|
---|
988 | this.user = null;
|
---|
989 | } else {
|
---|
990 | this.user = user;
|
---|
991 | }
|
---|
992 | }
|
---|
993 |
|
---|
994 | @Override
|
---|
995 | public boolean match(OsmPrimitive osm) {
|
---|
996 | if (osm.getUser() == null)
|
---|
997 | return user == null;
|
---|
998 | else
|
---|
999 | return osm.getUser().hasName(user);
|
---|
1000 | }
|
---|
1001 |
|
---|
1002 | @Override
|
---|
1003 | public String toString() {
|
---|
1004 | return "user=" + (user == null ? "" : user);
|
---|
1005 | }
|
---|
1006 | }
|
---|
1007 |
|
---|
1008 | /**
|
---|
1009 | * Matches objects with the given relation role (i.e. "outer").
|
---|
1010 | */
|
---|
1011 | private static class RoleMatch extends Match {
|
---|
1012 | private String role;
|
---|
1013 |
|
---|
1014 | RoleMatch(String role) {
|
---|
1015 | if (role == null) {
|
---|
1016 | this.role = "";
|
---|
1017 | } else {
|
---|
1018 | this.role = role;
|
---|
1019 | }
|
---|
1020 | }
|
---|
1021 |
|
---|
1022 | @Override
|
---|
1023 | public boolean match(OsmPrimitive osm) {
|
---|
1024 | for (OsmPrimitive ref: osm.getReferrers()) {
|
---|
1025 | if (ref instanceof Relation && !ref.isIncomplete() && !ref.isDeleted()) {
|
---|
1026 | for (RelationMember m : ((Relation) ref).getMembers()) {
|
---|
1027 | if (m.getMember() == osm) {
|
---|
1028 | String testRole = m.getRole();
|
---|
1029 | if (role.equals(testRole == null ? "" : testRole))
|
---|
1030 | return true;
|
---|
1031 | }
|
---|
1032 | }
|
---|
1033 | }
|
---|
1034 | }
|
---|
1035 | return false;
|
---|
1036 | }
|
---|
1037 |
|
---|
1038 | @Override
|
---|
1039 | public String toString() {
|
---|
1040 | return "role=" + role;
|
---|
1041 | }
|
---|
1042 | }
|
---|
1043 |
|
---|
1044 | /**
|
---|
1045 | * Matches the n-th object of a relation and/or the n-th node of a way.
|
---|
1046 | */
|
---|
1047 | private static class Nth extends Match {
|
---|
1048 |
|
---|
1049 | private final int nth;
|
---|
1050 | private final boolean modulo;
|
---|
1051 |
|
---|
1052 | Nth(PushbackTokenizer tokenizer, boolean modulo) throws SearchParseError {
|
---|
1053 | this((int) tokenizer.readNumber(tr("Positive integer expected")), modulo);
|
---|
1054 | }
|
---|
1055 |
|
---|
1056 | private Nth(int nth, boolean modulo) {
|
---|
1057 | this.nth = nth;
|
---|
1058 | this.modulo = modulo;
|
---|
1059 | }
|
---|
1060 |
|
---|
1061 | @Override
|
---|
1062 | public boolean match(OsmPrimitive osm) {
|
---|
1063 | for (OsmPrimitive p : osm.getReferrers()) {
|
---|
1064 | final int idx;
|
---|
1065 | final int maxIndex;
|
---|
1066 | if (p instanceof Way) {
|
---|
1067 | Way w = (Way) p;
|
---|
1068 | idx = w.getNodes().indexOf(osm);
|
---|
1069 | maxIndex = w.getNodesCount();
|
---|
1070 | } else if (p instanceof Relation) {
|
---|
1071 | Relation r = (Relation) p;
|
---|
1072 | idx = r.getMemberPrimitivesList().indexOf(osm);
|
---|
1073 | maxIndex = r.getMembersCount();
|
---|
1074 | } else {
|
---|
1075 | continue;
|
---|
1076 | }
|
---|
1077 | if (nth < 0 && idx - maxIndex == nth) {
|
---|
1078 | return true;
|
---|
1079 | } else if (idx == nth || (modulo && idx % nth == 0))
|
---|
1080 | return true;
|
---|
1081 | }
|
---|
1082 | return false;
|
---|
1083 | }
|
---|
1084 |
|
---|
1085 | @Override
|
---|
1086 | public String toString() {
|
---|
1087 | return "Nth{nth=" + nth + ", modulo=" + modulo + '}';
|
---|
1088 | }
|
---|
1089 | }
|
---|
1090 |
|
---|
1091 | /**
|
---|
1092 | * Matches objects with properties in a certain range.
|
---|
1093 | */
|
---|
1094 | private abstract static class RangeMatch extends Match {
|
---|
1095 |
|
---|
1096 | private final long min;
|
---|
1097 | private final long max;
|
---|
1098 |
|
---|
1099 | RangeMatch(long min, long max) {
|
---|
1100 | this.min = Math.min(min, max);
|
---|
1101 | this.max = Math.max(min, max);
|
---|
1102 | }
|
---|
1103 |
|
---|
1104 | RangeMatch(Range range) {
|
---|
1105 | this(range.getStart(), range.getEnd());
|
---|
1106 | }
|
---|
1107 |
|
---|
1108 | protected abstract Long getNumber(OsmPrimitive osm);
|
---|
1109 |
|
---|
1110 | protected abstract String getString();
|
---|
1111 |
|
---|
1112 | @Override
|
---|
1113 | public boolean match(OsmPrimitive osm) {
|
---|
1114 | Long num = getNumber(osm);
|
---|
1115 | if (num == null)
|
---|
1116 | return false;
|
---|
1117 | else
|
---|
1118 | return (num >= min) && (num <= max);
|
---|
1119 | }
|
---|
1120 |
|
---|
1121 | @Override
|
---|
1122 | public String toString() {
|
---|
1123 | return getString() + '=' + min + '-' + max;
|
---|
1124 | }
|
---|
1125 | }
|
---|
1126 |
|
---|
1127 | /**
|
---|
1128 | * Matches ways with a number of nodes in given range
|
---|
1129 | */
|
---|
1130 | private static class NodeCountRange extends RangeMatch {
|
---|
1131 | NodeCountRange(Range range) {
|
---|
1132 | super(range);
|
---|
1133 | }
|
---|
1134 |
|
---|
1135 | NodeCountRange(PushbackTokenizer tokenizer) throws SearchParseError {
|
---|
1136 | this(tokenizer.readRange(tr("Range of numbers expected")));
|
---|
1137 | }
|
---|
1138 |
|
---|
1139 | @Override
|
---|
1140 | protected Long getNumber(OsmPrimitive osm) {
|
---|
1141 | if (osm instanceof Way) {
|
---|
1142 | return (long) ((Way) osm).getRealNodesCount();
|
---|
1143 | } else if (osm instanceof Relation) {
|
---|
1144 | return (long) ((Relation) osm).getMemberPrimitives(Node.class).size();
|
---|
1145 | } else {
|
---|
1146 | return null;
|
---|
1147 | }
|
---|
1148 | }
|
---|
1149 |
|
---|
1150 | @Override
|
---|
1151 | protected String getString() {
|
---|
1152 | return "nodes";
|
---|
1153 | }
|
---|
1154 | }
|
---|
1155 |
|
---|
1156 | /**
|
---|
1157 | * Matches objects with the number of referring/contained ways in the given range
|
---|
1158 | */
|
---|
1159 | private static class WayCountRange extends RangeMatch {
|
---|
1160 | WayCountRange(Range range) {
|
---|
1161 | super(range);
|
---|
1162 | }
|
---|
1163 |
|
---|
1164 | WayCountRange(PushbackTokenizer tokenizer) throws SearchParseError {
|
---|
1165 | this(tokenizer.readRange(tr("Range of numbers expected")));
|
---|
1166 | }
|
---|
1167 |
|
---|
1168 | @Override
|
---|
1169 | protected Long getNumber(OsmPrimitive osm) {
|
---|
1170 | if (osm instanceof Node) {
|
---|
1171 | return (long) Utils.filteredCollection(osm.getReferrers(), Way.class).size();
|
---|
1172 | } else if (osm instanceof Relation) {
|
---|
1173 | return (long) ((Relation) osm).getMemberPrimitives(Way.class).size();
|
---|
1174 | } else {
|
---|
1175 | return null;
|
---|
1176 | }
|
---|
1177 | }
|
---|
1178 |
|
---|
1179 | @Override
|
---|
1180 | protected String getString() {
|
---|
1181 | return "ways";
|
---|
1182 | }
|
---|
1183 | }
|
---|
1184 |
|
---|
1185 | /**
|
---|
1186 | * Matches objects with a number of tags in given range
|
---|
1187 | */
|
---|
1188 | private static class TagCountRange extends RangeMatch {
|
---|
1189 | TagCountRange(Range range) {
|
---|
1190 | super(range);
|
---|
1191 | }
|
---|
1192 |
|
---|
1193 | TagCountRange(PushbackTokenizer tokenizer) throws SearchParseError {
|
---|
1194 | this(tokenizer.readRange(tr("Range of numbers expected")));
|
---|
1195 | }
|
---|
1196 |
|
---|
1197 | @Override
|
---|
1198 | protected Long getNumber(OsmPrimitive osm) {
|
---|
1199 | return (long) osm.getKeys().size();
|
---|
1200 | }
|
---|
1201 |
|
---|
1202 | @Override
|
---|
1203 | protected String getString() {
|
---|
1204 | return "tags";
|
---|
1205 | }
|
---|
1206 | }
|
---|
1207 |
|
---|
1208 | /**
|
---|
1209 | * Matches objects with a timestamp in given range
|
---|
1210 | */
|
---|
1211 | private static class TimestampRange extends RangeMatch {
|
---|
1212 |
|
---|
1213 | TimestampRange(long minCount, long maxCount) {
|
---|
1214 | super(minCount, maxCount);
|
---|
1215 | }
|
---|
1216 |
|
---|
1217 | @Override
|
---|
1218 | protected Long getNumber(OsmPrimitive osm) {
|
---|
1219 | return osm.getTimestamp().getTime();
|
---|
1220 | }
|
---|
1221 |
|
---|
1222 | @Override
|
---|
1223 | protected String getString() {
|
---|
1224 | return "timestamp";
|
---|
1225 | }
|
---|
1226 | }
|
---|
1227 |
|
---|
1228 | /**
|
---|
1229 | * Matches relations with a member of the given role
|
---|
1230 | */
|
---|
1231 | private static class HasRole extends Match {
|
---|
1232 | private final String role;
|
---|
1233 |
|
---|
1234 | HasRole(PushbackTokenizer tokenizer) {
|
---|
1235 | role = tokenizer.readTextOrNumber();
|
---|
1236 | }
|
---|
1237 |
|
---|
1238 | @Override
|
---|
1239 | public boolean match(OsmPrimitive osm) {
|
---|
1240 | return osm instanceof Relation && ((Relation) osm).getMemberRoles().contains(role);
|
---|
1241 | }
|
---|
1242 | }
|
---|
1243 |
|
---|
1244 | /**
|
---|
1245 | * Matches objects that are new (i.e. have not been uploaded to the server)
|
---|
1246 | */
|
---|
1247 | private static class New extends Match {
|
---|
1248 | @Override
|
---|
1249 | public boolean match(OsmPrimitive osm) {
|
---|
1250 | return osm.isNew();
|
---|
1251 | }
|
---|
1252 |
|
---|
1253 | @Override
|
---|
1254 | public String toString() {
|
---|
1255 | return "new";
|
---|
1256 | }
|
---|
1257 | }
|
---|
1258 |
|
---|
1259 | /**
|
---|
1260 | * Matches all objects that have been modified, created, or undeleted
|
---|
1261 | */
|
---|
1262 | private static class Modified extends Match {
|
---|
1263 | @Override
|
---|
1264 | public boolean match(OsmPrimitive osm) {
|
---|
1265 | return osm.isModified() || osm.isNewOrUndeleted();
|
---|
1266 | }
|
---|
1267 |
|
---|
1268 | @Override
|
---|
1269 | public String toString() {
|
---|
1270 | return "modified";
|
---|
1271 | }
|
---|
1272 | }
|
---|
1273 |
|
---|
1274 | /**
|
---|
1275 | * Matches all objects that have been deleted
|
---|
1276 | */
|
---|
1277 | private static class Deleted extends Match {
|
---|
1278 | @Override
|
---|
1279 | public boolean match(OsmPrimitive osm) {
|
---|
1280 | return osm.isDeleted();
|
---|
1281 | }
|
---|
1282 |
|
---|
1283 | @Override
|
---|
1284 | public String toString() {
|
---|
1285 | return "deleted";
|
---|
1286 | }
|
---|
1287 | }
|
---|
1288 |
|
---|
1289 | /**
|
---|
1290 | * Matches all objects currently selected
|
---|
1291 | */
|
---|
1292 | private static class Selected extends Match {
|
---|
1293 | @Override
|
---|
1294 | public boolean match(OsmPrimitive osm) {
|
---|
1295 | return osm.getDataSet().isSelected(osm);
|
---|
1296 | }
|
---|
1297 |
|
---|
1298 | @Override
|
---|
1299 | public String toString() {
|
---|
1300 | return "selected";
|
---|
1301 | }
|
---|
1302 | }
|
---|
1303 |
|
---|
1304 | /**
|
---|
1305 | * Match objects that are incomplete, where only id and type are known.
|
---|
1306 | * Typically some members of a relation are incomplete until they are
|
---|
1307 | * fetched from the server.
|
---|
1308 | */
|
---|
1309 | private static class Incomplete extends Match {
|
---|
1310 | @Override
|
---|
1311 | public boolean match(OsmPrimitive osm) {
|
---|
1312 | return osm.isIncomplete() || (osm instanceof Relation && ((Relation) osm).hasIncompleteMembers());
|
---|
1313 | }
|
---|
1314 |
|
---|
1315 | @Override
|
---|
1316 | public String toString() {
|
---|
1317 | return "incomplete";
|
---|
1318 | }
|
---|
1319 | }
|
---|
1320 |
|
---|
1321 | /**
|
---|
1322 | * Matches objects that don't have any interesting tags (i.e. only has source,
|
---|
1323 | * FIXME, etc.). The complete list of uninteresting tags can be found here:
|
---|
1324 | * org.openstreetmap.josm.data.osm.OsmPrimitive.getUninterestingKeys()
|
---|
1325 | */
|
---|
1326 | private static class Untagged extends Match {
|
---|
1327 | @Override
|
---|
1328 | public boolean match(OsmPrimitive osm) {
|
---|
1329 | return !osm.isTagged() && !osm.isIncomplete();
|
---|
1330 | }
|
---|
1331 |
|
---|
1332 | @Override
|
---|
1333 | public String toString() {
|
---|
1334 | return "untagged";
|
---|
1335 | }
|
---|
1336 | }
|
---|
1337 |
|
---|
1338 | /**
|
---|
1339 | * Matches ways which are closed (i.e. first and last node are the same)
|
---|
1340 | */
|
---|
1341 | private static class Closed extends Match {
|
---|
1342 | @Override
|
---|
1343 | public boolean match(OsmPrimitive osm) {
|
---|
1344 | return osm instanceof Way && ((Way) osm).isClosed();
|
---|
1345 | }
|
---|
1346 |
|
---|
1347 | @Override
|
---|
1348 | public String toString() {
|
---|
1349 | return "closed";
|
---|
1350 | }
|
---|
1351 | }
|
---|
1352 |
|
---|
1353 | /**
|
---|
1354 | * Matches objects if they are parents of the expression
|
---|
1355 | */
|
---|
1356 | public static class Parent extends UnaryMatch {
|
---|
1357 | public Parent(Match m) {
|
---|
1358 | super(m);
|
---|
1359 | }
|
---|
1360 |
|
---|
1361 | @Override
|
---|
1362 | public boolean match(OsmPrimitive osm) {
|
---|
1363 | boolean isParent = false;
|
---|
1364 |
|
---|
1365 | if (osm instanceof Way) {
|
---|
1366 | for (Node n : ((Way) osm).getNodes()) {
|
---|
1367 | isParent |= match.match(n);
|
---|
1368 | }
|
---|
1369 | } else if (osm instanceof Relation) {
|
---|
1370 | for (RelationMember member : ((Relation) osm).getMembers()) {
|
---|
1371 | isParent |= match.match(member.getMember());
|
---|
1372 | }
|
---|
1373 | }
|
---|
1374 | return isParent;
|
---|
1375 | }
|
---|
1376 |
|
---|
1377 | @Override
|
---|
1378 | public String toString() {
|
---|
1379 | return "parent(" + match + ')';
|
---|
1380 | }
|
---|
1381 | }
|
---|
1382 |
|
---|
1383 | /**
|
---|
1384 | * Matches objects if they are children of the expression
|
---|
1385 | */
|
---|
1386 | public static class Child extends UnaryMatch {
|
---|
1387 |
|
---|
1388 | public Child(Match m) {
|
---|
1389 | super(m);
|
---|
1390 | }
|
---|
1391 |
|
---|
1392 | @Override
|
---|
1393 | public boolean match(OsmPrimitive osm) {
|
---|
1394 | boolean isChild = false;
|
---|
1395 | for (OsmPrimitive p : osm.getReferrers()) {
|
---|
1396 | isChild |= match.match(p);
|
---|
1397 | }
|
---|
1398 | return isChild;
|
---|
1399 | }
|
---|
1400 |
|
---|
1401 | @Override
|
---|
1402 | public String toString() {
|
---|
1403 | return "child(" + match + ')';
|
---|
1404 | }
|
---|
1405 | }
|
---|
1406 |
|
---|
1407 | /**
|
---|
1408 | * Matches if the size of the area is within the given range
|
---|
1409 | *
|
---|
1410 | * @author Ole Jørgen Brønner
|
---|
1411 | */
|
---|
1412 | private static class AreaSize extends RangeMatch {
|
---|
1413 |
|
---|
1414 | AreaSize(Range range) {
|
---|
1415 | super(range);
|
---|
1416 | }
|
---|
1417 |
|
---|
1418 | AreaSize(PushbackTokenizer tokenizer) throws SearchParseError {
|
---|
1419 | this(tokenizer.readRange(tr("Range of numbers expected")));
|
---|
1420 | }
|
---|
1421 |
|
---|
1422 | @Override
|
---|
1423 | protected Long getNumber(OsmPrimitive osm) {
|
---|
1424 | final Double area = Geometry.computeArea(osm);
|
---|
1425 | return area == null ? null : area.longValue();
|
---|
1426 | }
|
---|
1427 |
|
---|
1428 | @Override
|
---|
1429 | protected String getString() {
|
---|
1430 | return "areasize";
|
---|
1431 | }
|
---|
1432 | }
|
---|
1433 |
|
---|
1434 | /**
|
---|
1435 | * Matches if the length of a way is within the given range
|
---|
1436 | */
|
---|
1437 | private static class WayLength extends RangeMatch {
|
---|
1438 |
|
---|
1439 | WayLength(Range range) {
|
---|
1440 | super(range);
|
---|
1441 | }
|
---|
1442 |
|
---|
1443 | WayLength(PushbackTokenizer tokenizer) throws SearchParseError {
|
---|
1444 | this(tokenizer.readRange(tr("Range of numbers expected")));
|
---|
1445 | }
|
---|
1446 |
|
---|
1447 | @Override
|
---|
1448 | protected Long getNumber(OsmPrimitive osm) {
|
---|
1449 | if (!(osm instanceof Way))
|
---|
1450 | return null;
|
---|
1451 | Way way = (Way) osm;
|
---|
1452 | return (long) way.getLength();
|
---|
1453 | }
|
---|
1454 |
|
---|
1455 | @Override
|
---|
1456 | protected String getString() {
|
---|
1457 | return "waylength";
|
---|
1458 | }
|
---|
1459 | }
|
---|
1460 |
|
---|
1461 | /**
|
---|
1462 | * Matches objects within the given bounds.
|
---|
1463 | */
|
---|
1464 | private abstract static class InArea extends Match {
|
---|
1465 |
|
---|
1466 | protected final boolean all;
|
---|
1467 |
|
---|
1468 | /**
|
---|
1469 | * @param all if true, all way nodes or relation members have to be within source area;if false, one suffices.
|
---|
1470 | */
|
---|
1471 | InArea(boolean all) {
|
---|
1472 | this.all = all;
|
---|
1473 | }
|
---|
1474 |
|
---|
1475 | protected abstract Collection<Bounds> getBounds(OsmPrimitive primitive);
|
---|
1476 |
|
---|
1477 | @Override
|
---|
1478 | public boolean match(OsmPrimitive osm) {
|
---|
1479 | if (!osm.isUsable())
|
---|
1480 | return false;
|
---|
1481 | else if (osm instanceof Node) {
|
---|
1482 | LatLon coordinate = ((Node) osm).getCoor();
|
---|
1483 | Collection<Bounds> allBounds = getBounds(osm);
|
---|
1484 | return coordinate != null && allBounds != null && allBounds.stream().anyMatch(bounds -> bounds.contains(coordinate));
|
---|
1485 | } else if (osm instanceof Way) {
|
---|
1486 | Collection<Node> nodes = ((Way) osm).getNodes();
|
---|
1487 | return all ? nodes.stream().allMatch(this) : nodes.stream().anyMatch(this);
|
---|
1488 | } else if (osm instanceof Relation) {
|
---|
1489 | Collection<OsmPrimitive> primitives = ((Relation) osm).getMemberPrimitivesList();
|
---|
1490 | return all ? primitives.stream().allMatch(this) : primitives.stream().anyMatch(this);
|
---|
1491 | } else
|
---|
1492 | return false;
|
---|
1493 | }
|
---|
1494 | }
|
---|
1495 |
|
---|
1496 | /**
|
---|
1497 | * Matches objects within source area ("downloaded area").
|
---|
1498 | */
|
---|
1499 | public static class InDataSourceArea extends InArea {
|
---|
1500 |
|
---|
1501 | /**
|
---|
1502 | * Constructs a new {@code InDataSourceArea}.
|
---|
1503 | * @param all if true, all way nodes or relation members have to be within source area; if false, one suffices.
|
---|
1504 | */
|
---|
1505 | public InDataSourceArea(boolean all) {
|
---|
1506 | super(all);
|
---|
1507 | }
|
---|
1508 |
|
---|
1509 | @Override
|
---|
1510 | protected Collection<Bounds> getBounds(OsmPrimitive primitive) {
|
---|
1511 | return primitive.getDataSet() != null ? primitive.getDataSet().getDataSourceBounds() : null;
|
---|
1512 | }
|
---|
1513 |
|
---|
1514 | @Override
|
---|
1515 | public String toString() {
|
---|
1516 | return all ? "allindownloadedarea" : "indownloadedarea";
|
---|
1517 | }
|
---|
1518 | }
|
---|
1519 |
|
---|
1520 | /**
|
---|
1521 | * Matches objects which are not outside the source area ("downloaded area").
|
---|
1522 | * Unlike {@link InDataSourceArea} this matches also if no source area is set (e.g., for new layers).
|
---|
1523 | */
|
---|
1524 | public static class NotOutsideDataSourceArea extends InDataSourceArea {
|
---|
1525 |
|
---|
1526 | /**
|
---|
1527 | * Constructs a new {@code NotOutsideDataSourceArea}.
|
---|
1528 | */
|
---|
1529 | public NotOutsideDataSourceArea() {
|
---|
1530 | super(false);
|
---|
1531 | }
|
---|
1532 |
|
---|
1533 | @Override
|
---|
1534 | protected Collection<Bounds> getBounds(OsmPrimitive primitive) {
|
---|
1535 | final Collection<Bounds> bounds = super.getBounds(primitive);
|
---|
1536 | return bounds == null || bounds.isEmpty() ? Collections.singleton(Main.getProjection().getWorldBoundsLatLon()) : bounds;
|
---|
1537 | }
|
---|
1538 |
|
---|
1539 | @Override
|
---|
1540 | public String toString() {
|
---|
1541 | return "NotOutsideDataSourceArea";
|
---|
1542 | }
|
---|
1543 | }
|
---|
1544 |
|
---|
1545 | /**
|
---|
1546 | * Matches objects within current map view.
|
---|
1547 | */
|
---|
1548 | private static class InView extends InArea {
|
---|
1549 |
|
---|
1550 | InView(boolean all) {
|
---|
1551 | super(all);
|
---|
1552 | }
|
---|
1553 |
|
---|
1554 | @Override
|
---|
1555 | protected Collection<Bounds> getBounds(OsmPrimitive primitive) {
|
---|
1556 | if (!MainApplication.isDisplayingMapView()) {
|
---|
1557 | return null;
|
---|
1558 | }
|
---|
1559 | return Collections.singleton(MainApplication.getMap().mapView.getRealBounds());
|
---|
1560 | }
|
---|
1561 |
|
---|
1562 | @Override
|
---|
1563 | public String toString() {
|
---|
1564 | return all ? "allinview" : "inview";
|
---|
1565 | }
|
---|
1566 | }
|
---|
1567 |
|
---|
1568 | /**
|
---|
1569 | * Matches presets.
|
---|
1570 | * @since 12464
|
---|
1571 | */
|
---|
1572 | private static class Preset extends Match {
|
---|
1573 | private final List<TaggingPreset> presets;
|
---|
1574 |
|
---|
1575 | Preset(String presetName) throws SearchParseError {
|
---|
1576 |
|
---|
1577 | if (presetName == null || presetName.isEmpty()) {
|
---|
1578 | throw new SearchParseError("The name of the preset is required");
|
---|
1579 | }
|
---|
1580 |
|
---|
1581 | int wildCardIdx = presetName.lastIndexOf('*');
|
---|
1582 | int length = presetName.length() - 1;
|
---|
1583 |
|
---|
1584 | /*
|
---|
1585 | * Match strictly (simply comparing the names) if there is no '*' symbol
|
---|
1586 | * at the end of the name or '*' is a part of the preset name.
|
---|
1587 | */
|
---|
1588 | boolean matchStrictly = wildCardIdx == -1 || wildCardIdx != length;
|
---|
1589 |
|
---|
1590 | this.presets = TaggingPresets.getTaggingPresets()
|
---|
1591 | .stream()
|
---|
1592 | .filter(preset -> !(preset instanceof TaggingPresetMenu || preset instanceof TaggingPresetSeparator))
|
---|
1593 | .filter(preset -> presetNameMatch(presetName, preset, matchStrictly))
|
---|
1594 | .collect(Collectors.toList());
|
---|
1595 |
|
---|
1596 | if (this.presets.isEmpty()) {
|
---|
1597 | throw new SearchParseError(tr("Unknown preset name: ") + presetName);
|
---|
1598 | }
|
---|
1599 | }
|
---|
1600 |
|
---|
1601 | @Override
|
---|
1602 | public boolean match(OsmPrimitive osm) {
|
---|
1603 | for (TaggingPreset preset : this.presets) {
|
---|
1604 | if (preset.test(osm)) {
|
---|
1605 | return true;
|
---|
1606 | }
|
---|
1607 | }
|
---|
1608 |
|
---|
1609 | return false;
|
---|
1610 | }
|
---|
1611 |
|
---|
1612 | private static boolean presetNameMatch(String name, TaggingPreset preset, boolean matchStrictly) {
|
---|
1613 | if (matchStrictly) {
|
---|
1614 | return name.equalsIgnoreCase(preset.getRawName());
|
---|
1615 | }
|
---|
1616 |
|
---|
1617 | try {
|
---|
1618 | String groupSuffix = name.substring(0, name.length() - 2); // try to remove '/*'
|
---|
1619 | TaggingPresetMenu group = preset.group;
|
---|
1620 |
|
---|
1621 | return group != null && groupSuffix.equalsIgnoreCase(group.getRawName());
|
---|
1622 | } catch (StringIndexOutOfBoundsException ex) {
|
---|
1623 | return false;
|
---|
1624 | }
|
---|
1625 | }
|
---|
1626 | }
|
---|
1627 |
|
---|
1628 | /**
|
---|
1629 | * Compiles the search expression.
|
---|
1630 | * @param searchStr the search expression
|
---|
1631 | * @return a {@link Match} object for the expression
|
---|
1632 | * @throws SearchParseError if an error has been encountered while compiling
|
---|
1633 | * @see #compile(org.openstreetmap.josm.actions.search.SearchAction.SearchSetting)
|
---|
1634 | */
|
---|
1635 | public static Match compile(String searchStr) throws SearchParseError {
|
---|
1636 | return new SearchCompiler(false, false,
|
---|
1637 | new PushbackTokenizer(
|
---|
1638 | new PushbackReader(new StringReader(searchStr))))
|
---|
1639 | .parse();
|
---|
1640 | }
|
---|
1641 |
|
---|
1642 | /**
|
---|
1643 | * Compiles the search expression.
|
---|
1644 | * @param setting the settings to use
|
---|
1645 | * @return a {@link Match} object for the expression
|
---|
1646 | * @throws SearchParseError if an error has been encountered while compiling
|
---|
1647 | * @see #compile(String)
|
---|
1648 | */
|
---|
1649 | public static Match compile(SearchAction.SearchSetting setting) throws SearchParseError {
|
---|
1650 | if (setting.mapCSSSearch) {
|
---|
1651 | return compileMapCSS(setting.text);
|
---|
1652 | }
|
---|
1653 | return new SearchCompiler(setting.caseSensitive, setting.regexSearch,
|
---|
1654 | new PushbackTokenizer(
|
---|
1655 | new PushbackReader(new StringReader(setting.text))))
|
---|
1656 | .parse();
|
---|
1657 | }
|
---|
1658 |
|
---|
1659 | static Match compileMapCSS(String mapCSS) throws SearchParseError {
|
---|
1660 | try {
|
---|
1661 | final List<Selector> selectors = new MapCSSParser(new StringReader(mapCSS)).selectors();
|
---|
1662 | return new Match() {
|
---|
1663 | @Override
|
---|
1664 | public boolean match(OsmPrimitive osm) {
|
---|
1665 | for (Selector selector : selectors) {
|
---|
1666 | if (selector.matches(new Environment(osm))) {
|
---|
1667 | return true;
|
---|
1668 | }
|
---|
1669 | }
|
---|
1670 | return false;
|
---|
1671 | }
|
---|
1672 | };
|
---|
1673 | } catch (ParseException e) {
|
---|
1674 | throw new SearchParseError(tr("Failed to parse MapCSS selector"), e);
|
---|
1675 | }
|
---|
1676 | }
|
---|
1677 |
|
---|
1678 | /**
|
---|
1679 | * Parse search string.
|
---|
1680 | *
|
---|
1681 | * @return match determined by search string
|
---|
1682 | * @throws org.openstreetmap.josm.data.osm.search.SearchParseError if search expression cannot be parsed
|
---|
1683 | */
|
---|
1684 | public Match parse() throws SearchParseError {
|
---|
1685 | Match m = Optional.ofNullable(parseExpression()).orElse(Always.INSTANCE);
|
---|
1686 | if (!tokenizer.readIfEqual(Token.EOF))
|
---|
1687 | throw new SearchParseError(tr("Unexpected token: {0}", tokenizer.nextToken()));
|
---|
1688 | Logging.debug("Parsed search expression is {0}", m);
|
---|
1689 | return m;
|
---|
1690 | }
|
---|
1691 |
|
---|
1692 | /**
|
---|
1693 | * Parse expression.
|
---|
1694 | *
|
---|
1695 | * @return match determined by parsing expression
|
---|
1696 | * @throws SearchParseError if search expression cannot be parsed
|
---|
1697 | */
|
---|
1698 | private Match parseExpression() throws SearchParseError {
|
---|
1699 | // Step 1: parse the whole expression and build a list of factors and logical tokens
|
---|
1700 | List<Object> list = parseExpressionStep1();
|
---|
1701 | // Step 2: iterate the list in reverse order to build the logical expression
|
---|
1702 | // This iterative approach avoids StackOverflowError for long expressions (see #14217)
|
---|
1703 | return parseExpressionStep2(list);
|
---|
1704 | }
|
---|
1705 |
|
---|
1706 | private List<Object> parseExpressionStep1() throws SearchParseError {
|
---|
1707 | Match factor;
|
---|
1708 | String token = null;
|
---|
1709 | String errorMessage = null;
|
---|
1710 | List<Object> list = new ArrayList<>();
|
---|
1711 | do {
|
---|
1712 | factor = parseFactor();
|
---|
1713 | if (factor != null) {
|
---|
1714 | if (token != null) {
|
---|
1715 | list.add(token);
|
---|
1716 | }
|
---|
1717 | list.add(factor);
|
---|
1718 | if (tokenizer.readIfEqual(Token.OR)) {
|
---|
1719 | token = "OR";
|
---|
1720 | errorMessage = tr("Missing parameter for OR");
|
---|
1721 | } else if (tokenizer.readIfEqual(Token.XOR)) {
|
---|
1722 | token = "XOR";
|
---|
1723 | errorMessage = tr("Missing parameter for XOR");
|
---|
1724 | } else {
|
---|
1725 | token = "AND";
|
---|
1726 | errorMessage = null;
|
---|
1727 | }
|
---|
1728 | } else if (errorMessage != null) {
|
---|
1729 | throw new SearchParseError(errorMessage);
|
---|
1730 | }
|
---|
1731 | } while (factor != null);
|
---|
1732 | return list;
|
---|
1733 | }
|
---|
1734 |
|
---|
1735 | private static Match parseExpressionStep2(List<Object> list) {
|
---|
1736 | Match result = null;
|
---|
1737 | for (int i = list.size() - 1; i >= 0; i--) {
|
---|
1738 | Object o = list.get(i);
|
---|
1739 | if (o instanceof Match && result == null) {
|
---|
1740 | result = (Match) o;
|
---|
1741 | } else if (o instanceof String && i > 0) {
|
---|
1742 | Match factor = (Match) list.get(i-1);
|
---|
1743 | switch ((String) o) {
|
---|
1744 | case "OR":
|
---|
1745 | result = new Or(factor, result);
|
---|
1746 | break;
|
---|
1747 | case "XOR":
|
---|
1748 | result = new Xor(factor, result);
|
---|
1749 | break;
|
---|
1750 | case "AND":
|
---|
1751 | result = new And(factor, result);
|
---|
1752 | break;
|
---|
1753 | default: throw new IllegalStateException(tr("Unexpected token: {0}", o));
|
---|
1754 | }
|
---|
1755 | i--;
|
---|
1756 | } else {
|
---|
1757 | throw new IllegalStateException("i=" + i + "; o=" + o);
|
---|
1758 | }
|
---|
1759 | }
|
---|
1760 | return result;
|
---|
1761 | }
|
---|
1762 |
|
---|
1763 | /**
|
---|
1764 | * Parse next factor (a search operator or search term).
|
---|
1765 | *
|
---|
1766 | * @return match determined by parsing factor string
|
---|
1767 | * @throws SearchParseError if search expression cannot be parsed
|
---|
1768 | */
|
---|
1769 | private Match parseFactor() throws SearchParseError {
|
---|
1770 | if (tokenizer.readIfEqual(Token.LEFT_PARENT)) {
|
---|
1771 | Match expression = parseExpression();
|
---|
1772 | if (!tokenizer.readIfEqual(Token.RIGHT_PARENT))
|
---|
1773 | throw new SearchParseError(Token.RIGHT_PARENT, tokenizer.nextToken());
|
---|
1774 | return expression;
|
---|
1775 | } else if (tokenizer.readIfEqual(Token.NOT)) {
|
---|
1776 | return new Not(parseFactor(tr("Missing operator for NOT")));
|
---|
1777 | } else if (tokenizer.readIfEqual(Token.KEY)) {
|
---|
1778 | // factor consists of key:value or key=value
|
---|
1779 | String key = tokenizer.getText();
|
---|
1780 | if (tokenizer.readIfEqual(Token.EQUALS)) {
|
---|
1781 | return new ExactKeyValue(regexSearch, key, tokenizer.readTextOrNumber());
|
---|
1782 | } else if (tokenizer.readIfEqual(Token.LESS_THAN)) {
|
---|
1783 | return new ValueComparison(key, tokenizer.readTextOrNumber(), -1);
|
---|
1784 | } else if (tokenizer.readIfEqual(Token.GREATER_THAN)) {
|
---|
1785 | return new ValueComparison(key, tokenizer.readTextOrNumber(), +1);
|
---|
1786 | } else if (tokenizer.readIfEqual(Token.COLON)) {
|
---|
1787 | // see if we have a Match that takes a data parameter
|
---|
1788 | SimpleMatchFactory factory = simpleMatchFactoryMap.get(key);
|
---|
1789 | if (factory != null)
|
---|
1790 | return factory.get(key, tokenizer);
|
---|
1791 |
|
---|
1792 | UnaryMatchFactory unaryFactory = unaryMatchFactoryMap.get(key);
|
---|
1793 | if (unaryFactory != null)
|
---|
1794 | return unaryFactory.get(key, parseFactor(), tokenizer);
|
---|
1795 |
|
---|
1796 | // key:value form where value is a string (may be OSM key search)
|
---|
1797 | final String value = tokenizer.readTextOrNumber();
|
---|
1798 | return new KeyValue(key, value != null ? value : "", regexSearch, caseSensitive);
|
---|
1799 | } else if (tokenizer.readIfEqual(Token.QUESTION_MARK))
|
---|
1800 | return new BooleanMatch(key, false);
|
---|
1801 | else {
|
---|
1802 | SimpleMatchFactory factory = simpleMatchFactoryMap.get(key);
|
---|
1803 | if (factory != null)
|
---|
1804 | return factory.get(key, null);
|
---|
1805 |
|
---|
1806 | UnaryMatchFactory unaryFactory = unaryMatchFactoryMap.get(key);
|
---|
1807 | if (unaryFactory != null)
|
---|
1808 | return unaryFactory.get(key, parseFactor(), null);
|
---|
1809 |
|
---|
1810 | // match string in any key or value
|
---|
1811 | return new Any(key, regexSearch, caseSensitive);
|
---|
1812 | }
|
---|
1813 | } else
|
---|
1814 | return null;
|
---|
1815 | }
|
---|
1816 |
|
---|
1817 | private Match parseFactor(String errorMessage) throws SearchParseError {
|
---|
1818 | return Optional.ofNullable(parseFactor()).orElseThrow(() -> new SearchParseError(errorMessage));
|
---|
1819 | }
|
---|
1820 |
|
---|
1821 | private static int regexFlags(boolean caseSensitive) {
|
---|
1822 | int searchFlags = 0;
|
---|
1823 |
|
---|
1824 | // Enables canonical Unicode equivalence so that e.g. the two
|
---|
1825 | // forms of "\u00e9gal" and "e\u0301gal" will match.
|
---|
1826 | //
|
---|
1827 | // It makes sense to match no matter how the character
|
---|
1828 | // happened to be constructed.
|
---|
1829 | searchFlags |= Pattern.CANON_EQ;
|
---|
1830 |
|
---|
1831 | // Make "." match any character including newline (/s in Perl)
|
---|
1832 | searchFlags |= Pattern.DOTALL;
|
---|
1833 |
|
---|
1834 | // CASE_INSENSITIVE by itself only matches US-ASCII case
|
---|
1835 | // insensitively, but the OSM data is in Unicode. With
|
---|
1836 | // UNICODE_CASE casefolding is made Unicode-aware.
|
---|
1837 | if (!caseSensitive) {
|
---|
1838 | searchFlags |= (Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
|
---|
1839 | }
|
---|
1840 |
|
---|
1841 | return searchFlags;
|
---|
1842 | }
|
---|
1843 |
|
---|
1844 | static String escapeStringForSearch(String s) {
|
---|
1845 | return s.replace("\\", "\\\\").replace("\"", "\\\"");
|
---|
1846 | }
|
---|
1847 |
|
---|
1848 | /**
|
---|
1849 | * Builds a search string for the given tag. If value is empty, the existence of the key is checked.
|
---|
1850 | *
|
---|
1851 | * @param key the tag key
|
---|
1852 | * @param value the tag value
|
---|
1853 | * @return a search string for the given tag
|
---|
1854 | */
|
---|
1855 | public static String buildSearchStringForTag(String key, String value) {
|
---|
1856 | final String forKey = '"' + escapeStringForSearch(key) + '"' + '=';
|
---|
1857 | if (value == null || value.isEmpty()) {
|
---|
1858 | return forKey + '*';
|
---|
1859 | } else {
|
---|
1860 | return forKey + '"' + escapeStringForSearch(value) + '"';
|
---|
1861 | }
|
---|
1862 | }
|
---|
1863 | }
|
---|