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

Last change on this file since 15998 was 15998, checked in by simon04, 4 years ago

see #18802 - MapCSSTagChecker: use Stream API

  • Property svn:eol-style set to native
File size: 40.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.validation.tests;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.geom.Area;
7import java.io.BufferedReader;
8import java.io.IOException;
9import java.io.InputStream;
10import java.io.Reader;
11import java.io.StringReader;
12import java.util.ArrayList;
13import java.util.Collection;
14import java.util.HashMap;
15import java.util.HashSet;
16import java.util.Iterator;
17import java.util.LinkedList;
18import java.util.List;
19import java.util.Map;
20import java.util.Objects;
21import java.util.Optional;
22import java.util.Set;
23import java.util.function.Consumer;
24import java.util.function.Predicate;
25import java.util.regex.Matcher;
26import java.util.regex.Pattern;
27import java.util.stream.Stream;
28
29import org.openstreetmap.josm.command.ChangePropertyCommand;
30import org.openstreetmap.josm.command.ChangePropertyKeyCommand;
31import org.openstreetmap.josm.command.Command;
32import org.openstreetmap.josm.command.DeleteCommand;
33import org.openstreetmap.josm.command.SequenceCommand;
34import org.openstreetmap.josm.data.osm.IPrimitive;
35import org.openstreetmap.josm.data.osm.OsmPrimitive;
36import org.openstreetmap.josm.data.osm.Tag;
37import org.openstreetmap.josm.data.osm.Way;
38import org.openstreetmap.josm.data.osm.WaySegment;
39import org.openstreetmap.josm.data.preferences.sources.SourceEntry;
40import org.openstreetmap.josm.data.preferences.sources.ValidatorPrefHelper;
41import org.openstreetmap.josm.data.validation.OsmValidator;
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.Expression;
50import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction;
51import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSRule;
52import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleIndex;
53import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
54import org.openstreetmap.josm.gui.mappaint.mapcss.Selector;
55import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.GeneralSelector;
56import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.MapCSSParser;
57import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.ParseException;
58import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.TokenMgrError;
59import org.openstreetmap.josm.gui.progress.ProgressMonitor;
60import org.openstreetmap.josm.io.CachedFile;
61import org.openstreetmap.josm.io.FileWatcher;
62import org.openstreetmap.josm.io.IllegalDataException;
63import org.openstreetmap.josm.io.UTFInputStreamReader;
64import org.openstreetmap.josm.spi.preferences.Config;
65import org.openstreetmap.josm.tools.CheckParameterUtil;
66import org.openstreetmap.josm.tools.I18n;
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 private MapCSSStyleIndex indexData;
77 final Map<MapCSSRule, MapCSSTagCheckerAndRule> ruleToCheckMap = new HashMap<>();
78 private final Set<OsmPrimitive> tested = new HashSet<>();
79 private static final Map<IPrimitive, Area> mpAreaCache = new HashMap<>();
80 static final boolean ALL_TESTS = true;
81 static final boolean ONLY_SELECTED_TESTS = false;
82
83 /**
84 * The preference key for tag checker source entries.
85 * @since 6670
86 */
87 public static final String ENTRIES_PREF_KEY = "validator." + MapCSSTagChecker.class.getName() + ".entries";
88
89 /**
90 * Constructs a new {@code MapCSSTagChecker}.
91 */
92 public MapCSSTagChecker() {
93 super(tr("Tag checker (MapCSS based)"), tr("This test checks for errors in tag keys and values."));
94 }
95
96 /**
97 * Represents a fix to a validation test. The fixing {@link Command} can be obtained by {@link #createCommand(OsmPrimitive, Selector)}.
98 */
99 @FunctionalInterface
100 interface FixCommand {
101 /**
102 * Creates the fixing {@link Command} for the given primitive. The {@code matchingSelector} is used to evaluate placeholders
103 * (cf. {@link MapCSSTagChecker.TagCheck#insertArguments(Selector, String, OsmPrimitive)}).
104 * @param p OSM primitive
105 * @param matchingSelector matching selector
106 * @return fix command
107 */
108 Command createCommand(OsmPrimitive p, Selector matchingSelector);
109
110 /**
111 * Checks that object is either an {@link Expression} or a {@link String}.
112 * @param obj object to check
113 * @throws IllegalArgumentException if object is not an {@code Expression} or a {@code String}
114 */
115 static void checkObject(final Object obj) {
116 CheckParameterUtil.ensureThat(obj instanceof Expression || obj instanceof String,
117 () -> "instance of Exception or String expected, but got " + obj);
118 }
119
120 /**
121 * Evaluates given object as {@link Expression} or {@link String} on the matched {@link OsmPrimitive} and {@code matchingSelector}.
122 * @param obj object to evaluate ({@link Expression} or {@link String})
123 * @param p OSM primitive
124 * @param matchingSelector matching selector
125 * @return result string
126 */
127 static String evaluateObject(final Object obj, final OsmPrimitive p, final Selector matchingSelector) {
128 final String s;
129 if (obj instanceof Expression) {
130 s = (String) ((Expression) obj).evaluate(new Environment(p));
131 } else if (obj instanceof String) {
132 s = (String) obj;
133 } else {
134 return null;
135 }
136 return TagCheck.insertArguments(matchingSelector, s, p);
137 }
138
139 /**
140 * Creates a fixing command which executes a {@link ChangePropertyCommand} on the specified tag.
141 * @param obj object to evaluate ({@link Expression} or {@link String})
142 * @return created fix command
143 */
144 static FixCommand fixAdd(final Object obj) {
145 checkObject(obj);
146 return new FixCommand() {
147 @Override
148 public Command createCommand(OsmPrimitive p, Selector matchingSelector) {
149 final Tag tag = Tag.ofString(FixCommand.evaluateObject(obj, p, matchingSelector));
150 return new ChangePropertyCommand(p, tag.getKey(), tag.getValue());
151 }
152
153 @Override
154 public String toString() {
155 return "fixAdd: " + obj;
156 }
157 };
158 }
159
160 /**
161 * Creates a fixing command which executes a {@link ChangePropertyCommand} to delete the specified key.
162 * @param obj object to evaluate ({@link Expression} or {@link String})
163 * @return created fix command
164 */
165 static FixCommand fixRemove(final Object obj) {
166 checkObject(obj);
167 return new FixCommand() {
168 @Override
169 public Command createCommand(OsmPrimitive p, Selector matchingSelector) {
170 final String key = FixCommand.evaluateObject(obj, p, matchingSelector);
171 return new ChangePropertyCommand(p, key, "");
172 }
173
174 @Override
175 public String toString() {
176 return "fixRemove: " + obj;
177 }
178 };
179 }
180
181 /**
182 * Creates a fixing command which executes a {@link ChangePropertyKeyCommand} on the specified keys.
183 * @param oldKey old key
184 * @param newKey new key
185 * @return created fix command
186 */
187 static FixCommand fixChangeKey(final String oldKey, final String newKey) {
188 return new FixCommand() {
189 @Override
190 public Command createCommand(OsmPrimitive p, Selector matchingSelector) {
191 return new ChangePropertyKeyCommand(p,
192 TagCheck.insertArguments(matchingSelector, oldKey, p),
193 TagCheck.insertArguments(matchingSelector, newKey, p));
194 }
195
196 @Override
197 public String toString() {
198 return "fixChangeKey: " + oldKey + " => " + newKey;
199 }
200 };
201 }
202 }
203
204 final MultiMap<String, TagCheck> checks = new MultiMap<>();
205
206 /**
207 * Result of {@link TagCheck#readMapCSS}
208 * @since 8936
209 */
210 public static class ParseResult {
211 /** Checks successfully parsed */
212 public final List<TagCheck> parseChecks;
213 /** Errors that occurred during parsing */
214 public final Collection<Throwable> parseErrors;
215
216 /**
217 * Constructs a new {@code ParseResult}.
218 * @param parseChecks Checks successfully parsed
219 * @param parseErrors Errors that occurred during parsing
220 */
221 public ParseResult(List<TagCheck> parseChecks, Collection<Throwable> parseErrors) {
222 this.parseChecks = parseChecks;
223 this.parseErrors = parseErrors;
224 }
225 }
226
227 /**
228 * Tag check.
229 */
230 public static class TagCheck implements Predicate<OsmPrimitive> {
231 /** The selector of this {@code TagCheck} */
232 protected final MapCSSRule rule;
233 /** Commands to apply in order to fix a matching primitive */
234 protected final List<FixCommand> fixCommands;
235 /** Tags (or arbitrary strings) of alternatives to be presented to the user */
236 protected final List<String> alternatives;
237 /** An {@link org.openstreetmap.josm.gui.mappaint.mapcss.Instruction.AssignmentInstruction}-{@link Severity} pair.
238 * Is evaluated on the matching primitive to give the error message. Map is checked to contain exactly one element. */
239 protected final Map<Instruction.AssignmentInstruction, Severity> errors;
240 /** MapCSS Classes to set on matching primitives */
241 protected final Collection<String> setClassExpressions;
242 /** Denotes whether the object should be deleted for fixing it */
243 protected boolean deletion;
244 /** A string used to group similar tests */
245 protected String group;
246
247 TagCheck(MapCSSRule rule) {
248 this.rule = rule;
249 this.fixCommands = new ArrayList<>();
250 this.alternatives = new ArrayList<>();
251 this.errors = new HashMap<>();
252 this.setClassExpressions = new HashSet<>();
253 }
254
255 TagCheck(TagCheck check) {
256 this.rule = check.rule;
257 this.fixCommands = Utils.toUnmodifiableList(check.fixCommands);
258 this.alternatives = Utils.toUnmodifiableList(check.alternatives);
259 this.errors = Utils.toUnmodifiableMap(check.errors);
260 this.setClassExpressions = Utils.toUnmodifiableList(check.setClassExpressions);
261 this.deletion = check.deletion;
262 this.group = check.group;
263 }
264
265 TagCheck toImmutable() {
266 return new TagCheck(this);
267 }
268
269 private static final String POSSIBLE_THROWS = "throwError/throwWarning/throwOther";
270
271 static TagCheck ofMapCSSRule(final MapCSSRule rule, AssertionConsumer assertionConsumer) throws IllegalDataException {
272 final TagCheck check = new TagCheck(rule);
273 final Map<String, Boolean> assertions = new HashMap<>();
274 for (Instruction i : rule.declaration.instructions) {
275 if (i instanceof Instruction.AssignmentInstruction) {
276 final Instruction.AssignmentInstruction ai = (Instruction.AssignmentInstruction) i;
277 if (ai.isSetInstruction) {
278 check.setClassExpressions.add(ai.key);
279 continue;
280 }
281 try {
282 final String val = ai.val instanceof Expression
283 ? Optional.ofNullable(((Expression) ai.val).evaluate(new Environment()))
284 .map(Object::toString).map(String::intern).orElse(null)
285 : ai.val instanceof String
286 ? (String) ai.val
287 : ai.val instanceof Keyword
288 ? ((Keyword) ai.val).val
289 : null;
290 if ("throwError".equals(ai.key)) {
291 check.errors.put(ai, Severity.ERROR);
292 } else if ("throwWarning".equals(ai.key)) {
293 check.errors.put(ai, Severity.WARNING);
294 } else if ("throwOther".equals(ai.key)) {
295 check.errors.put(ai, Severity.OTHER);
296 } else if (ai.key.startsWith("throw")) {
297 Logging.log(Logging.LEVEL_WARN,
298 "Unsupported " + ai.key + " instruction. Allowed instructions are " + POSSIBLE_THROWS + '.', null);
299 } else if ("fixAdd".equals(ai.key)) {
300 check.fixCommands.add(FixCommand.fixAdd(ai.val));
301 } else if ("fixRemove".equals(ai.key)) {
302 CheckParameterUtil.ensureThat(!(ai.val instanceof String) || !(val != null && val.contains("=")),
303 "Unexpected '='. Please only specify the key to remove in: " + ai);
304 check.fixCommands.add(FixCommand.fixRemove(ai.val));
305 } else if (val != null && "fixChangeKey".equals(ai.key)) {
306 CheckParameterUtil.ensureThat(val.contains("=>"), "Separate old from new key by '=>'!");
307 final String[] x = val.split("=>", 2);
308 check.fixCommands.add(FixCommand.fixChangeKey(Utils.removeWhiteSpaces(x[0]), Utils.removeWhiteSpaces(x[1])));
309 } else if (val != null && "fixDeleteObject".equals(ai.key)) {
310 CheckParameterUtil.ensureThat("this".equals(val), "fixDeleteObject must be followed by 'this'");
311 check.deletion = true;
312 } else if (val != null && "suggestAlternative".equals(ai.key)) {
313 check.alternatives.add(val);
314 } else if (val != null && "assertMatch".equals(ai.key)) {
315 assertions.put(val, Boolean.TRUE);
316 } else if (val != null && "assertNoMatch".equals(ai.key)) {
317 assertions.put(val, Boolean.FALSE);
318 } else if (val != null && "group".equals(ai.key)) {
319 check.group = val;
320 } else if (ai.key.startsWith("-")) {
321 Logging.debug("Ignoring extension instruction: " + ai.key + ": " + ai.val);
322 } else {
323 throw new IllegalDataException("Cannot add instruction " + ai.key + ": " + ai.val + '!');
324 }
325 } catch (IllegalArgumentException e) {
326 throw new IllegalDataException(e);
327 }
328 }
329 }
330 if (check.errors.isEmpty() && check.setClassExpressions.isEmpty()) {
331 throw new IllegalDataException(
332 "No "+POSSIBLE_THROWS+" given! You should specify a validation error message for " + rule.selectors);
333 } else if (check.errors.size() > 1) {
334 throw new IllegalDataException(
335 "More than one "+POSSIBLE_THROWS+" given! You should specify a single validation error message for "
336 + rule.selectors);
337 }
338 if (assertionConsumer != null) {
339 MapCSSTagCheckerAsserts.checkAsserts(check, assertions, assertionConsumer);
340 }
341 return check.toImmutable();
342 }
343
344 static ParseResult readMapCSS(Reader css) throws ParseException {
345 return readMapCSS(css, null);
346 }
347
348 static ParseResult readMapCSS(Reader css, AssertionConsumer assertionConsumer) throws ParseException {
349 CheckParameterUtil.ensureParameterNotNull(css, "css");
350
351 final MapCSSStyleSource source = new MapCSSStyleSource("");
352 final MapCSSParser preprocessor = new MapCSSParser(css, MapCSSParser.LexicalState.PREPROCESSOR);
353 try (StringReader mapcss = new StringReader(preprocessor.pp_root(source))) {
354 new MapCSSParser(mapcss, MapCSSParser.LexicalState.DEFAULT).sheet(source);
355 }
356 // Ignore "meta" rule(s) from external rules of JOSM wiki
357 source.removeMetaRules();
358 List<TagCheck> parseChecks = new ArrayList<>();
359 for (MapCSSRule rule : source.rules) {
360 try {
361 parseChecks.add(TagCheck.ofMapCSSRule(rule, assertionConsumer));
362 } catch (IllegalDataException e) {
363 Logging.error("Cannot add MapCss rule: "+e.getMessage());
364 source.logError(e);
365 }
366 }
367 return new ParseResult(parseChecks, source.getErrors());
368 }
369
370 @Override
371 public boolean test(OsmPrimitive primitive) {
372 // Tests whether the primitive contains a deprecated tag which is represented by this MapCSSTagChecker.
373 return whichSelectorMatchesPrimitive(primitive) != null;
374 }
375
376 Selector whichSelectorMatchesPrimitive(OsmPrimitive primitive) {
377 return whichSelectorMatchesEnvironment(new Environment(primitive));
378 }
379
380 Selector whichSelectorMatchesEnvironment(Environment env) {
381 return rule.selectors.stream()
382 .filter(i -> i.matches(env.clearSelectorMatchingInformation()))
383 .findFirst()
384 .orElse(null);
385 }
386
387 /**
388 * Determines the {@code index}-th key/value/tag (depending on {@code type}) of the
389 * {@link GeneralSelector}.
390 * @param matchingSelector matching selector
391 * @param index index
392 * @param type selector type ("key", "value" or "tag")
393 * @param p OSM primitive
394 * @return argument value, can be {@code null}
395 */
396 static String determineArgument(GeneralSelector matchingSelector, int index, String type, OsmPrimitive p) {
397 try {
398 final Condition c = matchingSelector.getConditions().get(index);
399 final Tag tag = c instanceof Condition.ToTagConvertable
400 ? ((Condition.ToTagConvertable) c).asTag(p)
401 : null;
402 if (tag == null) {
403 return null;
404 } else if ("key".equals(type)) {
405 return tag.getKey();
406 } else if ("value".equals(type)) {
407 return tag.getValue();
408 } else if ("tag".equals(type)) {
409 return tag.toString();
410 }
411 } catch (IndexOutOfBoundsException ignore) {
412 Logging.debug(ignore);
413 }
414 return null;
415 }
416
417 /**
418 * Replaces occurrences of <code>{i.key}</code>, <code>{i.value}</code>, <code>{i.tag}</code> in {@code s} by the corresponding
419 * key/value/tag of the {@code index}-th {@link Condition} of {@code matchingSelector}.
420 * @param matchingSelector matching selector
421 * @param s any string
422 * @param p OSM primitive
423 * @return string with arguments inserted
424 */
425 static String insertArguments(Selector matchingSelector, String s, OsmPrimitive p) {
426 if (s != null && matchingSelector instanceof Selector.ChildOrParentSelector) {
427 return insertArguments(((Selector.ChildOrParentSelector) matchingSelector).right, s, p);
428 } else if (s == null || !(matchingSelector instanceof GeneralSelector)) {
429 return s;
430 }
431 final Matcher m = Pattern.compile("\\{(\\d+)\\.(key|value|tag)\\}").matcher(s);
432 final StringBuffer sb = new StringBuffer();
433 while (m.find()) {
434 final String argument = determineArgument((GeneralSelector) matchingSelector,
435 Integer.parseInt(m.group(1)), m.group(2), p);
436 try {
437 // Perform replacement with null-safe + regex-safe handling
438 m.appendReplacement(sb, String.valueOf(argument).replace("^(", "").replace(")$", ""));
439 } catch (IndexOutOfBoundsException | IllegalArgumentException e) {
440 Logging.log(Logging.LEVEL_ERROR, tr("Unable to replace argument {0} in {1}: {2}", argument, sb, e.getMessage()), e);
441 }
442 }
443 m.appendTail(sb);
444 return sb.toString();
445 }
446
447 /**
448 * Constructs a fix in terms of a {@link org.openstreetmap.josm.command.Command} for the {@link OsmPrimitive}
449 * if the error is fixable, or {@code null} otherwise.
450 *
451 * @param p the primitive to construct the fix for
452 * @return the fix or {@code null}
453 */
454 Command fixPrimitive(OsmPrimitive p) {
455 if (fixCommands.isEmpty() && !deletion) {
456 return null;
457 }
458 try {
459 final Selector matchingSelector = whichSelectorMatchesPrimitive(p);
460 Collection<Command> cmds = new LinkedList<>();
461 for (FixCommand fixCommand : fixCommands) {
462 cmds.add(fixCommand.createCommand(p, matchingSelector));
463 }
464 if (deletion && !p.isDeleted()) {
465 cmds.add(new DeleteCommand(p));
466 }
467 return new SequenceCommand(tr("Fix of {0}", getDescriptionForMatchingSelector(p, matchingSelector)), cmds);
468 } catch (IllegalArgumentException e) {
469 Logging.error(e);
470 return null;
471 }
472 }
473
474 /**
475 * Constructs a (localized) message for this deprecation check.
476 * @param p OSM primitive
477 *
478 * @return a message
479 */
480 String getMessage(OsmPrimitive p) {
481 if (errors.isEmpty()) {
482 // Return something to avoid NPEs
483 return rule.declaration.toString();
484 } else {
485 final Object val = errors.keySet().iterator().next().val;
486 return String.valueOf(
487 val instanceof Expression
488 ? ((Expression) val).evaluate(new Environment(p))
489 : val
490 );
491 }
492 }
493
494 /**
495 * Constructs a (localized) description for this deprecation check.
496 * @param p OSM primitive
497 *
498 * @return a description (possibly with alternative suggestions)
499 * @see #getDescriptionForMatchingSelector
500 */
501 String getDescription(OsmPrimitive p) {
502 if (alternatives.isEmpty()) {
503 return getMessage(p);
504 } else {
505 /* I18N: {0} is the test error message and {1} is an alternative */
506 return tr("{0}, use {1} instead", getMessage(p), String.join(tr(" or "), alternatives));
507 }
508 }
509
510 /**
511 * Constructs a (localized) description for this deprecation check
512 * where any placeholders are replaced by values of the matched selector.
513 *
514 * @param matchingSelector matching selector
515 * @param p OSM primitive
516 * @return a description (possibly with alternative suggestions)
517 */
518 String getDescriptionForMatchingSelector(OsmPrimitive p, Selector matchingSelector) {
519 return insertArguments(matchingSelector, getDescription(p), p);
520 }
521
522 Severity getSeverity() {
523 return errors.isEmpty() ? null : errors.values().iterator().next();
524 }
525
526 @Override
527 public String toString() {
528 return getDescription(null);
529 }
530
531 /**
532 * Constructs a {@link TestError} for the given primitive, or returns null if the primitive does not give rise to an error.
533 *
534 * @param p the primitive to construct the error for
535 * @return an instance of {@link TestError}, or returns null if the primitive does not give rise to an error.
536 */
537 List<TestError> getErrorsForPrimitive(OsmPrimitive p) {
538 final Environment env = new Environment(p);
539 return getErrorsForPrimitive(p, whichSelectorMatchesEnvironment(env), env, null);
540 }
541
542 private List<TestError> getErrorsForPrimitive(OsmPrimitive p, Selector matchingSelector, Environment env, Test tester) {
543 List<TestError> res = new ArrayList<>();
544 if (matchingSelector != null && !errors.isEmpty()) {
545 final Command fix = fixPrimitive(p);
546 final String description = getDescriptionForMatchingSelector(p, matchingSelector);
547 final String description1 = group == null ? description : group;
548 final String description2 = group == null ? null : description;
549 TestError.Builder errorBuilder = TestError.builder(tester, getSeverity(), 3000)
550 .messageWithManuallyTranslatedDescription(description1, description2, matchingSelector.toString());
551 if (fix != null) {
552 errorBuilder = errorBuilder.fix(() -> fix);
553 }
554 if (env.child instanceof OsmPrimitive) {
555 res.add(errorBuilder.primitives(p, (OsmPrimitive) env.child).build());
556 } else if (env.children != null) {
557 for (IPrimitive c : env.children) {
558 if (c instanceof OsmPrimitive) {
559 errorBuilder = TestError.builder(tester, getSeverity(), 3000)
560 .messageWithManuallyTranslatedDescription(description1, description2,
561 matchingSelector.toString());
562 if (fix != null) {
563 errorBuilder = errorBuilder.fix(() -> fix);
564 }
565 // check if we have special information about highlighted objects */
566 boolean hiliteFound = false;
567 if (env.intersections != null) {
568 Area is = env.intersections.get(c);
569 if (is != null) {
570 errorBuilder = errorBuilder.highlight(is);
571 hiliteFound = true;
572 }
573 }
574 if (env.crossingWaysMap != null && !hiliteFound) {
575 Map<List<Way>, List<WaySegment>> is = env.crossingWaysMap.get(c);
576 if (is != null) {
577 Set<WaySegment> toHilite = new HashSet<>();
578 for (List<WaySegment> wsList : is.values()) {
579 toHilite.addAll(wsList);
580 }
581 errorBuilder = errorBuilder.highlightWaySegments(toHilite);
582 }
583 }
584 res.add(errorBuilder.primitives(p, (OsmPrimitive) c).build());
585 }
586 }
587 } else {
588 res.add(errorBuilder.primitives(p).build());
589 }
590 }
591 return res;
592 }
593
594 }
595
596 static class MapCSSTagCheckerAndRule extends MapCSSTagChecker {
597 public final MapCSSRule rule;
598 private final TagCheck tagCheck;
599 private final String source;
600
601 MapCSSTagCheckerAndRule(MapCSSRule rule) {
602 this.rule = rule;
603 this.tagCheck = null;
604 this.source = "";
605 }
606
607 MapCSSTagCheckerAndRule(TagCheck tagCheck, String source) {
608 this.rule = tagCheck.rule;
609 this.tagCheck = tagCheck;
610 this.source = source;
611 }
612
613 @Override
614 public String toString() {
615 return "MapCSSTagCheckerAndRule [rule=" + rule + ']';
616 }
617
618 @Override
619 public String getSource() {
620 return tr("URL / File: {0}", source);
621 }
622 }
623
624 static MapCSSStyleIndex createMapCSSTagCheckerIndex(MultiMap<String, TagCheck> checks, boolean includeOtherSeverity, boolean allTests) {
625 final MapCSSStyleIndex index = new MapCSSStyleIndex();
626 final Stream<MapCSSRule> ruleStream = checks.values().stream()
627 .flatMap(Collection::stream)
628 // Ignore "information" level checks if not wanted, unless they also set a MapCSS class
629 .filter(c -> includeOtherSeverity || Severity.OTHER != c.getSeverity() || !c.setClassExpressions.isEmpty())
630 .filter(c -> allTests || c.rule.selectors.stream().anyMatch(Selector.ChildOrParentSelector.class::isInstance))
631 .map(c -> c.rule);
632 index.buildIndex(ruleStream);
633 return index;
634 }
635
636 /**
637 * Obtains all {@link TestError}s for the {@link OsmPrimitive} {@code p}.
638 * @param p The OSM primitive
639 * @param includeOtherSeverity if {@code true}, errors of severity {@link Severity#OTHER} (info) will also be returned
640 * @return all errors for the given primitive, with or without those of "info" severity
641 */
642 public synchronized Collection<TestError> getErrorsForPrimitive(OsmPrimitive p, boolean includeOtherSeverity) {
643 final List<TestError> res = new ArrayList<>();
644 if (indexData == null) {
645 indexData = createMapCSSTagCheckerIndex(checks, includeOtherSeverity, ALL_TESTS);
646 }
647
648 Environment env = new Environment(p, new MultiCascade(), Environment.DEFAULT_LAYER, null);
649 env.mpAreaCache = mpAreaCache;
650
651 Iterator<MapCSSRule> candidates = indexData.getRuleCandidates(p);
652 while (candidates.hasNext()) {
653 MapCSSRule r = candidates.next();
654 for (Selector selector : r.selectors) {
655 env.clearSelectorMatchingInformation();
656 if (!selector.matches(env)) { // as side effect env.parent will be set (if s is a child selector)
657 continue;
658 }
659 MapCSSTagCheckerAndRule test = ruleToCheckMap.computeIfAbsent(r, rule -> checks.entrySet().stream()
660 .map(e -> e.getValue().stream()
661 // rule.selectors might be different due to MapCSSStyleIndex, however, the declarations are the same object
662 .filter(c -> c.rule.declaration == rule.declaration)
663 .findFirst()
664 .map(c -> new MapCSSTagCheckerAndRule(c, e.getKey()))
665 .orElse(null))
666 .filter(Objects::nonNull)
667 .findFirst()
668 .orElse(null));
669 TagCheck check = test == null ? null : test.tagCheck;
670 if (check != null) {
671 r.declaration.execute(env);
672 if (!check.errors.isEmpty()) {
673 for (TestError e: check.getErrorsForPrimitive(p, selector, env, test)) {
674 addIfNotSimilar(e, res);
675 }
676 }
677 }
678 }
679 }
680 return res;
681 }
682
683 /**
684 * See #12627
685 * Add error to given list if list doesn't already contain a similar error.
686 * Similar means same code and description and same combination of primitives and same combination of highlighted objects,
687 * but maybe with different orders.
688 * @param toAdd the error to add
689 * @param errors the list of errors
690 */
691 private static void addIfNotSimilar(TestError toAdd, List<TestError> errors) {
692 final boolean isDup = toAdd.getPrimitives().size() >= 2 && errors.stream()
693 .anyMatch(e -> e.getCode() == toAdd.getCode()
694 && e.getMessage().equals(toAdd.getMessage())
695 && e.getPrimitives().size() == toAdd.getPrimitives().size()
696 && e.getPrimitives().containsAll(toAdd.getPrimitives())
697 && highlightedIsEqual(e.getHighlighted(), toAdd.getHighlighted()));
698 if (!isDup)
699 errors.add(toAdd);
700 }
701
702 private static boolean highlightedIsEqual(Collection<?> highlighted, Collection<?> highlighted2) {
703 if (highlighted.size() == highlighted2.size()) {
704 if (!highlighted.isEmpty()) {
705 Object h1 = highlighted.iterator().next();
706 Object h2 = highlighted2.iterator().next();
707 if (h1 instanceof Area && h2 instanceof Area) {
708 return ((Area) h1).equals((Area) h2);
709 }
710 return highlighted.containsAll(highlighted2);
711 }
712 return true;
713 }
714 return false;
715 }
716
717 static Collection<TestError> getErrorsForPrimitive(OsmPrimitive p, boolean includeOtherSeverity,
718 Collection<Set<TagCheck>> checksCol) {
719 // this variant is only used by the assertion tests
720 final List<TestError> r = new ArrayList<>();
721 final Environment env = new Environment(p, new MultiCascade(), Environment.DEFAULT_LAYER, null);
722 env.mpAreaCache = mpAreaCache;
723 for (Set<TagCheck> schecks : checksCol) {
724 for (TagCheck check : schecks) {
725 boolean ignoreError = Severity.OTHER == check.getSeverity() && !includeOtherSeverity;
726 // Do not run "information" level checks if not wanted, unless they also set a MapCSS class
727 if (ignoreError && check.setClassExpressions.isEmpty()) {
728 continue;
729 }
730 final Selector selector = check.whichSelectorMatchesEnvironment(env);
731 if (selector != null) {
732 check.rule.declaration.execute(env);
733 if (!ignoreError && !check.errors.isEmpty()) {
734 r.addAll(check.getErrorsForPrimitive(p, selector, env, new MapCSSTagCheckerAndRule(check.rule)));
735 }
736 }
737 }
738 }
739 return r;
740 }
741
742 /**
743 * Visiting call for primitives.
744 *
745 * @param p The primitive to inspect.
746 */
747 @Override
748 public void check(OsmPrimitive p) {
749 for (TestError e : getErrorsForPrimitive(p, ValidatorPrefHelper.PREF_OTHER.get())) {
750 addIfNotSimilar(e, errors);
751 }
752 if (partialSelection) {
753 tested.add(p);
754 }
755 }
756
757 /**
758 * A handler for assertion error messages (for not fulfilled "assertMatch", "assertNoMatch").
759 */
760 @FunctionalInterface
761 interface AssertionConsumer extends Consumer<String> {
762 }
763
764 /**
765 * Adds a new MapCSS config file from the given URL.
766 * @param url The unique URL of the MapCSS config file
767 * @return List of tag checks and parsing errors, or null
768 * @throws ParseException if the config file does not match MapCSS syntax
769 * @throws IOException if any I/O error occurs
770 * @since 7275
771 */
772 public synchronized ParseResult addMapCSS(String url) throws ParseException, IOException {
773 // Check assertions, useful for development of local files
774 final boolean checkAssertions = Config.getPref().getBoolean("validator.check_assert_local_rules", false) && Utils.isLocalUrl(url);
775 return addMapCSS(url, checkAssertions ? Logging::warn : null);
776 }
777
778 synchronized ParseResult addMapCSS(String url, AssertionConsumer assertionConsumer) throws ParseException, IOException {
779 CheckParameterUtil.ensureParameterNotNull(url, "url");
780 ParseResult result;
781 try (CachedFile cache = new CachedFile(url);
782 InputStream zip = cache.findZipEntryInputStream("validator.mapcss", "");
783 InputStream s = zip != null ? zip : cache.getInputStream();
784 Reader reader = new BufferedReader(UTFInputStreamReader.create(s))) {
785 if (zip != null)
786 I18n.addTexts(cache.getFile());
787 result = TagCheck.readMapCSS(reader, assertionConsumer);
788 checks.remove(url);
789 checks.putAll(url, result.parseChecks);
790 indexData = null;
791 }
792 return result;
793 }
794
795 @Override
796 public synchronized void initialize() throws Exception {
797 checks.clear();
798 indexData = null;
799 for (SourceEntry source : new ValidatorPrefHelper().get()) {
800 if (!source.active) {
801 continue;
802 }
803 String i = source.url;
804 try {
805 if (!i.startsWith("resource:")) {
806 Logging.info(tr("Adding {0} to tag checker", i));
807 } else if (Logging.isDebugEnabled()) {
808 Logging.debug(tr("Adding {0} to tag checker", i));
809 }
810 addMapCSS(i);
811 if (Config.getPref().getBoolean("validator.auto_reload_local_rules", true) && source.isLocal()) {
812 FileWatcher.getDefaultInstance().registerSource(source);
813 }
814 } catch (IOException | IllegalStateException | IllegalArgumentException ex) {
815 Logging.warn(tr("Failed to add {0} to tag checker", i));
816 Logging.log(Logging.LEVEL_WARN, ex);
817 } catch (ParseException | TokenMgrError ex) {
818 Logging.warn(tr("Failed to add {0} to tag checker", i));
819 Logging.warn(ex);
820 }
821 }
822 MapCSSTagCheckerAsserts.clear();
823 }
824
825 /**
826 * Reload tagchecker rule.
827 * @param rule tagchecker rule to reload
828 * @since 12825
829 */
830 public static void reloadRule(SourceEntry rule) {
831 MapCSSTagChecker tagChecker = OsmValidator.getTest(MapCSSTagChecker.class);
832 if (tagChecker != null) {
833 try {
834 tagChecker.addMapCSS(rule.url);
835 } catch (IOException | ParseException | TokenMgrError e) {
836 Logging.warn(e);
837 }
838 }
839 }
840
841 @Override
842 public synchronized void startTest(ProgressMonitor progressMonitor) {
843 super.startTest(progressMonitor);
844 super.setShowElements(true);
845 if (indexData == null) {
846 indexData = createMapCSSTagCheckerIndex(checks, includeOtherSeverityChecks(), ALL_TESTS);
847 }
848 tested.clear();
849 mpAreaCache.clear();
850 }
851
852 @Override
853 public synchronized void endTest() {
854 if (partialSelection && !tested.isEmpty()) {
855 // #14287: see https://josm.openstreetmap.de/ticket/14287#comment:15
856 // execute tests for objects which might contain or cross previously tested elements
857
858 // rebuild index with a reduced set of rules (those that use ChildOrParentSelector) and thus may have left selectors
859 // matching the previously tested elements
860 indexData = createMapCSSTagCheckerIndex(checks, includeOtherSeverityChecks(), ONLY_SELECTED_TESTS);
861
862 Set<OsmPrimitive> surrounding = new HashSet<>();
863 for (OsmPrimitive p : tested) {
864 if (p.getDataSet() != null) {
865 surrounding.addAll(p.getDataSet().searchWays(p.getBBox()));
866 surrounding.addAll(p.getDataSet().searchRelations(p.getBBox()));
867 }
868 }
869 final boolean includeOtherSeverity = includeOtherSeverityChecks();
870 for (OsmPrimitive p : surrounding) {
871 if (tested.contains(p))
872 continue;
873 Collection<TestError> additionalErrors = getErrorsForPrimitive(p, includeOtherSeverity);
874 for (TestError e : additionalErrors) {
875 if (e.getPrimitives().stream().anyMatch(tested::contains))
876 addIfNotSimilar(e, errors);
877 }
878 }
879 tested.clear();
880 }
881 // no need to keep the index, it is quickly build and doubles the memory needs
882 indexData = null;
883 // always clear the cache to make sure that we catch changes in geometry
884 mpAreaCache.clear();
885 super.endTest();
886 }
887}
Note: See TracBrowser for help on using the repository browser.