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