Changeset 8266 in josm


Ignore:
Timestamp:
2015-04-25T19:13:12+02:00 (9 years ago)
Author:
simon04
Message:

fix #9782 fix #10859 - MapCSS validator: evaluate real key and value for KeyConditions

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/validation/tests/MapCSSTagChecker.java

    r8265 r8266  
    147147        /**
    148148         * Creates the fixing {@link Command} for the given primitive. The {@code matchingSelector} is used to
    149          * evaluate placeholders (cf. {@link org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker.TagCheck#insertArguments(Selector, String)}).
     149         * evaluate placeholders (cf. {@link org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker.TagCheck#insertArguments(Selector, String, OsmPrimitive)}).
    150150         */
    151151        abstract Command createCommand(final OsmPrimitive p, final Selector matchingSelector);
     
    167167                return null;
    168168            }
    169             return TagCheck.insertArguments(matchingSelector, s);
     169            return TagCheck.insertArguments(matchingSelector, s, p);
    170170        }
    171171
     
    386386         * {@link org.openstreetmap.josm.gui.mappaint.mapcss.Selector.GeneralSelector}.
    387387         */
    388         static String determineArgument(Selector.GeneralSelector matchingSelector, int index, String type) {
     388        static String determineArgument(Selector.GeneralSelector matchingSelector, int index, String type, OsmPrimitive p) {
    389389            try {
    390390                final Condition c = matchingSelector.getConditions().get(index);
    391391                final Tag tag = c instanceof Condition.KeyCondition
    392                         ? ((Condition.KeyCondition) c).asTag()
     392                        ? ((Condition.KeyCondition) c).asTag(p)
    393393                        : c instanceof Condition.SimpleKeyValueCondition
    394394                        ? ((Condition.SimpleKeyValueCondition) c).asTag()
     
    417417         * key/value/tag of the {@code index}-th {@link Condition} of {@code matchingSelector}.
    418418         */
    419         static String insertArguments(Selector matchingSelector, String s) {
     419        static String insertArguments(Selector matchingSelector, String s, OsmPrimitive p) {
    420420            if (s != null && matchingSelector instanceof Selector.ChildOrParentSelector) {
    421                 return  insertArguments(((Selector.ChildOrParentSelector)matchingSelector).right, s);
     421                return insertArguments(((Selector.ChildOrParentSelector)matchingSelector).right, s, p);
    422422            } else if (s == null || !(matchingSelector instanceof GeneralSelector)) {
    423423                return s;
     
    426426            final StringBuffer sb = new StringBuffer();
    427427            while (m.find()) {
    428                 final String argument = determineArgument((Selector.GeneralSelector) matchingSelector, Integer.parseInt(m.group(1)), m.group(2));
     428                final String argument = determineArgument((Selector.GeneralSelector) matchingSelector, Integer.parseInt(m.group(1)), m.group(2), p);
    429429                try {
    430430                    // Perform replacement with null-safe + regex-safe handling
     
    501501         */
    502502        String getDescriptionForMatchingSelector(OsmPrimitive p, Selector matchingSelector) {
    503             return insertArguments(matchingSelector, getDescription(p));
     503            return insertArguments(matchingSelector, getDescription(p), p);
    504504        }
    505505
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Condition.java

    r8252 r8266  
    44import java.text.MessageFormat;
    55import java.util.Arrays;
     6import java.util.Collection;
    67import java.util.EnumSet;
    78import java.util.Objects;
     
    340341        }
    341342
    342         public Tag asTag() {
    343             return new Tag(label);
     343        public Tag asTag(OsmPrimitive p) {
     344            String key = label;
     345            if (KeyMatchType.REGEX.equals(matchType)) {
     346                final Collection<String> matchingKeys = Utils.filter(p.keySet(), containsPattern);
     347                if (!matchingKeys.isEmpty()) {
     348                    key = matchingKeys.iterator().next();
     349                }
     350            }
     351            return new Tag(key, p.get(key));
    344352        }
    345353
  • trunk/test/unit/org/openstreetmap/josm/data/validation/tests/MapCSSTagCheckerTest.java

    r8265 r8266  
    88
    99import java.io.StringReader;
     10import java.util.Collection;
    1011import java.util.Iterator;
    1112import java.util.LinkedHashSet;
     
    2627import org.openstreetmap.josm.data.osm.OsmUtils;
    2728import org.openstreetmap.josm.data.validation.Severity;
     29import org.openstreetmap.josm.data.validation.TestError;
    2830import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker.TagCheck;
     31import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.ParseException;
    2932
    3033/**
     
    3942    public static void setUp() {
    4043        JOSMFixture.createUnitTestFixture().init();
     44    }
     45
     46    static MapCSSTagChecker buildTagChecker(String css) throws ParseException {
     47        final MapCSSTagChecker test = new MapCSSTagChecker();
     48        test.checks.putAll("test", TagCheck.readMapCSS(new StringReader(css)));
     49        return test;
    4150    }
    4251
     
    6978        n2.put("natural", "wood");
    7079        assertFalse(check.evaluate(n2));
    71         assertThat(MapCSSTagChecker.TagCheck.insertArguments(check.rule.selectors.get(0), "The key is {0.key} and the value is {0.value}"),
     80        assertThat(MapCSSTagChecker.TagCheck.insertArguments(check.rule.selectors.get(0), "The key is {0.key} and the value is {0.value}", null),
    7281                is("The key is natural and the value is marsh"));
    7382    }
     
    8998
    9099    @Test
     100    public void test9782() throws Exception {
     101        final MapCSSTagChecker test = buildTagChecker("*[/.+_name/][!name] {" +
     102                "throwWarning: tr(\"has {0} but not {1}\", \"{0.key}\", \"{1.key}\");}");
     103        final OsmPrimitive p = OsmUtils.createPrimitive("way alt_name=Foo");
     104        final Collection<TestError> errors = test.getErrorsForPrimitive(p, false);
     105        assertThat(errors.size(), is(1));
     106        assertThat(errors.iterator().next().getMessage(), is("has alt_name but not name"));
     107    }
     108
     109    @Test
     110    public void test10859() throws Exception {
     111        final MapCSSTagChecker test = buildTagChecker("way[highway=footway][foot?!] {\n" +
     112                "  throwWarning: tr(\"{0} used with {1}\", \"{0.value}\", \"{1.tag}\");}");
     113        final OsmPrimitive p = OsmUtils.createPrimitive("way highway=footway foot=no");
     114        final Collection<TestError> errors = test.getErrorsForPrimitive(p, false);
     115        assertThat(errors.size(), is(1));
     116        assertThat(errors.iterator().next().getMessage(), is("footway used with foot=no"));
     117    }
     118
     119    @Test
    91120    public void testInit() throws Exception {
    92121        MapCSSTagChecker c = new MapCSSTagChecker();
Note: See TracChangeset for help on using the changeset viewer.