1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.validation.tests;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.InputStreamReader;
|
---|
7 | import java.io.Reader;
|
---|
8 | import java.nio.charset.StandardCharsets;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.Arrays;
|
---|
11 | import java.util.Collections;
|
---|
12 | import java.util.List;
|
---|
13 |
|
---|
14 | import javax.script.Invocable;
|
---|
15 | import javax.script.ScriptEngine;
|
---|
16 | import javax.script.ScriptEngineManager;
|
---|
17 | import javax.script.ScriptException;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.Main;
|
---|
20 | import org.openstreetmap.josm.command.ChangePropertyCommand;
|
---|
21 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
22 | import org.openstreetmap.josm.data.validation.FixableTestError;
|
---|
23 | import org.openstreetmap.josm.data.validation.Severity;
|
---|
24 | import org.openstreetmap.josm.data.validation.Test;
|
---|
25 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
26 | import org.openstreetmap.josm.io.CachedFile;
|
---|
27 | import org.openstreetmap.josm.tools.LanguageInfo;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Tests the correct usage of the opening hour syntax of the tags
|
---|
31 | * {@code opening_hours}, {@code collection_times}, {@code service_times} according to
|
---|
32 | * <a href="https://github.com/ypid/opening_hours.js">opening_hours.js</a>.
|
---|
33 | *
|
---|
34 | * @since 6370
|
---|
35 | */
|
---|
36 | public class OpeningHourTest extends Test.TagTest {
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Javascript engine
|
---|
40 | */
|
---|
41 | public static final ScriptEngine ENGINE = new ScriptEngineManager().getEngineByName("JavaScript");
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Constructs a new {@code OpeningHourTest}.
|
---|
45 | */
|
---|
46 | public OpeningHourTest() {
|
---|
47 | super(tr("Opening hours syntax"),
|
---|
48 | tr("This test checks the correct usage of the opening hours syntax."));
|
---|
49 | }
|
---|
50 |
|
---|
51 | @Override
|
---|
52 | public void initialize() throws Exception {
|
---|
53 | super.initialize();
|
---|
54 | if (ENGINE != null) {
|
---|
55 | try (CachedFile cf = new CachedFile("resource://data/validator/opening_hours.js");
|
---|
56 | Reader reader = new InputStreamReader(cf.getInputStream(), StandardCharsets.UTF_8)) {
|
---|
57 | ENGINE.eval(reader);
|
---|
58 | ENGINE.eval("var opening_hours = require('opening_hours');");
|
---|
59 | // fake country/state to not get errors on holidays
|
---|
60 | ENGINE.eval("var nominatimJSON = {address: {state: 'Bayern', country_code: 'de'}};");
|
---|
61 | ENGINE.eval(
|
---|
62 | "var oh = function (value, mode, locale) {" +
|
---|
63 | " try {" +
|
---|
64 | " var r = new opening_hours(value, nominatimJSON, {mode: mode, locale: locale});" +
|
---|
65 | " r.getErrors = function() {return [];};" +
|
---|
66 | " return r;" +
|
---|
67 | " } catch (err) {" +
|
---|
68 | " return {" +
|
---|
69 | " getWarnings: function() {return [];}," +
|
---|
70 | " getErrors: function() {return [err.toString()]}" +
|
---|
71 | " };" +
|
---|
72 | " }" +
|
---|
73 | "};");
|
---|
74 | }
|
---|
75 | } else {
|
---|
76 | Main.warn("Unable to initialize OpeningHourTest because no JavaScript engine has been found");
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | enum CheckMode {
|
---|
81 | TIME_RANGE(0), POINTS_IN_TIME(1), BOTH(2);
|
---|
82 | private final int code;
|
---|
83 |
|
---|
84 | CheckMode(int code) {
|
---|
85 | this.code = code;
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | protected Object parse(String value, CheckMode mode, String locale) throws ScriptException, NoSuchMethodException {
|
---|
90 | return ((Invocable) ENGINE).invokeFunction("oh", value, mode.code, locale);
|
---|
91 | }
|
---|
92 |
|
---|
93 | @SuppressWarnings("unchecked")
|
---|
94 | protected List<Object> getList(Object obj) throws ScriptException, NoSuchMethodException {
|
---|
95 | if (obj == null || "".equals(obj)) {
|
---|
96 | return Arrays.asList();
|
---|
97 | } else if (obj instanceof String) {
|
---|
98 | final Object[] strings = ((String) obj).split("\\\\n");
|
---|
99 | return Arrays.asList(strings);
|
---|
100 | } else if (obj instanceof List) {
|
---|
101 | return (List<Object>) obj;
|
---|
102 | } else {
|
---|
103 | // recursively call getList() with argument converted to newline-separated string
|
---|
104 | return getList(((Invocable) ENGINE).invokeMethod(obj, "join", "\\n"));
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * An error concerning invalid syntax for an "opening_hours"-like tag.
|
---|
110 | */
|
---|
111 | public class OpeningHoursTestError {
|
---|
112 | private final Severity severity;
|
---|
113 | private final String message;
|
---|
114 | private final String prettifiedValue;
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Constructs a new {@code OpeningHoursTestError} with a known pretiffied value.
|
---|
118 | * @param message The error message
|
---|
119 | * @param severity The error severity
|
---|
120 | * @param prettifiedValue The prettified value
|
---|
121 | */
|
---|
122 | public OpeningHoursTestError(String message, Severity severity, String prettifiedValue) {
|
---|
123 | this.message = message;
|
---|
124 | this.severity = severity;
|
---|
125 | this.prettifiedValue = prettifiedValue;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Returns the real test error given to JOSM validator.
|
---|
130 | * @param p The incriminated OSM primitive.
|
---|
131 | * @param key The incriminated key, used for display.
|
---|
132 | * @return The real test error given to JOSM validator. Can be fixable or not if a prettified values has been determined.
|
---|
133 | */
|
---|
134 | public TestError getTestError(final OsmPrimitive p, final String key) {
|
---|
135 | final String messageEn = message; // todo obtain English message for ignore functionality
|
---|
136 | if (prettifiedValue == null || prettifiedValue.equals(p.get(key))) {
|
---|
137 | return new TestError(OpeningHourTest.this, severity, tr("Opening hours syntax"), message, messageEn, 2901, p);
|
---|
138 | } else {
|
---|
139 | return new FixableTestError(OpeningHourTest.this, severity, tr("Opening hours syntax"), message, messageEn, 2901, p,
|
---|
140 | new ChangePropertyCommand(p, key, prettifiedValue));
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Returns the error message.
|
---|
146 | * @return The error message.
|
---|
147 | */
|
---|
148 | public String getMessage() {
|
---|
149 | return message;
|
---|
150 | }
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Returns the prettified value.
|
---|
154 | * @return The prettified value.
|
---|
155 | */
|
---|
156 | public String getPrettifiedValue() {
|
---|
157 | return prettifiedValue;
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Returns the error severity.
|
---|
162 | * @return The error severity.
|
---|
163 | */
|
---|
164 | public Severity getSeverity() {
|
---|
165 | return severity;
|
---|
166 | }
|
---|
167 |
|
---|
168 | @Override
|
---|
169 | public String toString() {
|
---|
170 | return getMessage() + " => " + getPrettifiedValue();
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * Checks for a correct usage of the opening hour syntax of the {@code value} given according to
|
---|
176 | * <a href="https://github.com/ypid/opening_hours.js">opening_hours.js</a> and returns a list containing
|
---|
177 | * validation errors or an empty list. Null values result in an empty list.
|
---|
178 | * @param key the OSM key (should be "opening_hours", "collection_times" or "service_times"). Used in error message
|
---|
179 | * @param value the opening hour value to be checked.
|
---|
180 | * @param mode whether to validate {@code value} as a time range, or points in time, or both.
|
---|
181 | * @return a list of {@link TestError} or an empty list
|
---|
182 | */
|
---|
183 | public List<OpeningHoursTestError> checkOpeningHourSyntax(final String key, final String value, CheckMode mode) {
|
---|
184 | return checkOpeningHourSyntax(key, value, mode, false, LanguageInfo.getJOSMLocaleCode());
|
---|
185 | }
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * Checks for a correct usage of the opening hour syntax of the {@code value} given according to
|
---|
189 | * <a href="https://github.com/ypid/opening_hours.js">opening_hours.js</a> and returns a list containing
|
---|
190 | * validation errors or an empty list. Null values result in an empty list.
|
---|
191 | * @param key the OSM key (should be "opening_hours", "collection_times" or "service_times").
|
---|
192 | * @param value the opening hour value to be checked.
|
---|
193 | * @param mode whether to validate {@code value} as a time range, or points in time, or both.
|
---|
194 | * @param ignoreOtherSeverity whether to ignore errors with {@link Severity#OTHER}.
|
---|
195 | * @param locale the locale code used for localizing messages
|
---|
196 | * @return a list of {@link TestError} or an empty list
|
---|
197 | */
|
---|
198 | public List<OpeningHoursTestError> checkOpeningHourSyntax(final String key, final String value, CheckMode mode,
|
---|
199 | boolean ignoreOtherSeverity, String locale) {
|
---|
200 | if (ENGINE == null || value == null || value.trim().isEmpty()) {
|
---|
201 | return Collections.emptyList();
|
---|
202 | }
|
---|
203 | final List<OpeningHoursTestError> errors = new ArrayList<>();
|
---|
204 | try {
|
---|
205 | final Object r = parse(value, mode, locale);
|
---|
206 | String prettifiedValue = null;
|
---|
207 | try {
|
---|
208 | prettifiedValue = (String) ((Invocable) ENGINE).invokeMethod(r, "prettifyValue");
|
---|
209 | } catch (ScriptException | NoSuchMethodException e) {
|
---|
210 | Main.warn(e);
|
---|
211 | }
|
---|
212 | for (final Object i : getList(((Invocable) ENGINE).invokeMethod(r, "getErrors"))) {
|
---|
213 | errors.add(new OpeningHoursTestError(getErrorMessage(key, i), Severity.ERROR, prettifiedValue));
|
---|
214 | }
|
---|
215 | for (final Object i : getList(((Invocable) ENGINE).invokeMethod(r, "getWarnings"))) {
|
---|
216 | errors.add(new OpeningHoursTestError(getErrorMessage(key, i), Severity.WARNING, prettifiedValue));
|
---|
217 | }
|
---|
218 | if (!ignoreOtherSeverity && errors.isEmpty() && prettifiedValue != null && !value.equals(prettifiedValue)) {
|
---|
219 | errors.add(new OpeningHoursTestError(tr("opening_hours value can be prettified"), Severity.OTHER, prettifiedValue));
|
---|
220 | }
|
---|
221 | } catch (ScriptException | NoSuchMethodException ex) {
|
---|
222 | Main.error(ex);
|
---|
223 | }
|
---|
224 | return errors;
|
---|
225 | }
|
---|
226 |
|
---|
227 | /**
|
---|
228 | * Translates and shortens the error/warning message.
|
---|
229 | * @param key OSM key
|
---|
230 | * @param o error/warnign message
|
---|
231 | * @return translated/shortened error/warnign message
|
---|
232 | */
|
---|
233 | private static String getErrorMessage(String key, Object o) {
|
---|
234 | String msg = o.toString().trim()
|
---|
235 | .replace("Unexpected token:", tr("Unexpected token:"))
|
---|
236 | .replace("Unexpected token (school holiday parser):", tr("Unexpected token (school holiday parser):"))
|
---|
237 | .replace("Unexpected token in number range:", tr("Unexpected token in number range:"))
|
---|
238 | .replace("Unexpected token in week range:", tr("Unexpected token in week range:"))
|
---|
239 | .replace("Unexpected token in weekday range:", tr("Unexpected token in weekday range:"))
|
---|
240 | .replace("Unexpected token in month range:", tr("Unexpected token in month range:"))
|
---|
241 | .replace("Unexpected token in year range:", tr("Unexpected token in year range:"))
|
---|
242 | .replace("This means that the syntax is not valid at that point or it is currently not supported.", tr("Invalid/unsupported syntax."));
|
---|
243 | return key + " - " + msg;
|
---|
244 | }
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Checks for a correct usage of the opening hour syntax of the {@code value} given, in time range mode, according to
|
---|
248 | * <a href="https://github.com/ypid/opening_hours.js">opening_hours.js</a> and returns a list containing
|
---|
249 | * validation errors or an empty list. Null values result in an empty list.
|
---|
250 | * @param key the OSM key (should be "opening_hours", "collection_times" or "service_times"). Used in error message
|
---|
251 | * @param value the opening hour value to be checked.
|
---|
252 | * @return a list of {@link TestError} or an empty list
|
---|
253 | */
|
---|
254 | public List<OpeningHoursTestError> checkOpeningHourSyntax(final String key, final String value) {
|
---|
255 | return checkOpeningHourSyntax(key, value, "opening_hours".equals(key) ? CheckMode.TIME_RANGE : CheckMode.BOTH);
|
---|
256 | }
|
---|
257 |
|
---|
258 | protected void check(final OsmPrimitive p, final String key, CheckMode mode) {
|
---|
259 | for (OpeningHoursTestError e : checkOpeningHourSyntax(key, p.get(key), mode)) {
|
---|
260 | errors.add(e.getTestError(p, key));
|
---|
261 | }
|
---|
262 | }
|
---|
263 |
|
---|
264 | @Override
|
---|
265 | public void check(final OsmPrimitive p) {
|
---|
266 | check(p, "opening_hours", CheckMode.TIME_RANGE);
|
---|
267 | check(p, "collection_times", CheckMode.BOTH);
|
---|
268 | check(p, "service_times", CheckMode.BOTH);
|
---|
269 | }
|
---|
270 | }
|
---|