1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint.mapcss;
|
---|
3 |
|
---|
4 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
5 | import org.openstreetmap.josm.data.osm.Way;
|
---|
6 | import org.openstreetmap.josm.tools.Utils;
|
---|
7 |
|
---|
8 | abstract public class Condition {
|
---|
9 |
|
---|
10 | abstract public boolean applies(Environment e);
|
---|
11 |
|
---|
12 | public static enum Op {EQ, NEQ}
|
---|
13 |
|
---|
14 | public static class KeyValueCondition extends Condition {
|
---|
15 |
|
---|
16 | public String k;
|
---|
17 | public String v;
|
---|
18 | public Op op;
|
---|
19 |
|
---|
20 | public KeyValueCondition(String k, String v, Op op) {
|
---|
21 |
|
---|
22 | this.k = k;
|
---|
23 | this.v = v;
|
---|
24 | this.op = op;
|
---|
25 | }
|
---|
26 |
|
---|
27 | @Override
|
---|
28 | public boolean applies(Environment e) {
|
---|
29 | switch (op) {
|
---|
30 | case EQ:
|
---|
31 | return Utils.equal(e.osm.get(k), v);
|
---|
32 | case NEQ:
|
---|
33 | return !Utils.equal(e.osm.get(k), v);
|
---|
34 | default:
|
---|
35 | throw new AssertionError();
|
---|
36 | }
|
---|
37 | }
|
---|
38 |
|
---|
39 | @Override
|
---|
40 | public String toString() {
|
---|
41 | return "[" + k + (op == Op.EQ ? "=" : "!=") + v + "]";
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | public static class KeyCondition extends Condition {
|
---|
46 |
|
---|
47 | private String k;
|
---|
48 | private boolean not;
|
---|
49 |
|
---|
50 | public KeyCondition(String k, boolean not) {
|
---|
51 | this.k = k;
|
---|
52 | this.not = not;
|
---|
53 | }
|
---|
54 |
|
---|
55 | @Override
|
---|
56 | public boolean applies(Environment e) {
|
---|
57 | return e.osm.hasKey(k) ^ not;
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Override
|
---|
61 | public String toString() {
|
---|
62 | return "[" + (not ? "!" : "") + k + "]";
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | public static class PseudoClassCondition extends Condition {
|
---|
67 |
|
---|
68 | String id;
|
---|
69 | boolean not;
|
---|
70 |
|
---|
71 | public PseudoClassCondition(String id, boolean not) {
|
---|
72 | this.id = id;
|
---|
73 | this.not = not;
|
---|
74 | }
|
---|
75 |
|
---|
76 | @Override
|
---|
77 | public boolean applies(Environment e) {
|
---|
78 | if ("closed".equals(id)) {
|
---|
79 | if (e.osm instanceof Way && ((Way) e.osm).isClosed())
|
---|
80 | return true;
|
---|
81 | if (e.osm instanceof Relation && ((Relation) e.osm).isMultipolygon())
|
---|
82 | return true;
|
---|
83 | return false;
|
---|
84 | }
|
---|
85 | return true;
|
---|
86 | }
|
---|
87 |
|
---|
88 | @Override
|
---|
89 | public String toString() {
|
---|
90 | return ":" + (not ? "!" : "") + id;
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | public static class ExpressionCondition extends Condition {
|
---|
95 |
|
---|
96 | private Expression e;
|
---|
97 |
|
---|
98 | public ExpressionCondition(Expression e) {
|
---|
99 | this.e = e;
|
---|
100 | }
|
---|
101 |
|
---|
102 | @Override
|
---|
103 | public boolean applies(Environment env) {
|
---|
104 | Object o = e.evaluate(env);
|
---|
105 | if (o instanceof Boolean)
|
---|
106 | return (Boolean) o;
|
---|
107 | return false;
|
---|
108 | }
|
---|
109 |
|
---|
110 | @Override
|
---|
111 | public String toString() {
|
---|
112 | return "[" + e + "]";
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | }
|
---|