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

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

see #9414 - MapCSS-based tagchecker: provide {i.key}, {i.value}, {i.tag} variables to messages/fixes which refer to the corresponding match condition

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