Ticket #11498: patch.diff

File patch.diff, 9.2 KB (added by mdk, 9 years ago)
  • josm/src/org/openstreetmap/josm/data/validation/tests/TagChecker.java

     
    5555import org.openstreetmap.josm.io.UTFInputStreamReader;
    5656import org.openstreetmap.josm.tools.GBC;
    5757import org.openstreetmap.josm.tools.MultiMap;
     58import org.openstreetmap.josm.tools.Utils;
    5859
    5960/**
    6061 * Check for misspelled or wrong tags
     
    124125    protected static final int LONG_KEY          = 1209;
    125126    protected static final int LOW_CHAR_VALUE    = 1210;
    126127    protected static final int LOW_CHAR_KEY      = 1211;
     128    protected static final int MISSPELLED_VALUE  = 1212;
    127129    /** 1250 and up is used by tagcheck */
    128130
    129131    protected EditableList sourcesList;
     
    416418                                tr(i, key), MessageFormat.format(i, key), INVALID_VALUE, p) );
    417419                        withErrors.put(p, "UPK");
    418420                    } else if (!tagInPresets) {
    419                         String i = marktr("Value ''{0}'' for key ''{1}'' not in presets.");
    420                         errors.add( new TestError(this, Severity.OTHER, tr("Presets do not contain property value"),
    421                                 tr(i, prop.getValue(), key), MessageFormat.format(i, prop.getValue(), key), INVALID_VALUE, p) );
    422                         withErrors.put(p, "UPV");
     421                        // try to fix common typos and check again if value is still unknown
     422                        String fixedValue = prettifyValue(prop.getValue());
     423                        if (values != null && values.contains(fixedValue)) {
     424                            // misspelled preset value
     425                            String i = marktr("Value ''{0}'' for key ''{1}'' looks like ''{2}}.");
     426                            errors.add( new TestError(this, Severity.WARNING, tr("Misspelled property value"),
     427                                    tr(i, prop.getValue(), key, fixedValue), MessageFormat.format(i, prop.getValue(), fixedValue), MISSPELLED_VALUE, p) );
     428                            withErrors.put(p, "WPV");
     429                        } else {
     430                            // unknown preset value
     431                            String i = marktr("Value ''{0}'' for key ''{1}'' not in presets.");
     432                            errors.add( new TestError(this, Severity.OTHER, tr("Presets do not contain property value"),
     433                                    tr(i, prop.getValue(), key), MessageFormat.format(i, prop.getValue(), key), INVALID_VALUE, p) );
     434                            withErrors.put(p, "UPV");
     435                        }
    423436                    }
    424437                }
    425438            }
     
    436449        }
    437450    }
    438451
     452    private String prettifyValue(String value) {
     453        // convert to lower case
     454        value = value.toLowerCase().replace('-', '_').replace(' ', '_');
     455        // replace ' ' or '-' with '_'
     456        value = value.replace(' ', '_');
     457        value = value.replace('-', '_');
     458        // remove trailing or leading special chars
     459        return Utils.strip(value, "-_;:,");
     460    }
     461
    439462    @Override
    440463    public void startTest(ProgressMonitor monitor) {
    441464        super.startTest(monitor);
     
    570593                } else if (key.startsWith(" ") || key.endsWith(" ")) {
    571594                    commands.add(new ChangePropertyKeyCommand(p, key, Tag.removeWhiteSpaces(key)));
    572595                } else {
    573                     String evalue = entities.unescape(value);
    574                     if (!evalue.equals(value)) {
    575                         commands.add(new ChangePropertyCommand(p, key, evalue));
     596                    final Set<String> values = presetsValueData.get(key);
     597                    String fixedValue = prettifyValue(value);
     598                    if (!value.equals(fixedValue) && values != null && values.contains(fixedValue)) {
     599                        commands.add(new ChangePropertyCommand(p, key, fixedValue));
    576600                    } else {
    577                         String replacementKey = spellCheckKeyData.get(key);
    578                         if (replacementKey != null) {
    579                             commands.add(new ChangePropertyKeyCommand(p, key, replacementKey));
     601                        String evalue = entities.unescape(value);
     602                        if (!evalue.equals(value)) {
     603                            commands.add(new ChangePropertyCommand(p, key, evalue));
     604                        } else {
     605                            String replacementKey = spellCheckKeyData.get(key);
     606                            if (replacementKey != null) {
     607                                commands.add(new ChangePropertyKeyCommand(p, key, replacementKey));
     608                            }
    580609                        }
    581610                    }
    582611                }
     
    595624    public boolean isFixable(TestError testError) {
    596625        if (testError.getTester() instanceof TagChecker) {
    597626            int code = testError.getCode();
    598             return code == INVALID_KEY || code == EMPTY_VALUES || code == INVALID_SPACE || code == INVALID_KEY_SPACE || code == INVALID_HTML;
     627            return code == INVALID_KEY || code == EMPTY_VALUES || code == INVALID_SPACE || code == INVALID_KEY_SPACE || code == INVALID_HTML || code == MISSPELLED_VALUE;
    599628        }
    600629
    601630        return false;
  • josm/src/org/openstreetmap/josm/tools/Utils.java

     
    3939import java.util.Arrays;
    4040import java.util.Collection;
    4141import java.util.Collections;
     42import java.util.HashSet;
    4243import java.util.Iterator;
    4344import java.util.List;
    4445import java.util.Locale;
     
    898899     * @see <a href="https://bugs.openjdk.java.net/browse/JDK-4080617">JDK bug 4080617</a>
    899900     * @since 5772
    900901     */
    901     public static String strip(String str) {
     902    public static String strip(final String str) {
     903        return strip(str, null);
     904    }
     905
     906    /**
     907     * An alternative to {@link String#trim()} to effectively remove all leading and trailing white characters, including Unicode ones.
     908     * @param str The string to strip
     909     * @param skipChars additional characters to skip
     910     * @return <code>str</code>, without leading and trailing characters, according to
     911     *         {@link Character#isWhitespace(char)}, {@link Character#isSpaceChar(char)} and skipChars.
     912     * @since ????
     913     */
     914    public static String strip(final String str, final String skipChars) {
    902915        if (str == null || str.isEmpty()) {
    903916            return str;
    904917        }
     918        // create set with chars to skip
     919        HashSet<Character> skipSet = new HashSet<>();
     920        // '\u200B' (ZERO WIDTH SPACE character) needs to be handled manually because of change in Unicode 6.0 (Java 7, see #8918)
     921        skipSet.add('\u200B');
     922        // same for '\uFEFF' (ZERO WIDTH NO-BREAK SPACE)
     923        skipSet.add('\uFEFF');
     924        if (skipChars != null) {
     925            for(char c : skipChars.toCharArray()) {
     926                skipSet.add(c);
     927            }
     928        }
    905929        int start = 0, end = str.length();
    906         boolean leadingWhite = true;
    907         while (leadingWhite && start < end) {
     930        boolean leadingSkipChar = true;
     931        while (leadingSkipChar && start < end) {
    908932            char c = str.charAt(start);
    909             // '\u200B' (ZERO WIDTH SPACE character) needs to be handled manually because of change in Unicode 6.0 (Java 7, see #8918)
    910             // same for '\uFEFF' (ZERO WIDTH NO-BREAK SPACE)
    911             leadingWhite = (Character.isWhitespace(c) || Character.isSpaceChar(c) || c == '\u200B' || c == '\uFEFF');
    912             if (leadingWhite) {
     933            leadingSkipChar = Character.isWhitespace(c) || Character.isSpaceChar(c) || skipSet.contains(c);
     934            if (leadingSkipChar) {
    913935                start++;
    914936            }
    915937        }
    916         boolean trailingWhite = true;
    917         while (trailingWhite && end > start+1) {
     938        boolean trailingSkipChar = true;
     939        while (trailingSkipChar && end > start+1) {
    918940            char c = str.charAt(end-1);
    919             trailingWhite = (Character.isWhitespace(c) || Character.isSpaceChar(c) || c == '\u200B' || c == '\uFEFF');
    920             if (trailingWhite) {
     941            trailingSkipChar = Character.isWhitespace(c) || Character.isSpaceChar(c) || skipSet.contains(c);
     942            if (trailingSkipChar) {
    921943                end--;
    922944            }
    923945        }
  • josm/test/unit/org/openstreetmap/josm/tools/UtilsTest.java

     
    5959        Assert.assertEquals("a", Utils.strip(someWhite+"a"+someWhite));
    6060        Assert.assertEquals("ab", Utils.strip(someWhite+"ab"+someWhite));
    6161        Assert.assertEquals("abc", Utils.strip(someWhite+"abc"+someWhite));
     62
     63        // extended skip
     64        Assert.assertEquals("a", Utils.strip("a", "b"));
     65        Assert.assertEquals("b", Utils.strip("acbcac", "ac"));
    6266    }
    6367
    6468    /**