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

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

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

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