source: josm/trunk/src/org/openstreetmap/josm/data/validation/tests/MapCSSTagChecker.java@ 10728

Last change on this file since 10728 was 10728, checked in by Don-vip, 8 years ago

see #11390 - fix checkstyle violations

  • Property svn:eol-style set to native
File size: 35.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.validation.tests;
3
4import static org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker.FixCommand.evaluateObject;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.io.BufferedReader;
8import java.io.IOException;
9import java.io.InputStream;
10import java.io.Reader;
11import java.io.StringReader;
12import java.text.MessageFormat;
13import java.util.ArrayList;
14import java.util.Arrays;
15import java.util.Collection;
16import java.util.Collections;
17import java.util.HashMap;
18import java.util.HashSet;
19import java.util.Iterator;
20import java.util.LinkedHashMap;
21import java.util.LinkedHashSet;
22import java.util.LinkedList;
23import java.util.List;
24import java.util.Locale;
25import java.util.Map;
26import java.util.Objects;
27import java.util.Set;
28import java.util.function.Predicate;
29import java.util.regex.Matcher;
30import java.util.regex.Pattern;
31
32import org.openstreetmap.josm.Main;
33import org.openstreetmap.josm.command.ChangePropertyCommand;
34import org.openstreetmap.josm.command.ChangePropertyKeyCommand;
35import org.openstreetmap.josm.command.Command;
36import org.openstreetmap.josm.command.DeleteCommand;
37import org.openstreetmap.josm.command.SequenceCommand;
38import org.openstreetmap.josm.data.osm.DataSet;
39import org.openstreetmap.josm.data.osm.OsmPrimitive;
40import org.openstreetmap.josm.data.osm.OsmUtils;
41import org.openstreetmap.josm.data.osm.Tag;
42import org.openstreetmap.josm.data.validation.FixableTestError;
43import org.openstreetmap.josm.data.validation.Severity;
44import org.openstreetmap.josm.data.validation.Test;
45import org.openstreetmap.josm.data.validation.TestError;
46import org.openstreetmap.josm.gui.mappaint.Environment;
47import org.openstreetmap.josm.gui.mappaint.Keyword;
48import org.openstreetmap.josm.gui.mappaint.MultiCascade;
49import org.openstreetmap.josm.gui.mappaint.mapcss.Condition;
50import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.ClassCondition;
51import org.openstreetmap.josm.gui.mappaint.mapcss.Expression;
52import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction;
53import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSRule;
54import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSRule.Declaration;
55import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
56import org.openstreetmap.josm.gui.mappaint.mapcss.Selector;
57import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.AbstractSelector;
58import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.GeneralSelector;
59import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.MapCSSParser;
60import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.ParseException;
61import org.openstreetmap.josm.gui.preferences.SourceEntry;
62import org.openstreetmap.josm.gui.preferences.validator.ValidatorPreference;
63import org.openstreetmap.josm.gui.preferences.validator.ValidatorTagCheckerRulesPreference;
64import org.openstreetmap.josm.io.CachedFile;
65import org.openstreetmap.josm.io.IllegalDataException;
66import org.openstreetmap.josm.io.UTFInputStreamReader;
67import org.openstreetmap.josm.tools.CheckParameterUtil;
68import org.openstreetmap.josm.tools.MultiMap;
69import org.openstreetmap.josm.tools.Utils;
70
71/**
72 * MapCSS-based tag checker/fixer.
73 * @since 6506
74 */
75public class MapCSSTagChecker extends Test.TagTest {
76
77 /**
78 * A grouped MapCSSRule with multiple selectors for a single declaration.
79 * @see MapCSSRule
80 */
81 public static class GroupedMapCSSRule {
82 /** MapCSS selectors **/
83 public final List<Selector> selectors;
84 /** MapCSS declaration **/
85 public final Declaration declaration;
86
87 /**
88 * Constructs a new {@code GroupedMapCSSRule}.
89 * @param selectors MapCSS selectors
90 * @param declaration MapCSS declaration
91 */
92 public GroupedMapCSSRule(List<Selector> selectors, Declaration declaration) {
93 this.selectors = selectors;
94 this.declaration = declaration;
95 }
96
97 @Override
98 public int hashCode() {
99 return Objects.hash(selectors, declaration);
100 }
101
102 @Override
103 public boolean equals(Object obj) {
104 if (this == obj) return true;
105 if (obj == null || getClass() != obj.getClass()) return false;
106 GroupedMapCSSRule that = (GroupedMapCSSRule) obj;
107 return Objects.equals(selectors, that.selectors) &&
108 Objects.equals(declaration, that.declaration);
109 }
110
111 @Override
112 public String toString() {
113 return "GroupedMapCSSRule [selectors=" + selectors + ", declaration=" + declaration + ']';
114 }
115 }
116
117 /**
118 * The preference key for tag checker source entries.
119 * @since 6670
120 */
121 public static final String ENTRIES_PREF_KEY = "validator." + MapCSSTagChecker.class.getName() + ".entries";
122
123 /**
124 * Constructs a new {@code MapCSSTagChecker}.
125 */
126 public MapCSSTagChecker() {
127 super(tr("Tag checker (MapCSS based)"), tr("This test checks for errors in tag keys and values."));
128 }
129
130 /**
131 * Represents a fix to a validation test. The fixing {@link Command} can be obtained by {@link #createCommand(OsmPrimitive, Selector)}.
132 */
133 @FunctionalInterface
134 interface FixCommand {
135 /**
136 * Creates the fixing {@link Command} for the given primitive. The {@code matchingSelector} is used to evaluate placeholders
137 * (cf. {@link MapCSSTagChecker.TagCheck#insertArguments(Selector, String, OsmPrimitive)}).
138 * @param p OSM primitive
139 * @param matchingSelector matching selector
140 * @return fix command
141 */
142 Command createCommand(final OsmPrimitive p, final Selector matchingSelector);
143
144 static void checkObject(final Object obj) {
145 CheckParameterUtil.ensureThat(obj instanceof Expression || obj instanceof String,
146 "instance of Exception or String expected, but got " + obj);
147 }
148
149 /**
150 * Evaluates given object as {@link Expression} or {@link String} on the matched {@link OsmPrimitive} and {@code matchingSelector}.
151 * @param obj object to evaluate ({@link Expression} or {@link String})
152 * @param p OSM primitive
153 * @param matchingSelector matching selector
154 * @return result string
155 */
156 static String evaluateObject(final Object obj, final OsmPrimitive p, final Selector matchingSelector) {
157 final String s;
158 if (obj instanceof Expression) {
159 s = (String) ((Expression) obj).evaluate(new Environment(p));
160 } else if (obj instanceof String) {
161 s = (String) obj;
162 } else {
163 return null;
164 }
165 return TagCheck.insertArguments(matchingSelector, s, p);
166 }
167
168 /**
169 * Creates a fixing command which executes a {@link ChangePropertyCommand} on the specified tag.
170 * @param obj object to evaluate ({@link Expression} or {@link String})
171 * @return created fix command
172 */
173 static FixCommand fixAdd(final Object obj) {
174 checkObject(obj);
175 return new FixCommand() {
176 @Override
177 public Command createCommand(OsmPrimitive p, Selector matchingSelector) {
178 final Tag tag = Tag.ofString(evaluateObject(obj, p, matchingSelector));
179 return new ChangePropertyCommand(p, tag.getKey(), tag.getValue());
180 }
181
182 @Override
183 public String toString() {
184 return "fixAdd: " + obj;
185 }
186 };
187 }
188
189 /**
190 * Creates a fixing command which executes a {@link ChangePropertyCommand} to delete the specified key.
191 * @param obj object to evaluate ({@link Expression} or {@link String})
192 * @return created fix command
193 */
194 static FixCommand fixRemove(final Object obj) {
195 checkObject(obj);
196 return new FixCommand() {
197 @Override
198 public Command createCommand(OsmPrimitive p, Selector matchingSelector) {
199 final String key = evaluateObject(obj, p, matchingSelector);
200 return new ChangePropertyCommand(p, key, "");
201 }
202
203 @Override
204 public String toString() {
205 return "fixRemove: " + obj;
206 }
207 };
208 }
209
210 /**
211 * Creates a fixing command which executes a {@link ChangePropertyKeyCommand} on the specified keys.
212 * @param oldKey old key
213 * @param newKey new key
214 * @return created fix command
215 */
216 static FixCommand fixChangeKey(final String oldKey, final String newKey) {
217 return new FixCommand() {
218 @Override
219 public Command createCommand(OsmPrimitive p, Selector matchingSelector) {
220 return new ChangePropertyKeyCommand(p,
221 TagCheck.insertArguments(matchingSelector, oldKey, p),
222 TagCheck.insertArguments(matchingSelector, newKey, p));
223 }
224
225 @Override
226 public String toString() {
227 return "fixChangeKey: " + oldKey + " => " + newKey;
228 }
229 };
230 }
231 }
232
233 final MultiMap<String, TagCheck> checks = new MultiMap<>();
234
235 /**
236 * Result of {@link TagCheck#readMapCSS}
237 * @since 8936
238 */
239 public static class ParseResult {
240 /** Checks successfully parsed */
241 public final List<TagCheck> parseChecks;
242 /** Errors that occured during parsing */
243 public final Collection<Throwable> parseErrors;
244
245 /**
246 * Constructs a new {@code ParseResult}.
247 * @param parseChecks Checks successfully parsed
248 * @param parseErrors Errors that occured during parsing
249 */
250 public ParseResult(List<TagCheck> parseChecks, Collection<Throwable> parseErrors) {
251 this.parseChecks = parseChecks;
252 this.parseErrors = parseErrors;
253 }
254 }
255
256 public static class TagCheck implements Predicate<OsmPrimitive> {
257 /** The selector of this {@code TagCheck} */
258 protected final GroupedMapCSSRule rule;
259 /** Commands to apply in order to fix a matching primitive */
260 protected final List<FixCommand> fixCommands = new ArrayList<>();
261 /** Tags (or arbitraty strings) of alternatives to be presented to the user */
262 protected final List<String> alternatives = new ArrayList<>();
263 /** An {@link Instruction.AssignmentInstruction}-{@link Severity} pair.
264 * Is evaluated on the matching primitive to give the error message. Map is checked to contain exactly one element. */
265 protected final Map<Instruction.AssignmentInstruction, Severity> errors = new HashMap<>();
266 /** Unit tests */
267 protected final Map<String, Boolean> assertions = new HashMap<>();
268 /** MapCSS Classes to set on matching primitives */
269 protected final Set<String> setClassExpressions = new HashSet<>();
270 /** Denotes whether the object should be deleted for fixing it */
271 protected boolean deletion;
272 /** A string used to group similar tests */
273 protected String group;
274
275 TagCheck(GroupedMapCSSRule rule) {
276 this.rule = rule;
277 }
278
279 private static final String POSSIBLE_THROWS = possibleThrows();
280
281 static final String possibleThrows() {
282 StringBuilder sb = new StringBuilder();
283 for (Severity s : Severity.values()) {
284 if (sb.length() > 0) {
285 sb.append('/');
286 }
287 sb.append("throw")
288 .append(s.name().charAt(0))
289 .append(s.name().substring(1).toLowerCase(Locale.ENGLISH));
290 }
291 return sb.toString();
292 }
293
294 static TagCheck ofMapCSSRule(final GroupedMapCSSRule rule) throws IllegalDataException {
295 final TagCheck check = new TagCheck(rule);
296 for (Instruction i : rule.declaration.instructions) {
297 if (i instanceof Instruction.AssignmentInstruction) {
298 final Instruction.AssignmentInstruction ai = (Instruction.AssignmentInstruction) i;
299 if (ai.isSetInstruction) {
300 check.setClassExpressions.add(ai.key);
301 continue;
302 }
303 final String val = ai.val instanceof Expression
304 ? (String) ((Expression) ai.val).evaluate(new Environment())
305 : ai.val instanceof String
306 ? (String) ai.val
307 : ai.val instanceof Keyword
308 ? ((Keyword) ai.val).val
309 : null;
310 if (ai.key.startsWith("throw")) {
311 try {
312 final Severity severity = Severity.valueOf(ai.key.substring("throw".length()).toUpperCase(Locale.ENGLISH));
313 check.errors.put(ai, severity);
314 } catch (IllegalArgumentException e) {
315 Main.warn(e, "Unsupported "+ai.key+" instruction. Allowed instructions are "+POSSIBLE_THROWS+'.');
316 }
317 } else if ("fixAdd".equals(ai.key)) {
318 check.fixCommands.add(FixCommand.fixAdd(ai.val));
319 } else if ("fixRemove".equals(ai.key)) {
320 CheckParameterUtil.ensureThat(!(ai.val instanceof String) || !(val != null && val.contains("=")),
321 "Unexpected '='. Please only specify the key to remove!");
322 check.fixCommands.add(FixCommand.fixRemove(ai.val));
323 } else if ("fixChangeKey".equals(ai.key) && val != null) {
324 CheckParameterUtil.ensureThat(val.contains("=>"), "Separate old from new key by '=>'!");
325 final String[] x = val.split("=>", 2);
326 check.fixCommands.add(FixCommand.fixChangeKey(Tag.removeWhiteSpaces(x[0]), Tag.removeWhiteSpaces(x[1])));
327 } else if ("fixDeleteObject".equals(ai.key) && val != null) {
328 CheckParameterUtil.ensureThat("this".equals(val), "fixDeleteObject must be followed by 'this'");
329 check.deletion = true;
330 } else if ("suggestAlternative".equals(ai.key) && val != null) {
331 check.alternatives.add(val);
332 } else if ("assertMatch".equals(ai.key) && val != null) {
333 check.assertions.put(val, Boolean.TRUE);
334 } else if ("assertNoMatch".equals(ai.key) && val != null) {
335 check.assertions.put(val, Boolean.FALSE);
336 } else if ("group".equals(ai.key) && val != null) {
337 check.group = val;
338 } else {
339 throw new IllegalDataException("Cannot add instruction " + ai.key + ": " + ai.val + '!');
340 }
341 }
342 }
343 if (check.errors.isEmpty() && check.setClassExpressions.isEmpty()) {
344 throw new IllegalDataException(
345 "No "+POSSIBLE_THROWS+" given! You should specify a validation error message for " + rule.selectors);
346 } else if (check.errors.size() > 1) {
347 throw new IllegalDataException(
348 "More than one "+POSSIBLE_THROWS+" given! You should specify a single validation error message for "
349 + rule.selectors);
350 }
351 return check;
352 }
353
354 static ParseResult readMapCSS(Reader css) throws ParseException {
355 CheckParameterUtil.ensureParameterNotNull(css, "css");
356
357 final MapCSSStyleSource source = new MapCSSStyleSource("");
358 final MapCSSParser preprocessor = new MapCSSParser(css, MapCSSParser.LexicalState.PREPROCESSOR);
359
360 css = new StringReader(preprocessor.pp_root(source));
361 final MapCSSParser parser = new MapCSSParser(css, MapCSSParser.LexicalState.DEFAULT);
362 parser.sheet(source);
363 Collection<Throwable> parseErrors = source.getErrors();
364 assert parseErrors.isEmpty();
365 // Ignore "meta" rule(s) from external rules of JOSM wiki
366 removeMetaRules(source);
367 // group rules with common declaration block
368 Map<Declaration, List<Selector>> g = new LinkedHashMap<>();
369 for (MapCSSRule rule : source.rules) {
370 if (!g.containsKey(rule.declaration)) {
371 List<Selector> sels = new ArrayList<>();
372 sels.add(rule.selector);
373 g.put(rule.declaration, sels);
374 } else {
375 g.get(rule.declaration).add(rule.selector);
376 }
377 }
378 List<TagCheck> parseChecks = new ArrayList<>();
379 for (Map.Entry<Declaration, List<Selector>> map : g.entrySet()) {
380 try {
381 parseChecks.add(TagCheck.ofMapCSSRule(
382 new GroupedMapCSSRule(map.getValue(), map.getKey())));
383 } catch (IllegalDataException e) {
384 Main.error("Cannot add MapCss rule: "+e.getMessage());
385 parseErrors.add(e);
386 }
387 }
388 return new ParseResult(parseChecks, parseErrors);
389 }
390
391 private static void removeMetaRules(MapCSSStyleSource source) {
392 for (Iterator<MapCSSRule> it = source.rules.iterator(); it.hasNext();) {
393 MapCSSRule x = it.next();
394 if (x.selector instanceof GeneralSelector) {
395 GeneralSelector gs = (GeneralSelector) x.selector;
396 if ("meta".equals(gs.base) && gs.getConditions().isEmpty()) {
397 it.remove();
398 }
399 }
400 }
401 }
402
403 @Override
404 public boolean test(OsmPrimitive primitive) {
405 // Tests whether the primitive contains a deprecated tag which is represented by this MapCSSTagChecker.
406 return whichSelectorMatchesPrimitive(primitive) != null;
407 }
408
409 Selector whichSelectorMatchesPrimitive(OsmPrimitive primitive) {
410 return whichSelectorMatchesEnvironment(new Environment(primitive));
411 }
412
413 Selector whichSelectorMatchesEnvironment(Environment env) {
414 for (Selector i : rule.selectors) {
415 env.clearSelectorMatchingInformation();
416 if (i.matches(env)) {
417 return i;
418 }
419 }
420 return null;
421 }
422
423 /**
424 * Determines the {@code index}-th key/value/tag (depending on {@code type}) of the
425 * {@link org.openstreetmap.josm.gui.mappaint.mapcss.Selector.GeneralSelector}.
426 * @param matchingSelector matching selector
427 * @param index index
428 * @param type selector type ("key", "value" or "tag")
429 * @param p OSM primitive
430 * @return argument value, can be {@code null}
431 */
432 static String determineArgument(Selector.GeneralSelector matchingSelector, int index, String type, OsmPrimitive p) {
433 try {
434 final Condition c = matchingSelector.getConditions().get(index);
435 final Tag tag = c instanceof Condition.ToTagConvertable
436 ? ((Condition.ToTagConvertable) c).asTag(p)
437 : null;
438 if (tag == null) {
439 return null;
440 } else if ("key".equals(type)) {
441 return tag.getKey();
442 } else if ("value".equals(type)) {
443 return tag.getValue();
444 } else if ("tag".equals(type)) {
445 return tag.toString();
446 }
447 } catch (IndexOutOfBoundsException ignore) {
448 Main.debug(ignore);
449 }
450 return null;
451 }
452
453 /**
454 * Replaces occurrences of <code>{i.key}</code>, <code>{i.value}</code>, <code>{i.tag}</code> in {@code s} by the corresponding
455 * key/value/tag of the {@code index}-th {@link Condition} of {@code matchingSelector}.
456 * @param matchingSelector matching selector
457 * @param s any string
458 * @param p OSM primitive
459 * @return string with arguments inserted
460 */
461 static String insertArguments(Selector matchingSelector, String s, OsmPrimitive p) {
462 if (s != null && matchingSelector instanceof Selector.ChildOrParentSelector) {
463 return insertArguments(((Selector.ChildOrParentSelector) matchingSelector).right, s, p);
464 } else if (s == null || !(matchingSelector instanceof GeneralSelector)) {
465 return s;
466 }
467 final Matcher m = Pattern.compile("\\{(\\d+)\\.(key|value|tag)\\}").matcher(s);
468 final StringBuffer sb = new StringBuffer();
469 while (m.find()) {
470 final String argument = determineArgument((Selector.GeneralSelector) matchingSelector,
471 Integer.parseInt(m.group(1)), m.group(2), p);
472 try {
473 // Perform replacement with null-safe + regex-safe handling
474 m.appendReplacement(sb, String.valueOf(argument).replace("^(", "").replace(")$", ""));
475 } catch (IndexOutOfBoundsException | IllegalArgumentException e) {
476 Main.error(e, tr("Unable to replace argument {0} in {1}: {2}", argument, sb, e.getMessage()));
477 }
478 }
479 m.appendTail(sb);
480 return sb.toString();
481 }
482
483 /**
484 * Constructs a fix in terms of a {@link org.openstreetmap.josm.command.Command} for the {@link OsmPrimitive}
485 * if the error is fixable, or {@code null} otherwise.
486 *
487 * @param p the primitive to construct the fix for
488 * @return the fix or {@code null}
489 */
490 Command fixPrimitive(OsmPrimitive p) {
491 if (fixCommands.isEmpty() && !deletion) {
492 return null;
493 }
494 final Selector matchingSelector = whichSelectorMatchesPrimitive(p);
495 Collection<Command> cmds = new LinkedList<>();
496 for (FixCommand fixCommand : fixCommands) {
497 cmds.add(fixCommand.createCommand(p, matchingSelector));
498 }
499 if (deletion && !p.isDeleted()) {
500 cmds.add(new DeleteCommand(p));
501 }
502 return new SequenceCommand(tr("Fix of {0}", getDescriptionForMatchingSelector(p, matchingSelector)), cmds);
503 }
504
505 /**
506 * Constructs a (localized) message for this deprecation check.
507 * @param p OSM primitive
508 *
509 * @return a message
510 */
511 String getMessage(OsmPrimitive p) {
512 if (errors.isEmpty()) {
513 // Return something to avoid NPEs
514 return rule.declaration.toString();
515 } else {
516 final Object val = errors.keySet().iterator().next().val;
517 return String.valueOf(
518 val instanceof Expression
519 ? ((Expression) val).evaluate(new Environment(p))
520 : val
521 );
522 }
523 }
524
525 /**
526 * Constructs a (localized) description for this deprecation check.
527 * @param p OSM primitive
528 *
529 * @return a description (possibly with alternative suggestions)
530 * @see #getDescriptionForMatchingSelector
531 */
532 String getDescription(OsmPrimitive p) {
533 if (alternatives.isEmpty()) {
534 return getMessage(p);
535 } else {
536 /* I18N: {0} is the test error message and {1} is an alternative */
537 return tr("{0}, use {1} instead", getMessage(p), Utils.join(tr(" or "), alternatives));
538 }
539 }
540
541 /**
542 * Constructs a (localized) description for this deprecation check
543 * where any placeholders are replaced by values of the matched selector.
544 *
545 * @param matchingSelector matching selector
546 * @param p OSM primitive
547 * @return a description (possibly with alternative suggestions)
548 */
549 String getDescriptionForMatchingSelector(OsmPrimitive p, Selector matchingSelector) {
550 return insertArguments(matchingSelector, getDescription(p), p);
551 }
552
553 Severity getSeverity() {
554 return errors.isEmpty() ? null : errors.values().iterator().next();
555 }
556
557 @Override
558 public String toString() {
559 return getDescription(null);
560 }
561
562 /**
563 * Constructs a {@link TestError} for the given primitive, or returns null if the primitive does not give rise to an error.
564 *
565 * @param p the primitive to construct the error for
566 * @return an instance of {@link TestError}, or returns null if the primitive does not give rise to an error.
567 */
568 TestError getErrorForPrimitive(OsmPrimitive p) {
569 final Environment env = new Environment(p);
570 return getErrorForPrimitive(p, whichSelectorMatchesEnvironment(env), env);
571 }
572
573 TestError getErrorForPrimitive(OsmPrimitive p, Selector matchingSelector, Environment env) {
574 if (matchingSelector != null && !errors.isEmpty()) {
575 final Command fix = fixPrimitive(p);
576 final String description = getDescriptionForMatchingSelector(p, matchingSelector);
577 final String description1 = group == null ? description : group;
578 final String description2 = group == null ? null : description;
579 final List<OsmPrimitive> primitives;
580 if (env.child != null) {
581 primitives = Arrays.asList(p, env.child);
582 } else {
583 primitives = Collections.singletonList(p);
584 }
585 if (fix != null) {
586 return new FixableTestError(null, getSeverity(), description1, description2, matchingSelector.toString(), 3000,
587 primitives, fix);
588 } else {
589 return new TestError(null, getSeverity(), description1, description2, matchingSelector.toString(), 3000, primitives);
590 }
591 } else {
592 return null;
593 }
594 }
595
596 /**
597 * Returns the set of tagchecks on which this check depends on.
598 * @param schecks the collection of tagcheks to search in
599 * @return the set of tagchecks on which this check depends on
600 * @since 7881
601 */
602 public Set<TagCheck> getTagCheckDependencies(Collection<TagCheck> schecks) {
603 Set<TagCheck> result = new HashSet<>();
604 Set<String> classes = getClassesIds();
605 if (schecks != null && !classes.isEmpty()) {
606 for (TagCheck tc : schecks) {
607 if (this.equals(tc)) {
608 continue;
609 }
610 for (String id : tc.setClassExpressions) {
611 if (classes.contains(id)) {
612 result.add(tc);
613 break;
614 }
615 }
616 }
617 }
618 return result;
619 }
620
621 /**
622 * Returns the list of ids of all MapCSS classes referenced in the rule selectors.
623 * @return the list of ids of all MapCSS classes referenced in the rule selectors
624 * @since 7881
625 */
626 public Set<String> getClassesIds() {
627 Set<String> result = new HashSet<>();
628 for (Selector s : rule.selectors) {
629 if (s instanceof AbstractSelector) {
630 for (Condition c : ((AbstractSelector) s).getConditions()) {
631 if (c instanceof ClassCondition) {
632 result.add(((ClassCondition) c).id);
633 }
634 }
635 }
636 }
637 return result;
638 }
639 }
640
641 static class MapCSSTagCheckerAndRule extends MapCSSTagChecker {
642 public final GroupedMapCSSRule rule;
643
644 MapCSSTagCheckerAndRule(GroupedMapCSSRule rule) {
645 this.rule = rule;
646 }
647
648 @Override
649 public synchronized boolean equals(Object obj) {
650 return super.equals(obj)
651 || (obj instanceof TagCheck && rule.equals(((TagCheck) obj).rule))
652 || (obj instanceof GroupedMapCSSRule && rule.equals(obj));
653 }
654
655 @Override
656 public synchronized int hashCode() {
657 return Objects.hash(super.hashCode(), rule);
658 }
659
660 @Override
661 public String toString() {
662 return "MapCSSTagCheckerAndRule [rule=" + rule + ']';
663 }
664 }
665
666 /**
667 * Obtains all {@link TestError}s for the {@link OsmPrimitive} {@code p}.
668 * @param p The OSM primitive
669 * @param includeOtherSeverity if {@code true}, errors of severity {@link Severity#OTHER} (info) will also be returned
670 * @return all errors for the given primitive, with or without those of "info" severity
671 */
672 public synchronized Collection<TestError> getErrorsForPrimitive(OsmPrimitive p, boolean includeOtherSeverity) {
673 return getErrorsForPrimitive(p, includeOtherSeverity, checks.values());
674 }
675
676 private static Collection<TestError> getErrorsForPrimitive(OsmPrimitive p, boolean includeOtherSeverity,
677 Collection<Set<TagCheck>> checksCol) {
678 final List<TestError> r = new ArrayList<>();
679 final Environment env = new Environment(p, new MultiCascade(), Environment.DEFAULT_LAYER, null);
680 for (Set<TagCheck> schecks : checksCol) {
681 for (TagCheck check : schecks) {
682 if (Severity.OTHER.equals(check.getSeverity()) && !includeOtherSeverity) {
683 continue;
684 }
685 final Selector selector = check.whichSelectorMatchesEnvironment(env);
686 if (selector != null) {
687 check.rule.declaration.execute(env);
688 final TestError error = check.getErrorForPrimitive(p, selector, env);
689 if (error != null) {
690 error.setTester(new MapCSSTagCheckerAndRule(check.rule));
691 r.add(error);
692 }
693 }
694 }
695 }
696 return r;
697 }
698
699 /**
700 * Visiting call for primitives.
701 *
702 * @param p The primitive to inspect.
703 */
704 @Override
705 public void check(OsmPrimitive p) {
706 errors.addAll(getErrorsForPrimitive(p, ValidatorPreference.PREF_OTHER.get()));
707 }
708
709 /**
710 * Adds a new MapCSS config file from the given URL.
711 * @param url The unique URL of the MapCSS config file
712 * @return List of tag checks and parsing errors, or null
713 * @throws ParseException if the config file does not match MapCSS syntax
714 * @throws IOException if any I/O error occurs
715 * @since 7275
716 */
717 public synchronized ParseResult addMapCSS(String url) throws ParseException, IOException {
718 CheckParameterUtil.ensureParameterNotNull(url, "url");
719 CachedFile cache = new CachedFile(url);
720 InputStream zip = cache.findZipEntryInputStream("validator.mapcss", "");
721 ParseResult result;
722 try (InputStream s = zip != null ? zip : cache.getInputStream()) {
723 result = TagCheck.readMapCSS(new BufferedReader(UTFInputStreamReader.create(s)));
724 checks.remove(url);
725 checks.putAll(url, result.parseChecks);
726 // Check assertions, useful for development of local files
727 if (Main.pref.getBoolean("validator.check_assert_local_rules", false) && Utils.isLocalUrl(url)) {
728 for (String msg : checkAsserts(result.parseChecks)) {
729 Main.warn(msg);
730 }
731 }
732 } finally {
733 cache.close();
734 }
735 return result;
736 }
737
738 @Override
739 public synchronized void initialize() throws Exception {
740 checks.clear();
741 for (SourceEntry source : new ValidatorTagCheckerRulesPreference.RulePrefHelper().get()) {
742 if (!source.active) {
743 continue;
744 }
745 String i = source.url;
746 try {
747 if (!i.startsWith("resource:")) {
748 Main.info(tr("Adding {0} to tag checker", i));
749 } else if (Main.isDebugEnabled()) {
750 Main.debug(tr("Adding {0} to tag checker", i));
751 }
752 addMapCSS(i);
753 if (Main.pref.getBoolean("validator.auto_reload_local_rules", true) && source.isLocal()) {
754 try {
755 Main.fileWatcher.registerValidatorRule(source);
756 } catch (IOException e) {
757 Main.error(e);
758 }
759 }
760 } catch (IOException ex) {
761 Main.warn(tr("Failed to add {0} to tag checker", i));
762 Main.warn(ex, false);
763 } catch (ParseException ex) {
764 Main.warn(tr("Failed to add {0} to tag checker", i));
765 Main.warn(ex);
766 }
767 }
768 }
769
770 /**
771 * Checks that rule assertions are met for the given set of TagChecks.
772 * @param schecks The TagChecks for which assertions have to be checked
773 * @return A set of error messages, empty if all assertions are met
774 * @since 7356
775 */
776 public Set<String> checkAsserts(final Collection<TagCheck> schecks) {
777 Set<String> assertionErrors = new LinkedHashSet<>();
778 final DataSet ds = new DataSet();
779 for (final TagCheck check : schecks) {
780 if (Main.isDebugEnabled()) {
781 Main.debug("Check: "+check);
782 }
783 for (final Map.Entry<String, Boolean> i : check.assertions.entrySet()) {
784 if (Main.isDebugEnabled()) {
785 Main.debug("- Assertion: "+i);
786 }
787 final OsmPrimitive p = OsmUtils.createPrimitive(i.getKey());
788 // Build minimal ordered list of checks to run to test the assertion
789 List<Set<TagCheck>> checksToRun = new ArrayList<>();
790 Set<TagCheck> checkDependencies = check.getTagCheckDependencies(schecks);
791 if (!checkDependencies.isEmpty()) {
792 checksToRun.add(checkDependencies);
793 }
794 checksToRun.add(Collections.singleton(check));
795 // Add primitive to dataset to avoid DataIntegrityProblemException when evaluating selectors
796 ds.addPrimitive(p);
797 final Collection<TestError> pErrors = getErrorsForPrimitive(p, true, checksToRun);
798 if (Main.isDebugEnabled()) {
799 Main.debug("- Errors: "+pErrors);
800 }
801 final boolean isError = pErrors.stream().anyMatch(e -> e.getTester().equals(check.rule));
802 if (isError != i.getValue()) {
803 final String error = MessageFormat.format("Expecting test ''{0}'' (i.e., {1}) to {2} {3} (i.e., {4})",
804 check.getMessage(p), check.rule.selectors, i.getValue() ? "match" : "not match", i.getKey(), p.getKeys());
805 assertionErrors.add(error);
806 }
807 ds.removePrimitive(p);
808 }
809 }
810 return assertionErrors;
811 }
812
813 @Override
814 public synchronized int hashCode() {
815 return Objects.hash(super.hashCode(), checks);
816 }
817
818 @Override
819 public synchronized boolean equals(Object obj) {
820 if (this == obj) return true;
821 if (obj == null || getClass() != obj.getClass()) return false;
822 if (!super.equals(obj)) return false;
823 MapCSSTagChecker that = (MapCSSTagChecker) obj;
824 return Objects.equals(checks, that.checks);
825 }
826}
Note: See TracBrowser for help on using the repository browser.