Ignore:
Timestamp:
2015-09-13T16:36:09+02:00 (9 years ago)
Author:
simon04
Message:

fix #11774 - Warn about obvious misspelled tag keys (patch by mdk, modified)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/validation/tests/TagChecker.java

    r8510 r8753  
    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> harmonizedKeys = new HashMap<>();
    7575    /** The spell check preset values */
    7676    private static volatile MultiMap<String, String> presetsValueData;
     
    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
     
    161162        ignoreDataEndsWith.clear();
    162163        ignoreDataKeyPair.clear();
    163 
    164         spellCheckKeyData = new HashMap<>();
     164        harmonizedKeys.clear();
    165165
    166166        String errorSources = "";
     
    229229                        okValue = line.substring(1);
    230230                    } else if (line.charAt(0) == '-' && okValue != null) {
    231                         spellCheckKeyData.put(line.substring(1), okValue);
     231                        harmonizedKeys.put(harmonizeKey(line.substring(1)), okValue);
    232232                    } else {
    233233                        Main.error(tr("Invalid spellcheck line: {0}", line));
     
    291291            try {
    292292                presetsValueData.putAll(ky.key, values);
     293                harmonizedKeys.put(harmonizeKey(ky.key), ky.key);
    293294            } catch (NullPointerException e) {
    294295                Main.error(p+": Unable to initialize "+ky);
     
    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"),
     
    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 = harmonizeKey(key);
     411                        String fixedKey = harmonizedKeys.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
    420                         String fixedValue = prettifyValue(prop.getValue());
     428                        String fixedValue = harmonizeValue(prop.getValue());
    421429                        Map<String, String> possibleValues = getPossibleValues(values);
    422430                        if (possibleValues.containsKey(fixedValue)) {
     
    465473    }
    466474
    467     private static String prettifyValue(String value) {
    468         // convert to lower case, replace ' ' or '-' with '_'
     475    private static String harmonizeKey(String key) {
     476        key = key.toLowerCase(Locale.ENGLISH).replace('-', '_').replace(':', '_').replace(' ', '_');
     477        return Utils.strip(key, "-_;:,");
     478    }
     479
     480    private static String harmonizeValue(String value) {
    469481        value = value.toLowerCase(Locale.ENGLISH).replace('-', '_').replace(' ', '_');
    470         // remove trailing or leading special chars
    471482        return Utils.strip(value, "-_;:,");
    472483    }
     
    612623                        if (!evalue.equals(value)) {
    613624                            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                             }
    619625                        }
    620626                    }
Note: See TracChangeset for help on using the changeset viewer.