source: josm/trunk/src/org/openstreetmap/josm/data/validation/tests/TagChecker.java@ 14704

Last change on this file since 14704 was 14704, checked in by GerdP, 5 years ago

fix #17219 TagChecker.isTagInPresets() doesn't work

  • Property svn:eol-style set to native
File size: 43.8 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.marktr;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.GridBagConstraints;
8import java.awt.event.ActionListener;
9import java.io.BufferedReader;
10import java.io.IOException;
11import java.util.ArrayList;
12import java.util.Arrays;
13import java.util.Collection;
14import java.util.Collections;
15import java.util.HashMap;
16import java.util.HashSet;
17import java.util.List;
18import java.util.Locale;
19import java.util.Map;
20import java.util.Map.Entry;
21import java.util.Set;
22import java.util.regex.Matcher;
23import java.util.regex.Pattern;
24import java.util.regex.PatternSyntaxException;
25
26import javax.swing.JCheckBox;
27import javax.swing.JLabel;
28import javax.swing.JPanel;
29
30import org.openstreetmap.josm.command.ChangePropertyCommand;
31import org.openstreetmap.josm.command.ChangePropertyKeyCommand;
32import org.openstreetmap.josm.command.Command;
33import org.openstreetmap.josm.command.SequenceCommand;
34import org.openstreetmap.josm.data.osm.AbstractPrimitive;
35import org.openstreetmap.josm.data.osm.OsmPrimitive;
36import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
37import org.openstreetmap.josm.data.osm.OsmUtils;
38import org.openstreetmap.josm.data.osm.Tag;
39import org.openstreetmap.josm.data.osm.Tagged;
40import org.openstreetmap.josm.data.preferences.sources.ValidatorPrefHelper;
41import org.openstreetmap.josm.data.validation.Severity;
42import org.openstreetmap.josm.data.validation.Test.TagTest;
43import org.openstreetmap.josm.data.validation.TestError;
44import org.openstreetmap.josm.data.validation.util.Entities;
45import org.openstreetmap.josm.gui.progress.ProgressMonitor;
46import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset;
47import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetItem;
48import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets;
49import org.openstreetmap.josm.gui.tagging.presets.items.Check;
50import org.openstreetmap.josm.gui.tagging.presets.items.CheckGroup;
51import org.openstreetmap.josm.gui.tagging.presets.items.KeyedItem;
52import org.openstreetmap.josm.gui.widgets.EditableList;
53import org.openstreetmap.josm.io.CachedFile;
54import org.openstreetmap.josm.spi.preferences.Config;
55import org.openstreetmap.josm.tools.GBC;
56import org.openstreetmap.josm.tools.Logging;
57import org.openstreetmap.josm.tools.MultiMap;
58import org.openstreetmap.josm.tools.Utils;
59
60/**
61 * Check for misspelled or wrong tags
62 *
63 * @author frsantos
64 * @since 3669
65 */
66public class TagChecker extends TagTest {
67
68 /** The config file of ignored tags */
69 public static final String IGNORE_FILE = "resource://data/validator/ignoretags.cfg";
70 /** The config file of dictionary words */
71 public static final String SPELL_FILE = "resource://data/validator/words.cfg";
72
73 /** Normalized keys: the key should be substituted by the value if the key was not found in presets */
74 private static final Map<String, String> harmonizedKeys = new HashMap<>();
75 /** The spell check preset values which are not stored in TaggingPresets */
76 private static volatile MultiMap<String, String> additionalPresetsValueData;
77 /** The spell check preset values which are not stored in TaggingPresets */
78 private static volatile MultiMap<String, String> oftenUsedValueData = new MultiMap<>();
79
80 /** The TagChecker data */
81 private static final List<CheckerData> checkerData = new ArrayList<>();
82 private static final List<String> ignoreDataStartsWith = new ArrayList<>();
83 private static final Set<String> ignoreDataEquals = new HashSet<>();
84 private static final List<String> ignoreDataEndsWith = new ArrayList<>();
85 private static final List<Tag> ignoreDataTag = new ArrayList<>();
86 /** tag keys that have only numerical values in the presets */
87 private static final Set<String> ignoreForLevenshtein = new HashSet<>();
88
89 /** The preferences prefix */
90 protected static final String PREFIX = ValidatorPrefHelper.PREFIX + "." + TagChecker.class.getSimpleName();
91
92 /**
93 * The preference key to check values
94 */
95 public static final String PREF_CHECK_VALUES = PREFIX + ".checkValues";
96 /**
97 * The preference key to check keys
98 */
99 public static final String PREF_CHECK_KEYS = PREFIX + ".checkKeys";
100 /**
101 * The preference key to enable complex checks
102 */
103 public static final String PREF_CHECK_COMPLEX = PREFIX + ".checkComplex";
104 /**
105 * The preference key to search for fixme tags
106 */
107 public static final String PREF_CHECK_FIXMES = PREFIX + ".checkFixmes";
108
109 /**
110 * The preference key for source files
111 * @see #DEFAULT_SOURCES
112 */
113 public static final String PREF_SOURCES = PREFIX + ".source";
114
115 /**
116 * The preference key to check keys - used before upload
117 */
118 public static final String PREF_CHECK_KEYS_BEFORE_UPLOAD = PREF_CHECK_KEYS + "BeforeUpload";
119 /**
120 * The preference key to check values - used before upload
121 */
122 public static final String PREF_CHECK_VALUES_BEFORE_UPLOAD = PREF_CHECK_VALUES + "BeforeUpload";
123 /**
124 * The preference key to run complex tests - used before upload
125 */
126 public static final String PREF_CHECK_COMPLEX_BEFORE_UPLOAD = PREF_CHECK_COMPLEX + "BeforeUpload";
127 /**
128 * The preference key to search for fixmes - used before upload
129 */
130 public static final String PREF_CHECK_FIXMES_BEFORE_UPLOAD = PREF_CHECK_FIXMES + "BeforeUpload";
131
132 private static final int MAX_LEVENSHTEIN_DISTANCE = 2;
133
134 protected boolean checkKeys;
135 protected boolean checkValues;
136 protected boolean checkComplex;
137 protected boolean checkFixmes;
138
139 protected JCheckBox prefCheckKeys;
140 protected JCheckBox prefCheckValues;
141 protected JCheckBox prefCheckComplex;
142 protected JCheckBox prefCheckFixmes;
143 protected JCheckBox prefCheckPaint;
144
145 protected JCheckBox prefCheckKeysBeforeUpload;
146 protected JCheckBox prefCheckValuesBeforeUpload;
147 protected JCheckBox prefCheckComplexBeforeUpload;
148 protected JCheckBox prefCheckFixmesBeforeUpload;
149 protected JCheckBox prefCheckPaintBeforeUpload;
150
151 // CHECKSTYLE.OFF: SingleSpaceSeparator
152 protected static final int EMPTY_VALUES = 1200;
153 protected static final int INVALID_KEY = 1201;
154 protected static final int INVALID_VALUE = 1202;
155 protected static final int FIXME = 1203;
156 protected static final int INVALID_SPACE = 1204;
157 protected static final int INVALID_KEY_SPACE = 1205;
158 protected static final int INVALID_HTML = 1206; /* 1207 was PAINT */
159 protected static final int LONG_VALUE = 1208;
160 protected static final int LONG_KEY = 1209;
161 protected static final int LOW_CHAR_VALUE = 1210;
162 protected static final int LOW_CHAR_KEY = 1211;
163 protected static final int MISSPELLED_VALUE = 1212;
164 protected static final int MISSPELLED_KEY = 1213;
165 protected static final int MULTIPLE_SPACES = 1214;
166 protected static final int MISSPELLED_VALUE_NO_FIX = 1215;
167 // CHECKSTYLE.ON: SingleSpaceSeparator
168 // 1250 and up is used by tagcheck
169
170 protected EditableList sourcesList;
171
172 private static final List<String> DEFAULT_SOURCES = Arrays.asList(/*DATA_FILE, */IGNORE_FILE, SPELL_FILE);
173
174 /**
175 * Constructor
176 */
177 public TagChecker() {
178 super(tr("Tag checker"), tr("This test checks for errors in tag keys and values."));
179 }
180
181 @Override
182 public void initialize() throws IOException {
183 initializeData();
184 initializePresets();
185 analysePresets();
186 }
187
188 /**
189 * Add presets that contain only numerical values to the ignore list
190 */
191 private void analysePresets() {
192 for (String key : TaggingPresets.getPresetKeys()) {
193 if (isKeyIgnored(key))
194 continue;
195 boolean allNumerical = true;
196 Set<String> values = TaggingPresets.getPresetValues(key);
197 if (values.isEmpty())
198 allNumerical = false;
199 for (String val : values) {
200 if (!isNum(val)) {
201 allNumerical = false;
202 break;
203 }
204 }
205 if (allNumerical) {
206 ignoreForLevenshtein.add(key);
207 }
208 }
209 }
210
211 /**
212 * Reads the spellcheck file into a HashMap.
213 * The data file is a list of words, beginning with +/-. If it starts with +,
214 * the word is valid, but if it starts with -, the word should be replaced
215 * by the nearest + word before this.
216 *
217 * @throws IOException if any I/O error occurs
218 */
219 private static void initializeData() throws IOException {
220 checkerData.clear();
221 ignoreDataStartsWith.clear();
222 ignoreDataEquals.clear();
223 ignoreDataEndsWith.clear();
224 ignoreDataTag.clear();
225 harmonizedKeys.clear();
226 ignoreForLevenshtein.clear();
227
228 StringBuilder errorSources = new StringBuilder();
229 for (String source : Config.getPref().getList(PREF_SOURCES, DEFAULT_SOURCES)) {
230 try (
231 CachedFile cf = new CachedFile(source);
232 BufferedReader reader = cf.getContentReader()
233 ) {
234 String okValue = null;
235 boolean tagcheckerfile = false;
236 boolean ignorefile = false;
237 boolean isFirstLine = true;
238 String line;
239 while ((line = reader.readLine()) != null && (tagcheckerfile || !line.isEmpty())) {
240 if (line.startsWith("#")) {
241 if (line.startsWith("# JOSM TagChecker")) {
242 tagcheckerfile = true;
243 if (!DEFAULT_SOURCES.contains(source)) {
244 Logging.info(tr("Adding {0} to tag checker", source));
245 }
246 } else
247 if (line.startsWith("# JOSM IgnoreTags")) {
248 ignorefile = true;
249 if (!DEFAULT_SOURCES.contains(source)) {
250 Logging.info(tr("Adding {0} to ignore tags", source));
251 }
252 }
253 } else if (ignorefile) {
254 line = line.trim();
255 if (line.length() < 4) {
256 continue;
257 }
258 try {
259 String key = line.substring(0, 2);
260 line = line.substring(2);
261
262 switch (key) {
263 case "S:":
264 ignoreDataStartsWith.add(line);
265 break;
266 case "E:":
267 ignoreDataEquals.add(line);
268 break;
269 case "F:":
270 ignoreDataEndsWith.add(line);
271 break;
272 case "K:":
273 Tag tag = Tag.ofString(line);
274 ignoreDataTag.add(tag);
275 oftenUsedValueData.put(tag.getKey(), tag.getValue());
276 break;
277 default:
278 if (!key.startsWith(";")) {
279 Logging.warn("Unsupported TagChecker key: " + key);
280 }
281 }
282 } catch (IllegalArgumentException e) {
283 Logging.error("Invalid line in {0} : {1}", source, e.getMessage());
284 Logging.trace(e);
285 }
286 } else if (tagcheckerfile) {
287 if (!line.isEmpty()) {
288 CheckerData d = new CheckerData();
289 String err = d.getData(line);
290
291 if (err == null) {
292 checkerData.add(d);
293 } else {
294 Logging.error(tr("Invalid tagchecker line - {0}: {1}", err, line));
295 }
296 }
297 } else if (line.charAt(0) == '+') {
298 okValue = line.substring(1);
299 } else if (line.charAt(0) == '-' && okValue != null) {
300 String hk = harmonizeKey(line.substring(1));
301 if (!okValue.equals(hk)) {
302 if (harmonizedKeys.put(hk, okValue) != null) {
303 Logging.debug(tr("Line was ignored: {0}", line));
304 }
305 }
306 } else {
307 Logging.error(tr("Invalid spellcheck line: {0}", line));
308 }
309 if (isFirstLine) {
310 isFirstLine = false;
311 if (!(tagcheckerfile || ignorefile) && !DEFAULT_SOURCES.contains(source)) {
312 Logging.info(tr("Adding {0} to spellchecker", source));
313 }
314 }
315 }
316 } catch (IOException e) {
317 Logging.error(e);
318 errorSources.append(source).append('\n');
319 }
320 }
321
322 if (errorSources.length() > 0)
323 throw new IOException(tr("Could not access data file(s):\n{0}", errorSources));
324 }
325
326 /**
327 * Reads the presets data.
328 *
329 */
330 public static void initializePresets() {
331
332 if (!Config.getPref().getBoolean(PREF_CHECK_VALUES, true))
333 return;
334
335 Collection<TaggingPreset> presets = TaggingPresets.getTaggingPresets();
336 if (!presets.isEmpty()) {
337 additionalPresetsValueData = new MultiMap<>();
338 for (String a : AbstractPrimitive.getUninterestingKeys()) {
339 additionalPresetsValueData.putVoid(a);
340 }
341 // TODO directionKeys are no longer in OsmPrimitive (search pattern is used instead)
342 for (String a : Config.getPref().getList(ValidatorPrefHelper.PREFIX + ".knownkeys",
343 Arrays.asList("is_in", "int_ref", "fixme", "population"))) {
344 additionalPresetsValueData.putVoid(a);
345 }
346 for (TaggingPreset p : presets) {
347 for (TaggingPresetItem i : p.data) {
348 if (i instanceof KeyedItem) {
349 addPresetValue((KeyedItem) i);
350 } else if (i instanceof CheckGroup) {
351 for (Check c : ((CheckGroup) i).checks) {
352 addPresetValue(c);
353 }
354 }
355 }
356 }
357 }
358 }
359
360 private static void addPresetValue(KeyedItem ky) {
361 if (ky.key != null && ky.getValues() != null) {
362 String hk = harmonizeKey(ky.key);
363 if (!ky.key.equals(hk)) {
364 harmonizedKeys.put(hk, ky.key);
365 }
366 }
367 }
368
369 /**
370 * Checks given string (key or value) if it contains characters with code below 0x20 (either newline or some other special characters)
371 * @param s string to check
372 * @return {@code true} if {@code s} contains characters with code below 0x20
373 */
374 private static boolean containsLow(String s) {
375 if (s == null)
376 return false;
377 for (int i = 0; i < s.length(); i++) {
378 if (s.charAt(i) < 0x20)
379 return true;
380 }
381 return false;
382 }
383
384 private static Set<String> getPresetValues(String key) {
385 Set<String> res = TaggingPresets.getPresetValues(key);
386 if (res != null)
387 return res;
388 return additionalPresetsValueData.get(key);
389 }
390
391 /**
392 * Determines if the given key is in internal presets.
393 * @param key key
394 * @return {@code true} if the given key is in internal presets
395 * @since 9023
396 */
397 public static boolean isKeyInPresets(String key) {
398 return TaggingPresets.getPresetValues(key) != null;
399 }
400
401 /**
402 * Determines if the given tag is in internal presets.
403 * @param key key
404 * @param value value
405 * @return {@code true} if the given tag is in internal presets
406 * @since 9023
407 */
408 public static boolean isTagInPresets(String key, String value) {
409 final Set<String> values = getPresetValues(key);
410 return values != null && values.contains(value);
411 }
412
413 /**
414 * Returns the list of ignored tags.
415 * @return the list of ignored tags
416 * @since 9023
417 */
418 public static List<Tag> getIgnoredTags() {
419 return new ArrayList<>(ignoreDataTag);
420 }
421
422 /**
423 * Determines if the given tag key is ignored for checks "key/tag not in presets".
424 * @param key key
425 * @return true if the given key is ignored
426 */
427 private static boolean isKeyIgnored(String key) {
428 if (ignoreDataEquals.contains(key)) {
429 return true;
430 }
431 for (String a : ignoreDataStartsWith) {
432 if (key.startsWith(a)) {
433 return true;
434 }
435 }
436 for (String a : ignoreDataEndsWith) {
437 if (key.endsWith(a)) {
438 return true;
439 }
440 }
441 return false;
442 }
443
444 /**
445 * Determines if the given tag is ignored for checks "key/tag not in presets".
446 * @param key key
447 * @param value value
448 * @return {@code true} if the given tag is ignored
449 * @since 9023
450 */
451 public static boolean isTagIgnored(String key, String value) {
452 if (isKeyIgnored(key))
453 return true;
454 final Set<String> values = getPresetValues(key);
455 if (values != null && values.isEmpty())
456 return true;
457 if (!isTagInPresets(key, value)) {
458 for (Tag a : ignoreDataTag) {
459 if (key.equals(a.getKey()) && value.equals(a.getValue())) {
460 return true;
461 }
462 }
463 }
464 return false;
465 }
466
467 /**
468 * Checks the primitive tags
469 * @param p The primitive to check
470 */
471 @Override
472 public void check(OsmPrimitive p) {
473 if (!p.isTagged())
474 return;
475
476 // Just a collection to know if a primitive has been already marked with error
477 MultiMap<OsmPrimitive, String> withErrors = new MultiMap<>();
478
479 if (checkComplex) {
480 Map<String, String> keys = p.getKeys();
481 for (CheckerData d : checkerData) {
482 if (d.match(p, keys)) {
483 errors.add(TestError.builder(this, d.getSeverity(), d.getCode())
484 .message(tr("Suspicious tag/value combinations"), d.getDescription())
485 .primitives(p)
486 .build());
487 withErrors.put(p, "TC");
488 }
489 }
490 }
491
492 for (Entry<String, String> prop : p.getKeys().entrySet()) {
493 String s = marktr("Tag ''{0}'' invalid.");
494 String key = prop.getKey();
495 String value = prop.getValue();
496 if (checkValues && (containsLow(value)) && !withErrors.contains(p, "ICV")) {
497 errors.add(TestError.builder(this, Severity.WARNING, LOW_CHAR_VALUE)
498 .message(tr("Tag value contains character with code less than 0x20"), s, key)
499 .primitives(p)
500 .build());
501 withErrors.put(p, "ICV");
502 }
503 if (checkKeys && (containsLow(key)) && !withErrors.contains(p, "ICK")) {
504 errors.add(TestError.builder(this, Severity.WARNING, LOW_CHAR_KEY)
505 .message(tr("Tag key contains character with code less than 0x20"), s, key)
506 .primitives(p)
507 .build());
508 withErrors.put(p, "ICK");
509 }
510 if (checkValues && (value != null && value.length() > Tagged.MAX_TAG_LENGTH) && !withErrors.contains(p, "LV")) {
511 errors.add(TestError.builder(this, Severity.ERROR, LONG_VALUE)
512 .message(tr("Tag value longer than {0} characters ({1} characters)", Tagged.MAX_TAG_LENGTH, value.length()), s, key)
513 .primitives(p)
514 .build());
515 withErrors.put(p, "LV");
516 }
517 if (checkKeys && (key != null && key.length() > Tagged.MAX_TAG_LENGTH) && !withErrors.contains(p, "LK")) {
518 errors.add(TestError.builder(this, Severity.ERROR, LONG_KEY)
519 .message(tr("Tag key longer than {0} characters ({1} characters)", Tagged.MAX_TAG_LENGTH, key.length()), s, key)
520 .primitives(p)
521 .build());
522 withErrors.put(p, "LK");
523 }
524 if (checkValues && (value == null || value.trim().isEmpty()) && !withErrors.contains(p, "EV")) {
525 errors.add(TestError.builder(this, Severity.WARNING, EMPTY_VALUES)
526 .message(tr("Tags with empty values"), s, key)
527 .primitives(p)
528 .build());
529 withErrors.put(p, "EV");
530 }
531 if (checkKeys && key != null && key.indexOf(' ') >= 0 && !withErrors.contains(p, "IPK")) {
532 errors.add(TestError.builder(this, Severity.WARNING, INVALID_KEY_SPACE)
533 .message(tr("Invalid white space in property key"), s, key)
534 .primitives(p)
535 .build());
536 withErrors.put(p, "IPK");
537 }
538 if (checkValues && value != null && (value.startsWith(" ") || value.endsWith(" ")) && !withErrors.contains(p, "SPACE")) {
539 errors.add(TestError.builder(this, Severity.WARNING, INVALID_SPACE)
540 .message(tr("Property values start or end with white space"), s, key)
541 .primitives(p)
542 .build());
543 withErrors.put(p, "SPACE");
544 }
545 if (checkValues && value != null && value.contains(" ") && !withErrors.contains(p, "SPACE")) {
546 errors.add(TestError.builder(this, Severity.WARNING, MULTIPLE_SPACES)
547 .message(tr("Property values contain multiple white spaces"), s, key)
548 .primitives(p)
549 .build());
550 withErrors.put(p, "SPACE");
551 }
552 if (checkValues && value != null && !value.equals(Entities.unescape(value)) && !withErrors.contains(p, "HTML")) {
553 errors.add(TestError.builder(this, Severity.OTHER, INVALID_HTML)
554 .message(tr("Property values contain HTML entity"), s, key)
555 .primitives(p)
556 .build());
557 withErrors.put(p, "HTML");
558 }
559 if (checkValues && key != null && value != null && !value.isEmpty() && additionalPresetsValueData != null
560 && !isTagIgnored(key, value)) {
561 if (!isKeyInPresets(key)) {
562 String prettifiedKey = harmonizeKey(key);
563 String fixedKey = isKeyInPresets(prettifiedKey) ? prettifiedKey : harmonizedKeys.get(prettifiedKey);
564 if (fixedKey != null && !"".equals(fixedKey) && !fixedKey.equals(key)) {
565 // misspelled preset key
566 final TestError.Builder error = TestError.builder(this, Severity.WARNING, MISSPELLED_KEY)
567 .message(tr("Misspelled property key"), marktr("Key ''{0}'' looks like ''{1}''."), key, fixedKey)
568 .primitives(p);
569 if (p.hasKey(fixedKey)) {
570 errors.add(error.build());
571 } else {
572 errors.add(error.fix(() -> new ChangePropertyKeyCommand(p, key, fixedKey)).build());
573 }
574 withErrors.put(p, "WPK");
575 } else {
576 errors.add(TestError.builder(this, Severity.OTHER, INVALID_VALUE)
577 .message(tr("Presets do not contain property key"), marktr("Key ''{0}'' not in presets."), key)
578 .primitives(p)
579 .build());
580 withErrors.put(p, "UPK");
581 }
582 } else if (!isTagInPresets(key, value)) {
583 tryGuess(p, key, value, withErrors);
584 }
585 }
586 if (checkFixmes && key != null && value != null && !value.isEmpty() && isFixme(key, value) && !withErrors.contains(p, "FIXME")) {
587 errors.add(TestError.builder(this, Severity.OTHER, FIXME)
588 .message(tr("FIXMES"))
589 .primitives(p)
590 .build());
591 withErrors.put(p, "FIXME");
592 }
593 }
594 }
595
596 private void tryGuess(OsmPrimitive p, String key, String value, MultiMap<OsmPrimitive, String> withErrors) {
597 // try to fix common typos and check again if value is still unknown
598 final String harmonizedValue = harmonizeValue(value);
599 String fixedValue = null;
600 Set<String> presetValues = getPresetValues(key);
601 Set<String> oftenUsedValues = oftenUsedValueData.get(key);
602 for (Set<String> possibleValues: Arrays.asList(presetValues, oftenUsedValues)) {
603 if (possibleValues != null && possibleValues.contains(harmonizedValue)) {
604 fixedValue = harmonizedValue;
605 break;
606 }
607 }
608 if (fixedValue == null && !ignoreForLevenshtein.contains(key)) {
609 int maxPresetValueLen = 0;
610 List<String> fixVals = new ArrayList<>();
611 // use Levenshtein distance to find typical typos
612 int minDist = MAX_LEVENSHTEIN_DISTANCE + 1;
613 String closest = null;
614 for (Set<String> possibleValues: Arrays.asList(presetValues, oftenUsedValues)) {
615 if (possibleValues == null)
616 continue;
617 for (String possibleVal : possibleValues) {
618 if (possibleVal.isEmpty())
619 continue;
620 maxPresetValueLen = Math.max(maxPresetValueLen, possibleVal.length());
621 if (harmonizedValue.length() < 3 && possibleVal.length() >= harmonizedValue.length() + MAX_LEVENSHTEIN_DISTANCE) {
622 // don't suggest fix value when given value is short and lengths are too different
623 // for example surface=u would result in surface=mud
624 continue;
625 }
626 int dist = Utils.getLevenshteinDistance(possibleVal, harmonizedValue);
627 if (dist >= harmonizedValue.length()) {
628 // short value, all characters are different. Don't warn, might say Value '10' for key 'fee' looks like 'no'.
629 continue;
630 }
631 if (dist < minDist) {
632 closest = possibleVal;
633 minDist = dist;
634 fixVals.clear();
635 fixVals.add(possibleVal);
636 } else if (dist == minDist) {
637 fixVals.add(possibleVal);
638 }
639 }
640 }
641
642 if (minDist <= MAX_LEVENSHTEIN_DISTANCE && maxPresetValueLen > MAX_LEVENSHTEIN_DISTANCE
643 && (harmonizedValue.length() > 3 || minDist < MAX_LEVENSHTEIN_DISTANCE)) {
644 if (fixVals.size() < 2) {
645 fixedValue = closest;
646 } else {
647 Collections.sort(fixVals);
648 // misspelled preset value with multiple good alternatives
649 errors.add(TestError.builder(this, Severity.WARNING, MISSPELLED_VALUE_NO_FIX)
650 .message(tr("Unknown property value"),
651 marktr("Value ''{0}'' for key ''{1}'' is unknown, maybe one of {2} is meant?"),
652 value, key, fixVals)
653 .primitives(p).build());
654 withErrors.put(p, "WPV");
655 return;
656 }
657 }
658 }
659 if (fixedValue != null) {
660 final String newValue = fixedValue;
661 // misspelled preset value
662 errors.add(TestError.builder(this, Severity.WARNING, MISSPELLED_VALUE)
663 .message(tr("Unknown property value"),
664 marktr("Value ''{0}'' for key ''{1}'' is unknown, maybe ''{2}'' is meant?"), value, key, newValue)
665 .primitives(p)
666 .build());
667 withErrors.put(p, "WPV");
668 } else {
669 // unknown preset value
670 errors.add(TestError.builder(this, Severity.OTHER, INVALID_VALUE)
671 .message(tr("Presets do not contain property value"),
672 marktr("Value ''{0}'' for key ''{1}'' not in presets."), value, key)
673 .primitives(p)
674 .build());
675 withErrors.put(p, "UPV");
676 }
677 }
678
679 private boolean isNum(String harmonizedValue) {
680 try {
681 Double.parseDouble(harmonizedValue);
682 return true;
683 } catch (NumberFormatException e) {
684 return false;
685 }
686 }
687
688 private static boolean isFixme(String key, String value) {
689 return key.toLowerCase(Locale.ENGLISH).contains("fixme") || key.contains("todo")
690 || value.toLowerCase(Locale.ENGLISH).contains("fixme") || value.contains("check and delete");
691 }
692
693 private static String harmonizeKey(String key) {
694 return Utils.strip(key.toLowerCase(Locale.ENGLISH).replace('-', '_').replace(':', '_').replace(' ', '_'), "-_;:,");
695 }
696
697 private static String harmonizeValue(String value) {
698 return Utils.strip(value.toLowerCase(Locale.ENGLISH).replace('-', '_').replace(' ', '_'), "-_;:,");
699 }
700
701 @Override
702 public void startTest(ProgressMonitor monitor) {
703 super.startTest(monitor);
704 checkKeys = Config.getPref().getBoolean(PREF_CHECK_KEYS, true);
705 if (isBeforeUpload) {
706 checkKeys = checkKeys && Config.getPref().getBoolean(PREF_CHECK_KEYS_BEFORE_UPLOAD, true);
707 }
708
709 checkValues = Config.getPref().getBoolean(PREF_CHECK_VALUES, true);
710 if (isBeforeUpload) {
711 checkValues = checkValues && Config.getPref().getBoolean(PREF_CHECK_VALUES_BEFORE_UPLOAD, true);
712 }
713
714 checkComplex = Config.getPref().getBoolean(PREF_CHECK_COMPLEX, true) && !checkerData.isEmpty();
715 if (isBeforeUpload) {
716 checkComplex = checkComplex && Config.getPref().getBoolean(PREF_CHECK_COMPLEX_BEFORE_UPLOAD, true);
717 }
718
719 checkFixmes = Config.getPref().getBoolean(PREF_CHECK_FIXMES, true);
720 if (isBeforeUpload) {
721 checkFixmes = checkFixmes && Config.getPref().getBoolean(PREF_CHECK_FIXMES_BEFORE_UPLOAD, true);
722 }
723 }
724
725 @Override
726 public void visit(Collection<OsmPrimitive> selection) {
727 if (checkKeys || checkValues || checkComplex || checkFixmes) {
728 super.visit(selection);
729 }
730 }
731
732 @Override
733 public void addGui(JPanel testPanel) {
734 GBC a = GBC.eol();
735 a.anchor = GridBagConstraints.EAST;
736
737 testPanel.add(new JLabel(name+" :"), GBC.eol().insets(3, 0, 0, 0));
738
739 prefCheckKeys = new JCheckBox(tr("Check property keys."), Config.getPref().getBoolean(PREF_CHECK_KEYS, true));
740 prefCheckKeys.setToolTipText(tr("Validate that property keys are valid checking against list of words."));
741 testPanel.add(prefCheckKeys, GBC.std().insets(20, 0, 0, 0));
742
743 prefCheckKeysBeforeUpload = new JCheckBox();
744 prefCheckKeysBeforeUpload.setSelected(Config.getPref().getBoolean(PREF_CHECK_KEYS_BEFORE_UPLOAD, true));
745 testPanel.add(prefCheckKeysBeforeUpload, a);
746
747 prefCheckComplex = new JCheckBox(tr("Use complex property checker."), Config.getPref().getBoolean(PREF_CHECK_COMPLEX, true));
748 prefCheckComplex.setToolTipText(tr("Validate property values and tags using complex rules."));
749 testPanel.add(prefCheckComplex, GBC.std().insets(20, 0, 0, 0));
750
751 prefCheckComplexBeforeUpload = new JCheckBox();
752 prefCheckComplexBeforeUpload.setSelected(Config.getPref().getBoolean(PREF_CHECK_COMPLEX_BEFORE_UPLOAD, true));
753 testPanel.add(prefCheckComplexBeforeUpload, a);
754
755 final Collection<String> sources = Config.getPref().getList(PREF_SOURCES, DEFAULT_SOURCES);
756 sourcesList = new EditableList(tr("TagChecker source"));
757 sourcesList.setItems(sources);
758 testPanel.add(new JLabel(tr("Data sources ({0})", "*.cfg")), GBC.eol().insets(23, 0, 0, 0));
759 testPanel.add(sourcesList, GBC.eol().fill(GridBagConstraints.HORIZONTAL).insets(23, 0, 0, 0));
760
761 ActionListener disableCheckActionListener = e -> handlePrefEnable();
762 prefCheckKeys.addActionListener(disableCheckActionListener);
763 prefCheckKeysBeforeUpload.addActionListener(disableCheckActionListener);
764 prefCheckComplex.addActionListener(disableCheckActionListener);
765 prefCheckComplexBeforeUpload.addActionListener(disableCheckActionListener);
766
767 handlePrefEnable();
768
769 prefCheckValues = new JCheckBox(tr("Check property values."), Config.getPref().getBoolean(PREF_CHECK_VALUES, true));
770 prefCheckValues.setToolTipText(tr("Validate that property values are valid checking against presets."));
771 testPanel.add(prefCheckValues, GBC.std().insets(20, 0, 0, 0));
772
773 prefCheckValuesBeforeUpload = new JCheckBox();
774 prefCheckValuesBeforeUpload.setSelected(Config.getPref().getBoolean(PREF_CHECK_VALUES_BEFORE_UPLOAD, true));
775 testPanel.add(prefCheckValuesBeforeUpload, a);
776
777 prefCheckFixmes = new JCheckBox(tr("Check for FIXMES."), Config.getPref().getBoolean(PREF_CHECK_FIXMES, true));
778 prefCheckFixmes.setToolTipText(tr("Looks for nodes or ways with FIXME in any property value."));
779 testPanel.add(prefCheckFixmes, GBC.std().insets(20, 0, 0, 0));
780
781 prefCheckFixmesBeforeUpload = new JCheckBox();
782 prefCheckFixmesBeforeUpload.setSelected(Config.getPref().getBoolean(PREF_CHECK_FIXMES_BEFORE_UPLOAD, true));
783 testPanel.add(prefCheckFixmesBeforeUpload, a);
784 }
785
786 /**
787 * Enables/disables the source list field
788 */
789 public void handlePrefEnable() {
790 boolean selected = prefCheckKeys.isSelected() || prefCheckKeysBeforeUpload.isSelected()
791 || prefCheckComplex.isSelected() || prefCheckComplexBeforeUpload.isSelected();
792 sourcesList.setEnabled(selected);
793 }
794
795 @Override
796 public boolean ok() {
797 enabled = prefCheckKeys.isSelected() || prefCheckValues.isSelected() || prefCheckComplex.isSelected() || prefCheckFixmes.isSelected();
798 testBeforeUpload = prefCheckKeysBeforeUpload.isSelected() || prefCheckValuesBeforeUpload.isSelected()
799 || prefCheckFixmesBeforeUpload.isSelected() || prefCheckComplexBeforeUpload.isSelected();
800
801 Config.getPref().putBoolean(PREF_CHECK_VALUES, prefCheckValues.isSelected());
802 Config.getPref().putBoolean(PREF_CHECK_COMPLEX, prefCheckComplex.isSelected());
803 Config.getPref().putBoolean(PREF_CHECK_KEYS, prefCheckKeys.isSelected());
804 Config.getPref().putBoolean(PREF_CHECK_FIXMES, prefCheckFixmes.isSelected());
805 Config.getPref().putBoolean(PREF_CHECK_VALUES_BEFORE_UPLOAD, prefCheckValuesBeforeUpload.isSelected());
806 Config.getPref().putBoolean(PREF_CHECK_COMPLEX_BEFORE_UPLOAD, prefCheckComplexBeforeUpload.isSelected());
807 Config.getPref().putBoolean(PREF_CHECK_KEYS_BEFORE_UPLOAD, prefCheckKeysBeforeUpload.isSelected());
808 Config.getPref().putBoolean(PREF_CHECK_FIXMES_BEFORE_UPLOAD, prefCheckFixmesBeforeUpload.isSelected());
809 return Config.getPref().putList(PREF_SOURCES, sourcesList.getItems());
810 }
811
812 @Override
813 public Command fixError(TestError testError) {
814 List<Command> commands = new ArrayList<>(50);
815
816 Collection<? extends OsmPrimitive> primitives = testError.getPrimitives();
817 for (OsmPrimitive p : primitives) {
818 Map<String, String> tags = p.getKeys();
819 if (tags.isEmpty()) {
820 continue;
821 }
822
823 for (Entry<String, String> prop: tags.entrySet()) {
824 String key = prop.getKey();
825 String value = prop.getValue();
826 if (value == null || value.trim().isEmpty()) {
827 commands.add(new ChangePropertyCommand(p, key, null));
828 } else if (value.startsWith(" ") || value.endsWith(" ") || value.contains(" ")) {
829 commands.add(new ChangePropertyCommand(p, key, Utils.removeWhiteSpaces(value)));
830 } else if (key.startsWith(" ") || key.endsWith(" ") || key.contains(" ")) {
831 commands.add(new ChangePropertyKeyCommand(p, key, Utils.removeWhiteSpaces(key)));
832 } else {
833 String evalue = Entities.unescape(value);
834 if (!evalue.equals(value)) {
835 commands.add(new ChangePropertyCommand(p, key, evalue));
836 }
837 }
838 }
839 }
840
841 if (commands.isEmpty())
842 return null;
843 if (commands.size() == 1)
844 return commands.get(0);
845
846 return new SequenceCommand(tr("Fix tags"), commands);
847 }
848
849 @Override
850 public boolean isFixable(TestError testError) {
851 if (testError.getTester() instanceof TagChecker) {
852 int code = testError.getCode();
853 return code == INVALID_KEY || code == EMPTY_VALUES || code == INVALID_SPACE ||
854 code == INVALID_KEY_SPACE || code == INVALID_HTML ||
855 code == MULTIPLE_SPACES;
856 }
857
858 return false;
859 }
860
861 protected static class CheckerData {
862 private String description;
863 protected List<CheckerElement> data = new ArrayList<>();
864 private OsmPrimitiveType type;
865 private TagCheckLevel level;
866 protected Severity severity;
867
868 private enum TagCheckLevel {
869 TAG_CHECK_ERROR(1250),
870 TAG_CHECK_WARN(1260),
871 TAG_CHECK_INFO(1270);
872
873 final int code;
874
875 TagCheckLevel(int code) {
876 this.code = code;
877 }
878 }
879
880 protected static class CheckerElement {
881 public Object tag;
882 public Object value;
883 public boolean noMatch;
884 public boolean tagAll;
885 public boolean valueAll;
886 public boolean valueBool;
887
888 private static Pattern getPattern(String str) {
889 if (str.endsWith("/i"))
890 return Pattern.compile(str.substring(1, str.length()-2), Pattern.CASE_INSENSITIVE);
891 if (str.endsWith("/"))
892 return Pattern.compile(str.substring(1, str.length()-1));
893
894 throw new IllegalStateException();
895 }
896
897 public CheckerElement(String exp) {
898 Matcher m = Pattern.compile("(.+)([!=]=)(.+)").matcher(exp);
899 m.matches();
900
901 String n = m.group(1).trim();
902
903 if ("*".equals(n)) {
904 tagAll = true;
905 } else {
906 tag = n.startsWith("/") ? getPattern(n) : n;
907 noMatch = "!=".equals(m.group(2));
908 n = m.group(3).trim();
909 if ("*".equals(n)) {
910 valueAll = true;
911 } else if ("BOOLEAN_TRUE".equals(n)) {
912 valueBool = true;
913 value = OsmUtils.TRUE_VALUE;
914 } else if ("BOOLEAN_FALSE".equals(n)) {
915 valueBool = true;
916 value = OsmUtils.FALSE_VALUE;
917 } else {
918 value = n.startsWith("/") ? getPattern(n) : n;
919 }
920 }
921 }
922
923 public boolean match(Map<String, String> keys) {
924 for (Entry<String, String> prop: keys.entrySet()) {
925 String key = prop.getKey();
926 String val = valueBool ? OsmUtils.getNamedOsmBoolean(prop.getValue()) : prop.getValue();
927 if ((tagAll || (tag instanceof Pattern ? ((Pattern) tag).matcher(key).matches() : key.equals(tag)))
928 && (valueAll || (value instanceof Pattern ? ((Pattern) value).matcher(val).matches() : val.equals(value))))
929 return !noMatch;
930 }
931 return noMatch;
932 }
933 }
934
935 private static final Pattern CLEAN_STR_PATTERN = Pattern.compile(" *# *([^#]+) *$");
936 private static final Pattern SPLIT_TRIMMED_PATTERN = Pattern.compile(" *: *");
937 private static final Pattern SPLIT_ELEMENTS_PATTERN = Pattern.compile(" *&& *");
938
939 public String getData(final String str) {
940 Matcher m = CLEAN_STR_PATTERN.matcher(str);
941 String trimmed = m.replaceFirst("").trim();
942 try {
943 description = m.group(1);
944 if (description != null && description.isEmpty()) {
945 description = null;
946 }
947 } catch (IllegalStateException e) {
948 Logging.error(e);
949 description = null;
950 }
951 String[] n = SPLIT_TRIMMED_PATTERN.split(trimmed, 3);
952 switch (n[0]) {
953 case "way":
954 type = OsmPrimitiveType.WAY;
955 break;
956 case "node":
957 type = OsmPrimitiveType.NODE;
958 break;
959 case "relation":
960 type = OsmPrimitiveType.RELATION;
961 break;
962 case "*":
963 type = null;
964 break;
965 default:
966 return tr("Could not find element type");
967 }
968 if (n.length != 3)
969 return tr("Incorrect number of parameters");
970
971 switch (n[1]) {
972 case "W":
973 severity = Severity.WARNING;
974 level = TagCheckLevel.TAG_CHECK_WARN;
975 break;
976 case "E":
977 severity = Severity.ERROR;
978 level = TagCheckLevel.TAG_CHECK_ERROR;
979 break;
980 case "I":
981 severity = Severity.OTHER;
982 level = TagCheckLevel.TAG_CHECK_INFO;
983 break;
984 default:
985 return tr("Could not find warning level");
986 }
987 for (String exp: SPLIT_ELEMENTS_PATTERN.split(n[2])) {
988 try {
989 data.add(new CheckerElement(exp));
990 } catch (IllegalStateException e) {
991 Logging.trace(e);
992 return tr("Illegal expression ''{0}''", exp);
993 } catch (PatternSyntaxException e) {
994 Logging.trace(e);
995 return tr("Illegal regular expression ''{0}''", exp);
996 }
997 }
998 return null;
999 }
1000
1001 public boolean match(OsmPrimitive osm, Map<String, String> keys) {
1002 if (type != null && OsmPrimitiveType.from(osm) != type)
1003 return false;
1004
1005 for (CheckerElement ce : data) {
1006 if (!ce.match(keys))
1007 return false;
1008 }
1009 return true;
1010 }
1011
1012 /**
1013 * Returns the error description.
1014 * @return the error description
1015 */
1016 public String getDescription() {
1017 return description;
1018 }
1019
1020 /**
1021 * Returns the error severity.
1022 * @return the error severity
1023 */
1024 public Severity getSeverity() {
1025 return severity;
1026 }
1027
1028 /**
1029 * Returns the error code.
1030 * @return the error code
1031 */
1032 public int getCode() {
1033 if (type == null)
1034 return level.code;
1035
1036 return level.code + type.ordinal() + 1;
1037 }
1038 }
1039}
Note: See TracBrowser for help on using the repository browser.