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.util.ArrayList;
|
---|
7 | import java.util.Arrays;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.HashSet;
|
---|
10 | import java.util.List;
|
---|
11 | import java.util.Set;
|
---|
12 | import java.util.regex.Matcher;
|
---|
13 | import java.util.regex.Pattern;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.Main;
|
---|
16 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
17 | import org.openstreetmap.josm.data.validation.Severity;
|
---|
18 | import org.openstreetmap.josm.data.validation.Test;
|
---|
19 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
20 | import org.openstreetmap.josm.tools.LanguageInfo;
|
---|
21 | import org.openstreetmap.josm.tools.SubclassFilteredCollection;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Checks for <a href="http://wiki.openstreetmap.org/wiki/Conditional_restrictions">conditional restrictions</a>
|
---|
25 | * @since 6605
|
---|
26 | */
|
---|
27 | public class ConditionalKeys extends Test.TagTest {
|
---|
28 |
|
---|
29 | private final OpeningHourTest openingHourTest = new OpeningHourTest();
|
---|
30 | private static final Set<String> RESTRICTION_TYPES = new HashSet<>(Arrays.asList("oneway", "toll", "noexit", "maxspeed", "minspeed",
|
---|
31 | "maxstay", "maxweight", "maxaxleload", "maxheight", "maxwidth", "maxlength", "overtaking", "maxgcweight", "maxgcweightrating",
|
---|
32 | "fee"));
|
---|
33 | private static final Set<String> RESTRICTION_VALUES = new HashSet<>(Arrays.asList("yes", "official", "designated", "destination",
|
---|
34 | "delivery", "permissive", "private", "agricultural", "forestry", "no"));
|
---|
35 | private static final Set<String> TRANSPORT_MODES = new HashSet<>(Arrays.asList("access", "foot", "ski", "inline_skates", "ice_skates",
|
---|
36 | "horse", "vehicle", "bicycle", "carriage", "trailer", "caravan", "motor_vehicle", "motorcycle", "moped", "mofa",
|
---|
37 | "motorcar", "motorhome", "psv", "bus", "taxi", "tourist_bus", "goods", "hgv", "agricultural", "atv", "snowmobile"
|
---|
38 | /*,"hov","emergency","hazmat","disabled"*/));
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Constructs a new {@code ConditionalKeys}.
|
---|
42 | */
|
---|
43 | public ConditionalKeys() {
|
---|
44 | super(tr("Conditional Keys"), tr("Tests for the correct usage of ''*:conditional'' tags."));
|
---|
45 | }
|
---|
46 |
|
---|
47 | @Override
|
---|
48 | public void initialize() throws Exception {
|
---|
49 | super.initialize();
|
---|
50 | openingHourTest.initialize();
|
---|
51 | }
|
---|
52 |
|
---|
53 | public static boolean isRestrictionType(String part) {
|
---|
54 | return RESTRICTION_TYPES.contains(part);
|
---|
55 | }
|
---|
56 |
|
---|
57 | public static boolean isRestrictionValue(String part) {
|
---|
58 | return RESTRICTION_VALUES.contains(part);
|
---|
59 | }
|
---|
60 |
|
---|
61 | public static boolean isTransportationMode(String part) {
|
---|
62 | // http://wiki.openstreetmap.org/wiki/Key:access#Transport_mode_restrictions
|
---|
63 | return TRANSPORT_MODES.contains(part);
|
---|
64 | }
|
---|
65 |
|
---|
66 | public static boolean isDirection(String part) {
|
---|
67 | return "forward".equals(part) || "backward".equals(part);
|
---|
68 | }
|
---|
69 |
|
---|
70 | public boolean isKeyValid(String key) {
|
---|
71 | // <restriction-type>[:<transportation mode>][:<direction>]:conditional
|
---|
72 | // -- or -- <transportation mode> [:<direction>]:conditional
|
---|
73 | if (!key.endsWith(":conditional")) {
|
---|
74 | return false;
|
---|
75 | }
|
---|
76 | final String[] parts = key.replaceAll(":conditional", "").split(":");
|
---|
77 | return isKeyValid3Parts(parts) || isKeyValid1Part(parts) || isKeyValid2Parts(parts);
|
---|
78 | }
|
---|
79 |
|
---|
80 | private static boolean isKeyValid3Parts(String... parts) {
|
---|
81 | return parts.length == 3 && isRestrictionType(parts[0]) && isTransportationMode(parts[1]) && isDirection(parts[2]);
|
---|
82 | }
|
---|
83 |
|
---|
84 | private static boolean isKeyValid2Parts(String... parts) {
|
---|
85 | return parts.length == 2 && ((isRestrictionType(parts[0]) && (isTransportationMode(parts[1]) || isDirection(parts[1])))
|
---|
86 | || (isTransportationMode(parts[0]) && isDirection(parts[1])));
|
---|
87 | }
|
---|
88 |
|
---|
89 | private static boolean isKeyValid1Part(String... parts) {
|
---|
90 | return parts.length == 1 && (isRestrictionType(parts[0]) || isTransportationMode(parts[0]));
|
---|
91 | }
|
---|
92 |
|
---|
93 | public boolean isValueValid(String key, String value) {
|
---|
94 | return validateValue(key, value) == null;
|
---|
95 | }
|
---|
96 |
|
---|
97 | static class ConditionalParsingException extends RuntimeException {
|
---|
98 | ConditionalParsingException(String message) {
|
---|
99 | super(message);
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | public static class ConditionalValue {
|
---|
104 | public final String restrictionValue;
|
---|
105 | public final Collection<String> conditions;
|
---|
106 |
|
---|
107 | public ConditionalValue(String restrictionValue, Collection<String> conditions) {
|
---|
108 | this.restrictionValue = restrictionValue;
|
---|
109 | this.conditions = conditions;
|
---|
110 | }
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Parses the condition values as string.
|
---|
114 | * @param value value, must match {@code <restriction-value> @ <condition>[;<restriction-value> @ <condition>]} pattern
|
---|
115 | * @return list of {@code ConditionalValue}s
|
---|
116 | * @throws ConditionalParsingException if {@code value} does not match expected pattern
|
---|
117 | */
|
---|
118 | public static List<ConditionalValue> parse(String value) {
|
---|
119 | // <restriction-value> @ <condition>[;<restriction-value> @ <condition>]
|
---|
120 | final List<ConditionalValue> r = new ArrayList<>();
|
---|
121 | final String part = Pattern.compile("([^@\\p{Space}][^@]*?)"
|
---|
122 | + "\\s*@\\s*" + "(\\([^)\\p{Space}][^)]+?\\)|[^();\\p{Space}][^();]*?)\\s*").toString();
|
---|
123 | final Matcher m = Pattern.compile('(' + part + ")(;\\s*" + part + ")*").matcher(value);
|
---|
124 | if (!m.matches()) {
|
---|
125 | throw new ConditionalParsingException(tr("Does not match pattern ''restriction value @ condition''"));
|
---|
126 | } else {
|
---|
127 | int i = 2;
|
---|
128 | while (i + 1 <= m.groupCount() && m.group(i + 1) != null) {
|
---|
129 | final String restrictionValue = m.group(i);
|
---|
130 | final String[] conditions = m.group(i + 1).replace("(", "").replace(")", "").split("\\s+(AND|and)\\s+");
|
---|
131 | r.add(new ConditionalValue(restrictionValue, Arrays.asList(conditions)));
|
---|
132 | i += 3;
|
---|
133 | }
|
---|
134 | }
|
---|
135 | return r;
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | public String validateValue(String key, String value) {
|
---|
140 | try {
|
---|
141 | for (final ConditionalValue conditional : ConditionalValue.parse(value)) {
|
---|
142 | // validate restriction value
|
---|
143 | if (isTransportationMode(key.split(":")[0]) && !isRestrictionValue(conditional.restrictionValue)) {
|
---|
144 | return tr("{0} is not a valid restriction value", conditional.restrictionValue);
|
---|
145 | }
|
---|
146 | // validate opening hour if the value contains an hour (heuristic)
|
---|
147 | for (final String condition : conditional.conditions) {
|
---|
148 | if (condition.matches(".*[0-9]:[0-9]{2}.*")) {
|
---|
149 | final List<OpeningHourTest.OpeningHoursTestError> errors = openingHourTest.checkOpeningHourSyntax(
|
---|
150 | "", condition, OpeningHourTest.CheckMode.TIME_RANGE, true, LanguageInfo.getJOSMLocaleCode());
|
---|
151 | if (!errors.isEmpty()) {
|
---|
152 | return errors.get(0).getMessage();
|
---|
153 | }
|
---|
154 | }
|
---|
155 | }
|
---|
156 | }
|
---|
157 | } catch (ConditionalParsingException ex) {
|
---|
158 | Main.debug(ex);
|
---|
159 | return ex.getMessage();
|
---|
160 | }
|
---|
161 | return null;
|
---|
162 | }
|
---|
163 |
|
---|
164 | public List<TestError> validatePrimitive(OsmPrimitive p) {
|
---|
165 | final List<TestError> errors = new ArrayList<>();
|
---|
166 | for (final String key : SubclassFilteredCollection.filter(p.keySet(),
|
---|
167 | Pattern.compile(":conditional(:.*)?$").asPredicate())) {
|
---|
168 | if (!isKeyValid(key)) {
|
---|
169 | errors.add(TestError.builder(this, Severity.WARNING, 3201)
|
---|
170 | .message(tr("Wrong syntax in {0} key", key))
|
---|
171 | .primitives(p)
|
---|
172 | .build());
|
---|
173 | continue;
|
---|
174 | }
|
---|
175 | final String value = p.get(key);
|
---|
176 | final String error = validateValue(key, value);
|
---|
177 | if (error != null) {
|
---|
178 | errors.add(TestError.builder(this, Severity.WARNING, 3202)
|
---|
179 | .message(tr("Error in {0} value: {1}", key, error))
|
---|
180 | .primitives(p)
|
---|
181 | .build());
|
---|
182 | }
|
---|
183 | }
|
---|
184 | return errors;
|
---|
185 | }
|
---|
186 |
|
---|
187 | @Override
|
---|
188 | public void check(OsmPrimitive p) {
|
---|
189 | errors.addAll(validatePrimitive(p));
|
---|
190 | }
|
---|
191 | }
|
---|