Changeset 13147 in josm


Ignore:
Timestamp:
2017-11-23T00:44:47+01:00 (6 years ago)
Author:
Don-vip
Message:

see #15582 - mode is needed for conditional keys

Location:
trunk
Files:
3 edited

Legend:

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

    r13145 r13147  
    199199                    if (condition.matches(".*[0-9]:[0-9]{2}.*")) {
    200200                        final List<OpeningHourTest.OpeningHoursTestError> errors = openingHourTest.checkOpeningHourSyntax(
    201                                 "", condition, true, LanguageInfo.getJOSMLocaleCode());
     201                                "", condition, OpeningHourTest.CheckMode.TIME_RANGE, true, LanguageInfo.getJOSMLocaleCode());
    202202                        if (!errors.isEmpty()) {
    203203                            return errors.get(0).getMessage();
  • trunk/src/org/openstreetmap/josm/data/validation/tests/OpeningHourTest.java

    r13145 r13147  
    5757                ENGINE.eval("var nominatimJSON = {address: {state: 'Bayern', country_code: 'de'}};");
    5858                ENGINE.eval(
    59                         "var oh = function (value, tag_key, locale) {" +
     59                        "var oh = function (value, tag_key, mode, locale) {" +
    6060                        " try {" +
    61                         "    var r = new opening_hours(value, nominatimJSON, {tag_key: tag_key, locale: locale});" +
     61                        "    var conf = {tag_key: tag_key, locale: locale};" +
     62                        "    if (mode > -1) {" +
     63                        "      conf.mode = mode;" +
     64                        "    }" +
     65                        "    var r = new opening_hours(value, nominatimJSON, conf);" +
    6266                        "    r.getErrors = function() {return [];};" +
    6367                        "    return r;" +
     
    7781
    7882    /**
     83     * In OSM, the syntax originally designed to describe opening hours, is now used to describe a few other things as well.
     84     * Some of those other tags work with points in time instead of time ranges.
     85     * To support this the mode can be specified.
     86     * @since 13147
     87     */
     88    public enum CheckMode {
     89        /** time ranges (opening_hours, lit, …) default */
     90        TIME_RANGE(0),
     91        /** points in time */
     92        POINTS_IN_TIME(1),
     93        /** both (time ranges and points in time, used by collection_times, service_times, …) */
     94        BOTH(2);
     95        private final int code;
     96
     97        CheckMode(int code) {
     98            this.code = code;
     99        }
     100    }
     101
     102    /**
    79103     * Parses the opening hour syntax of the {@code value} given according to
    80104     * <a href="https://github.com/ypid/opening_hours.js">opening_hours.js</a> and returns an object on which
     
    82106     * @param value the opening hour value to be checked
    83107     * @param tagKey the OSM key (should be "opening_hours", "collection_times" or "service_times")
     108     * @param mode whether to validate {@code value} as a time range, or points in time, or both. Can be null
    84109     * @param locale the locale code used for localizing messages
    85110     * @return The value returned by the underlying method. Usually a {@code jdk.nashorn.api.scripting.ScriptObjectMirror}
    86111     * @throws ScriptException if an error occurs during invocation of the underlying method
    87112     * @throws NoSuchMethodException if underlying method with given name or matching argument types cannot be found
    88      * @since 13145
    89      */
    90     public Object parse(String value, String tagKey, String locale) throws ScriptException, NoSuchMethodException {
    91         return ((Invocable) ENGINE).invokeFunction("oh", value, tagKey, locale);
     113     * @since 13147
     114     */
     115    public Object parse(String value, String tagKey, CheckMode mode, String locale) throws ScriptException, NoSuchMethodException {
     116        return ((Invocable) ENGINE).invokeFunction("oh", value, tagKey, mode != null ? mode.code : -1, locale);
    92117    }
    93118
     
    183208     */
    184209    public List<OpeningHoursTestError> checkOpeningHourSyntax(final String key, final String value) {
    185         return checkOpeningHourSyntax(key, value, false, LanguageInfo.getJOSMLocaleCode());
     210        return checkOpeningHourSyntax(key, value, null, false, LanguageInfo.getJOSMLocaleCode());
    186211    }
    187212
     
    192217     * @param key the OSM key (should be "opening_hours", "collection_times" or "service_times").
    193218     * @param value the opening hour value to be checked.
     219     * @param mode whether to validate {@code value} as a time range, or points in time, or both. Can be null
    194220     * @param ignoreOtherSeverity whether to ignore errors with {@link Severity#OTHER}.
    195221     * @param locale the locale code used for localizing messages
    196222     * @return a list of {@link TestError} or an empty list
    197223     */
    198     public List<OpeningHoursTestError> checkOpeningHourSyntax(final String key, final String value,
     224    public List<OpeningHoursTestError> checkOpeningHourSyntax(final String key, final String value, CheckMode mode,
    199225            boolean ignoreOtherSeverity, String locale) {
    200226        if (ENGINE == null || value == null || value.isEmpty()) {
     
    203229        final List<OpeningHoursTestError> errors = new ArrayList<>();
    204230        try {
    205             final Object r = parse(value, key, locale);
     231            final Object r = parse(value, key, mode, locale);
    206232            String prettifiedValue = null;
    207233            try {
  • trunk/test/unit/org/openstreetmap/josm/data/validation/tests/OpeningHourTestTest.java

    r13145 r13147  
    7777    @Test
    7878    public void testI18n() {
    79         assertTrue(openingHourTest.checkOpeningHourSyntax("opening_hours", ".", false, "de")
     79        assertTrue(openingHourTest.checkOpeningHourSyntax("opening_hours", ".", null, false, "de")
    8080                .get(0).toString().contains("Unerwartetes Zeichen"));
    81         assertFalse(openingHourTest.checkOpeningHourSyntax("opening_hours", ".", false, "en")
     81        assertFalse(openingHourTest.checkOpeningHourSyntax("opening_hours", ".", null, false, "en")
    8282                .get(0).toString().contains("Unerwartetes Zeichen"));
    8383    }
Note: See TracChangeset for help on using the changeset viewer.