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