Ticket #11774: validateKeys1.diff
File validateKeys1.diff, 4.9 KB (added by , 10 years ago) |
---|
-
josm/src/org/openstreetmap/josm/data/validation/tests/TagChecker.java
71 71 public static final String SPELL_FILE = "resource://data/validator/words.cfg"; 72 72 73 73 /** The spell check key substitutions: the key should be substituted by the value */ 74 private static volatile Map<String, String> spellCheckKeyData; 74 private static final Map<String, String> spellCheckKeyData = new HashMap<>(); 75 private static final Map<String, String> prettyfiedKeys = new HashMap<>(); 75 76 /** The spell check preset values */ 76 77 private static volatile MultiMap<String, String> presetsValueData; 77 78 /** The TagChecker data */ … … 125 126 protected static final int LOW_CHAR_VALUE = 1210; 126 127 protected static final int LOW_CHAR_KEY = 1211; 127 128 protected static final int MISSPELLED_VALUE = 1212; 129 protected static final int MISSPELLED_KEY = 1213; 128 130 /** 1250 and up is used by tagcheck */ 129 131 130 132 protected EditableList sourcesList; … … 160 162 ignoreDataEquals.clear(); 161 163 ignoreDataEndsWith.clear(); 162 164 ignoreDataKeyPair.clear(); 165 spellCheckKeyData.clear(); 166 prettyfiedKeys.clear(); 163 167 164 spellCheckKeyData = new HashMap<>();165 166 168 String errorSources = ""; 167 169 for (String source : Main.pref.getCollection(PREF_SOURCES, DEFAULT_SOURCES)) { 168 170 try ( … … 290 292 if (ky.key != null && values != null) { 291 293 try { 292 294 presetsValueData.putAll(ky.key, values); 295 addKey(prettifyKey(ky.key), ky.key); 293 296 } catch (NullPointerException e) { 294 297 Main.error(p+": Unable to initialize "+ky); 295 298 } … … 411 414 412 415 if (!ignore) { 413 416 if (!keyInPresets) { 414 String i = marktr("Key ''{0}'' not in presets."); 415 errors.add(new TestError(this, Severity.OTHER, tr("Presets do not contain property key"), 416 tr(i, key), MessageFormat.format(i, key), INVALID_VALUE, p)); 417 withErrors.put(p, "UPK"); 417 String prettifiedKey = prettifyKey(key); 418 String fixedKey = prettyfiedKeys.get(prettifiedKey); 419 if (fixedKey != null && !"".equals(fixedKey)) { 420 // misspelled preset key 421 String i = marktr("Key ''{0}'' looks like ''{1}''."); 422 errors.add(new FixableTestError(this, Severity.WARNING, tr("Misspelled property key"), 423 tr(i, key, fixedKey), 424 MessageFormat.format(i, key, fixedKey), MISSPELLED_KEY, p, 425 new ChangePropertyKeyCommand(p, key, fixedKey))); 426 withErrors.put(p, "WPK"); 427 } else { 428 String i = marktr("Key ''{0}'' not in presets."); 429 errors.add(new TestError(this, Severity.OTHER, tr("Presets do not contain property key"), 430 tr(i, key), MessageFormat.format(i, key), INVALID_VALUE, p)); 431 withErrors.put(p, "UPK"); 432 } 418 433 } else if (!tagInPresets) { 419 434 // try to fix common typos and check again if value is still unknown 420 435 String fixedValue = prettifyValue(prop.getValue()); … … 464 479 return map; 465 480 } 466 481 482 private static void addKey(String prettyKey, String key) { 483 String otherKey = prettyfiedKeys.get(prettyKey); 484 if (otherKey == null) { 485 prettyfiedKeys.put(prettyKey, key); 486 // Main.debug(prettyKey + " -> " + key); 487 } else if ("".equals(key)) { 488 Main.warn("Ignore also: " + prettyKey + " -> " + key); 489 } else if (!otherKey.equals(key)) { 490 Main.warn("Ignore '" + prettyKey + "' bacause it's the normalized form of preset key '" + key + "' and '" + otherKey + "'!"); 491 prettyfiedKeys.put(prettyKey, ""); 492 } 493 } 494 495 private static String prettifyKey(String key) { 496 // convert to lower case, replace ' ', ':' or '-' with '_' 497 key = key.toLowerCase(Locale.ENGLISH).replace('-', '_').replace(':', '_').replace(' ', '_'); 498 // remove trailing or leading special chars 499 return Utils.strip(key, "-_;:,"); 500 } 501 467 502 private static String prettifyValue(String value) { 468 503 // convert to lower case, replace ' ' or '-' with '_' 469 504 value = value.toLowerCase(Locale.ENGLISH).replace('-', '_').replace(' ', '_');