Ignore:
Timestamp:
2014-06-29T17:35:10+02:00 (10 years ago)
Author:
Don-vip
Message:

see #9518 - refactor internals of MapCSSTagChecker to prepare later work, improve javadoc

File:
1 edited

Legend:

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

    r7248 r7275  
    1818import java.util.List;
    1919import java.util.Map;
     20import java.util.Set;
    2021import java.util.regex.Matcher;
    2122import java.util.regex.Pattern;
     
    4950import org.openstreetmap.josm.io.UTFInputStreamReader;
    5051import org.openstreetmap.josm.tools.CheckParameterUtil;
     52import org.openstreetmap.josm.tools.MultiMap;
    5153import org.openstreetmap.josm.tools.Predicate;
    5254import org.openstreetmap.josm.tools.Utils;
     
    5860public class MapCSSTagChecker extends Test.TagTest {
    5961
     62    /**
     63     * A grouped MapCSSRule with multiple selectors for a single declaration.
     64     * @see MapCSSRule
     65     */
    6066    public static class GroupedMapCSSRule {
     67        /** MapCSS selectors **/
    6168        final public List<Selector> selectors;
     69        /** MapCSS declaration **/
    6270        final public Declaration declaration;
    6371
     72        /**
     73         * Constructs a new {@code GroupedMapCSSRule}.
     74         * @param selectors MapCSS selectors
     75         * @param declaration MapCSS declaration
     76         */
    6477        public GroupedMapCSSRule(List<Selector> selectors, Declaration declaration) {
    6578            this.selectors = selectors;
     
    112125    }
    113126
    114     final List<TagCheck> checks = new ArrayList<>();
     127    final MultiMap<String, TagCheck> checks = new MultiMap<>();
    115128
    116129    static class TagCheck implements Predicate<OsmPrimitive> {
     
    204217                        check.change.add(toTag);
    205218                    } else if ("fixRemove".equals(ai.key)) {
    206                         CheckParameterUtil.ensureThat(!(ai.val instanceof String) || !val.contains("="), "Unexpected '='. Please only specify the key to remove!");
     219                        CheckParameterUtil.ensureThat(!(ai.val instanceof String) || !(val != null && val.contains("=")),
     220                                "Unexpected '='. Please only specify the key to remove!");
    207221                        final PrimitiveToTag toTag = PrimitiveToTag.ofMapCSSObject(ai.val, true);
    208222                        check.change.add(toTag);
     
    323337
    324338        /**
    325          * Replaces occurrences of {@code {i.key}}, {@code {i.value}}, {@code {i.tag}} in {@code s} by the corresponding
     339         * Replaces occurrences of <code>{i.key}</code>, <code>{i.value}</code>, <code>{i.tag}</code> in {@code s} by the corresponding
    326340         * key/value/tag of the {@code index}-th {@link Condition} of {@code matchingSelector}.
    327341         */
     
    484498    /**
    485499     * Obtains all {@link TestError}s for the {@link OsmPrimitive} {@code p}.
     500     * @param p The OSM primitive
     501     * @param includeOtherSeverity if {@code true}, errors of severity {@link Severity#OTHER} (info) will also be returned
     502     * @return all errors for the given primitive, with or without those of "info" severity
    486503     */
    487504    public Collection<TestError> getErrorsForPrimitive(OsmPrimitive p, boolean includeOtherSeverity) {
    488505        final ArrayList<TestError> r = new ArrayList<>();
    489506        final Environment env = new Environment(p, new MultiCascade(), Environment.DEFAULT_LAYER, null);
    490         for (TagCheck check : checks) {
    491             if (Severity.OTHER.equals(check.getSeverity()) && !includeOtherSeverity) {
    492                 continue;
    493             }
    494             final Selector selector = check.whichSelectorMatchesEnvironment(env);
    495             if (selector != null) {
    496                 check.rule.declaration.execute(env);
    497                 final TestError error = check.getErrorForPrimitive(p, selector, env);
    498                 if (error != null) {
    499                     error.setTester(new MapCSSTagCheckerAndRule(check.rule));
    500                     r.add(error);
     507        for (Set<TagCheck> schecks : checks.values()) {
     508            for (TagCheck check : schecks) {
     509                if (Severity.OTHER.equals(check.getSeverity()) && !includeOtherSeverity) {
     510                    continue;
     511                }
     512                final Selector selector = check.whichSelectorMatchesEnvironment(env);
     513                if (selector != null) {
     514                    check.rule.declaration.execute(env);
     515                    final TestError error = check.getErrorForPrimitive(p, selector, env);
     516                    if (error != null) {
     517                        error.setTester(new MapCSSTagCheckerAndRule(check.rule));
     518                        r.add(error);
     519                    }
    501520                }
    502521            }
     
    516535
    517536    /**
    518      * Adds a new MapCSS config file from the given {@code Reader}.
    519      * @param css The reader
     537     * Adds a new MapCSS config file from the given URL.
     538     * @param url The unique URL of the MapCSS config file
    520539     * @throws ParseException if the config file does not match MapCSS syntax
     540     * @throws IOException if any I/O error occurs
     541     * @since
    521542     */
    522     public void addMapCSS(Reader css) throws ParseException {
    523         checks.addAll(TagCheck.readMapCSS(css));
     543    public void addMapCSS(String url) throws ParseException, IOException {
     544        CheckParameterUtil.ensureParameterNotNull(url, "url");
     545        try (InputStream s = new CachedFile(url).getInputStream()) {
     546            checks.putAll(url, TagCheck.readMapCSS(new BufferedReader(UTFInputStreamReader.create(s))));
     547        }
    524548    }
    525549
     
    534558                    Main.info(tr("Adding {0} to tag checker", i));
    535559                }
    536                 try (InputStream s = new CachedFile(i).getInputStream()) {
    537                     addMapCSS(new BufferedReader(UTFInputStreamReader.create(s)));
    538                 }
     560                addMapCSS(i);
    539561            } catch (IOException ex) {
    540562                Main.warn(tr("Failed to add {0} to tag checker", i));
Note: See TracChangeset for help on using the changeset viewer.