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

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

see #8465 - use diamond operator where applicable

File size: 21.0 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.io.BufferedReader;
7import java.io.IOException;
8import java.io.Reader;
9import java.util.ArrayList;
10import java.util.Arrays;
11import java.util.Collection;
12import java.util.Collections;
13import java.util.HashMap;
14import java.util.Iterator;
15import java.util.LinkedHashMap;
16import java.util.LinkedList;
17import java.util.List;
18import java.util.Map;
19import java.util.regex.Matcher;
20import java.util.regex.Pattern;
21
22import org.openstreetmap.josm.Main;
23import org.openstreetmap.josm.command.ChangePropertyCommand;
24import org.openstreetmap.josm.command.ChangePropertyKeyCommand;
25import org.openstreetmap.josm.command.Command;
26import org.openstreetmap.josm.command.SequenceCommand;
27import org.openstreetmap.josm.data.osm.OsmPrimitive;
28import org.openstreetmap.josm.data.osm.Tag;
29import org.openstreetmap.josm.data.validation.FixableTestError;
30import org.openstreetmap.josm.data.validation.Severity;
31import org.openstreetmap.josm.data.validation.Test;
32import org.openstreetmap.josm.data.validation.TestError;
33import org.openstreetmap.josm.gui.mappaint.Environment;
34import org.openstreetmap.josm.gui.mappaint.MultiCascade;
35import org.openstreetmap.josm.gui.mappaint.mapcss.Condition;
36import org.openstreetmap.josm.gui.mappaint.mapcss.Expression;
37import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction;
38import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSRule;
39import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
40import org.openstreetmap.josm.gui.mappaint.mapcss.Selector;
41import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.GeneralSelector;
42import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.MapCSSParser;
43import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.ParseException;
44import org.openstreetmap.josm.gui.preferences.validator.ValidatorPreference;
45import org.openstreetmap.josm.gui.preferences.validator.ValidatorTagCheckerRulesPreference;
46import org.openstreetmap.josm.io.MirroredInputStream;
47import org.openstreetmap.josm.io.UTFInputStreamReader;
48import org.openstreetmap.josm.tools.CheckParameterUtil;
49import org.openstreetmap.josm.tools.Predicate;
50import org.openstreetmap.josm.tools.Utils;
51
52/**
53 * MapCSS-based tag checker/fixer.
54 * @since 6506
55 */
56public class MapCSSTagChecker extends Test.TagTest {
57
58 /**
59 * The preference key for tag checker source entries.
60 * @since 6670
61 */
62 public static final String ENTRIES_PREF_KEY = "validator." + MapCSSTagChecker.class.getName() + ".entries";
63
64 /**
65 * Constructs a new {@code MapCSSTagChecker}.
66 */
67 public MapCSSTagChecker() {
68 super(tr("Tag checker (MapCSS based)"), tr("This test checks for errors in tag keys and values."));
69 }
70
71 final List<TagCheck> checks = new ArrayList<>();
72
73 static class TagCheck implements Predicate<OsmPrimitive> {
74 protected final MapCSSRule rule;
75 protected final List<PrimitiveToTag> change = new ArrayList<>();
76 protected final Map<String, String> keyChange = new LinkedHashMap<>();
77 protected final List<String> alternatives = new ArrayList<>();
78 protected final Map<Instruction.AssignmentInstruction, Severity> errors = new HashMap<>();
79 protected final Map<String, Boolean> assertions = new HashMap<>();
80
81 TagCheck(MapCSSRule rule) {
82 this.rule = rule;
83 }
84
85 /**
86 * A function mapping the matched {@link OsmPrimitive} to a {@link Tag}.
87 */
88 abstract static class PrimitiveToTag implements Utils.Function<OsmPrimitive, Tag> {
89
90 private PrimitiveToTag() {
91 // Hide implicit public constructor for utility class
92 }
93
94 /**
95 * Creates a new mapping from an {@code MapCSS} object.
96 * In case of an {@link Expression}, that is evaluated on the matched {@link OsmPrimitive}.
97 * In case of a {@link String}, that is "compiled" to a {@link Tag} instance.
98 */
99 static PrimitiveToTag ofMapCSSObject(final Object obj, final boolean keyOnly) {
100 if (obj instanceof Expression) {
101 return new PrimitiveToTag() {
102 @Override
103 public Tag apply(OsmPrimitive p) {
104 final String s = (String) ((Expression) obj).evaluate(new Environment().withPrimitive(p));
105 return keyOnly? new Tag(s) : Tag.ofString(s);
106 }
107 };
108 } else if (obj instanceof String) {
109 final Tag tag = keyOnly ? new Tag((String) obj) : Tag.ofString((String) obj);
110 return new PrimitiveToTag() {
111 @Override
112 public Tag apply(OsmPrimitive ignore) {
113 return tag;
114 }
115 };
116 } else {
117 return null;
118 }
119 }
120 }
121
122 static TagCheck ofMapCSSRule(final MapCSSRule rule) {
123 final TagCheck check = new TagCheck(rule);
124 boolean containsSetClassExpression = false;
125 for (Instruction i : rule.declaration) {
126 if (i instanceof Instruction.AssignmentInstruction) {
127 final Instruction.AssignmentInstruction ai = (Instruction.AssignmentInstruction) i;
128 if (ai.isSetInstruction) {
129 containsSetClassExpression = true;
130 continue;
131 }
132 final String val = ai.val instanceof Expression
133 ? (String) ((Expression) ai.val).evaluate(new Environment())
134 : ai.val instanceof String
135 ? (String) ai.val
136 : null;
137 if (ai.key.startsWith("throw")) {
138 final Severity severity = Severity.valueOf(ai.key.substring("throw".length()).toUpperCase());
139 check.errors.put(ai, severity);
140 } else if ("fixAdd".equals(ai.key)) {
141 final PrimitiveToTag toTag = PrimitiveToTag.ofMapCSSObject(ai.val, false);
142 check.change.add(toTag);
143 } else if ("fixRemove".equals(ai.key)) {
144 CheckParameterUtil.ensureThat(!(ai.val instanceof String) || !val.contains("="), "Unexpected '='. Please only specify the key to remove!");
145 final PrimitiveToTag toTag = PrimitiveToTag.ofMapCSSObject(ai.val, true);
146 check.change.add(toTag);
147 } else if ("fixChangeKey".equals(ai.key) && val != null) {
148 CheckParameterUtil.ensureThat(val.contains("=>"), "Separate old from new key by '=>'!");
149 final String[] x = val.split("=>", 2);
150 check.keyChange.put(Tag.removeWhiteSpaces(x[0]), Tag.removeWhiteSpaces(x[1]));
151 } else if ("suggestAlternative".equals(ai.key) && val != null) {
152 check.alternatives.add(val);
153 } else if ("assertMatch".equals(ai.key) && val != null) {
154 check.assertions.put(val, true);
155 } else if ("assertNoMatch".equals(ai.key) && val != null) {
156 check.assertions.put(val, false);
157 } else {
158 throw new RuntimeException("Cannot add instruction " + ai.key + ": " + ai.val + "!");
159 }
160 }
161 }
162 if (check.errors.isEmpty() && !containsSetClassExpression) {
163 throw new RuntimeException("No throwError/throwWarning/throwOther given! You should specify a validation error message for " + rule.selectors);
164 } else if (check.errors.size() > 1) {
165 throw new RuntimeException("More than one throwError/throwWarning/throwOther given! You should specify a single validation error message for " + rule.selectors);
166 }
167 return check;
168 }
169
170 static List<TagCheck> readMapCSS(Reader css) throws ParseException {
171 CheckParameterUtil.ensureParameterNotNull(css, "css");
172 return readMapCSS(new MapCSSParser(css));
173 }
174
175 static List<TagCheck> readMapCSS(MapCSSParser css) throws ParseException {
176 CheckParameterUtil.ensureParameterNotNull(css, "css");
177 final MapCSSStyleSource source = new MapCSSStyleSource("");
178 css.sheet(source);
179 assert source.getErrors().isEmpty();
180 // Ignore "meta" rule(s) from external rules of JOSM wiki
181 removeMetaRules(source);
182 return new ArrayList<>(Utils.transform(source.rules, new Utils.Function<MapCSSRule, TagCheck>() {
183 @Override
184 public TagCheck apply(MapCSSRule x) {
185 return TagCheck.ofMapCSSRule(x);
186 }
187 }));
188 }
189
190 private static void removeMetaRules(MapCSSStyleSource source) {
191 for (Iterator<MapCSSRule> it = source.rules.iterator(); it.hasNext(); ) {
192 MapCSSRule x = it.next();
193 if (x.selectors.size() == 1) {
194 Selector sel = x.selectors.get(0);
195 if (sel instanceof GeneralSelector) {
196 GeneralSelector gs = (GeneralSelector) sel;
197 if ("meta".equals(gs.base) && gs.getConditions().isEmpty()) {
198 it.remove();
199 }
200 }
201 }
202 }
203 }
204
205 @Override
206 public boolean evaluate(OsmPrimitive primitive) {
207 return matchesPrimitive(primitive);
208 }
209
210 /**
211 * Tests whether the {@link OsmPrimitive} contains a deprecated tag which is represented by this {@code MapCSSTagChecker}.
212 *
213 * @param primitive the primitive to test
214 * @return true when the primitive contains a deprecated tag
215 * @deprecated since it does not handle MapCSS-classes
216 */
217 @Deprecated
218 boolean matchesPrimitive(OsmPrimitive primitive) {
219 return whichSelectorMatchesPrimitive(primitive) != null;
220 }
221
222 Selector whichSelectorMatchesPrimitive(OsmPrimitive primitive) {
223 return whichSelectorMatchesEnvironment(new Environment().withPrimitive(primitive));
224 }
225
226 Selector whichSelectorMatchesEnvironment(Environment env) {
227 for (Selector i : rule.selectors) {
228 env.clearSelectorMatchingInformation();
229 if (i.matches(env)) {
230 return i;
231 }
232 }
233 return null;
234 }
235
236 /**
237 * Determines the {@code index}-th key/value/tag (depending on {@code type}) of the
238 * {@link org.openstreetmap.josm.gui.mappaint.mapcss.Selector.GeneralSelector}.
239 */
240 static String determineArgument(Selector.GeneralSelector matchingSelector, int index, String type) {
241 try {
242 final Condition c = matchingSelector.getConditions().get(index);
243 final Tag tag = c instanceof Condition.KeyCondition
244 ? ((Condition.KeyCondition) c).asTag()
245 : c instanceof Condition.KeyValueCondition
246 ? ((Condition.KeyValueCondition) c).asTag()
247 : null;
248 if (tag == null) {
249 return null;
250 } else if ("key".equals(type)) {
251 return tag.getKey();
252 } else if ("value".equals(type)) {
253 return tag.getValue();
254 } else if ("tag".equals(type)) {
255 return tag.toString();
256 }
257 } catch (IndexOutOfBoundsException ignore) {
258 Main.debug(ignore.getMessage());
259 }
260 return null;
261 }
262
263 /**
264 * Replaces occurrences of {@code {i.key}}, {@code {i.value}}, {@code {i.tag}} in {@code s} by the corresponding
265 * key/value/tag of the {@code index}-th {@link Condition} of {@code matchingSelector}.
266 */
267 static String insertArguments(Selector matchingSelector, String s) {
268 if (s != null && matchingSelector instanceof Selector.ChildOrParentSelector) {
269 return insertArguments(((Selector.ChildOrParentSelector)matchingSelector).right, s);
270 } else if (s == null || !(matchingSelector instanceof GeneralSelector)) {
271 return s;
272 }
273 final Matcher m = Pattern.compile("\\{(\\d+)\\.(key|value|tag)\\}").matcher(s);
274 final StringBuffer sb = new StringBuffer();
275 while (m.find()) {
276 final String argument = determineArgument((Selector.GeneralSelector) matchingSelector, Integer.parseInt(m.group(1)), m.group(2));
277 try {
278 m.appendReplacement(sb, String.valueOf(argument));
279 } catch (IndexOutOfBoundsException e) {
280 Main.error(tr("Unable to replace argument {0} in {1}: {2}", argument, sb, e.getMessage()));
281 }
282 }
283 m.appendTail(sb);
284 return sb.toString();
285 }
286
287 /**
288 * Constructs a fix in terms of a {@link org.openstreetmap.josm.command.Command} for the {@link OsmPrimitive}
289 * if the error is fixable, or {@code null} otherwise.
290 *
291 * @param p the primitive to construct the fix for
292 * @return the fix or {@code null}
293 */
294 Command fixPrimitive(OsmPrimitive p) {
295 if (change.isEmpty() && keyChange.isEmpty()) {
296 return null;
297 }
298 final Selector matchingSelector = whichSelectorMatchesPrimitive(p);
299 Collection<Command> cmds = new LinkedList<>();
300 for (PrimitiveToTag toTag : change) {
301 final Tag tag = toTag.apply(p);
302 final String key = insertArguments(matchingSelector, tag.getKey());
303 final String value = insertArguments(matchingSelector, tag.getValue());
304 cmds.add(new ChangePropertyCommand(p, key, value));
305 }
306 for (Map.Entry<String, String> i : keyChange.entrySet()) {
307 final String oldKey = insertArguments(matchingSelector, i.getKey());
308 final String newKey = insertArguments(matchingSelector, i.getValue());
309 cmds.add(new ChangePropertyKeyCommand(p, oldKey, newKey));
310 }
311 return new SequenceCommand(tr("Fix of {0}", getDescriptionForMatchingSelector(p, matchingSelector)), cmds);
312 }
313
314 /**
315 * Constructs a (localized) message for this deprecation check.
316 *
317 * @return a message
318 */
319 String getMessage(OsmPrimitive p) {
320 if (errors.isEmpty()) {
321 // Return something to avoid NPEs
322 return rule.declaration.toString();
323 } else {
324 final Object val = errors.keySet().iterator().next().val;
325 return String.valueOf(
326 val instanceof Expression
327 ? ((Expression) val).evaluate(new Environment().withPrimitive(p))
328 : val
329 );
330 }
331 }
332
333 /**
334 * Constructs a (localized) description for this deprecation check.
335 *
336 * @return a description (possibly with alternative suggestions)
337 * @see #getDescriptionForMatchingSelector
338 */
339 String getDescription(OsmPrimitive p) {
340 if (alternatives.isEmpty()) {
341 return getMessage(p);
342 } else {
343 /* I18N: {0} is the test error message and {1} is an alternative */
344 return tr("{0}, use {1} instead", getMessage(p), Utils.join(tr(" or "), alternatives));
345 }
346 }
347
348 /**
349 * Constructs a (localized) description for this deprecation check
350 * where any placeholders are replaced by values of the matched selector.
351 *
352 * @return a description (possibly with alternative suggestions)
353 */
354 String getDescriptionForMatchingSelector(OsmPrimitive p, Selector matchingSelector) {
355 return insertArguments(matchingSelector, getDescription(p));
356 }
357
358 Severity getSeverity() {
359 return errors.isEmpty() ? null : errors.values().iterator().next();
360 }
361
362 @Override
363 public String toString() {
364 return getDescription(null);
365 }
366
367 /**
368 * Constructs a {@link TestError} for the given primitive, or returns null if the primitive does not give rise to an error.
369 *
370 * @param p the primitive to construct the error for
371 * @return an instance of {@link TestError}, or returns null if the primitive does not give rise to an error.
372 */
373 TestError getErrorForPrimitive(OsmPrimitive p) {
374 final Environment env = new Environment().withPrimitive(p);
375 return getErrorForPrimitive(p, whichSelectorMatchesEnvironment(env), env);
376 }
377
378 TestError getErrorForPrimitive(OsmPrimitive p, Selector matchingSelector, Environment env) {
379 if (matchingSelector != null && !errors.isEmpty()) {
380 final Command fix = fixPrimitive(p);
381 final String description = getDescriptionForMatchingSelector(p, matchingSelector);
382 final List<OsmPrimitive> primitives;
383 if (env.child != null) {
384 primitives = Arrays.asList(p, env.child);
385 } else {
386 primitives = Collections.singletonList(p);
387 }
388 if (fix != null) {
389 return new FixableTestError(null, getSeverity(), description, null, matchingSelector.toString(), 3000, primitives, fix);
390 } else {
391 return new TestError(null, getSeverity(), description, null, matchingSelector.toString(), 3000, primitives);
392 }
393 } else {
394 return null;
395 }
396 }
397 }
398
399 static class MapCSSTagCheckerAndRule extends MapCSSTagChecker {
400 public final MapCSSRule rule;
401
402 MapCSSTagCheckerAndRule(MapCSSRule rule) {
403 this.rule = rule;
404 }
405
406 @Override
407 public boolean equals(Object obj) {
408 return super.equals(obj)
409 || (obj instanceof TagCheck && rule.equals(((TagCheck) obj).rule))
410 || (obj instanceof MapCSSRule && rule.equals(obj));
411 }
412 }
413
414 /**
415 * Obtains all {@link TestError}s for the {@link OsmPrimitive} {@code p}.
416 */
417 public Collection<TestError> getErrorsForPrimitive(OsmPrimitive p, boolean includeOtherSeverity) {
418 final ArrayList<TestError> r = new ArrayList<>();
419 final Environment env = new Environment(p, new MultiCascade(), Environment.DEFAULT_LAYER, null);
420 for (TagCheck check : checks) {
421 if (Severity.OTHER.equals(check.getSeverity()) && !includeOtherSeverity) {
422 continue;
423 }
424 final Selector selector = check.whichSelectorMatchesEnvironment(env);
425 if (selector != null) {
426 check.rule.execute(env);
427 final TestError error = check.getErrorForPrimitive(p, selector, env);
428 if (error != null) {
429 error.setTester(new MapCSSTagCheckerAndRule(check.rule));
430 r.add(error);
431 }
432 }
433 }
434 return r;
435 }
436
437 /**
438 * Visiting call for primitives.
439 *
440 * @param p The primitive to inspect.
441 */
442 @Override
443 public void check(OsmPrimitive p) {
444 errors.addAll(getErrorsForPrimitive(p, ValidatorPreference.PREF_OTHER.get()));
445 }
446
447 /**
448 * Adds a new MapCSS config file from the given {@code Reader}.
449 * @param css The reader
450 * @throws ParseException if the config file does not match MapCSS syntax
451 */
452 public void addMapCSS(Reader css) throws ParseException {
453 checks.addAll(TagCheck.readMapCSS(css));
454 }
455
456 @Override
457 public synchronized void initialize() throws Exception {
458 checks.clear();
459 for (String i : new ValidatorTagCheckerRulesPreference.RulePrefHelper().getActiveUrls()) {
460 try {
461 if (i.startsWith("resource:")) {
462 Main.debug(tr("Adding {0} to tag checker", i));
463 } else {
464 Main.info(tr("Adding {0} to tag checker", i));
465 }
466 MirroredInputStream s = new MirroredInputStream(i);
467 try {
468 addMapCSS(new BufferedReader(UTFInputStreamReader.create(s)));
469 } finally {
470 Utils.close(s);
471 }
472 } catch (IOException ex) {
473 Main.warn(tr("Failed to add {0} to tag checker", i));
474 Main.warn(ex, false);
475 } catch (Exception ex) {
476 Main.warn(tr("Failed to add {0} to tag checker", i));
477 Main.warn(ex);
478 }
479 }
480 }
481}
Note: See TracBrowser for help on using the repository browser.