Changeset 7356 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2014-08-01T19:17:40+02:00 (10 years ago)
Author:
Don-vip
Message:

fix #10206 - Check MapCSS validator assertions for local rules if new advanced option validator.check_assert_local_rules is enabled in preferences

Location:
trunk/src/org/openstreetmap/josm
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/OsmUtils.java

    r7005 r7356  
    66import java.util.List;
    77import java.util.Locale;
     8import java.util.Map;
     9
     10import org.openstreetmap.josm.tools.CheckParameterUtil;
     11import org.openstreetmap.josm.tools.TextTagParser;
    812
    913public final class OsmUtils {
     
    4852        return FALSE_VALUES.contains(value);
    4953    }
     54
     55    /**
     56     * Creates a new OSM primitive according to the given assertion. Originally written for unit tests,
     57     * this can also be used in another places like validation of local MapCSS validator rules.
     58     * @param assertion The assertion describing OSM primitive (ex: "way name=Foo railway=rail")
     59     * @return a new OSM primitive according to the given assertion
     60     * @throws IllegalArgumentException if assertion is null or if the primitive type cannot be deduced from it
     61     * @since 7356
     62     */
     63    public static OsmPrimitive createPrimitive(String assertion) {
     64        CheckParameterUtil.ensureParameterNotNull(assertion, "assertion");
     65        final String[] x = assertion.split("\\s+", 2);
     66        final OsmPrimitive p = "n".equals(x[0]) || "node".equals(x[0])
     67                ? new Node()
     68                : "w".equals(x[0]) || "way".equals(x[0])
     69                ? new Way()
     70                : "r".equals(x[0]) || "relation".equals(x[0])
     71                ? new Relation()
     72                : null;
     73        if (p == null) {
     74            throw new IllegalArgumentException("Expecting n/node/w/way/r/relation, but got " + x[0]);
     75        }
     76        for (final Map.Entry<String, String> i : TextTagParser.readTagsFromText(x[1]).entrySet()) {
     77            p.put(i.getKey(), i.getValue());
     78        }
     79        return p;
     80    }
    5081}
  • trunk/src/org/openstreetmap/josm/data/validation/tests/MapCSSTagChecker.java

    r7300 r7356  
    88import java.io.InputStream;
    99import java.io.Reader;
     10import java.text.MessageFormat;
    1011import java.util.ArrayList;
    1112import java.util.Arrays;
     
    1516import java.util.Iterator;
    1617import java.util.LinkedHashMap;
     18import java.util.LinkedHashSet;
    1719import java.util.LinkedList;
    1820import java.util.List;
     
    2830import org.openstreetmap.josm.command.SequenceCommand;
    2931import org.openstreetmap.josm.data.osm.OsmPrimitive;
     32import org.openstreetmap.josm.data.osm.OsmUtils;
    3033import org.openstreetmap.josm.data.osm.Tag;
    3134import org.openstreetmap.josm.data.validation.FixableTestError;
     
    340343                }
    341344            } catch (IndexOutOfBoundsException ignore) {
    342                 Main.debug(ignore.getMessage());
     345                if (Main.isDebugEnabled()) {
     346                    Main.debug(ignore.getMessage());
     347                }
    343348            }
    344349            return null;
     
    557562            checks.remove(url);
    558563            checks.putAll(url, tagchecks);
     564            // Check assertions, useful for development of local files
     565            if (Main.pref.getBoolean("validator.check_assert_local_rules", false) && Utils.isLocalUrl(url)) {
     566                for (String msg : checkAsserts(tagchecks)) {
     567                    Main.warn(msg);
     568                }
     569            }
    559570        }
    560571    }
     
    569580            String i = source.url;
    570581            try {
    571                 if (i.startsWith("resource:")) {
     582                if (!i.startsWith("resource:")) {
     583                    Main.info(tr("Adding {0} to tag checker", i));
     584                } else if (Main.isDebugEnabled()) {
    572585                    Main.debug(tr("Adding {0} to tag checker", i));
    573                 } else {
    574                     Main.info(tr("Adding {0} to tag checker", i));
    575586                }
    576587                addMapCSS(i);
     
    590601            }
    591602        }
     603    }
     604
     605    /**
     606     * Checks that rule assertions are met for the given set of TagChecks.
     607     * @param schecks The TagChecks for which assertions have to be checked
     608     * @return A set of error messages, empty if all assertions are met
     609     * @since 7356
     610     */
     611    public Set<String> checkAsserts(final Collection<TagCheck> schecks) {
     612        Set<String> assertionErrors = new LinkedHashSet<>();
     613        for (final TagCheck check : schecks) {
     614            if (Main.isDebugEnabled()) {
     615                Main.debug("Check: "+check);
     616            }
     617            for (final Map.Entry<String, Boolean> i : check.assertions.entrySet()) {
     618                if (Main.isDebugEnabled()) {
     619                    Main.debug("- Assertion: "+i);
     620                }
     621                final OsmPrimitive p = OsmUtils.createPrimitive(i.getKey());
     622                final boolean isError = Utils.exists(getErrorsForPrimitive(p, true), new Predicate<TestError>() {
     623                    @Override
     624                    public boolean evaluate(TestError e) {
     625                        //noinspection EqualsBetweenInconvertibleTypes
     626                        return e.getTester().equals(check.rule);
     627                    }
     628                });
     629                if (isError != i.getValue()) {
     630                    final String error = MessageFormat.format("Expecting test ''{0}'' (i.e., {1}) to {2} {3} (i.e., {4})",
     631                            check.getMessage(p), check.rule.selectors, i.getValue() ? "match" : "not match", i.getKey(), p.getKeys());
     632                    assertionErrors.add(error);
     633                }
     634            }
     635        }
     636        return assertionErrors;
    592637    }
    593638
  • trunk/src/org/openstreetmap/josm/gui/preferences/SourceEntry.java

    r7276 r7356  
    88
    99import org.openstreetmap.josm.Main;
     10import org.openstreetmap.josm.tools.Utils;
    1011
    1112/**
     
    178179     */
    179180    public boolean isLocal() {
    180         if (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("resource://"))
    181             return false;
    182         return true;
     181        return Utils.isLocalUrl(url);
    183182    }
    184183
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r7321 r7356  
    10531053    }
    10541054
     1055    /**
     1056     * Determines if the given URL denotes a file on a local filesystem.
     1057     * @param url The URL to test
     1058     * @return {@code true} if the url points to a local file
     1059     * @since 7356
     1060     */
     1061    public static boolean isLocalUrl(String url) {
     1062        if (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("resource://"))
     1063            return false;
     1064        return true;
     1065    }
    10551066}
Note: See TracChangeset for help on using the changeset viewer.