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