Ticket #11774: validateKeys1.diff

File validateKeys1.diff, 4.9 KB (added by mdk, 10 years ago)

simple patch

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

     
    7171    public static final String SPELL_FILE = "resource://data/validator/words.cfg";
    7272
    7373    /** 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<>();
    7576    /** The spell check preset values */
    7677    private static volatile MultiMap<String, String> presetsValueData;
    7778    /** The TagChecker data */
     
    125126    protected static final int LOW_CHAR_VALUE    = 1210;
    126127    protected static final int LOW_CHAR_KEY      = 1211;
    127128    protected static final int MISSPELLED_VALUE  = 1212;
     129    protected static final int MISSPELLED_KEY    = 1213;
    128130    /** 1250 and up is used by tagcheck */
    129131
    130132    protected EditableList sourcesList;
     
    160162        ignoreDataEquals.clear();
    161163        ignoreDataEndsWith.clear();
    162164        ignoreDataKeyPair.clear();
     165        spellCheckKeyData.clear();
     166        prettyfiedKeys.clear();
    163167
    164         spellCheckKeyData = new HashMap<>();
    165 
    166168        String errorSources = "";
    167169        for (String source : Main.pref.getCollection(PREF_SOURCES, DEFAULT_SOURCES)) {
    168170            try (
     
    290292        if (ky.key != null && values != null) {
    291293            try {
    292294                presetsValueData.putAll(ky.key, values);
     295                addKey(prettifyKey(ky.key), ky.key);
    293296            } catch (NullPointerException e) {
    294297                Main.error(p+": Unable to initialize "+ky);
    295298            }
     
    411414
    412415                if (!ignore) {
    413416                    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                        }
    418433                    } else if (!tagInPresets) {
    419434                        // try to fix common typos and check again if value is still unknown
    420435                        String fixedValue = prettifyValue(prop.getValue());
     
    464479        return map;
    465480    }
    466481
     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
    467502    private static String prettifyValue(String value) {
    468503        // convert to lower case, replace ' ' or '-' with '_'
    469504        value = value.toLowerCase(Locale.ENGLISH).replace('-', '_').replace(' ', '_');