Ignore:
Timestamp:
2013-11-07T22:14:53+01:00 (10 years ago)
Author:
simon04
Message:

see #9157 - make some errors in opening_hours fixable

File:
1 edited

Legend:

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

    r6376 r6377  
    1818
    1919import org.openstreetmap.josm.Main;
     20import org.openstreetmap.josm.command.ChangePropertyCommand;
    2021import org.openstreetmap.josm.data.osm.Node;
    2122import org.openstreetmap.josm.data.osm.OsmPrimitive;
    2223import org.openstreetmap.josm.data.osm.Relation;
    2324import org.openstreetmap.josm.data.osm.Way;
     25import org.openstreetmap.josm.data.validation.FixableTestError;
    2426import org.openstreetmap.josm.data.validation.Severity;
    2527import org.openstreetmap.josm.data.validation.Test;
     
    108110    }
    109111
     112    public class OpeningHoursTestError {
     113        final Severity severity;
     114        final String message, prettifiedValue;
     115
     116        public OpeningHoursTestError(String message, Severity severity, String prettifiedValue) {
     117            this.message = message;
     118            this.severity = severity;
     119            this.prettifiedValue = prettifiedValue;
     120        }
     121
     122        public OpeningHoursTestError(String message, Severity severity) {
     123            this(message, severity, null);
     124        }
     125
     126        public TestError getTestError(final OsmPrimitive p, final String key) {
     127            if (prettifiedValue == null) {
     128                return new TestError(OpeningHourTest.this, severity, message, 2901, p);
     129            } else {
     130                return new FixableTestError(OpeningHourTest.this, severity, message, 2901, p,
     131                        new ChangePropertyCommand(p, key, prettifiedValue));
     132            }
     133        }
     134
     135        public String getMessage() {
     136            return message;
     137        }
     138
     139        public String getPrettifiedValue() {
     140            return prettifiedValue;
     141        }
     142
     143        public Severity getSeverity() {
     144            return severity;
     145        }
     146    }
     147
    110148    /**
    111149     * Checks for a correct usage of the opening hour syntax of the {@code value} given according to
     
    113151     * validation errors or an empty list. Null values result in an empty list.
    114152     * @param value the opening hour value to be checked.
     153     * @param mode whether to validate {@code value} as a time range, or points in time, or both.
    115154     * @return a list of {@link TestError} or an empty list
    116155     */
    117     public List<TestError> checkOpeningHourSyntax(final String value, CheckMode mode) {
     156    public List<OpeningHoursTestError> checkOpeningHourSyntax(final String value, CheckMode mode) {
    118157        if (ENGINE == null || value == null || value.trim().isEmpty()) {
    119158            return Collections.emptyList();
     
    121160        try {
    122161            final Object r = parse(value, mode);
    123             final List<TestError> errors = new ArrayList<TestError>();
     162            final List<OpeningHoursTestError> errors = new ArrayList<OpeningHoursTestError>();
     163            String prettifiedValue = null;
     164            try {
     165                prettifiedValue = (String) ((Invocable) ENGINE).invokeMethod(r, "prettifyValue");
     166            } catch (Exception ignore) {
     167            }
    124168            for (final Object i : getList(((Invocable) ENGINE).invokeMethod(r, "getWarnings"))) {
    125                 errors.add(new TestError(this, Severity.WARNING, i.toString(), 2901, Collections.<OsmPrimitive>emptyList()));
     169                errors.add(new OpeningHoursTestError(i.toString(), Severity.WARNING, prettifiedValue));
    126170            }
    127171            return errors;
     
    131175                    .replaceAll("\\(<Unknown source.*", "")
    132176                    .trim();
    133             return Arrays.asList(new TestError(this, Severity.ERROR, message, 2901, Collections.<OsmPrimitive>emptyList()));
     177            return Arrays.asList(new OpeningHoursTestError(message, Severity.ERROR));
    134178        } catch (final Exception ex) {
    135179            throw new RuntimeException(ex);
     
    137181    }
    138182
    139     public List<TestError> checkOpeningHourSyntax(final String value) {
     183    public List<OpeningHoursTestError> checkOpeningHourSyntax(final String value) {
    140184        return checkOpeningHourSyntax(value, CheckMode.TIME_RANGE);
    141185    }
    142186
    143     protected void check(final OsmPrimitive p, final String tagValue, CheckMode mode) {
    144         for (TestError e : checkOpeningHourSyntax(tagValue, mode)) {
    145             e.setPrimitives(Collections.singletonList(p));
    146             errors.add(e);
     187    protected void check(final OsmPrimitive p, final String key, CheckMode mode) {
     188        for (OpeningHoursTestError e : checkOpeningHourSyntax(p.get(key), mode)) {
     189            errors.add(e.getTestError(p, key));
    147190        }
    148191    }
    149192
    150193    protected void check(final OsmPrimitive p) {
    151         check(p, p.get("opening_hours"), CheckMode.TIME_RANGE);
    152         check(p, p.get("collection_times"), CheckMode.BOTH);
    153         check(p, p.get("service_times"), CheckMode.BOTH);
     194        check(p, "opening_hours", CheckMode.TIME_RANGE);
     195        check(p, "collection_times", CheckMode.BOTH);
     196        check(p, "service_times", CheckMode.BOTH);
    154197    }
    155198
Note: See TracChangeset for help on using the changeset viewer.