Ignore:
Timestamp:
2016-10-15T00:17:47+02:00 (8 years ago)
Author:
simon04
Message:

fix #13799 - Use builder pattern for TestError

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/validation/tests/TagChecker.java

    r11042 r11129  
    99import java.io.BufferedReader;
    1010import java.io.IOException;
    11 import java.text.MessageFormat;
    1211import java.util.ArrayList;
    1312import java.util.Arrays;
     
    3736import org.openstreetmap.josm.data.osm.OsmUtils;
    3837import org.openstreetmap.josm.data.osm.Tag;
    39 import org.openstreetmap.josm.data.validation.FixableTestError;
    4038import org.openstreetmap.josm.data.validation.Severity;
    4139import org.openstreetmap.josm.data.validation.Test.TagTest;
     
    392390            for (CheckerData d : checkerData) {
    393391                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());
    396396                    withErrors.put(p, "TC");
    397397                }
     
    404404            String value = prop.getValue();
    405405            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());
    408410                withErrors.put(p, "ICV");
    409411            }
    410412            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());
    413417                withErrors.put(p, "ICK");
    414418            }
    415419            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());
    418424                withErrors.put(p, "LV");
    419425            }
    420426            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());
    423431                withErrors.put(p, "LK");
    424432            }
    425433            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());
    428438                withErrors.put(p, "EV");
    429439            }
    430440            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());
    433445                withErrors.put(p, "IPK");
    434446            }
    435447            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());
    438452                withErrors.put(p, "SPACE");
    439453            }
    440454            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());
    443459                withErrors.put(p, "SPACE");
    444460            }
    445461            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());
    448466                withErrors.put(p, "HTML");
    449467            }
     
    455473                        if (fixedKey != null && !"".equals(fixedKey) && !fixedKey.equals(key)) {
    456474                            // 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);
    459478                            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());
    463480                            } 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());
    468482                            }
    469                             errors.add(error);
    470483                            withErrors.put(p, "WPK");
    471484                        } 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());
    475489                            withErrors.put(p, "UPK");
    476490                        }
     
    480494                        Map<String, String> possibleValues = getPossibleValues(presetsValueData.get(key));
    481495                        if (possibleValues.containsKey(fixedValue)) {
    482                             fixedValue = possibleValues.get(fixedValue);
     496                            final String newKey = possibleValues.get(fixedValue);
    483497                            // 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());
    488504                            withErrors.put(p, "WPV");
    489505                        } else {
    490506                            // 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());
    494512                            withErrors.put(p, "UPV");
    495513                        }
     
    502520                        || key.contains("todo") || key.toLowerCase(Locale.ENGLISH).contains("fixme"))
    503521                        && !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());
    506526                    withErrors.put(p, "FIXME");
    507527                }
     
    646666        List<Command> commands = new ArrayList<>(50);
    647667
    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));
    672688                    }
    673689                }
     
    840856
    841857        public String getDescription() {
    842             return tr(description);
    843         }
    844 
    845         public String getDescriptionOrig() {
    846858            return description;
    847859        }
Note: See TracChangeset for help on using the changeset viewer.