Changeset 6377 in josm


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

see #9157 - make some errors in opening_hours fixable

Location:
trunk
Files:
1 added
2 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
  • trunk/test/unit/org/openstreetmap/josm/data/validation/tests/OpeningHourTestTest.java

    r6376 r6377  
    66import org.openstreetmap.josm.data.Preferences;
    77import org.openstreetmap.josm.data.validation.Severity;
    8 import org.openstreetmap.josm.data.validation.TestError;
    98
    109import java.util.List;
     
    3433    @Test
    3534    public void testCheckOpeningHourSyntax2() throws Exception {
    36         final List<TestError> errors = OPENING_HOUR_TEST.checkOpeningHourSyntax("Mo-Tue");
     35        final List<OpeningHourTest.OpeningHoursTestError> errors = OPENING_HOUR_TEST.checkOpeningHourSyntax("Mo-Tue");
    3736        assertThat(errors.size(), is(1));
    3837        assertThat(errors.get(0).getMessage(), is("Mo-Tue <--- (Please use the abbreviation \"Tu\" for \"tue\".)"));
     
    4241    @Test
    4342    public void testCheckOpeningHourSyntax3() throws Exception {
    44         final List<TestError> errors = OPENING_HOUR_TEST.checkOpeningHourSyntax("Sa-Su 10.00-20.00");
     43        final List<OpeningHourTest.OpeningHoursTestError> errors = OPENING_HOUR_TEST.checkOpeningHourSyntax("Sa-Su 10.00-20.00");
    4544        assertThat(errors.size(), is(2));
    4645        assertThat(errors.get(0).getMessage(), is("Sa-Su 10. <--- (Please use \":\" as hour/minute-separator)"));
    4746        assertThat(errors.get(0).getSeverity(), is(Severity.WARNING));
     47        assertThat(errors.get(0).getPrettifiedValue(), is("Sa-Su 10:00-20:00"));
    4848        assertThat(errors.get(1).getMessage(), is("Sa-Su 10.00-20. <--- (Please use \":\" as hour/minute-separator)"));
    4949        assertThat(errors.get(1).getSeverity(), is(Severity.WARNING));
     
    6262        assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax("badtext").get(0).getMessage(),
    6363                is("opening_hours - ba <--- (Unexpected token: \"b\" This means that the syntax is not valid at that point or it is currently not supported.)"));
     64        assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax("5.00 p.m-11.00 p.m").size(), is(1));
     65        assertThat(OPENING_HOUR_TEST.checkOpeningHourSyntax("5.00 p.m-11.00 p.m").get(0).getMessage(),
     66                is("opening_hours - 5.00 p <--- (hyphen (-) or open end (+) in time range expected. For working with points in time, the mode for opening_hours.js has to be altered. Maybe wrong tag?)"));
    6467    }
    6568
Note: See TracChangeset for help on using the changeset viewer.