Changeset 14933 in josm
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/validation/tests/TagChecker.java
r14897 r14933 20 20 import java.util.Map.Entry; 21 21 import java.util.Set; 22 import java.util.regex.Pattern; 22 23 23 24 import javax.swing.JCheckBox; … … 72 73 /** often used tags which are not in presets */ 73 74 private static volatile MultiMap<String, String> oftenUsedTags = new MultiMap<>(); 75 76 private static final Pattern NON_PRINTING_CONTROL_CHARACTERS = Pattern.compile( 77 "[\\x00-\\x09\\x0B\\x0C\\x0E-\\x1F\\x7F\\u200c-\\u200f\\u202a-\\u202e]"); 74 78 75 79 /** The TagChecker data */ … … 372 376 373 377 /** 374 * Checks given string (key or value) if it contains characters with code below 0x20 (either newline or some other specialcharacters)378 * Checks given string (key or value) if it contains non-printing control characters (either ASCII or Unicode bidi characters) 375 379 * @param s string to check 376 * @return {@code true} if {@code s} contains characters with code below 0x20377 */ 378 private static boolean contains Low(String s) {380 * @return {@code true} if {@code s} contains non-printing control characters 381 */ 382 private static boolean containsNonPrintingControlCharacter(String s) { 379 383 if (s == null) 380 384 return false; 381 385 for (int i = 0; i < s.length(); i++) { 382 if (s.charAt(i) < 0x20) 386 char c = s.charAt(i); 387 if ((IsAsciiControlChar(c) && !isNewLineChar(c)) || IsBidiControlChar(c)) 383 388 return true; 384 389 } 385 390 return false; 391 } 392 393 private static boolean IsAsciiControlChar(char c) { 394 return c < 0x20 || c == 0x7F; 395 } 396 397 private static boolean isNewLineChar(char c) { 398 return c == 0x0a || c == 0x0d; 399 } 400 401 private static boolean IsBidiControlChar(char c) { 402 /* check for range 0x200c to 0x200f (ZWNJ, ZWJ, LRM, RLM) or 403 0x202a to 0x202e (LRE, RLE, PDF, LRO, RLO) */ 404 return (((c & 0xfffffffc) == 0x200c) || ((c >= 0x202a) && (c <= 0x202e))); 405 } 406 407 static String removeNonPrintingControlCharacters(String s) { 408 return NON_PRINTING_CONTROL_CHARACTERS.matcher(s).replaceAll(""); 386 409 } 387 410 … … 515 538 if (!checkValues || value == null) 516 539 return; 517 if ((contains Low(value)) && !withErrors.contains(p, "ICV")) {540 if ((containsNonPrintingControlCharacter(value)) && !withErrors.contains(p, "ICV")) { 518 541 errors.add(TestError.builder(this, Severity.WARNING, LOW_CHAR_VALUE) 519 .message(tr("Tag value contains character with code less than 0x20"), s, key) 520 .primitives(p) 542 .message(tr("Tag value contains non-printing character"), s, key) 543 .primitives(p) 544 .fix(() -> new ChangePropertyCommand(p, key, removeNonPrintingControlCharacters(value))) 521 545 .build()); 522 546 withErrors.put(p, "ICV"); … … 563 587 if (!checkKeys || key == null) 564 588 return; 565 if ((contains Low(key)) && !withErrors.contains(p, "ICK")) {589 if ((containsNonPrintingControlCharacter(key)) && !withErrors.contains(p, "ICK")) { 566 590 errors.add(TestError.builder(this, Severity.WARNING, LOW_CHAR_KEY) 567 .message(tr("Tag key contains character with code less than 0x20"), s, key) 568 .primitives(p) 591 .message(tr("Tag key contains non-printing character"), s, key) 592 .primitives(p) 593 .fix(() -> new ChangePropertyCommand(p, key, removeNonPrintingControlCharacters(key))) 569 594 .build()); 570 595 withErrors.put(p, "ICK"); -
trunk/test/unit/org/openstreetmap/josm/data/validation/tests/TagCheckerTest.java
r14897 r14933 248 248 } 249 249 250 /** 251 * Unit test of {@link TagChecker#removeNonPrintingControlCharacters} 252 */ 253 @Test 254 public void testRemoveUnprintableControlCharacters() { 255 // Check 65 ASCII control characters are removed, except new lines 256 for (char c = 0x0; c < 0x20; c++) { 257 if (c != '\r' && c != '\n') { 258 assertTrue(TagChecker.removeNonPrintingControlCharacters(Character.toString(c)).isEmpty()); 259 } else { 260 assertFalse(TagChecker.removeNonPrintingControlCharacters(Character.toString(c)).isEmpty()); 261 } 262 } 263 assertTrue(TagChecker.removeNonPrintingControlCharacters(Character.toString((char) 0x7F)).isEmpty()); 264 // Check 9 Unicode bidi control characters are removed 265 for (char c = 0x200c; c <= 0x200f; c++) { 266 assertTrue(TagChecker.removeNonPrintingControlCharacters(Character.toString(c)).isEmpty()); 267 } 268 for (char c = 0x202a; c <= 0x202e; c++) { 269 assertTrue(TagChecker.removeNonPrintingControlCharacters(Character.toString(c)).isEmpty()); 270 } 271 } 250 272 }
Note:
See TracChangeset
for help on using the changeset viewer.