1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint.mapcss;
|
---|
3 |
|
---|
4 | import java.lang.reflect.InvocationTargetException;
|
---|
5 | import java.lang.reflect.Method;
|
---|
6 | import java.text.MessageFormat;
|
---|
7 | import java.util.Arrays;
|
---|
8 | import java.util.EnumSet;
|
---|
9 | import java.util.Map;
|
---|
10 | import java.util.Objects;
|
---|
11 | import java.util.Set;
|
---|
12 | import java.util.function.BiFunction;
|
---|
13 | import java.util.function.IntFunction;
|
---|
14 | import java.util.function.Predicate;
|
---|
15 | import java.util.regex.Pattern;
|
---|
16 | import java.util.regex.PatternSyntaxException;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.actions.search.SearchCompiler.InDataSourceArea;
|
---|
19 | import org.openstreetmap.josm.data.osm.Node;
|
---|
20 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
21 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
22 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
23 | import org.openstreetmap.josm.data.osm.Tag;
|
---|
24 | import org.openstreetmap.josm.data.osm.Way;
|
---|
25 | import org.openstreetmap.josm.data.osm.visitor.paint.relations.MultipolygonCache;
|
---|
26 | import org.openstreetmap.josm.gui.mappaint.Cascade;
|
---|
27 | import org.openstreetmap.josm.gui.mappaint.ElemStyles;
|
---|
28 | import org.openstreetmap.josm.gui.mappaint.Environment;
|
---|
29 | import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Context;
|
---|
30 | import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.ToTagConvertable;
|
---|
31 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
32 | import org.openstreetmap.josm.tools.JosmRuntimeException;
|
---|
33 | import org.openstreetmap.josm.tools.Utils;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Factory to generate {@link Condition}s.
|
---|
37 | * @since 10837 (Extracted from Condition)
|
---|
38 | */
|
---|
39 | public final class ConditionFactory {
|
---|
40 |
|
---|
41 | private ConditionFactory() {
|
---|
42 | // Hide default constructor for utils classes
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Create a new condition that checks the key and the value of the object.
|
---|
47 | * @param k The key.
|
---|
48 | * @param v The reference value
|
---|
49 | * @param op The operation to use when comparing the value
|
---|
50 | * @param context The type of context to use.
|
---|
51 | * @param considerValAsKey whether to consider {@code v} as another key and compare the values of key {@code k} and key {@code v}.
|
---|
52 | * @return The new condition.
|
---|
53 | * @throws MapCSSException if the arguments are incorrect
|
---|
54 | */
|
---|
55 | public static Condition createKeyValueCondition(String k, String v, Op op, Context context, boolean considerValAsKey) {
|
---|
56 | switch (context) {
|
---|
57 | case PRIMITIVE:
|
---|
58 | if (KeyValueRegexpCondition.SUPPORTED_OPS.contains(op) && !considerValAsKey) {
|
---|
59 | try {
|
---|
60 | return new KeyValueRegexpCondition(k, v, op, false);
|
---|
61 | } catch (PatternSyntaxException e) {
|
---|
62 | throw new MapCSSException(e);
|
---|
63 | }
|
---|
64 | }
|
---|
65 | if (!considerValAsKey && op.equals(Op.EQ))
|
---|
66 | return new SimpleKeyValueCondition(k, v);
|
---|
67 | return new KeyValueCondition(k, v, op, considerValAsKey);
|
---|
68 | case LINK:
|
---|
69 | if (considerValAsKey)
|
---|
70 | throw new MapCSSException("''considerValAsKey'' not supported in LINK context");
|
---|
71 | if ("role".equalsIgnoreCase(k))
|
---|
72 | return new RoleCondition(v, op);
|
---|
73 | else if ("index".equalsIgnoreCase(k))
|
---|
74 | return new IndexCondition(v, op);
|
---|
75 | else
|
---|
76 | throw new MapCSSException(
|
---|
77 | MessageFormat.format("Expected key ''role'' or ''index'' in link context. Got ''{0}''.", k));
|
---|
78 |
|
---|
79 | default: throw new AssertionError();
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Create a condition in which the key and the value need to match a given regexp
|
---|
85 | * @param k The key regexp
|
---|
86 | * @param v The value regexp
|
---|
87 | * @param op The operation to use when comparing the key and the value.
|
---|
88 | * @return The new condition.
|
---|
89 | */
|
---|
90 | public static Condition createRegexpKeyRegexpValueCondition(String k, String v, Op op) {
|
---|
91 | return new RegexpKeyValueRegexpCondition(k, v, op);
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Creates a condition that checks the given key.
|
---|
96 | * @param k The key to test for
|
---|
97 | * @param not <code>true</code> to invert the match
|
---|
98 | * @param matchType The match type to check for.
|
---|
99 | * @param context The context this rule is found in.
|
---|
100 | * @return the new condition.
|
---|
101 | */
|
---|
102 | public static Condition createKeyCondition(String k, boolean not, KeyMatchType matchType, Context context) {
|
---|
103 | switch (context) {
|
---|
104 | case PRIMITIVE:
|
---|
105 | return new KeyCondition(k, not, matchType);
|
---|
106 | case LINK:
|
---|
107 | if (matchType != null)
|
---|
108 | throw new MapCSSException("Question mark operator ''?'' and regexp match not supported in LINK context");
|
---|
109 | if (not)
|
---|
110 | return new RoleCondition(k, Op.NEQ);
|
---|
111 | else
|
---|
112 | return new RoleCondition(k, Op.EQ);
|
---|
113 |
|
---|
114 | default: throw new AssertionError();
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Create a new pseudo class condition
|
---|
120 | * @param id The id of the pseudo class
|
---|
121 | * @param not <code>true</code> to invert the condition
|
---|
122 | * @param context The context the class is found in.
|
---|
123 | * @return The new condition
|
---|
124 | */
|
---|
125 | public static PseudoClassCondition createPseudoClassCondition(String id, boolean not, Context context) {
|
---|
126 | return PseudoClassCondition.createPseudoClassCondition(id, not, context);
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Create a new class condition
|
---|
131 | * @param id The id of the class to match
|
---|
132 | * @param not <code>true</code> to invert the condition
|
---|
133 | * @param context Ignored
|
---|
134 | * @return The new condition
|
---|
135 | */
|
---|
136 | public static ClassCondition createClassCondition(String id, boolean not, Context context) {
|
---|
137 | return new ClassCondition(id, not);
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Create a new condition that a expression needs to be fulfilled
|
---|
142 | * @param e the expression to check
|
---|
143 | * @param context Ignored
|
---|
144 | * @return The new condition
|
---|
145 | */
|
---|
146 | public static ExpressionCondition createExpressionCondition(Expression e, Context context) {
|
---|
147 | return new ExpressionCondition(e);
|
---|
148 | }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * This is the operation that {@link KeyValueCondition} uses to match.
|
---|
152 | */
|
---|
153 | public enum Op {
|
---|
154 | /** The value equals the given reference. */
|
---|
155 | EQ(Objects::equals),
|
---|
156 | /** The value does not equal the reference. */
|
---|
157 | NEQ(EQ),
|
---|
158 | /** The value is greater than or equal to the given reference value (as float). */
|
---|
159 | GREATER_OR_EQUAL(comparisonResult -> comparisonResult >= 0),
|
---|
160 | /** The value is greater than the given reference value (as float). */
|
---|
161 | GREATER(comparisonResult -> comparisonResult > 0),
|
---|
162 | /** The value is less than or equal to the given reference value (as float). */
|
---|
163 | LESS_OR_EQUAL(comparisonResult -> comparisonResult <= 0),
|
---|
164 | /** The value is less than the given reference value (as float). */
|
---|
165 | LESS(comparisonResult -> comparisonResult < 0),
|
---|
166 | /** The reference is treated as regular expression and the value needs to match it. */
|
---|
167 | REGEX((test, prototype) -> Pattern.compile(prototype).matcher(test).find()),
|
---|
168 | /** The reference is treated as regular expression and the value needs to not match it. */
|
---|
169 | NREGEX(REGEX),
|
---|
170 | /** The reference is treated as a list separated by ';'. Spaces around the ; are ignored.
|
---|
171 | * The value needs to be equal one of the list elements. */
|
---|
172 | ONE_OF((test, prototype) -> Arrays.asList(test.split("\\s*;\\s*")).contains(prototype)),
|
---|
173 | /** The value needs to begin with the reference string. */
|
---|
174 | BEGINS_WITH(String::startsWith),
|
---|
175 | /** The value needs to end with the reference string. */
|
---|
176 | ENDS_WITH(String::endsWith),
|
---|
177 | /** The value needs to contain the reference string. */
|
---|
178 | CONTAINS(String::contains);
|
---|
179 |
|
---|
180 | static final Set<Op> NEGATED_OPS = EnumSet.of(NEQ, NREGEX);
|
---|
181 |
|
---|
182 | private final BiFunction<String, String, Boolean> function;
|
---|
183 |
|
---|
184 | private final boolean negated;
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Create a new string operation.
|
---|
188 | * @param func The function to apply during {@link #eval(String, String)}.
|
---|
189 | */
|
---|
190 | Op(BiFunction<String, String, Boolean> func) {
|
---|
191 | this.function = func;
|
---|
192 | negated = false;
|
---|
193 | }
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Create a new float operation that compares two float values
|
---|
197 | * @param comparatorResult A function to mapt the result of the comparison
|
---|
198 | */
|
---|
199 | Op(IntFunction<Boolean> comparatorResult) {
|
---|
200 | this.function = (test, prototype) -> {
|
---|
201 | float testFloat;
|
---|
202 | try {
|
---|
203 | testFloat = Float.parseFloat(test);
|
---|
204 | } catch (NumberFormatException e) {
|
---|
205 | return Boolean.FALSE;
|
---|
206 | }
|
---|
207 | float prototypeFloat = Float.parseFloat(prototype);
|
---|
208 |
|
---|
209 | int res = Float.compare(testFloat, prototypeFloat);
|
---|
210 | return comparatorResult.apply(res);
|
---|
211 | };
|
---|
212 | negated = false;
|
---|
213 | }
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * Create a new Op by negating an other op.
|
---|
217 | * @param negate inverse operation
|
---|
218 | */
|
---|
219 | Op(Op negate) {
|
---|
220 | this.function = (a, b) -> !negate.function.apply(a, b);
|
---|
221 | negated = true;
|
---|
222 | }
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * Evaluates a value against a reference string.
|
---|
226 | * @param testString The value. May be <code>null</code>
|
---|
227 | * @param prototypeString The reference string-
|
---|
228 | * @return <code>true</code> if and only if this operation matches for the given value/reference pair.
|
---|
229 | */
|
---|
230 | public boolean eval(String testString, String prototypeString) {
|
---|
231 | if (testString == null)
|
---|
232 | return negated;
|
---|
233 | else
|
---|
234 | return function.apply(testString, prototypeString);
|
---|
235 | }
|
---|
236 | }
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * Most common case of a KeyValueCondition, this is the basic key=value case.
|
---|
240 | *
|
---|
241 | * Extra class for performance reasons.
|
---|
242 | */
|
---|
243 | public static class SimpleKeyValueCondition implements Condition, ToTagConvertable {
|
---|
244 | /**
|
---|
245 | * The key to search for.
|
---|
246 | */
|
---|
247 | public final String k;
|
---|
248 | /**
|
---|
249 | * The value to search for.
|
---|
250 | */
|
---|
251 | public final String v;
|
---|
252 |
|
---|
253 | /**
|
---|
254 | * Create a new SimpleKeyValueCondition.
|
---|
255 | * @param k The key
|
---|
256 | * @param v The value.
|
---|
257 | */
|
---|
258 | public SimpleKeyValueCondition(String k, String v) {
|
---|
259 | this.k = k;
|
---|
260 | this.v = v;
|
---|
261 | }
|
---|
262 |
|
---|
263 | @Override
|
---|
264 | public boolean applies(Environment e) {
|
---|
265 | return v.equals(e.osm.get(k));
|
---|
266 | }
|
---|
267 |
|
---|
268 | @Override
|
---|
269 | public Tag asTag(OsmPrimitive primitive) {
|
---|
270 | return new Tag(k, v);
|
---|
271 | }
|
---|
272 |
|
---|
273 | @Override
|
---|
274 | public String toString() {
|
---|
275 | return '[' + k + '=' + v + ']';
|
---|
276 | }
|
---|
277 |
|
---|
278 | }
|
---|
279 |
|
---|
280 | /**
|
---|
281 | * <p>Represents a key/value condition which is either applied to a primitive.</p>
|
---|
282 | *
|
---|
283 | */
|
---|
284 | public static class KeyValueCondition implements Condition, ToTagConvertable {
|
---|
285 | /**
|
---|
286 | * The key to search for.
|
---|
287 | */
|
---|
288 | public final String k;
|
---|
289 | /**
|
---|
290 | * The value to search for.
|
---|
291 | */
|
---|
292 | public final String v;
|
---|
293 | /**
|
---|
294 | * The key/value match operation.
|
---|
295 | */
|
---|
296 | public final Op op;
|
---|
297 | /**
|
---|
298 | * If this flag is set, {@link #v} is treated as a key and the value is the value set for that key.
|
---|
299 | */
|
---|
300 | public final boolean considerValAsKey;
|
---|
301 |
|
---|
302 | /**
|
---|
303 | * <p>Creates a key/value-condition.</p>
|
---|
304 | *
|
---|
305 | * @param k the key
|
---|
306 | * @param v the value
|
---|
307 | * @param op the operation
|
---|
308 | * @param considerValAsKey whether to consider {@code v} as another key and compare the values of key {@code k} and key {@code v}.
|
---|
309 | */
|
---|
310 | public KeyValueCondition(String k, String v, Op op, boolean considerValAsKey) {
|
---|
311 | this.k = k;
|
---|
312 | this.v = v;
|
---|
313 | this.op = op;
|
---|
314 | this.considerValAsKey = considerValAsKey;
|
---|
315 | }
|
---|
316 |
|
---|
317 | @Override
|
---|
318 | public boolean applies(Environment env) {
|
---|
319 | return op.eval(env.osm.get(k), considerValAsKey ? env.osm.get(v) : v);
|
---|
320 | }
|
---|
321 |
|
---|
322 | @Override
|
---|
323 | public Tag asTag(OsmPrimitive primitive) {
|
---|
324 | return new Tag(k, v);
|
---|
325 | }
|
---|
326 |
|
---|
327 | @Override
|
---|
328 | public String toString() {
|
---|
329 | return '[' + k + '\'' + op + '\'' + v + ']';
|
---|
330 | }
|
---|
331 | }
|
---|
332 |
|
---|
333 | /**
|
---|
334 | * This condition requires a fixed key to match a given regexp
|
---|
335 | */
|
---|
336 | public static class KeyValueRegexpCondition extends KeyValueCondition {
|
---|
337 | protected static final Set<Op> SUPPORTED_OPS = EnumSet.of(Op.REGEX, Op.NREGEX);
|
---|
338 |
|
---|
339 | final Pattern pattern;
|
---|
340 |
|
---|
341 | /**
|
---|
342 | * Constructs a new {@code KeyValueRegexpCondition}.
|
---|
343 | * @param k key
|
---|
344 | * @param v value
|
---|
345 | * @param op operation
|
---|
346 | * @param considerValAsKey must be false
|
---|
347 | * @throws PatternSyntaxException if the value syntax is invalid
|
---|
348 | */
|
---|
349 | public KeyValueRegexpCondition(String k, String v, Op op, boolean considerValAsKey) {
|
---|
350 | super(k, v, op, considerValAsKey);
|
---|
351 | CheckParameterUtil.ensureThat(!considerValAsKey, "considerValAsKey is not supported");
|
---|
352 | CheckParameterUtil.ensureThat(SUPPORTED_OPS.contains(op), "Op must be REGEX or NREGEX");
|
---|
353 | this.pattern = Pattern.compile(v);
|
---|
354 | }
|
---|
355 |
|
---|
356 | protected boolean matches(Environment env) {
|
---|
357 | final String value = env.osm.get(k);
|
---|
358 | return value != null && pattern.matcher(value).find();
|
---|
359 | }
|
---|
360 |
|
---|
361 | @Override
|
---|
362 | public boolean applies(Environment env) {
|
---|
363 | if (Op.REGEX.equals(op)) {
|
---|
364 | return matches(env);
|
---|
365 | } else if (Op.NREGEX.equals(op)) {
|
---|
366 | return !matches(env);
|
---|
367 | } else {
|
---|
368 | throw new IllegalStateException();
|
---|
369 | }
|
---|
370 | }
|
---|
371 | }
|
---|
372 |
|
---|
373 | /**
|
---|
374 | * A condition that checks that a key with the matching pattern has a value with the matching pattern.
|
---|
375 | */
|
---|
376 | public static class RegexpKeyValueRegexpCondition extends KeyValueRegexpCondition {
|
---|
377 |
|
---|
378 | final Pattern keyPattern;
|
---|
379 |
|
---|
380 | /**
|
---|
381 | * Create a condition in which the key and the value need to match a given regexp
|
---|
382 | * @param k The key regexp
|
---|
383 | * @param v The value regexp
|
---|
384 | * @param op The operation to use when comparing the key and the value.
|
---|
385 | */
|
---|
386 | public RegexpKeyValueRegexpCondition(String k, String v, Op op) {
|
---|
387 | super(k, v, op, false);
|
---|
388 | this.keyPattern = Pattern.compile(k);
|
---|
389 | }
|
---|
390 |
|
---|
391 | @Override
|
---|
392 | protected boolean matches(Environment env) {
|
---|
393 | for (Map.Entry<String, String> kv: env.osm.getKeys().entrySet()) {
|
---|
394 | if (keyPattern.matcher(kv.getKey()).find() && pattern.matcher(kv.getValue()).find()) {
|
---|
395 | return true;
|
---|
396 | }
|
---|
397 | }
|
---|
398 | return false;
|
---|
399 | }
|
---|
400 | }
|
---|
401 |
|
---|
402 | /**
|
---|
403 | * Role condition.
|
---|
404 | */
|
---|
405 | public static class RoleCondition implements Condition {
|
---|
406 | final String role;
|
---|
407 | final Op op;
|
---|
408 |
|
---|
409 | /**
|
---|
410 | * Constructs a new {@code RoleCondition}.
|
---|
411 | * @param role role
|
---|
412 | * @param op operation
|
---|
413 | */
|
---|
414 | public RoleCondition(String role, Op op) {
|
---|
415 | this.role = role;
|
---|
416 | this.op = op;
|
---|
417 | }
|
---|
418 |
|
---|
419 | @Override
|
---|
420 | public boolean applies(Environment env) {
|
---|
421 | String testRole = env.getRole();
|
---|
422 | if (testRole == null) return false;
|
---|
423 | return op.eval(testRole, role);
|
---|
424 | }
|
---|
425 | }
|
---|
426 |
|
---|
427 | /**
|
---|
428 | * Index condition.
|
---|
429 | */
|
---|
430 | public static class IndexCondition implements Condition {
|
---|
431 | final String index;
|
---|
432 | final Op op;
|
---|
433 |
|
---|
434 | /**
|
---|
435 | * Constructs a new {@code IndexCondition}.
|
---|
436 | * @param index index
|
---|
437 | * @param op operation
|
---|
438 | */
|
---|
439 | public IndexCondition(String index, Op op) {
|
---|
440 | this.index = index;
|
---|
441 | this.op = op;
|
---|
442 | }
|
---|
443 |
|
---|
444 | @Override
|
---|
445 | public boolean applies(Environment env) {
|
---|
446 | if (env.index == null) return false;
|
---|
447 | if (index.startsWith("-")) {
|
---|
448 | return env.count != null && op.eval(Integer.toString(env.index - env.count), index);
|
---|
449 | } else {
|
---|
450 | return op.eval(Integer.toString(env.index + 1), index);
|
---|
451 | }
|
---|
452 | }
|
---|
453 | }
|
---|
454 |
|
---|
455 | /**
|
---|
456 | * This defines how {@link KeyCondition} matches a given key.
|
---|
457 | */
|
---|
458 | public enum KeyMatchType {
|
---|
459 | /**
|
---|
460 | * The key needs to be equal to the given label.
|
---|
461 | */
|
---|
462 | EQ,
|
---|
463 | /**
|
---|
464 | * The key needs to have a true value (yes, ...)
|
---|
465 | * @see OsmUtils#isTrue(String)
|
---|
466 | */
|
---|
467 | TRUE,
|
---|
468 | /**
|
---|
469 | * The key needs to have a false value (no, ...)
|
---|
470 | * @see OsmUtils#isFalse(String)
|
---|
471 | */
|
---|
472 | FALSE,
|
---|
473 | /**
|
---|
474 | * The key needs to match the given regular expression.
|
---|
475 | */
|
---|
476 | REGEX
|
---|
477 | }
|
---|
478 |
|
---|
479 | /**
|
---|
480 | * <p>KeyCondition represent one of the following conditions in either the link or the
|
---|
481 | * primitive context:</p>
|
---|
482 | * <pre>
|
---|
483 | * ["a label"] PRIMITIVE: the primitive has a tag "a label"
|
---|
484 | * LINK: the parent is a relation and it has at least one member with the role
|
---|
485 | * "a label" referring to the child
|
---|
486 | *
|
---|
487 | * [!"a label"] PRIMITIVE: the primitive doesn't have a tag "a label"
|
---|
488 | * LINK: the parent is a relation but doesn't have a member with the role
|
---|
489 | * "a label" referring to the child
|
---|
490 | *
|
---|
491 | * ["a label"?] PRIMITIVE: the primitive has a tag "a label" whose value evaluates to a true-value
|
---|
492 | * LINK: not supported
|
---|
493 | *
|
---|
494 | * ["a label"?!] PRIMITIVE: the primitive has a tag "a label" whose value evaluates to a false-value
|
---|
495 | * LINK: not supported
|
---|
496 | * </pre>
|
---|
497 | */
|
---|
498 | public static class KeyCondition implements Condition, ToTagConvertable {
|
---|
499 |
|
---|
500 | /**
|
---|
501 | * The key name.
|
---|
502 | */
|
---|
503 | public final String label;
|
---|
504 | /**
|
---|
505 | * If we should negate the result of the match.
|
---|
506 | */
|
---|
507 | public final boolean negateResult;
|
---|
508 | /**
|
---|
509 | * Describes how to match the label against the key.
|
---|
510 | * @see KeyMatchType
|
---|
511 | */
|
---|
512 | public final KeyMatchType matchType;
|
---|
513 | /**
|
---|
514 | * A predicate used to match a the regexp against the key. Only used if the match type is regexp.
|
---|
515 | */
|
---|
516 | public final Predicate<String> containsPattern;
|
---|
517 |
|
---|
518 | /**
|
---|
519 | * Creates a new KeyCondition
|
---|
520 | * @param label The key name (or regexp) to use.
|
---|
521 | * @param negateResult If we should negate the result.,
|
---|
522 | * @param matchType The match type.
|
---|
523 | */
|
---|
524 | public KeyCondition(String label, boolean negateResult, KeyMatchType matchType) {
|
---|
525 | this.label = label;
|
---|
526 | this.negateResult = negateResult;
|
---|
527 | this.matchType = matchType == null ? KeyMatchType.EQ : matchType;
|
---|
528 | this.containsPattern = KeyMatchType.REGEX.equals(matchType)
|
---|
529 | ? Pattern.compile(label).asPredicate()
|
---|
530 | : null;
|
---|
531 | }
|
---|
532 |
|
---|
533 | @Override
|
---|
534 | public boolean applies(Environment e) {
|
---|
535 | switch(e.getContext()) {
|
---|
536 | case PRIMITIVE:
|
---|
537 | switch (matchType) {
|
---|
538 | case TRUE:
|
---|
539 | return e.osm.isKeyTrue(label) ^ negateResult;
|
---|
540 | case FALSE:
|
---|
541 | return e.osm.isKeyFalse(label) ^ negateResult;
|
---|
542 | case REGEX:
|
---|
543 | return e.osm.keySet().stream().anyMatch(containsPattern) ^ negateResult;
|
---|
544 | default:
|
---|
545 | return e.osm.hasKey(label) ^ negateResult;
|
---|
546 | }
|
---|
547 | case LINK:
|
---|
548 | Utils.ensure(false, "Illegal state: KeyCondition not supported in LINK context");
|
---|
549 | return false;
|
---|
550 | default: throw new AssertionError();
|
---|
551 | }
|
---|
552 | }
|
---|
553 |
|
---|
554 | /**
|
---|
555 | * Get the matched key and the corresponding value.
|
---|
556 | * <p>
|
---|
557 | * WARNING: This ignores {@link #negateResult}.
|
---|
558 | * <p>
|
---|
559 | * WARNING: For regexp, the regular expression is returned instead of a key if the match failed.
|
---|
560 | * @param p The primitive to get the value from.
|
---|
561 | * @return The tag.
|
---|
562 | */
|
---|
563 | @Override
|
---|
564 | public Tag asTag(OsmPrimitive p) {
|
---|
565 | String key = label;
|
---|
566 | if (KeyMatchType.REGEX.equals(matchType)) {
|
---|
567 | key = p.keySet().stream().filter(containsPattern).findAny().orElse(key);
|
---|
568 | }
|
---|
569 | return new Tag(key, p.get(key));
|
---|
570 | }
|
---|
571 |
|
---|
572 | @Override
|
---|
573 | public String toString() {
|
---|
574 | return '[' + (negateResult ? "!" : "") + label + ']';
|
---|
575 | }
|
---|
576 | }
|
---|
577 |
|
---|
578 | /**
|
---|
579 | * Class condition.
|
---|
580 | */
|
---|
581 | public static class ClassCondition implements Condition {
|
---|
582 |
|
---|
583 | /** Class identifier */
|
---|
584 | public final String id;
|
---|
585 | final boolean not;
|
---|
586 |
|
---|
587 | /**
|
---|
588 | * Constructs a new {@code ClassCondition}.
|
---|
589 | * @param id id
|
---|
590 | * @param not negation or not
|
---|
591 | */
|
---|
592 | public ClassCondition(String id, boolean not) {
|
---|
593 | this.id = id;
|
---|
594 | this.not = not;
|
---|
595 | }
|
---|
596 |
|
---|
597 | @Override
|
---|
598 | public boolean applies(Environment env) {
|
---|
599 | Cascade cascade = env.getCascade(env.layer);
|
---|
600 | return cascade != null && (not ^ cascade.containsKey(id));
|
---|
601 | }
|
---|
602 |
|
---|
603 | @Override
|
---|
604 | public String toString() {
|
---|
605 | return (not ? "!" : "") + '.' + id;
|
---|
606 | }
|
---|
607 | }
|
---|
608 |
|
---|
609 | /**
|
---|
610 | * Like <a href="http://www.w3.org/TR/css3-selectors/#pseudo-classes">CSS pseudo classes</a>, MapCSS pseudo classes
|
---|
611 | * are written in lower case with dashes between words.
|
---|
612 | */
|
---|
613 | public static final class PseudoClasses {
|
---|
614 |
|
---|
615 | private PseudoClasses() {
|
---|
616 | // Hide default constructor for utilities classes
|
---|
617 | }
|
---|
618 |
|
---|
619 | /**
|
---|
620 | * {@code closed} tests whether the way is closed or the relation is a closed multipolygon
|
---|
621 | * @param e MapCSS environment
|
---|
622 | * @return {@code true} if the way is closed or the relation is a closed multipolygon
|
---|
623 | */
|
---|
624 | static boolean closed(Environment e) { // NO_UCD (unused code)
|
---|
625 | if (e.osm instanceof Way && ((Way) e.osm).isClosed())
|
---|
626 | return true;
|
---|
627 | return e.osm instanceof Relation && ((Relation) e.osm).isMultipolygon();
|
---|
628 | }
|
---|
629 |
|
---|
630 | /**
|
---|
631 | * {@code :modified} tests whether the object has been modified.
|
---|
632 | * @param e MapCSS environment
|
---|
633 | * @return {@code true} if the object has been modified
|
---|
634 | * @see OsmPrimitive#isModified()
|
---|
635 | */
|
---|
636 | static boolean modified(Environment e) { // NO_UCD (unused code)
|
---|
637 | return e.osm.isModified() || e.osm.isNewOrUndeleted();
|
---|
638 | }
|
---|
639 |
|
---|
640 | /**
|
---|
641 | * {@code ;new} tests whether the object is new.
|
---|
642 | * @param e MapCSS environment
|
---|
643 | * @return {@code true} if the object is new
|
---|
644 | * @see OsmPrimitive#isNew()
|
---|
645 | */
|
---|
646 | static boolean _new(Environment e) { // NO_UCD (unused code)
|
---|
647 | return e.osm.isNew();
|
---|
648 | }
|
---|
649 |
|
---|
650 | /**
|
---|
651 | * {@code :connection} tests whether the object is a connection node.
|
---|
652 | * @param e MapCSS environment
|
---|
653 | * @return {@code true} if the object is a connection node
|
---|
654 | * @see Node#isConnectionNode()
|
---|
655 | */
|
---|
656 | static boolean connection(Environment e) { // NO_UCD (unused code)
|
---|
657 | return e.osm instanceof Node && e.osm.getDataSet() != null && ((Node) e.osm).isConnectionNode();
|
---|
658 | }
|
---|
659 |
|
---|
660 | /**
|
---|
661 | * {@code :tagged} tests whether the object is tagged.
|
---|
662 | * @param e MapCSS environment
|
---|
663 | * @return {@code true} if the object is tagged
|
---|
664 | * @see OsmPrimitive#isTagged()
|
---|
665 | */
|
---|
666 | static boolean tagged(Environment e) { // NO_UCD (unused code)
|
---|
667 | return e.osm.isTagged();
|
---|
668 | }
|
---|
669 |
|
---|
670 | /**
|
---|
671 | * {@code :same-tags} tests whether the object has the same tags as its child/parent.
|
---|
672 | * @param e MapCSS environment
|
---|
673 | * @return {@code true} if the object has the same tags as its child/parent
|
---|
674 | * @see OsmPrimitive#hasSameInterestingTags(OsmPrimitive)
|
---|
675 | */
|
---|
676 | static boolean sameTags(Environment e) { // NO_UCD (unused code)
|
---|
677 | return e.osm.hasSameInterestingTags(Utils.firstNonNull(e.child, e.parent));
|
---|
678 | }
|
---|
679 |
|
---|
680 | /**
|
---|
681 | * {@code :area-style} tests whether the object has an area style. This is useful for validators.
|
---|
682 | * @param e MapCSS environment
|
---|
683 | * @return {@code true} if the object has an area style
|
---|
684 | * @see ElemStyles#hasAreaElemStyle(OsmPrimitive, boolean)
|
---|
685 | */
|
---|
686 | static boolean areaStyle(Environment e) { // NO_UCD (unused code)
|
---|
687 | // only for validator
|
---|
688 | return ElemStyles.hasAreaElemStyle(e.osm, false);
|
---|
689 | }
|
---|
690 |
|
---|
691 | /**
|
---|
692 | * {@code unconnected}: tests whether the object is a unconnected node.
|
---|
693 | * @param e MapCSS environment
|
---|
694 | * @return {@code true} if the object is a unconnected node
|
---|
695 | */
|
---|
696 | static boolean unconnected(Environment e) { // NO_UCD (unused code)
|
---|
697 | return e.osm instanceof Node && ((Node) e.osm).getParentWays().isEmpty();
|
---|
698 | }
|
---|
699 |
|
---|
700 | /**
|
---|
701 | * {@code righthandtraffic} checks if there is right-hand traffic at the current location.
|
---|
702 | * @param e MapCSS environment
|
---|
703 | * @return {@code true} if there is right-hand traffic at the current location
|
---|
704 | * @see ExpressionFactory.Functions#is_right_hand_traffic(Environment)
|
---|
705 | */
|
---|
706 | static boolean righthandtraffic(Environment e) { // NO_UCD (unused code)
|
---|
707 | return ExpressionFactory.Functions.is_right_hand_traffic(e);
|
---|
708 | }
|
---|
709 |
|
---|
710 | /**
|
---|
711 | * {@code clockwise} whether the way is closed and oriented clockwise,
|
---|
712 | * or non-closed and the 1st, 2nd and last node are in clockwise order.
|
---|
713 | * @param e MapCSS environment
|
---|
714 | * @return {@code true} if the way clockwise
|
---|
715 | * @see ExpressionFactory.Functions#is_clockwise(Environment)
|
---|
716 | */
|
---|
717 | static boolean clockwise(Environment e) { // NO_UCD (unused code)
|
---|
718 | return ExpressionFactory.Functions.is_clockwise(e);
|
---|
719 | }
|
---|
720 |
|
---|
721 | /**
|
---|
722 | * {@code anticlockwise} whether the way is closed and oriented anticlockwise,
|
---|
723 | * or non-closed and the 1st, 2nd and last node are in anticlockwise order.
|
---|
724 | * @param e MapCSS environment
|
---|
725 | * @return {@code true} if the way clockwise
|
---|
726 | * @see ExpressionFactory.Functions#is_anticlockwise(Environment)
|
---|
727 | */
|
---|
728 | static boolean anticlockwise(Environment e) { // NO_UCD (unused code)
|
---|
729 | return ExpressionFactory.Functions.is_anticlockwise(e);
|
---|
730 | }
|
---|
731 |
|
---|
732 | /**
|
---|
733 | * {@code unclosed-multipolygon} tests whether the object is an unclosed multipolygon.
|
---|
734 | * @param e MapCSS environment
|
---|
735 | * @return {@code true} if the object is an unclosed multipolygon
|
---|
736 | */
|
---|
737 | static boolean unclosed_multipolygon(Environment e) { // NO_UCD (unused code)
|
---|
738 | return e.osm instanceof Relation && ((Relation) e.osm).isMultipolygon() &&
|
---|
739 | !e.osm.isIncomplete() && !((Relation) e.osm).hasIncompleteMembers() &&
|
---|
740 | !MultipolygonCache.getInstance().get((Relation) e.osm).getOpenEnds().isEmpty();
|
---|
741 | }
|
---|
742 |
|
---|
743 | private static final Predicate<OsmPrimitive> IN_DOWNLOADED_AREA = new InDataSourceArea(false);
|
---|
744 |
|
---|
745 | /**
|
---|
746 | * {@code in-downloaded-area} tests whether the object is within source area ("downloaded area").
|
---|
747 | * @param e MapCSS environment
|
---|
748 | * @return {@code true} if the object is within source area ("downloaded area")
|
---|
749 | * @see InDataSourceArea
|
---|
750 | */
|
---|
751 | static boolean inDownloadedArea(Environment e) { // NO_UCD (unused code)
|
---|
752 | return IN_DOWNLOADED_AREA.test(e.osm);
|
---|
753 | }
|
---|
754 |
|
---|
755 | static boolean completely_downloaded(Environment e) { // NO_UCD (unused code)
|
---|
756 | if (e.osm instanceof Relation) {
|
---|
757 | return !((Relation) e.osm).hasIncompleteMembers();
|
---|
758 | } else {
|
---|
759 | return true;
|
---|
760 | }
|
---|
761 | }
|
---|
762 |
|
---|
763 | static boolean closed2(Environment e) { // NO_UCD (unused code)
|
---|
764 | if (e.osm instanceof Way && ((Way) e.osm).isClosed())
|
---|
765 | return true;
|
---|
766 | if (e.osm instanceof Relation && ((Relation) e.osm).isMultipolygon())
|
---|
767 | return MultipolygonCache.getInstance().get((Relation) e.osm).getOpenEnds().isEmpty();
|
---|
768 | return false;
|
---|
769 | }
|
---|
770 |
|
---|
771 | static boolean selected(Environment e) { // NO_UCD (unused code)
|
---|
772 | Cascade c = e.mc.getCascade(e.layer);
|
---|
773 | c.setDefaultSelectedHandling(false);
|
---|
774 | return e.osm.isSelected();
|
---|
775 | }
|
---|
776 | }
|
---|
777 |
|
---|
778 | /**
|
---|
779 | * Pseudo class condition.
|
---|
780 | */
|
---|
781 | public static class PseudoClassCondition implements Condition {
|
---|
782 |
|
---|
783 | final Method method;
|
---|
784 | final boolean not;
|
---|
785 |
|
---|
786 | protected PseudoClassCondition(Method method, boolean not) {
|
---|
787 | this.method = method;
|
---|
788 | this.not = not;
|
---|
789 | }
|
---|
790 |
|
---|
791 | /**
|
---|
792 | * Create a new pseudo class condition
|
---|
793 | * @param id The id of the pseudo class
|
---|
794 | * @param not <code>true</code> to invert the condition
|
---|
795 | * @param context The context the class is found in.
|
---|
796 | * @return The new condition
|
---|
797 | */
|
---|
798 | public static PseudoClassCondition createPseudoClassCondition(String id, boolean not, Context context) {
|
---|
799 | CheckParameterUtil.ensureThat(!"sameTags".equals(id) || Context.LINK.equals(context), "sameTags only supported in LINK context");
|
---|
800 | if ("open_end".equals(id)) {
|
---|
801 | return new OpenEndPseudoClassCondition(not);
|
---|
802 | }
|
---|
803 | final Method method = getMethod(id);
|
---|
804 | if (method != null) {
|
---|
805 | return new PseudoClassCondition(method, not);
|
---|
806 | }
|
---|
807 | throw new MapCSSException("Invalid pseudo class specified: " + id);
|
---|
808 | }
|
---|
809 |
|
---|
810 | protected static Method getMethod(String id) {
|
---|
811 | String cleanId = id.replaceAll("-|_", "");
|
---|
812 | for (Method method : PseudoClasses.class.getDeclaredMethods()) {
|
---|
813 | // for backwards compatibility, consider :sameTags == :same-tags == :same_tags (#11150)
|
---|
814 | final String methodName = method.getName().replaceAll("-|_", "");
|
---|
815 | if (methodName.equalsIgnoreCase(cleanId)) {
|
---|
816 | return method;
|
---|
817 | }
|
---|
818 | }
|
---|
819 | return null;
|
---|
820 | }
|
---|
821 |
|
---|
822 | @Override
|
---|
823 | public boolean applies(Environment e) {
|
---|
824 | try {
|
---|
825 | return not ^ (Boolean) method.invoke(null, e);
|
---|
826 | } catch (IllegalAccessException | InvocationTargetException ex) {
|
---|
827 | throw new JosmRuntimeException(ex);
|
---|
828 | }
|
---|
829 | }
|
---|
830 |
|
---|
831 | @Override
|
---|
832 | public String toString() {
|
---|
833 | return (not ? "!" : "") + ':' + method.getName();
|
---|
834 | }
|
---|
835 | }
|
---|
836 |
|
---|
837 | /**
|
---|
838 | * Open end pseudo class condition.
|
---|
839 | */
|
---|
840 | public static class OpenEndPseudoClassCondition extends PseudoClassCondition {
|
---|
841 | /**
|
---|
842 | * Constructs a new {@code OpenEndPseudoClassCondition}.
|
---|
843 | * @param not negation or not
|
---|
844 | */
|
---|
845 | public OpenEndPseudoClassCondition(boolean not) {
|
---|
846 | super(null, not);
|
---|
847 | }
|
---|
848 |
|
---|
849 | @Override
|
---|
850 | public boolean applies(Environment e) {
|
---|
851 | return true;
|
---|
852 | }
|
---|
853 | }
|
---|
854 |
|
---|
855 | /**
|
---|
856 | * A condition that is fulfilled whenever the expression is evaluated to be true.
|
---|
857 | */
|
---|
858 | public static class ExpressionCondition implements Condition {
|
---|
859 |
|
---|
860 | final Expression e;
|
---|
861 |
|
---|
862 | /**
|
---|
863 | * Constructs a new {@code ExpressionFactory}
|
---|
864 | * @param e expression
|
---|
865 | */
|
---|
866 | public ExpressionCondition(Expression e) {
|
---|
867 | this.e = e;
|
---|
868 | }
|
---|
869 |
|
---|
870 | @Override
|
---|
871 | public boolean applies(Environment env) {
|
---|
872 | Boolean b = Cascade.convertTo(e.evaluate(env), Boolean.class);
|
---|
873 | return b != null && b;
|
---|
874 | }
|
---|
875 |
|
---|
876 | @Override
|
---|
877 | public String toString() {
|
---|
878 | return '[' + e.toString() + ']';
|
---|
879 | }
|
---|
880 | }
|
---|
881 | }
|
---|