1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint.mapcss;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.Utils.equal;
|
---|
5 |
|
---|
6 | import java.text.MessageFormat;
|
---|
7 | import java.util.EnumSet;
|
---|
8 | import java.util.regex.Pattern;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.data.osm.Node;
|
---|
11 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
12 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
13 | import org.openstreetmap.josm.data.osm.Tag;
|
---|
14 | import org.openstreetmap.josm.data.osm.Way;
|
---|
15 | import org.openstreetmap.josm.gui.mappaint.Cascade;
|
---|
16 | import org.openstreetmap.josm.gui.mappaint.ElemStyles;
|
---|
17 | import org.openstreetmap.josm.gui.mappaint.Environment;
|
---|
18 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
19 | import org.openstreetmap.josm.tools.Predicate;
|
---|
20 | import org.openstreetmap.josm.tools.Predicates;
|
---|
21 | import org.openstreetmap.josm.tools.Utils;
|
---|
22 |
|
---|
23 | abstract public class Condition {
|
---|
24 |
|
---|
25 | abstract public boolean applies(Environment e);
|
---|
26 |
|
---|
27 | public static Condition createKeyValueCondition(String k, String v, Op op, Context context, boolean considerValAsKey) {
|
---|
28 | switch (context) {
|
---|
29 | case PRIMITIVE:
|
---|
30 | return KeyValueRegexpCondition.SUPPORTED_OPS.contains(op) && !considerValAsKey
|
---|
31 | ? new KeyValueRegexpCondition(k, v, op, false)
|
---|
32 | : new KeyValueCondition(k, v, op, considerValAsKey);
|
---|
33 | case LINK:
|
---|
34 | if (considerValAsKey)
|
---|
35 | throw new MapCSSException("''considerValAsKey'' not supported in LINK context");
|
---|
36 | if ("role".equalsIgnoreCase(k))
|
---|
37 | return new RoleCondition(v, op);
|
---|
38 | else if ("index".equalsIgnoreCase(k))
|
---|
39 | return new IndexCondition(v, op);
|
---|
40 | else
|
---|
41 | throw new MapCSSException(
|
---|
42 | MessageFormat.format("Expected key ''role'' or ''index'' in link context. Got ''{0}''.", k));
|
---|
43 |
|
---|
44 | default: throw new AssertionError();
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | public static Condition createKeyCondition(String k, boolean not, KeyMatchType matchType, Context context) {
|
---|
49 | switch (context) {
|
---|
50 | case PRIMITIVE:
|
---|
51 | return new KeyCondition(k, not, matchType);
|
---|
52 | case LINK:
|
---|
53 | if (matchType != null)
|
---|
54 | throw new MapCSSException("Question mark operator ''?'' and regexp match not supported in LINK context");
|
---|
55 | if (not)
|
---|
56 | return new RoleCondition(k, Op.NEQ);
|
---|
57 | else
|
---|
58 | return new RoleCondition(k, Op.EQ);
|
---|
59 |
|
---|
60 | default: throw new AssertionError();
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | public static Condition createPseudoClassCondition(String id, boolean not, Context context) {
|
---|
65 | return new PseudoClassCondition(id, not, context);
|
---|
66 | }
|
---|
67 |
|
---|
68 | public static Condition createClassCondition(String id, boolean not, Context context) {
|
---|
69 | return new ClassCondition(id, not);
|
---|
70 | }
|
---|
71 |
|
---|
72 | public static Condition createExpressionCondition(Expression e, Context context) {
|
---|
73 | return new ExpressionCondition(e);
|
---|
74 | }
|
---|
75 |
|
---|
76 | public static enum Op {
|
---|
77 | EQ, NEQ, GREATER_OR_EQUAL, GREATER, LESS_OR_EQUAL, LESS,
|
---|
78 | REGEX, NREGEX, ONE_OF, BEGINS_WITH, ENDS_WITH, CONTAINS;
|
---|
79 |
|
---|
80 | public boolean eval(String testString, String prototypeString) {
|
---|
81 | if (testString == null && this != NEQ)
|
---|
82 | return false;
|
---|
83 | switch (this) {
|
---|
84 | case EQ:
|
---|
85 | return equal(testString, prototypeString);
|
---|
86 | case NEQ:
|
---|
87 | return !equal(testString, prototypeString);
|
---|
88 | case REGEX:
|
---|
89 | case NREGEX:
|
---|
90 | final boolean contains = Pattern.compile(prototypeString).matcher(testString).find();
|
---|
91 | return REGEX.equals(this) ? contains : !contains;
|
---|
92 | case ONE_OF:
|
---|
93 | String[] parts = testString.split(";");
|
---|
94 | for (String part : parts) {
|
---|
95 | if (equal(prototypeString, part.trim()))
|
---|
96 | return true;
|
---|
97 | }
|
---|
98 | return false;
|
---|
99 | case BEGINS_WITH:
|
---|
100 | return testString.startsWith(prototypeString);
|
---|
101 | case ENDS_WITH:
|
---|
102 | return testString.endsWith(prototypeString);
|
---|
103 | case CONTAINS:
|
---|
104 | return testString.contains(prototypeString);
|
---|
105 | }
|
---|
106 |
|
---|
107 | float test_float;
|
---|
108 | try {
|
---|
109 | test_float = Float.parseFloat(testString);
|
---|
110 | } catch (NumberFormatException e) {
|
---|
111 | return false;
|
---|
112 | }
|
---|
113 | float prototype_float = Float.parseFloat(prototypeString);
|
---|
114 |
|
---|
115 | switch (this) {
|
---|
116 | case GREATER_OR_EQUAL:
|
---|
117 | return test_float >= prototype_float;
|
---|
118 | case GREATER:
|
---|
119 | return test_float > prototype_float;
|
---|
120 | case LESS_OR_EQUAL:
|
---|
121 | return test_float <= prototype_float;
|
---|
122 | case LESS:
|
---|
123 | return test_float < prototype_float;
|
---|
124 | default:
|
---|
125 | throw new AssertionError();
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * context, where the condition applies
|
---|
132 | */
|
---|
133 | public static enum Context {
|
---|
134 | /**
|
---|
135 | * normal primitive selector, e.g. way[highway=residential]
|
---|
136 | */
|
---|
137 | PRIMITIVE,
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * link between primitives, e.g. relation >[role=outer] way
|
---|
141 | */
|
---|
142 | LINK
|
---|
143 | }
|
---|
144 |
|
---|
145 | public final static EnumSet<Op> COMPARISON_OPERATERS =
|
---|
146 | EnumSet.of(Op.GREATER_OR_EQUAL, Op.GREATER, Op.LESS_OR_EQUAL, Op.LESS);
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * <p>Represents a key/value condition which is either applied to a primitive.</p>
|
---|
150 | *
|
---|
151 | */
|
---|
152 | public static class KeyValueCondition extends Condition {
|
---|
153 |
|
---|
154 | public final String k;
|
---|
155 | public final String v;
|
---|
156 | public final Op op;
|
---|
157 | public boolean considerValAsKey;
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * <p>Creates a key/value-condition.</p>
|
---|
161 | *
|
---|
162 | * @param k the key
|
---|
163 | * @param v the value
|
---|
164 | * @param op the operation
|
---|
165 | * @param considerValAsKey whether to consider {@code v} as another key and compare the values of key {@code k} and key {@code v}.
|
---|
166 | */
|
---|
167 | public KeyValueCondition(String k, String v, Op op, boolean considerValAsKey) {
|
---|
168 | this.k = k;
|
---|
169 | this.v = v;
|
---|
170 | this.op = op;
|
---|
171 | this.considerValAsKey = considerValAsKey;
|
---|
172 | }
|
---|
173 |
|
---|
174 | @Override
|
---|
175 | public boolean applies(Environment env) {
|
---|
176 | return op.eval(env.osm.get(k), considerValAsKey ? env.osm.get(v) : v);
|
---|
177 | }
|
---|
178 |
|
---|
179 | public Tag asTag() {
|
---|
180 | return new Tag(k, v);
|
---|
181 | }
|
---|
182 |
|
---|
183 | @Override
|
---|
184 | public String toString() {
|
---|
185 | return "[" + k + "'" + op + "'" + v + "]";
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | public static class KeyValueRegexpCondition extends KeyValueCondition {
|
---|
190 |
|
---|
191 | public final Pattern pattern;
|
---|
192 | public static final EnumSet<Op> SUPPORTED_OPS = EnumSet.of(Op.REGEX, Op.NREGEX);
|
---|
193 |
|
---|
194 | public KeyValueRegexpCondition(String k, String v, Op op, boolean considerValAsKey) {
|
---|
195 | super(k, v, op, considerValAsKey);
|
---|
196 | CheckParameterUtil.ensureThat(!considerValAsKey, "considerValAsKey is not supported");
|
---|
197 | CheckParameterUtil.ensureThat(SUPPORTED_OPS.contains(op), "Op must be REGEX or NREGEX");
|
---|
198 | this.pattern = Pattern.compile(v);
|
---|
199 | }
|
---|
200 |
|
---|
201 | @Override
|
---|
202 | public boolean applies(Environment env) {
|
---|
203 | final String value = env.osm.get(k);
|
---|
204 | return value != null && (op.equals(Op.REGEX)
|
---|
205 | ? pattern.matcher(value).find()
|
---|
206 | : !pattern.matcher(value).find());
|
---|
207 | }
|
---|
208 | }
|
---|
209 |
|
---|
210 | public static class RoleCondition extends Condition {
|
---|
211 | public final String role;
|
---|
212 | public final Op op;
|
---|
213 |
|
---|
214 | public RoleCondition(String role, Op op) {
|
---|
215 | this.role = role;
|
---|
216 | this.op = op;
|
---|
217 | }
|
---|
218 |
|
---|
219 | @Override
|
---|
220 | public boolean applies(Environment env) {
|
---|
221 | String testRole = env.getRole();
|
---|
222 | if (testRole == null) return false;
|
---|
223 | return op.eval(testRole, role);
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | public static class IndexCondition extends Condition {
|
---|
228 | public final String index;
|
---|
229 | public final Op op;
|
---|
230 |
|
---|
231 | public IndexCondition(String index, Op op) {
|
---|
232 | this.index = index;
|
---|
233 | this.op = op;
|
---|
234 | }
|
---|
235 |
|
---|
236 | @Override
|
---|
237 | public boolean applies(Environment env) {
|
---|
238 | if (env.index == null) return false;
|
---|
239 | return op.eval(Integer.toString(env.index + 1), index);
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | public static enum KeyMatchType {
|
---|
244 | EQ, TRUE, FALSE, REGEX
|
---|
245 | }
|
---|
246 |
|
---|
247 | /**
|
---|
248 | * <p>KeyCondition represent one of the following conditions in either the link or the
|
---|
249 | * primitive context:</p>
|
---|
250 | * <pre>
|
---|
251 | * ["a label"] PRIMITIVE: the primitive has a tag "a label"
|
---|
252 | * LINK: the parent is a relation and it has at least one member with the role
|
---|
253 | * "a label" referring to the child
|
---|
254 | *
|
---|
255 | * [!"a label"] PRIMITIVE: the primitive doesn't have a tag "a label"
|
---|
256 | * LINK: the parent is a relation but doesn't have a member with the role
|
---|
257 | * "a label" referring to the child
|
---|
258 | *
|
---|
259 | * ["a label"?] PRIMITIVE: the primitive has a tag "a label" whose value evaluates to a true-value
|
---|
260 | * LINK: not supported
|
---|
261 | *
|
---|
262 | * ["a label"?!] PRIMITIVE: the primitive has a tag "a label" whose value evaluates to a false-value
|
---|
263 | * LINK: not supported
|
---|
264 | * </pre>
|
---|
265 | */
|
---|
266 | public static class KeyCondition extends Condition {
|
---|
267 |
|
---|
268 | public final String label;
|
---|
269 | public final boolean negateResult;
|
---|
270 | public final KeyMatchType matchType;
|
---|
271 | public Predicate<String> containsPattern;
|
---|
272 |
|
---|
273 | public KeyCondition(String label, boolean negateResult, KeyMatchType matchType){
|
---|
274 | this.label = label;
|
---|
275 | this.negateResult = negateResult;
|
---|
276 | this.matchType = matchType;
|
---|
277 | this.containsPattern = KeyMatchType.REGEX.equals(matchType)
|
---|
278 | ? Predicates.stringContainsPattern(Pattern.compile(label))
|
---|
279 | : null;
|
---|
280 | }
|
---|
281 |
|
---|
282 | @Override
|
---|
283 | public boolean applies(Environment e) {
|
---|
284 | switch(e.getContext()) {
|
---|
285 | case PRIMITIVE:
|
---|
286 | if (KeyMatchType.TRUE.equals(matchType))
|
---|
287 | return e.osm.isKeyTrue(label) ^ negateResult;
|
---|
288 | else if (KeyMatchType.FALSE.equals(matchType))
|
---|
289 | return e.osm.isKeyFalse(label) ^ negateResult;
|
---|
290 | else if (KeyMatchType.REGEX.equals(matchType)) {
|
---|
291 | return Utils.exists(e.osm.keySet(), containsPattern) ^ negateResult;
|
---|
292 | } else {
|
---|
293 | return e.osm.hasKey(label) ^ negateResult;
|
---|
294 | }
|
---|
295 | case LINK:
|
---|
296 | Utils.ensure(false, "Illegal state: KeyCondition not supported in LINK context");
|
---|
297 | return false;
|
---|
298 | default: throw new AssertionError();
|
---|
299 | }
|
---|
300 | }
|
---|
301 |
|
---|
302 | public Tag asTag() {
|
---|
303 | return new Tag(label);
|
---|
304 | }
|
---|
305 |
|
---|
306 | @Override
|
---|
307 | public String toString() {
|
---|
308 | return "[" + (negateResult ? "!" : "") + label + "]";
|
---|
309 | }
|
---|
310 | }
|
---|
311 |
|
---|
312 | public static class ClassCondition extends Condition {
|
---|
313 |
|
---|
314 | public final String id;
|
---|
315 | public final boolean not;
|
---|
316 |
|
---|
317 | public ClassCondition(String id, boolean not) {
|
---|
318 | this.id = id;
|
---|
319 | this.not = not;
|
---|
320 | }
|
---|
321 |
|
---|
322 | @Override
|
---|
323 | public boolean applies(Environment env) {
|
---|
324 | return env != null && env.getCascade(env.layer) != null && not ^ env.getCascade(env.layer).containsKey(id);
|
---|
325 | }
|
---|
326 |
|
---|
327 | @Override
|
---|
328 | public String toString() {
|
---|
329 | return (not ? "!" : "") + "." + id;
|
---|
330 | }
|
---|
331 | }
|
---|
332 |
|
---|
333 | public static class PseudoClassCondition extends Condition {
|
---|
334 |
|
---|
335 | public final String id;
|
---|
336 | public final boolean not;
|
---|
337 |
|
---|
338 | public PseudoClassCondition(String id, boolean not, Context context) {
|
---|
339 | this.id = id;
|
---|
340 | this.not = not;
|
---|
341 | CheckParameterUtil.ensureThat(!"sameTags".equals(id) || Context.LINK.equals(context), "sameTags only supported in LINK context");
|
---|
342 | }
|
---|
343 |
|
---|
344 | @Override
|
---|
345 | public boolean applies(Environment e) {
|
---|
346 | return not ^ appliesImpl(e);
|
---|
347 | }
|
---|
348 |
|
---|
349 | public boolean appliesImpl(Environment e) {
|
---|
350 | if ("closed".equals(id)) {
|
---|
351 | if (e.osm instanceof Way && ((Way) e.osm).isClosed())
|
---|
352 | return true;
|
---|
353 | if (e.osm instanceof Relation && ((Relation) e.osm).isMultipolygon())
|
---|
354 | return true;
|
---|
355 | return false;
|
---|
356 | } else if ("modified".equals(id)) {
|
---|
357 | return e.osm.isModified() || e.osm.isNewOrUndeleted();
|
---|
358 | } else if ("new".equals(id)) {
|
---|
359 | return e.osm.isNew();
|
---|
360 | } else if ("connection".equals(id) && (e.osm instanceof Node)) {
|
---|
361 | return ((Node) e.osm).isConnectionNode();
|
---|
362 | } else if ("tagged".equals(id)) {
|
---|
363 | return e.osm.isTagged();
|
---|
364 | } else if ("sameTags".equals(id)) {
|
---|
365 | return e.osm.hasSameInterestingTags(Utils.firstNonNull(e.child, e.parent));
|
---|
366 | } else if ("areaStyle".equals(id)) {
|
---|
367 | return ElemStyles.hasAreaElemStyle(e.osm, false);
|
---|
368 | } else if ("unconnected".equals(id) && (e.osm instanceof Node)) {
|
---|
369 | return OsmPrimitive.getFilteredList(e.osm.getReferrers(), Way.class).isEmpty();
|
---|
370 | }
|
---|
371 | return true;
|
---|
372 | }
|
---|
373 |
|
---|
374 | @Override
|
---|
375 | public String toString() {
|
---|
376 | return ":" + (not ? "!" : "") + id;
|
---|
377 | }
|
---|
378 | }
|
---|
379 |
|
---|
380 | public static class ExpressionCondition extends Condition {
|
---|
381 |
|
---|
382 | private Expression e;
|
---|
383 |
|
---|
384 | public ExpressionCondition(Expression e) {
|
---|
385 | this.e = e;
|
---|
386 | }
|
---|
387 |
|
---|
388 | @Override
|
---|
389 | public boolean applies(Environment env) {
|
---|
390 | Boolean b = Cascade.convertTo(e.evaluate(env), Boolean.class);
|
---|
391 | return b != null && b;
|
---|
392 | }
|
---|
393 |
|
---|
394 | @Override
|
---|
395 | public String toString() {
|
---|
396 | return "[" + e + "]";
|
---|
397 | }
|
---|
398 | }
|
---|
399 | }
|
---|