Changeset 11129 in josm for trunk/src/org/openstreetmap/josm/data/validation/tests/TagChecker.java
- Timestamp:
- 2016-10-15T00:17:47+02:00 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/validation/tests/TagChecker.java
r11042 r11129 9 9 import java.io.BufferedReader; 10 10 import java.io.IOException; 11 import java.text.MessageFormat;12 11 import java.util.ArrayList; 13 12 import java.util.Arrays; … … 37 36 import org.openstreetmap.josm.data.osm.OsmUtils; 38 37 import org.openstreetmap.josm.data.osm.Tag; 39 import org.openstreetmap.josm.data.validation.FixableTestError;40 38 import org.openstreetmap.josm.data.validation.Severity; 41 39 import org.openstreetmap.josm.data.validation.Test.TagTest; … … 392 390 for (CheckerData d : checkerData) { 393 391 if (d.match(p, keys)) { 394 errors.add(new TestError(this, d.getSeverity(), tr("Suspicious tag/value combinations"), 395 d.getDescription(), d.getDescriptionOrig(), d.getCode(), p)); 392 errors.add(TestError.builder(this, d.getSeverity(), d.getCode()) 393 .message(tr("Suspicious tag/value combinations"), d.getDescription()) 394 .primitives(p) 395 .build()); 396 396 withErrors.put(p, "TC"); 397 397 } … … 404 404 String value = prop.getValue(); 405 405 if (checkValues && (containsLow(value)) && !withErrors.contains(p, "ICV")) { 406 errors.add(new TestError(this, Severity.WARNING, tr("Tag value contains character with code less than 0x20"), 407 tr(s, key), MessageFormat.format(s, key), LOW_CHAR_VALUE, p)); 406 errors.add(TestError.builder(this, Severity.WARNING, LOW_CHAR_VALUE) 407 .message(tr("Tag value contains character with code less than 0x20"), s, key) 408 .primitives(p) 409 .build()); 408 410 withErrors.put(p, "ICV"); 409 411 } 410 412 if (checkKeys && (containsLow(key)) && !withErrors.contains(p, "ICK")) { 411 errors.add(new TestError(this, Severity.WARNING, tr("Tag key contains character with code less than 0x20"), 412 tr(s, key), MessageFormat.format(s, key), LOW_CHAR_KEY, p)); 413 errors.add(TestError.builder(this, Severity.WARNING, LOW_CHAR_KEY) 414 .message(tr("Tag key contains character with code less than 0x20"), s, key) 415 .primitives(p) 416 .build()); 413 417 withErrors.put(p, "ICK"); 414 418 } 415 419 if (checkValues && (value != null && value.length() > 255) && !withErrors.contains(p, "LV")) { 416 errors.add(new TestError(this, Severity.ERROR, tr("Tag value longer than allowed"), 417 tr(s, key), MessageFormat.format(s, key), LONG_VALUE, p)); 420 errors.add(TestError.builder(this, Severity.ERROR, LONG_VALUE) 421 .message(tr("Tag value longer than allowed"), s, key) 422 .primitives(p) 423 .build()); 418 424 withErrors.put(p, "LV"); 419 425 } 420 426 if (checkKeys && (key != null && key.length() > 255) && !withErrors.contains(p, "LK")) { 421 errors.add(new TestError(this, Severity.ERROR, tr("Tag key longer than allowed"), 422 tr(s, key), MessageFormat.format(s, key), LONG_KEY, p)); 427 errors.add(TestError.builder(this, Severity.ERROR, LONG_KEY) 428 .message(tr("Tag key longer than allowed"), s, key) 429 .primitives(p) 430 .build()); 423 431 withErrors.put(p, "LK"); 424 432 } 425 433 if (checkValues && (value == null || value.trim().isEmpty()) && !withErrors.contains(p, "EV")) { 426 errors.add(new TestError(this, Severity.WARNING, tr("Tags with empty values"), 427 tr(s, key), MessageFormat.format(s, key), EMPTY_VALUES, p)); 434 errors.add(TestError.builder(this, Severity.WARNING, EMPTY_VALUES) 435 .message(tr("Tags with empty values"), s, key) 436 .primitives(p) 437 .build()); 428 438 withErrors.put(p, "EV"); 429 439 } 430 440 if (checkKeys && key != null && key.indexOf(' ') >= 0 && !withErrors.contains(p, "IPK")) { 431 errors.add(new TestError(this, Severity.WARNING, tr("Invalid white space in property key"), 432 tr(s, key), MessageFormat.format(s, key), INVALID_KEY_SPACE, p)); 441 errors.add(TestError.builder(this, Severity.WARNING, INVALID_KEY_SPACE) 442 .message(tr("Invalid white space in property key"), s, key) 443 .primitives(p) 444 .build()); 433 445 withErrors.put(p, "IPK"); 434 446 } 435 447 if (checkValues && value != null && (value.startsWith(" ") || value.endsWith(" ")) && !withErrors.contains(p, "SPACE")) { 436 errors.add(new TestError(this, Severity.WARNING, tr("Property values start or end with white space"), 437 tr(s, key), MessageFormat.format(s, key), INVALID_SPACE, p)); 448 errors.add(TestError.builder(this, Severity.WARNING, INVALID_SPACE) 449 .message(tr("Property values start or end with white space"), s, key) 450 .primitives(p) 451 .build()); 438 452 withErrors.put(p, "SPACE"); 439 453 } 440 454 if (checkValues && value != null && value.contains(" ") && !withErrors.contains(p, "SPACE")) { 441 errors.add(new TestError(this, Severity.WARNING, tr("Property values contain multiple white spaces"), 442 tr(s, key), MessageFormat.format(s, key), MULTIPLE_SPACES, p)); 455 errors.add(TestError.builder(this, Severity.WARNING, MULTIPLE_SPACES) 456 .message(tr("Property values contain multiple white spaces"), s, key) 457 .primitives(p) 458 .build()); 443 459 withErrors.put(p, "SPACE"); 444 460 } 445 461 if (checkValues && value != null && !value.equals(Entities.unescape(value)) && !withErrors.contains(p, "HTML")) { 446 errors.add(new TestError(this, Severity.OTHER, tr("Property values contain HTML entity"), 447 tr(s, key), MessageFormat.format(s, key), INVALID_HTML, p)); 462 errors.add(TestError.builder(this, Severity.OTHER, INVALID_HTML) 463 .message(tr("Property values contain HTML entity"), s, key) 464 .primitives(p) 465 .build()); 448 466 withErrors.put(p, "HTML"); 449 467 } … … 455 473 if (fixedKey != null && !"".equals(fixedKey) && !fixedKey.equals(key)) { 456 474 // misspelled preset key 457 String i = marktr("Key ''{0}'' looks like ''{1}''."); 458 final TestError error; 475 final TestError.Builder error = TestError.builder(this, Severity.WARNING, MISSPELLED_KEY) 476 .message(tr("Misspelled property key"), marktr("Key ''{0}'' looks like ''{1}''."), key, fixedKey) 477 .primitives(p); 459 478 if (p.hasKey(fixedKey)) { 460 error = new TestError(this, Severity.WARNING, tr("Misspelled property key"), 461 tr(i, key, fixedKey), 462 MessageFormat.format(i, key, fixedKey), MISSPELLED_KEY, p); 479 errors.add(error.build()); 463 480 } else { 464 error = new FixableTestError(this, Severity.WARNING, tr("Misspelled property key"), 465 tr(i, key, fixedKey), 466 MessageFormat.format(i, key, fixedKey), MISSPELLED_KEY, p, 467 new ChangePropertyKeyCommand(p, key, fixedKey)); 481 errors.add(error.fix(() -> new ChangePropertyKeyCommand(p, key, fixedKey)).build()); 468 482 } 469 errors.add(error);470 483 withErrors.put(p, "WPK"); 471 484 } else { 472 String i = marktr("Key ''{0}'' not in presets."); 473 errors.add(new TestError(this, Severity.OTHER, tr("Presets do not contain property key"), 474 tr(i, key), MessageFormat.format(i, key), INVALID_VALUE, p)); 485 errors.add(TestError.builder(this, Severity.OTHER, INVALID_VALUE) 486 .message(tr("Presets do not contain property key"), marktr("Key ''{0}'' not in presets."), key) 487 .primitives(p) 488 .build()); 475 489 withErrors.put(p, "UPK"); 476 490 } … … 480 494 Map<String, String> possibleValues = getPossibleValues(presetsValueData.get(key)); 481 495 if (possibleValues.containsKey(fixedValue)) { 482 fi xedValue= possibleValues.get(fixedValue);496 final String newKey = possibleValues.get(fixedValue); 483 497 // misspelled preset value 484 String i = marktr("Value ''{0}'' for key ''{1}'' looks like ''{2}''."); 485 errors.add(new FixableTestError(this, Severity.WARNING, tr("Misspelled property value"), 486 tr(i, prop.getValue(), key, fixedValue), MessageFormat.format(i, prop.getValue(), fixedValue), 487 MISSPELLED_VALUE, p, new ChangePropertyCommand(p, key, fixedValue))); 498 errors.add(TestError.builder(this, Severity.WARNING, MISSPELLED_VALUE) 499 .message(tr("Misspelled property value"), 500 marktr("Value ''{0}'' for key ''{1}'' looks like ''{2}''."), prop.getValue(), key, fixedValue) 501 .primitives(p) 502 .fix(() -> new ChangePropertyCommand(p, key, newKey)) 503 .build()); 488 504 withErrors.put(p, "WPV"); 489 505 } else { 490 506 // unknown preset value 491 String i = marktr("Value ''{0}'' for key ''{1}'' not in presets."); 492 errors.add(new TestError(this, Severity.OTHER, tr("Presets do not contain property value"), 493 tr(i, prop.getValue(), key), MessageFormat.format(i, prop.getValue(), key), INVALID_VALUE, p)); 507 errors.add(TestError.builder(this, Severity.OTHER, INVALID_VALUE) 508 .message(tr("Presets do not contain property value"), 509 marktr("Value ''{0}'' for key ''{1}'' not in presets."), prop.getValue(), key) 510 .primitives(p) 511 .build()); 494 512 withErrors.put(p, "UPV"); 495 513 } … … 502 520 || key.contains("todo") || key.toLowerCase(Locale.ENGLISH).contains("fixme")) 503 521 && !withErrors.contains(p, "FIXME")) { 504 errors.add(new TestError(this, Severity.OTHER, 505 tr("FIXMES"), FIXME, p)); 522 errors.add(TestError.builder(this, Severity.OTHER, FIXME) 523 .message(tr("FIXMES")) 524 .primitives(p) 525 .build()); 506 526 withErrors.put(p, "FIXME"); 507 527 } … … 646 666 List<Command> commands = new ArrayList<>(50); 647 667 648 if (testError instanceof FixableTestError) { 649 commands.add(testError.getFix()); 650 } else { 651 Collection<? extends OsmPrimitive> primitives = testError.getPrimitives(); 652 for (OsmPrimitive p : primitives) { 653 Map<String, String> tags = p.getKeys(); 654 if (tags == null || tags.isEmpty()) { 655 continue; 656 } 657 658 for (Entry<String, String> prop: tags.entrySet()) { 659 String key = prop.getKey(); 660 String value = prop.getValue(); 661 if (value == null || value.trim().isEmpty()) { 662 commands.add(new ChangePropertyCommand(p, key, null)); 663 } else if (value.startsWith(" ") || value.endsWith(" ") || value.contains(" ")) { 664 commands.add(new ChangePropertyCommand(p, key, Tag.removeWhiteSpaces(value))); 665 } else if (key.startsWith(" ") || key.endsWith(" ") || key.contains(" ")) { 666 commands.add(new ChangePropertyKeyCommand(p, key, Tag.removeWhiteSpaces(key))); 667 } else { 668 String evalue = Entities.unescape(value); 669 if (!evalue.equals(value)) { 670 commands.add(new ChangePropertyCommand(p, key, evalue)); 671 } 668 Collection<? extends OsmPrimitive> primitives = testError.getPrimitives(); 669 for (OsmPrimitive p : primitives) { 670 Map<String, String> tags = p.getKeys(); 671 if (tags == null || tags.isEmpty()) { 672 continue; 673 } 674 675 for (Entry<String, String> prop: tags.entrySet()) { 676 String key = prop.getKey(); 677 String value = prop.getValue(); 678 if (value == null || value.trim().isEmpty()) { 679 commands.add(new ChangePropertyCommand(p, key, null)); 680 } else if (value.startsWith(" ") || value.endsWith(" ") || value.contains(" ")) { 681 commands.add(new ChangePropertyCommand(p, key, Tag.removeWhiteSpaces(value))); 682 } else if (key.startsWith(" ") || key.endsWith(" ") || key.contains(" ")) { 683 commands.add(new ChangePropertyKeyCommand(p, key, Tag.removeWhiteSpaces(key))); 684 } else { 685 String evalue = Entities.unescape(value); 686 if (!evalue.equals(value)) { 687 commands.add(new ChangePropertyCommand(p, key, evalue)); 672 688 } 673 689 } … … 840 856 841 857 public String getDescription() { 842 return tr(description);843 }844 845 public String getDescriptionOrig() {846 858 return description; 847 859 }
Note:
See TracChangeset
for help on using the changeset viewer.