source: josm/trunk/src/org/openstreetmap/josm/data/validation/tests/OpeningHourTest.java@ 7082

Last change on this file since 7082 was 7082, checked in by Don-vip, 10 years ago

see #8465 - replace Utils.UTF_8 by StandardCharsets.UTF_8, new in Java 7

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