Changeset 16086 in josm for trunk/src/org
- Timestamp:
- 2020-03-08T19:21:11+01:00 (5 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/data/validation/tests
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/validation/tests/ConditionalKeys.java
r15978 r16086 9 9 import java.util.HashSet; 10 10 import java.util.List; 11 import java.util.Locale;12 11 import java.util.Set; 13 12 import java.util.regex.Matcher; … … 204 203 for (final String condition : conditional.conditions) { 205 204 if (condition.matches(".*[0-9]:[0-9]{2}.*")) { 206 final List<OpeningHourTest.OpeningHoursTestError> errors = openingHourTest.checkOpeningHourSyntax( 207 "", condition, true, Locale.getDefault()); 205 final List<TestError> errors = openingHourTest.checkOpeningHourSyntax("", condition); 208 206 if (!errors.isEmpty()) { 209 return errors.get(0).get Message();207 return errors.get(0).getDescription(); 210 208 } 211 209 } -
trunk/src/org/openstreetmap/josm/data/validation/tests/OpeningHourTest.java
r16085 r16086 5 5 6 6 import java.io.StringReader; 7 import java.util.Arrays; 8 import java.util.Collection; 7 9 import java.util.Collections; 8 10 import java.util.List; … … 29 31 public class OpeningHourTest extends TagTest { 30 32 33 private static final Collection<String> KEYS_TO_CHECK = Arrays.asList("opening_hours", "collection_times", "service_times"); 34 31 35 /** 32 36 * Constructs a new {@code OpeningHourTest}. … … 38 42 39 43 /** 40 * An error concerning invalid syntax for an "opening_hours"-like tag. 44 * Returns the real test error given to JOSM validator. 45 * @param severity The error severity 46 * @param message The error message 47 * @param key The incriminated key, used for display. 48 * @param prettifiedValue The prettified value 49 * @param p The incriminated OSM primitive. 50 * @return The real test error given to JOSM validator. Can be fixable or not if a prettified values has been determined. 41 51 */ 42 public class OpeningHoursTestError { 43 private final Severity severity; 44 private final String message; 45 private final String prettifiedValue; 46 47 /** 48 * Constructs a new {@code OpeningHoursTestError} with a known prettified value. 49 * @param message The error message 50 * @param severity The error severity 51 * @param prettifiedValue The prettified value 52 */ 53 public OpeningHoursTestError(String message, Severity severity, String prettifiedValue) { 54 this.message = message; 55 this.severity = severity; 56 this.prettifiedValue = prettifiedValue; 57 } 58 59 /** 60 * Returns the real test error given to JOSM validator. 61 * @param p The incriminated OSM primitive. 62 * @param key The incriminated key, used for display. 63 * @return The real test error given to JOSM validator. Can be fixable or not if a prettified values has been determined. 64 */ 65 public TestError getTestError(final OsmPrimitive p, final String key) { 66 final TestError.Builder error = TestError.builder(OpeningHourTest.this, severity, 2901) 67 .message(tr("Opening hours syntax"), message) // todo obtain English message for ignore functionality 68 .primitives(p); 69 if (prettifiedValue == null || prettifiedValue.equals(p.get(key))) { 70 return error.build(); 71 } else { 72 return error.fix(() -> new ChangePropertyCommand(p, key, prettifiedValue)).build(); 73 } 74 } 75 76 /** 77 * Returns the error message. 78 * @return The error message. 79 */ 80 public String getMessage() { 81 return message; 82 } 83 84 /** 85 * Returns the prettified value. 86 * @return The prettified value. 87 */ 88 public String getPrettifiedValue() { 89 return prettifiedValue; 90 } 91 92 /** 93 * Returns the error severity. 94 * @return The error severity. 95 */ 96 public Severity getSeverity() { 97 return severity; 98 } 99 100 @Override 101 public String toString() { 102 return getMessage() + " => " + getPrettifiedValue(); 52 private TestError createTestError(Severity severity, String message, String key, String prettifiedValue, OsmPrimitive p) { 53 final TestError.Builder error = TestError.builder(this, severity, 2901) 54 .message(tr("Opening hours syntax"), message) // todo obtain English message for ignore functionality 55 .primitives(p); 56 if (prettifiedValue == null || prettifiedValue.equals(p.get(key))) { 57 return error.build(); 58 } else { 59 return error.fix(() -> new ChangePropertyCommand(p, key, prettifiedValue)).build(); 103 60 } 104 61 } … … 111 68 * @return a list of {@link TestError} or an empty list 112 69 */ 113 public List< OpeningHoursTestError> checkOpeningHourSyntax(final String key, final String value) {114 return checkOpeningHourSyntax(key, value, false, Locale.getDefault());70 public List<TestError> checkOpeningHourSyntax(final String key, final String value) { 71 return checkOpeningHourSyntax(key, value, null, Locale.getDefault()); 115 72 } 116 73 … … 120 77 * @param key the OSM key (should be "opening_hours", "collection_times" or "service_times"). 121 78 * @param value the opening hour value to be checked. 122 * @param ignoreOtherSeverity whether to ignore errors with {@link Severity#OTHER}.79 * @param p the primitive to check/fix. 123 80 * @param locale the locale code used for localizing messages 124 81 * @return a list of {@link TestError} or an empty list 125 82 */ 126 public List<OpeningHoursTestError> checkOpeningHourSyntax(final String key, final String value, boolean ignoreOtherSeverity, Locale locale) {83 List<TestError> checkOpeningHourSyntax(final String key, final String value, OsmPrimitive p, Locale locale) { 127 84 if (value == null || value.isEmpty()) { 128 85 return Collections.emptyList(); … … 139 96 } 140 97 } catch (ParseException e) { 141 return Collections.singletonList( new OpeningHoursTestError(e.getMessage(), Severity.WARNING, prettifiedValue));98 return Collections.singletonList(createTestError(Severity.WARNING, e.getMessage(), key, prettifiedValue, p)); 142 99 } 143 100 144 if ( ignoreOtherSeverity || Objects.equals(value, prettifiedValue)) {101 if (!includeOtherSeverityChecks() || Objects.equals(value, prettifiedValue) || p == null) { 145 102 return Collections.emptyList(); 146 103 } else { 147 return Collections.singletonList( 148 new OpeningHoursTestError(tr("{0} value can be prettified", key), Severity.OTHER, prettifiedValue)); 149 } 150 } 151 152 protected void check(final OsmPrimitive p, final String key) { 153 for (OpeningHoursTestError e : checkOpeningHourSyntax(key, p.get(key))) { 154 errors.add(e.getTestError(p, key)); 104 final String message = tr("{0} value can be prettified", key); 105 return Collections.singletonList(createTestError(Severity.OTHER, message, key, prettifiedValue, p)); 155 106 } 156 107 } … … 158 109 @Override 159 110 public void check(final OsmPrimitive p) { 160 check(p, "opening_hours");161 check(p, "collection_times");162 check(p, "service_times");111 for (String key : KEYS_TO_CHECK) { 112 errors.addAll(checkOpeningHourSyntax(key, p.get(key), p, Locale.getDefault())); 113 } 163 114 } 164 115 }
Note:
See TracChangeset
for help on using the changeset viewer.