Ignore:
Timestamp:
2014-01-02T23:58:58+01:00 (10 years ago)
Author:
simon04
Message:

see #9414 fix #9485 - MapCSSTagChecker: add support for set class_name instruction and .class_name condition

File:
1 edited

Legend:

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

    r6591 r6601  
    2323import org.openstreetmap.josm.command.Command;
    2424import org.openstreetmap.josm.command.SequenceCommand;
    25 import org.openstreetmap.josm.data.osm.Node;
    2625import org.openstreetmap.josm.data.osm.OsmPrimitive;
    27 import org.openstreetmap.josm.data.osm.Relation;
    2826import org.openstreetmap.josm.data.osm.Tag;
    29 import org.openstreetmap.josm.data.osm.Way;
    3027import org.openstreetmap.josm.data.preferences.CollectionProperty;
    3128import org.openstreetmap.josm.data.validation.FixableTestError;
     
    3431import org.openstreetmap.josm.data.validation.TestError;
    3532import org.openstreetmap.josm.gui.mappaint.Environment;
     33import org.openstreetmap.josm.gui.mappaint.MultiCascade;
    3634import org.openstreetmap.josm.gui.mappaint.mapcss.Condition;
    3735import org.openstreetmap.josm.gui.mappaint.mapcss.Expression;
     
    6866
    6967    static class TagCheck implements Predicate<OsmPrimitive> {
    70         protected final List<Selector> selector;
     68        protected final MapCSSRule rule;
    7169        protected final List<PrimitiveToTag> change = new ArrayList<PrimitiveToTag>();
    7270        protected final Map<String, String> keyChange = new LinkedHashMap<String, String>();
     
    7573        protected final Map<String, Boolean> assertions = new HashMap<String, Boolean>();
    7674
    77         TagCheck(List<Selector> selector) {
    78             this.selector = selector;
     75        TagCheck(MapCSSRule rule) {
     76            this.rule = rule;
    7977        }
    8078
     
    113111
    114112        static TagCheck ofMapCSSRule(final MapCSSRule rule) {
    115             final TagCheck check = new TagCheck(rule.selectors);
     113            final TagCheck check = new TagCheck(rule);
     114            boolean containsSetClassExpression = false;
    116115            for (Instruction i : rule.declaration) {
    117116                if (i instanceof Instruction.AssignmentInstruction) {
     
    142141                    } else if ("assertNoMatch".equals(ai.key) && val != null) {
    143142                        check.assertions.put(val, false);
     143                    } else if (ai.val instanceof Boolean && ((Boolean) ai.val)) {
     144                        containsSetClassExpression = true;
    144145                    } else {
    145146                        throw new RuntimeException("Cannot add instruction " + ai.key + ": " + ai.val + "!");
     
    147148                }
    148149            }
    149             if (check.errors.isEmpty()) {
     150            if (check.errors.isEmpty() && !containsSetClassExpression) {
    150151                throw new RuntimeException("No throwError/throwWarning/throwOther given! You should specify a validation error message for " + rule.selectors);
    151152            } else if (check.errors.size() > 1) {
     
    183184         * @param primitive the primitive to test
    184185         * @return true when the primitive contains a deprecated tag
    185          */
     186         * @deprecated since it does not handle MapCSS-classes
     187         */
     188        @Deprecated
    186189        boolean matchesPrimitive(OsmPrimitive primitive) {
    187190            return whichSelectorMatchesPrimitive(primitive) != null;
     
    189192
    190193        Selector whichSelectorMatchesPrimitive(OsmPrimitive primitive) {
    191             final Environment env = new Environment().withPrimitive(primitive);
    192             for (Selector i : selector) {
     194            return whichSelectorMatchesEnvironment(new Environment().withPrimitive(primitive));
     195        }
     196
     197        Selector whichSelectorMatchesEnvironment(Environment env) {
     198            for (Selector i : rule.selectors) {
     199                env.clearSelectorMatchingInformation();
    193200                if (i.matches(env)) {
    194201                    return i;
     
    318325         */
    319326        TestError getErrorForPrimitive(OsmPrimitive p) {
    320             final Selector matchingSelector = whichSelectorMatchesPrimitive(p);
    321             if (matchingSelector != null) {
     327            return getErrorForPrimitive(p, whichSelectorMatchesPrimitive(p));
     328        }
     329
     330        TestError getErrorForPrimitive(OsmPrimitive p, Selector matchingSelector) {
     331            if (matchingSelector != null && !errors.isEmpty()) {
    322332                final Command fix = fixPrimitive(p);
    323333                final String description = getDescriptionForMatchingSelector(matchingSelector);
     
    331341            }
    332342        }
     343    }
     344
     345    static class MapCSSTagCheckerAndRule extends MapCSSTagChecker {
     346        public final MapCSSRule rule;
     347
     348        MapCSSTagCheckerAndRule(MapCSSRule rule) {
     349            this.rule = rule;
     350        }
     351
     352        @Override
     353        public boolean equals(Object obj) {
     354            return super.equals(obj)
     355                    || (obj instanceof TagCheck && rule.equals(((TagCheck) obj).rule))
     356                    || (obj instanceof MapCSSRule && rule.equals(obj));
     357        }
     358    }
     359
     360    /**
     361     * Obtains all {@link TestError}s for the {@link OsmPrimitive} {@code p}.
     362     */
     363    public Collection<TestError> getErrorsForPrimitive(OsmPrimitive p) {
     364        final ArrayList<TestError> r = new ArrayList<TestError>();
     365        final Environment env = new Environment(p, new MultiCascade(), Environment.DEFAULT_LAYER, null);
     366        for (TagCheck check : checks) {
     367            final Selector selector = check.whichSelectorMatchesEnvironment(env);
     368            if (selector != null) {
     369                check.rule.execute(env);
     370                final TestError error = check.getErrorForPrimitive(p, selector);
     371                if (error != null) {
     372                    error.setTester(new MapCSSTagCheckerAndRule(check.rule));
     373                    r.add(error);
     374                }
     375            }
     376        }
     377        return r;
    333378    }
    334379
     
    340385    @Override
    341386    public void check(OsmPrimitive p) {
    342         for (TagCheck check : checks) {
    343             final TestError error = check.getErrorForPrimitive(p);
    344             if (error != null) {
    345                 error.setTester(this);
    346                 errors.add(error);
    347             }
    348         }
     387        errors.addAll(getErrorsForPrimitive(p));
    349388    }
    350389
Note: See TracChangeset for help on using the changeset viewer.