Ticket #11774: validateKeys2.diff

File validateKeys2.diff, 6.8 KB (added by mdk, 10 years ago)

extended patch

  • josm/src/org/openstreetmap/josm/data/validation/tests/TagChecker.java

     
    7070    /** The config file of dictionary words */
    7171    public static final String SPELL_FILE = "resource://data/validator/words.cfg";
    7272
    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<>();
    7575    /** The spell check preset values */
    7676    private static volatile MultiMap<String, String> presetsValueData;
    7777    /** The TagChecker data */
     
    125125    protected static final int LOW_CHAR_VALUE    = 1210;
    126126    protected static final int LOW_CHAR_KEY      = 1211;
    127127    protected static final int MISSPELLED_VALUE  = 1212;
     128    protected static final int MISSPELLED_KEY    = 1213;
    128129    /** 1250 and up is used by tagcheck */
    129130
    130131    protected EditableList sourcesList;
     
    160161        ignoreDataEquals.clear();
    161162        ignoreDataEndsWith.clear();
    162163        ignoreDataKeyPair.clear();
     164        prettyfiedKeys.clear();
    163165
    164         spellCheckKeyData = new HashMap<>();
    165 
    166166        String errorSources = "";
    167167        for (String source : Main.pref.getCollection(PREF_SOURCES, DEFAULT_SOURCES)) {
    168168            try (
     
    228228                    } else if (line.charAt(0) == '+') {
    229229                        okValue = line.substring(1);
    230230                    } else if (line.charAt(0) == '-' && okValue != null) {
    231                         spellCheckKeyData.put(line.substring(1), okValue);
     231                        addKey(prettifyKey(line.substring(1)), okValue);
    232232                    } else {
    233233                        Main.error(tr("Invalid spellcheck line: {0}", line));
    234234                    }
     
    290290        if (ky.key != null && values != null) {
    291291            try {
    292292                presetsValueData.putAll(ky.key, values);
     293                addKey(prettifyKey(ky.key), ky.key);
    293294            } catch (NullPointerException e) {
    294295                Main.error(p+": Unable to initialize "+ky);
    295296            }
     
    359360                        tr(s, key), MessageFormat.format(s, key), EMPTY_VALUES, p));
    360361                withErrors.put(p, "EV");
    361362            }
    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             }
    367363            if (checkKeys && key != null && key.indexOf(' ') >= 0 && !withErrors.contains(p, "IPK")) {
    368364                errors.add(new TestError(this, Severity.WARNING, tr("Invalid white space in property key"),
    369365                        tr(s, key), MessageFormat.format(s, key), INVALID_KEY_SPACE, p));
     
    411407
    412408                if (!ignore) {
    413409                    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                        }
    418426                    } else if (!tagInPresets) {
    419427                        // try to fix common typos and check again if value is still unknown
    420428                        String fixedValue = prettifyValue(prop.getValue());
     
    464472        return map;
    465473    }
    466474
     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
    467495    private static String prettifyValue(String value) {
    468496        // convert to lower case, replace ' ' or '-' with '_'
    469497        value = value.toLowerCase(Locale.ENGLISH).replace('-', '_').replace(' ', '_');
     
    611639                        String evalue = entities.unescape(value);
    612640                        if (!evalue.equals(value)) {
    613641                            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                             }
    619642                        }
    620643                    }
    621644                }