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.util.EnumSet;
|
---|
7 | import java.util.regex.Matcher;
|
---|
8 | import java.util.regex.Pattern;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.data.osm.Node;
|
---|
11 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
12 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
13 | import org.openstreetmap.josm.data.osm.Way;
|
---|
14 | import org.openstreetmap.josm.gui.mappaint.Cascade;
|
---|
15 | import org.openstreetmap.josm.gui.mappaint.Environment;
|
---|
16 | import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Op;
|
---|
17 |
|
---|
18 | abstract public class Condition {
|
---|
19 |
|
---|
20 | abstract public boolean applies(Environment e);
|
---|
21 |
|
---|
22 | public static enum Op { EQ, NEQ, GREATER_OR_EQUAL, GREATER, LESS_OR_EQUAL, LESS,
|
---|
23 | REGEX, ONE_OF, BEGINS_WITH, ENDS_WITH, CONTAINS }
|
---|
24 |
|
---|
25 | public final static EnumSet<Op> COMPARISON_OPERATERS =
|
---|
26 | EnumSet.of(Op.GREATER_OR_EQUAL, Op.GREATER, Op.LESS_OR_EQUAL, Op.LESS);
|
---|
27 |
|
---|
28 | public static class KeyValueCondition extends Condition {
|
---|
29 |
|
---|
30 | public String k;
|
---|
31 | public String v;
|
---|
32 | public Op op;
|
---|
33 | private float v_float;
|
---|
34 |
|
---|
35 | public KeyValueCondition(String k, String v, Op op) {
|
---|
36 | this.k = k;
|
---|
37 | this.v = v;
|
---|
38 | this.op = op;
|
---|
39 | if (COMPARISON_OPERATERS.contains(op)) {
|
---|
40 | v_float = Float.parseFloat(v);
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | @Override
|
---|
45 | public boolean applies(Environment env) {
|
---|
46 | String val = env.osm.get(k);
|
---|
47 | if (val == null && op != Op.NEQ)
|
---|
48 | return false;
|
---|
49 | switch (op) {
|
---|
50 | case EQ:
|
---|
51 | return equal(val, v);
|
---|
52 | case NEQ:
|
---|
53 | return !equal(val, v);
|
---|
54 | case REGEX:
|
---|
55 | Pattern p = Pattern.compile(v);
|
---|
56 | Matcher m = p.matcher(val);
|
---|
57 | return m.find();
|
---|
58 | case ONE_OF:
|
---|
59 | String[] parts = val.split(";");
|
---|
60 | for (String part : parts) {
|
---|
61 | if (equal(v, part.trim()))
|
---|
62 | return true;
|
---|
63 | }
|
---|
64 | return false;
|
---|
65 | case BEGINS_WITH:
|
---|
66 | return val.startsWith(v);
|
---|
67 | case ENDS_WITH:
|
---|
68 | return val.endsWith(v);
|
---|
69 | case CONTAINS:
|
---|
70 | return val.contains(v);
|
---|
71 | }
|
---|
72 | float val_float;
|
---|
73 | try {
|
---|
74 | val_float = Float.parseFloat(val);
|
---|
75 | } catch (NumberFormatException e) {
|
---|
76 | return false;
|
---|
77 | }
|
---|
78 | switch (op) {
|
---|
79 | case GREATER_OR_EQUAL:
|
---|
80 | return val_float >= v_float;
|
---|
81 | case GREATER:
|
---|
82 | return val_float > v_float;
|
---|
83 | case LESS_OR_EQUAL:
|
---|
84 | return val_float <= v_float;
|
---|
85 | case LESS:
|
---|
86 | return val_float < v_float;
|
---|
87 | default:
|
---|
88 | throw new AssertionError();
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | @Override
|
---|
93 | public String toString() {
|
---|
94 | return "[" + k + "'" + op + "'" + v + "]";
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | public static class KeyCondition extends Condition {
|
---|
99 |
|
---|
100 | private String k;
|
---|
101 | private boolean not;
|
---|
102 | private boolean yes;
|
---|
103 |
|
---|
104 | public KeyCondition(String k, boolean not, boolean yes) {
|
---|
105 | this.k = k;
|
---|
106 | this.not = not;
|
---|
107 | this.yes = yes;
|
---|
108 | }
|
---|
109 |
|
---|
110 | @Override
|
---|
111 | public boolean applies(Environment e) {
|
---|
112 | if (yes)
|
---|
113 | return OsmUtils.isTrue(e.osm.get(k)) ^ not;
|
---|
114 | else
|
---|
115 | return e.osm.hasKey(k) ^ not;
|
---|
116 | }
|
---|
117 |
|
---|
118 | @Override
|
---|
119 | public String toString() {
|
---|
120 | return "[" + (not ? "!" : "") + k + "]";
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | public static class PseudoClassCondition extends Condition {
|
---|
125 |
|
---|
126 | String id;
|
---|
127 | boolean not;
|
---|
128 |
|
---|
129 | public PseudoClassCondition(String id, boolean not) {
|
---|
130 | this.id = id;
|
---|
131 | this.not = not;
|
---|
132 | }
|
---|
133 |
|
---|
134 | @Override
|
---|
135 | public boolean applies(Environment e) {
|
---|
136 | return not ^ appliesImpl(e);
|
---|
137 | }
|
---|
138 |
|
---|
139 | public boolean appliesImpl(Environment e) {
|
---|
140 | if (equal(id, "closed")) {
|
---|
141 | if (e.osm instanceof Way && ((Way) e.osm).isClosed())
|
---|
142 | return true;
|
---|
143 | if (e.osm instanceof Relation && ((Relation) e.osm).isMultipolygon())
|
---|
144 | return true;
|
---|
145 | return false;
|
---|
146 | } else if (equal(id, "modified")) {
|
---|
147 | return e.osm.isModified() || e.osm.isNewOrUndeleted();
|
---|
148 | } else if (equal(id, "new")) {
|
---|
149 | return e.osm.isNew();
|
---|
150 | } else if (equal(id, "connection") && (e.osm instanceof Node)) {
|
---|
151 | return ((Node) e.osm).isConnectionNode();
|
---|
152 | } else if (equal(id, "tagged")) {
|
---|
153 | return e.osm.isTagged();
|
---|
154 | }
|
---|
155 | return true;
|
---|
156 | }
|
---|
157 |
|
---|
158 | @Override
|
---|
159 | public String toString() {
|
---|
160 | return ":" + (not ? "!" : "") + id;
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | public static class ExpressionCondition extends Condition {
|
---|
165 |
|
---|
166 | private Expression e;
|
---|
167 |
|
---|
168 | public ExpressionCondition(Expression e) {
|
---|
169 | this.e = e;
|
---|
170 | }
|
---|
171 |
|
---|
172 | @Override
|
---|
173 | public boolean applies(Environment env) {
|
---|
174 | Boolean b = Cascade.convertTo(e.evaluate(env), Boolean.class);
|
---|
175 | return b != null && b;
|
---|
176 | }
|
---|
177 |
|
---|
178 | @Override
|
---|
179 | public String toString() {
|
---|
180 | return "[" + e + "]";
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | }
|
---|