Ticket #11774: validateKeys2.diff
File validateKeys2.diff, 6.8 KB (added by , 10 years ago) |
---|
-
josm/src/org/openstreetmap/josm/data/validation/tests/TagChecker.java
70 70 /** The config file of dictionary words */ 71 71 public static final String SPELL_FILE = "resource://data/validator/words.cfg"; 72 72 73 /** The spell check key substitutions: the key should be substituted by the value*/74 private static volatile Map<String, String> spellCheckKeyData;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> prettyfiedKeys = new HashMap<>(); 75 75 /** The spell check preset values */ 76 76 private static volatile MultiMap<String, String> presetsValueData; 77 77 /** The TagChecker data */ … … 125 125 protected static final int LOW_CHAR_VALUE = 1210; 126 126 protected static final int LOW_CHAR_KEY = 1211; 127 127 protected static final int MISSPELLED_VALUE = 1212; 128 protected static final int MISSPELLED_KEY = 1213; 128 129 /** 1250 and up is used by tagcheck */ 129 130 130 131 protected EditableList sourcesList; … … 160 161 ignoreDataEquals.clear(); 161 162 ignoreDataEndsWith.clear(); 162 163 ignoreDataKeyPair.clear(); 164 prettyfiedKeys.clear(); 163 165 164 spellCheckKeyData = new HashMap<>();165 166 166 String errorSources = ""; 167 167 for (String source : Main.pref.getCollection(PREF_SOURCES, DEFAULT_SOURCES)) { 168 168 try ( … … 228 228 } else if (line.charAt(0) == '+') { 229 229 okValue = line.substring(1); 230 230 } else if (line.charAt(0) == '-' && okValue != null) { 231 spellCheckKeyData.put(line.substring(1), okValue);231 addKey(prettifyKey(line.substring(1)), okValue); 232 232 } else { 233 233 Main.error(tr("Invalid spellcheck line: {0}", line)); 234 234 } … … 290 290 if (ky.key != null && values != null) { 291 291 try { 292 292 presetsValueData.putAll(ky.key, values); 293 addKey(prettifyKey(ky.key), ky.key); 293 294 } catch (NullPointerException e) { 294 295 Main.error(p+": Unable to initialize "+ky); 295 296 } … … 359 360 tr(s, key), MessageFormat.format(s, key), EMPTY_VALUES, p)); 360 361 withErrors.put(p, "EV"); 361 362 } 362 if (checkKeys && spellCheckKeyData.containsKey(key) && !withErrors.contains(p, "IPK")) {363 errors.add(new TestError(this, Severity.WARNING, tr("Invalid property key"),364 tr(s, key), MessageFormat.format(s, key), INVALID_KEY, p));365 withErrors.put(p, "IPK");366 }367 363 if (checkKeys && key != null && key.indexOf(' ') >= 0 && !withErrors.contains(p, "IPK")) { 368 364 errors.add(new TestError(this, Severity.WARNING, tr("Invalid white space in property key"), 369 365 tr(s, key), MessageFormat.format(s, key), INVALID_KEY_SPACE, p)); … … 411 407 412 408 if (!ignore) { 413 409 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"); 410 String prettifiedKey = prettifyKey(key); 411 String fixedKey = prettyfiedKeys.get(prettifiedKey); 412 if (fixedKey != null && !"".equals(fixedKey)) { 413 // misspelled preset key 414 String i = marktr("Key ''{0}'' looks like ''{1}''."); 415 errors.add(new FixableTestError(this, Severity.WARNING, tr("Misspelled property key"), 416 tr(i, key, fixedKey), 417 MessageFormat.format(i, key, fixedKey), MISSPELLED_KEY, p, 418 new ChangePropertyKeyCommand(p, key, fixedKey))); 419 withErrors.put(p, "WPK"); 420 } else { 421 String i = marktr("Key ''{0}'' not in presets."); 422 errors.add(new TestError(this, Severity.OTHER, tr("Presets do not contain property key"), 423 tr(i, key), MessageFormat.format(i, key), INVALID_VALUE, p)); 424 withErrors.put(p, "UPK"); 425 } 418 426 } else if (!tagInPresets) { 419 427 // try to fix common typos and check again if value is still unknown 420 428 String fixedValue = prettifyValue(prop.getValue()); … … 464 472 return map; 465 473 } 466 474 475 private static void addKey(String prettyKey, String key) { 476 String otherKey = prettyfiedKeys.get(prettyKey); 477 if (otherKey == null) { 478 prettyfiedKeys.put(prettyKey, key); 479 // Main.debug(prettyKey + " -> " + key); 480 } else if ("".equals(key)) { 481 Main.warn("Ignore also: " + prettyKey + " -> " + key); 482 } else if (!otherKey.equals(key)) { 483 Main.warn("Ignore '" + prettyKey + "' bacause it's the normalized form of preset key '" + key + "' and '" + otherKey + "'!"); 484 prettyfiedKeys.put(prettyKey, ""); 485 } 486 } 487 488 private static String prettifyKey(String key) { 489 // convert to lower case, replace ' ', ':' or '-' with '_' 490 key = key.toLowerCase(Locale.ENGLISH).replace('-', '_').replace(':', '_').replace(' ', '_'); 491 // remove trailing or leading special chars 492 return Utils.strip(key, "-_;:,"); 493 } 494 467 495 private static String prettifyValue(String value) { 468 496 // convert to lower case, replace ' ' or '-' with '_' 469 497 value = value.toLowerCase(Locale.ENGLISH).replace('-', '_').replace(' ', '_'); … … 611 639 String evalue = entities.unescape(value); 612 640 if (!evalue.equals(value)) { 613 641 commands.add(new ChangePropertyCommand(p, key, evalue)); 614 } else {615 String replacementKey = spellCheckKeyData.get(key);616 if (replacementKey != null) {617 commands.add(new ChangePropertyKeyCommand(p, key, replacementKey));618 }619 642 } 620 643 } 621 644 }