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

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

see #18802 - MapCSSTagChecker: simplify throwError/throwWarning/throwOther parsing

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