diff --git a/src/org/openstreetmap/josm/gui/mappaint/mapcss/Condition.java b/src/org/openstreetmap/josm/gui/mappaint/mapcss/Condition.java
index 56b5bba..ffbe0af 100644
a
|
b
|
|
7 | 7 | import java.util.Arrays; |
8 | 8 | import java.util.Collection; |
9 | 9 | import java.util.EnumSet; |
| 10 | import java.util.Map; |
10 | 11 | import java.util.Objects; |
11 | 12 | import java.util.Set; |
12 | 13 | import java.util.regex.Pattern; |
… |
… |
else if ("index".equalsIgnoreCase(k))
|
55 | 56 | } |
56 | 57 | } |
57 | 58 | |
| 59 | public static Condition createRegexpKeyRegexpValueCondition(String k, String v, Op op) { |
| 60 | return new RegexpKeyValueRegexpCondition(k, v, op); |
| 61 | } |
| 62 | |
58 | 63 | public static Condition createKeyCondition(String k, boolean not, KeyMatchType matchType, Context context) { |
59 | 64 | switch (context) { |
60 | 65 | case PRIMITIVE: |
… |
… |
public KeyValueRegexpCondition(String k, String v, Op op, boolean considerValAsK
|
286 | 291 | this.pattern = Pattern.compile(v); |
287 | 292 | } |
288 | 293 | |
289 | | @Override |
290 | | public boolean applies(Environment env) { |
| 294 | protected boolean matches(Environment env) { |
291 | 295 | final String value = env.osm.get(k); |
| 296 | return value != null && pattern.matcher(value).find(); |
| 297 | } |
| 298 | |
| 299 | @Override |
| 300 | public boolean applies(Environment env) { |
292 | 301 | if (Op.REGEX.equals(op)) { |
293 | | return value != null && pattern.matcher(value).find(); |
| 302 | return matches(env); |
294 | 303 | } else if (Op.NREGEX.equals(op)) { |
295 | | return value == null || !pattern.matcher(value).find(); |
| 304 | return !matches(env); |
296 | 305 | } else { |
297 | 306 | throw new IllegalStateException(); |
298 | 307 | } |
299 | 308 | } |
300 | 309 | } |
301 | 310 | |
| 311 | public static class RegexpKeyValueRegexpCondition extends KeyValueRegexpCondition { |
| 312 | |
| 313 | public final Pattern keyPattern; |
| 314 | |
| 315 | public RegexpKeyValueRegexpCondition(String k, String v, Op op) { |
| 316 | super(k, v, op, false); |
| 317 | this.keyPattern = Pattern.compile(k); |
| 318 | } |
| 319 | |
| 320 | @Override |
| 321 | protected boolean matches(Environment env) { |
| 322 | for (Map.Entry<String,String> kv: env.osm.getKeys().entrySet()) { |
| 323 | if (keyPattern.matcher(kv.getKey()).find() && pattern.matcher(kv.getValue()).find()) { |
| 324 | return true; |
| 325 | } |
| 326 | } |
| 327 | return false; |
| 328 | } |
| 329 | } |
| 330 | |
302 | 331 | public static class RoleCondition extends Condition { |
303 | 332 | public final String role; |
304 | 333 | public final Op op; |
diff --git a/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSParser.jj b/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSParser.jj
index d9e4f01..3bd4370 100644
a
|
b
|
Condition simple_key_value_condition(Context context) :
|
754 | 754 | String val; |
755 | 755 | float f; |
756 | 756 | int i; |
| 757 | Condition.KeyMatchType matchType = null;; |
757 | 758 | Condition.Op op; |
758 | 759 | boolean considerValAsKey = false; |
759 | 760 | } |
760 | 761 | { |
761 | | key=tag_key() s() |
| 762 | ( |
| 763 | key = regex() s() { matchType = Condition.KeyMatchType.REGEX; } |
| 764 | | |
| 765 | key=tag_key() s() |
| 766 | ) |
762 | 767 | ( |
763 | 768 | LOOKAHEAD(3) |
764 | 769 | ( |
… |
… |
Condition simple_key_value_condition(Context context) :
|
806 | 811 | s() |
807 | 812 | f=float_() { val=Float.toString(f); } |
808 | 813 | ) |
809 | | { return Condition.createKeyValueCondition(key, val, op, context, considerValAsKey); } |
| 814 | { return Condition.KeyMatchType.REGEX == matchType |
| 815 | ? Condition.createRegexpKeyRegexpValueCondition(key, val, op) |
| 816 | : Condition.createKeyValueCondition(key, val, op, context, considerValAsKey); } |
810 | 817 | } |
811 | 818 | |
812 | 819 | Condition class_or_pseudoclass(Context context) : |
diff --git a/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/KeyValueConditionTest.groovy b/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/KeyValueConditionTest.groovy
index d025e94..675a623 100644
a
|
b
|
|
1 | 1 | // License: GPL. For details, see LICENSE file. |
2 | 2 | package org.openstreetmap.josm.gui.mappaint.mapcss; |
3 | 3 | |
4 | | import static org.junit.Assert.* |
5 | | |
6 | 4 | import org.junit.* |
7 | 5 | import org.openstreetmap.josm.JOSMFixture |
8 | 6 | import org.openstreetmap.josm.data.coor.LatLon |
9 | 7 | import org.openstreetmap.josm.data.osm.DataSet |
10 | 8 | import org.openstreetmap.josm.data.osm.Node |
| 9 | import org.openstreetmap.josm.data.osm.OsmUtils |
11 | 10 | import org.openstreetmap.josm.data.osm.Relation |
12 | 11 | import org.openstreetmap.josm.data.osm.RelationMember |
13 | 12 | import org.openstreetmap.josm.gui.mappaint.Environment |
14 | 13 | import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Context |
15 | 14 | import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Op |
| 15 | import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.MapCSSParser |
16 | 16 | |
17 | 17 | |
18 | 18 | class KeyValueConditionTest { |
… |
… |
class KeyValueConditionTest {
|
85 | 85 | cond = Condition.createKeyValueCondition("role", "another_role", Op.NEQ, Context.LINK, false) |
86 | 86 | assert cond.applies(e) |
87 | 87 | } |
| 88 | |
| 89 | @Test |
| 90 | public void testKeyRegexValueRegex() throws Exception { |
| 91 | def selPos = new MapCSSParser(new StringReader("*[/^source/ =~ /.*,.*/]")).selector() |
| 92 | def selNeg = new MapCSSParser(new StringReader("*[/^source/ !~ /.*,.*/]")).selector() |
| 93 | assert !selPos.matches(new Environment(OsmUtils.createPrimitive("way foo=bar"))) |
| 94 | assert selPos.matches(new Environment(OsmUtils.createPrimitive("way source=1,2"))) |
| 95 | assert selPos.matches(new Environment(OsmUtils.createPrimitive("way source_foo_bar=1,2"))) |
| 96 | assert !selPos.matches(new Environment(OsmUtils.createPrimitive("way source=1"))) |
| 97 | assert !selPos.matches(new Environment(OsmUtils.createPrimitive("way source=1"))) |
| 98 | assert !selNeg.matches(new Environment(OsmUtils.createPrimitive("way source=1,2"))) |
| 99 | assert !selNeg.matches(new Environment(OsmUtils.createPrimitive("way foo=bar source=1,2"))) |
| 100 | assert selNeg.matches(new Environment(OsmUtils.createPrimitive("way foo=bar source=baz"))) |
| 101 | assert selNeg.matches(new Environment(OsmUtils.createPrimitive("way foo=bar src=1,2"))) |
| 102 | } |
88 | 103 | } |