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

Last change on this file since 6651 was 6651, checked in by simon04, 10 years ago

see #9414 - MapCSS validator preference: display extension *.validator.mapcss

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