Changeset 8874 in josm


Ignore:
Timestamp:
2015-10-14T22:56:14+02:00 (9 years ago)
Author:
simon04
Message:

fix #10467 - MapCSS: allow comparisons in regexp key conditions

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Condition.java

    r8846 r8874  
    88import java.util.Collection;
    99import java.util.EnumSet;
     10import java.util.Map;
    1011import java.util.Objects;
    1112import java.util.Set;
     
    5455        default: throw new AssertionError();
    5556        }
     57    }
     58
     59    public static Condition createRegexpKeyRegexpValueCondition(String k, String v, Op op) {
     60        return new RegexpKeyValueRegexpCondition(k, v, op);
    5661    }
    5762
     
    287292        }
    288293
     294        protected boolean matches(Environment env) {
     295            final String value = env.osm.get(k);
     296            return value != null && pattern.matcher(value).find();
     297        }
     298
    289299        @Override
    290300        public boolean applies(Environment env) {
    291             final String value = env.osm.get(k);
    292301            if (Op.REGEX.equals(op)) {
    293                 return value != null && pattern.matcher(value).find();
     302                return matches(env);
    294303            } else if (Op.NREGEX.equals(op)) {
    295                 return value == null || !pattern.matcher(value).find();
     304                return !matches(env);
    296305            } else {
    297306                throw new IllegalStateException();
    298307            }
     308        }
     309    }
     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;
    299328        }
    300329    }
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSParser.jj

    r8822 r8874  
    755755    float f;
    756756    int i;
     757    Condition.KeyMatchType matchType = null;;
    757758    Condition.Op op;
    758759    boolean considerValAsKey = false;
    759760}
    760761{
    761     key=tag_key() s()
     762    (
     763        key = regex() s() { matchType = Condition.KeyMatchType.REGEX; }
     764    |
     765        key=tag_key() s()
     766    )
    762767    (
    763768        LOOKAHEAD(3)
     
    807812            f=float_() { val=Float.toString(f); }
    808813    )
    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); }
    810817}
    811818
  • trunk/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/KeyValueConditionTest.groovy

    r8415 r8874  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.gui.mappaint.mapcss;
    3 
    4 import static org.junit.Assert.*
    53
    64import org.junit.*
     
    97import org.openstreetmap.josm.data.osm.DataSet
    108import org.openstreetmap.josm.data.osm.Node
     9import org.openstreetmap.josm.data.osm.OsmUtils
    1110import org.openstreetmap.josm.data.osm.Relation
    1211import org.openstreetmap.josm.data.osm.RelationMember
     
    1413import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Context
    1514import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Op
     15import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.MapCSSParser
    1616
    1717
     
    8686        assert cond.applies(e)
    8787    }
     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    }
    88103}
Note: See TracChangeset for help on using the changeset viewer.