- Timestamp:
- 2014-01-02T23:58:58+01:00 (11 years ago)
- Location:
- trunk
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/data/validator/highway.mapcss
r6550 r6601 1 way[highway=~/^(motorway|trunk|primary|secondary|tertiary)(_link)?$/] { 2 set major_road; 3 } 4 way[highway=~/^(unclassified|residential|living_street|service)$/] { 5 set minor_road; 6 } 7 1 8 way[highway][name =~ /(?i).* (Ave|Blvd|Br|Brg|Cct|Cir|Cl|Cr|Crct|Cres|Crt|Ct|Dr|Drv|Esp|Espl|Hwy|Ln|Mw|Mwy|Pl|Rd|Qy|Qys|Sq|St|Str|Ter|Tce|Tr|Wy)[.]?$/] { 2 9 throwWarning: tr("abbreviated street name"); … … 40 47 } 41 48 42 way [highway =~ /motorway|trunk|primary|secondary|tertiary/][!ref] {49 way.major_road[!ref] { 43 50 throwOther: tr("highway without a reference"); 44 51 assertMatch: "way highway=primary"; … … 46 53 } 47 54 48 way [foot][highway =~ /motorway|trunk|primary|secondary|tertiary/] {49 throwWarning: tr("{0} used with {1}", "{0.key}", "{1.tag}");55 way.major_road[foot] { 56 throwWarning: tr("{0} used with {1}", tr("major road"), "{0.tag}"); 50 57 suggestAlternative: "sidewalk"; 51 suggestAlternative: "separate footway";58 suggestAlternative: tr("separate footway"); 52 59 assertMatch: "way highway=primary foot=yes"; 53 60 assertNoMatch: "way highway=primary"; -
trunk/src/org/openstreetmap/josm/data/validation/tests/MapCSSTagChecker.java
r6591 r6601 23 23 import org.openstreetmap.josm.command.Command; 24 24 import org.openstreetmap.josm.command.SequenceCommand; 25 import org.openstreetmap.josm.data.osm.Node;26 25 import org.openstreetmap.josm.data.osm.OsmPrimitive; 27 import org.openstreetmap.josm.data.osm.Relation;28 26 import org.openstreetmap.josm.data.osm.Tag; 29 import org.openstreetmap.josm.data.osm.Way;30 27 import org.openstreetmap.josm.data.preferences.CollectionProperty; 31 28 import org.openstreetmap.josm.data.validation.FixableTestError; … … 34 31 import org.openstreetmap.josm.data.validation.TestError; 35 32 import org.openstreetmap.josm.gui.mappaint.Environment; 33 import org.openstreetmap.josm.gui.mappaint.MultiCascade; 36 34 import org.openstreetmap.josm.gui.mappaint.mapcss.Condition; 37 35 import org.openstreetmap.josm.gui.mappaint.mapcss.Expression; … … 68 66 69 67 static class TagCheck implements Predicate<OsmPrimitive> { 70 protected final List<Selector> selector;68 protected final MapCSSRule rule; 71 69 protected final List<PrimitiveToTag> change = new ArrayList<PrimitiveToTag>(); 72 70 protected final Map<String, String> keyChange = new LinkedHashMap<String, String>(); … … 75 73 protected final Map<String, Boolean> assertions = new HashMap<String, Boolean>(); 76 74 77 TagCheck( List<Selector> selector) {78 this. selector = selector;75 TagCheck(MapCSSRule rule) { 76 this.rule = rule; 79 77 } 80 78 … … 113 111 114 112 static TagCheck ofMapCSSRule(final MapCSSRule rule) { 115 final TagCheck check = new TagCheck(rule.selectors); 113 final TagCheck check = new TagCheck(rule); 114 boolean containsSetClassExpression = false; 116 115 for (Instruction i : rule.declaration) { 117 116 if (i instanceof Instruction.AssignmentInstruction) { … … 142 141 } else if ("assertNoMatch".equals(ai.key) && val != null) { 143 142 check.assertions.put(val, false); 143 } else if (ai.val instanceof Boolean && ((Boolean) ai.val)) { 144 containsSetClassExpression = true; 144 145 } else { 145 146 throw new RuntimeException("Cannot add instruction " + ai.key + ": " + ai.val + "!"); … … 147 148 } 148 149 } 149 if (check.errors.isEmpty() ) {150 if (check.errors.isEmpty() && !containsSetClassExpression) { 150 151 throw new RuntimeException("No throwError/throwWarning/throwOther given! You should specify a validation error message for " + rule.selectors); 151 152 } else if (check.errors.size() > 1) { … … 183 184 * @param primitive the primitive to test 184 185 * @return true when the primitive contains a deprecated tag 185 */ 186 * @deprecated since it does not handle MapCSS-classes 187 */ 188 @Deprecated 186 189 boolean matchesPrimitive(OsmPrimitive primitive) { 187 190 return whichSelectorMatchesPrimitive(primitive) != null; … … 189 192 190 193 Selector whichSelectorMatchesPrimitive(OsmPrimitive primitive) { 191 final Environment env = new Environment().withPrimitive(primitive); 192 for (Selector i : selector) { 194 return whichSelectorMatchesEnvironment(new Environment().withPrimitive(primitive)); 195 } 196 197 Selector whichSelectorMatchesEnvironment(Environment env) { 198 for (Selector i : rule.selectors) { 199 env.clearSelectorMatchingInformation(); 193 200 if (i.matches(env)) { 194 201 return i; … … 318 325 */ 319 326 TestError getErrorForPrimitive(OsmPrimitive p) { 320 final Selector matchingSelector = whichSelectorMatchesPrimitive(p); 321 if (matchingSelector != null) { 327 return getErrorForPrimitive(p, whichSelectorMatchesPrimitive(p)); 328 } 329 330 TestError getErrorForPrimitive(OsmPrimitive p, Selector matchingSelector) { 331 if (matchingSelector != null && !errors.isEmpty()) { 322 332 final Command fix = fixPrimitive(p); 323 333 final String description = getDescriptionForMatchingSelector(matchingSelector); … … 331 341 } 332 342 } 343 } 344 345 static class MapCSSTagCheckerAndRule extends MapCSSTagChecker { 346 public final MapCSSRule rule; 347 348 MapCSSTagCheckerAndRule(MapCSSRule rule) { 349 this.rule = rule; 350 } 351 352 @Override 353 public boolean equals(Object obj) { 354 return super.equals(obj) 355 || (obj instanceof TagCheck && rule.equals(((TagCheck) obj).rule)) 356 || (obj instanceof MapCSSRule && rule.equals(obj)); 357 } 358 } 359 360 /** 361 * Obtains all {@link TestError}s for the {@link OsmPrimitive} {@code p}. 362 */ 363 public Collection<TestError> getErrorsForPrimitive(OsmPrimitive p) { 364 final ArrayList<TestError> r = new ArrayList<TestError>(); 365 final Environment env = new Environment(p, new MultiCascade(), Environment.DEFAULT_LAYER, null); 366 for (TagCheck check : checks) { 367 final Selector selector = check.whichSelectorMatchesEnvironment(env); 368 if (selector != null) { 369 check.rule.execute(env); 370 final TestError error = check.getErrorForPrimitive(p, selector); 371 if (error != null) { 372 error.setTester(new MapCSSTagCheckerAndRule(check.rule)); 373 r.add(error); 374 } 375 } 376 } 377 return r; 333 378 } 334 379 … … 340 385 @Override 341 386 public void check(OsmPrimitive p) { 342 for (TagCheck check : checks) { 343 final TestError error = check.getErrorForPrimitive(p); 344 if (error != null) { 345 error.setTester(this); 346 errors.add(error); 347 } 348 } 387 errors.addAll(getErrorsForPrimitive(p)); 349 388 } 350 389 -
trunk/src/org/openstreetmap/josm/gui/mappaint/Environment.java
r6175 r6601 21 21 public StyleSource source; 22 22 private Context context = Context.PRIMITIVE; 23 public static final String DEFAULT_LAYER = "default"; 23 24 24 25 /** -
trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Condition.java
r6579 r6601 291 291 @Override 292 292 public boolean applies(Environment env) { 293 return not ^ env.mc.getCascade(env.layer).containsKey(id);293 return env != null && env.mc != null && env.mc.getCascade(env.layer) != null && not ^ env.mc.getCascade(env.layer).containsKey(id); 294 294 } 295 295 -
trunk/test/unit/org/openstreetmap/TestUtils.java
r6592 r6601 60 60 assertThat(p.get("railway"), is("rail")); 61 61 } 62 63 @Test(expected = IllegalArgumentException.class) 64 public void testCreatePrimitiveFail() throws Exception { 65 TestUtils.createPrimitive("noway name=Foo"); 66 } 67 62 68 } -
trunk/test/unit/org/openstreetmap/josm/data/validation/tests/MapCSSTagCheckerTest.java
r6592 r6601 9 9 import org.openstreetmap.josm.data.osm.Node; 10 10 import org.openstreetmap.josm.data.osm.OsmPrimitive; 11 import org.openstreetmap.josm.data.osm.Relation;12 11 import org.openstreetmap.josm.data.osm.Tag; 13 import org.openstreetmap.josm.data.osm.Way;14 12 import org.openstreetmap.josm.data.validation.Severity; 15 import org.openstreetmap.josm.tools.TextTagParser; 13 import org.openstreetmap.josm.data.validation.TestError; 14 import org.openstreetmap.josm.tools.Predicate; 15 import org.openstreetmap.josm.tools.Predicates; 16 import org.openstreetmap.josm.tools.Utils; 16 17 17 18 import java.io.StringReader; … … 62 63 n2.put("natural", "wood"); 63 64 assertFalse(check.matchesPrimitive(n2)); 64 assertThat(MapCSSTagChecker.TagCheck.insertArguments(check. selector.get(0), "The key is {0.key} and the value is {0.value}"),65 assertThat(MapCSSTagChecker.TagCheck.insertArguments(check.rule.selectors.get(0), "The key is {0.key} and the value is {0.value}"), 65 66 is("The key is natural and the value is marsh")); 66 }67 68 @Test(expected = IllegalArgumentException.class)69 public void testCreatePrimitiveForAssertionFail() throws Exception {70 final OsmPrimitive p = TestUtils.createPrimitive("noway name=Foo");71 67 } 72 68 … … 80 76 for (final Map.Entry<String, Boolean> i : check.assertions.entrySet()) { 81 77 final OsmPrimitive p = TestUtils.createPrimitive(i.getKey()); 82 if (check.matchesPrimitive(p) != i.getValue()) { 78 final boolean isError = Utils.exists(c.getErrorsForPrimitive(p), new Predicate<TestError>() { 79 @Override 80 public boolean evaluate(TestError e) { 81 //noinspection EqualsBetweenInconvertibleTypes 82 return e.getTester().equals(check.rule); 83 } 84 }); 85 if (isError != i.getValue()) { 83 86 final String error = MessageFormat.format("Expecting test ''{0}'' (i.e., {1}) to {2} {3} (i.e., {4})", 84 check.getMessage(), check. selector, i.getValue() ? "match" : "not match", i.getKey(), p.getKeys());87 check.getMessage(), check.rule.selectors, i.getValue() ? "match" : "not match", i.getKey(), p.getKeys()); 85 88 System.err.println(error); 86 89 assertionErrors.add(error);
Note:
See TracChangeset
for help on using the changeset viewer.