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

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

fix #15341 - better error message

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