Ticket #11498: patch.diff
File patch.diff, 9.2 KB (added by , 9 years ago) |
---|
-
josm/src/org/openstreetmap/josm/data/validation/tests/TagChecker.java
55 55 import org.openstreetmap.josm.io.UTFInputStreamReader; 56 56 import org.openstreetmap.josm.tools.GBC; 57 57 import org.openstreetmap.josm.tools.MultiMap; 58 import org.openstreetmap.josm.tools.Utils; 58 59 59 60 /** 60 61 * Check for misspelled or wrong tags … … 124 125 protected static final int LONG_KEY = 1209; 125 126 protected static final int LOW_CHAR_VALUE = 1210; 126 127 protected static final int LOW_CHAR_KEY = 1211; 128 protected static final int MISSPELLED_VALUE = 1212; 127 129 /** 1250 and up is used by tagcheck */ 128 130 129 131 protected EditableList sourcesList; … … 416 418 tr(i, key), MessageFormat.format(i, key), INVALID_VALUE, p) ); 417 419 withErrors.put(p, "UPK"); 418 420 } 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 } 423 436 } 424 437 } 425 438 } … … 436 449 } 437 450 } 438 451 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 439 462 @Override 440 463 public void startTest(ProgressMonitor monitor) { 441 464 super.startTest(monitor); … … 570 593 } else if (key.startsWith(" ") || key.endsWith(" ")) { 571 594 commands.add(new ChangePropertyKeyCommand(p, key, Tag.removeWhiteSpaces(key))); 572 595 } 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)); 576 600 } 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 } 580 609 } 581 610 } 582 611 } … … 595 624 public boolean isFixable(TestError testError) { 596 625 if (testError.getTester() instanceof TagChecker) { 597 626 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; 599 628 } 600 629 601 630 return false; -
josm/src/org/openstreetmap/josm/tools/Utils.java
39 39 import java.util.Arrays; 40 40 import java.util.Collection; 41 41 import java.util.Collections; 42 import java.util.HashSet; 42 43 import java.util.Iterator; 43 44 import java.util.List; 44 45 import java.util.Locale; … … 898 899 * @see <a href="https://bugs.openjdk.java.net/browse/JDK-4080617">JDK bug 4080617</a> 899 900 * @since 5772 900 901 */ 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) { 902 915 if (str == null || str.isEmpty()) { 903 916 return str; 904 917 } 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 } 905 929 int start = 0, end = str.length(); 906 boolean leading White= true;907 while (leading White&& start < end) {930 boolean leadingSkipChar = true; 931 while (leadingSkipChar && start < end) { 908 932 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) { 913 935 start++; 914 936 } 915 937 } 916 boolean trailing White= true;917 while (trailing White&& end > start+1) {938 boolean trailingSkipChar = true; 939 while (trailingSkipChar && end > start+1) { 918 940 char c = str.charAt(end-1); 919 trailing White = (Character.isWhitespace(c) || Character.isSpaceChar(c) || c == '\u200B' || c == '\uFEFF');920 if (trailing White) {941 trailingSkipChar = Character.isWhitespace(c) || Character.isSpaceChar(c) || skipSet.contains(c); 942 if (trailingSkipChar) { 921 943 end--; 922 944 } 923 945 } -
josm/test/unit/org/openstreetmap/josm/tools/UtilsTest.java
59 59 Assert.assertEquals("a", Utils.strip(someWhite+"a"+someWhite)); 60 60 Assert.assertEquals("ab", Utils.strip(someWhite+"ab"+someWhite)); 61 61 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")); 62 66 } 63 67 64 68 /**