Changeset 18757 in josm for trunk/src/org/openstreetmap/josm/data
- Timestamp:
- 2023-06-14T18:01:00+02:00 (2 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/data/validation/tests
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/validation/tests/MapCSSTagChecker.java
r18365 r18757 232 232 final Selector selector = check.whichSelectorMatchesEnvironment(env); 233 233 if (selector != null) { 234 check.rule.declaration.execute(env); 234 final Environment envWithSelector = env.withSelector(selector); 235 check.rule.declaration.execute(envWithSelector); 235 236 if (!ignoreError && !check.errors.isEmpty()) { 236 r.addAll(check.getErrorsForPrimitive(p, selector, env, new MapCSSTagCheckerAndRule(check.rule))); 237 r.addAll(check.getErrorsForPrimitive(p, selector, envWithSelector, new MapCSSTagCheckerAndRule(check.rule))); 237 238 } 238 239 } -
trunk/src/org/openstreetmap/josm/data/validation/tests/MapCSSTagCheckerAsserts.java
r18365 r18757 75 75 Command fix = check.fixPrimitive(p); 76 76 if (fix != null && fix.executeCommand() && !MapCSSTagChecker.getErrorsForPrimitive(p, true, checksToRun).isEmpty()) { 77 assertionConsumer.accept(MessageFormat.format("Autofix does not work for test ''{0}'' (i.e., {1})", 78 check.getMessage(p), check.rule.selectors)); 77 assertionConsumer.accept(MessageFormat.format("Autofix does not work for test ''{0}'' (i.e., {1}). Failing test: {2}", 78 check.getMessage(p), check.rule.selectors, i.getKey())); 79 79 } 80 80 } -
trunk/src/org/openstreetmap/josm/data/validation/tests/MapCSSTagCheckerFixCommand.java
r17620 r18757 49 49 final String s; 50 50 if (obj instanceof Expression) { 51 s = (String) ((Expression) obj).evaluate(new Environment(p)); 51 s = (String) ((Expression) obj).evaluate(new Environment(p).withSelector(matchingSelector)); 52 52 } else if (obj instanceof String) { 53 53 s = (String) obj; -
trunk/src/org/openstreetmap/josm/data/validation/tests/MapCSSTagCheckerRule.java
r18365 r18757 18 18 import java.util.function.Consumer; 19 19 import java.util.function.Predicate; 20 import java.util.regex.Matcher;21 import java.util.regex.Pattern;22 20 import java.util.stream.Collectors; 23 21 … … 27 25 import org.openstreetmap.josm.data.osm.IPrimitive; 28 26 import org.openstreetmap.josm.data.osm.OsmPrimitive; 29 import org.openstreetmap.josm.data.osm.Tag;30 27 import org.openstreetmap.josm.data.osm.Way; 31 28 import org.openstreetmap.josm.data.osm.WaySegment; … … 35 32 import org.openstreetmap.josm.gui.mappaint.Environment; 36 33 import org.openstreetmap.josm.gui.mappaint.Keyword; 34 import org.openstreetmap.josm.gui.mappaint.MultiCascade; 37 35 import org.openstreetmap.josm.gui.mappaint.mapcss.Condition; 38 import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.TagCondition;39 36 import org.openstreetmap.josm.gui.mappaint.mapcss.Expression; 40 37 import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction; 41 38 import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSRule; 42 39 import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource; 40 import org.openstreetmap.josm.gui.mappaint.mapcss.PlaceholderExpression; 43 41 import org.openstreetmap.josm.gui.mappaint.mapcss.Selector; 44 42 import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.MapCSSParser; … … 215 213 216 214 Selector whichSelectorMatchesPrimitive(OsmPrimitive primitive) { 217 return whichSelectorMatchesEnvironment(new Environment(primitive)); 215 return whichSelectorMatchesEnvironment(new Environment(primitive, new MultiCascade(), Environment.DEFAULT_LAYER, null)); 218 216 } 219 217 … … 226 224 227 225 /** 228 * Determines the {@code index}-th key/value/tag (depending on {@code type}) of the229 * {@link org.openstreetmap.josm.gui.mappaint.mapcss.Selector.GeneralSelector}.230 *231 * @param matchingSelector matching selector232 * @param index index233 * @param type selector type ("key", "value" or "tag")234 * @param p OSM primitive235 * @return argument value, can be {@code null}236 */237 static String determineArgument(Selector.GeneralSelector matchingSelector, int index, String type, OsmPrimitive p) {238 try {239 final Condition c = matchingSelector.getConditions().get(index);240 final Tag tag = c instanceof TagCondition241 ? ((TagCondition) c).asTag(p)242 : null;243 if (tag == null) {244 return null;245 } else if ("key".equals(type)) {246 return tag.getKey();247 } else if ("value".equals(type)) {248 return tag.getValue();249 } else if ("tag".equals(type)) {250 return tag.toString();251 }252 } catch (IndexOutOfBoundsException ignore) {253 Logging.debug(ignore);254 }255 return null;256 }257 258 /**259 226 * Replaces occurrences of <code>{i.key}</code>, <code>{i.value}</code>, <code>{i.tag}</code> in {@code s} by the corresponding 260 227 * key/value/tag of the {@code index}-th {@link Condition} of {@code matchingSelector}. … … 266 233 */ 267 234 static String insertArguments(Selector matchingSelector, String s, OsmPrimitive p) { 268 if (s != null && matchingSelector instanceof Selector.ChildOrParentSelector) { 269 return insertArguments(((Selector.ChildOrParentSelector) matchingSelector).right, s, p); 270 } else if (s == null || !(matchingSelector instanceof Selector.GeneralSelector)) { 271 return s; 272 } 273 final Matcher m = Pattern.compile("\\{(\\d+)\\.(key|value|tag)\\}").matcher(s); 274 final StringBuffer sb = new StringBuffer(); 275 while (m.find()) { 276 final String argument = determineArgument((Selector.GeneralSelector) matchingSelector, 277 Integer.parseInt(m.group(1)), m.group(2), p); 278 try { 279 // Perform replacement with null-safe + regex-safe handling 280 m.appendReplacement(sb, String.valueOf(argument).replace("^(", "").replace(")$", "")); 281 } catch (IndexOutOfBoundsException | IllegalArgumentException e) { 282 Logging.log(Logging.LEVEL_ERROR, tr("Unable to replace argument {0} in {1}: {2}", argument, sb, e.getMessage()), e); 283 } 284 } 285 m.appendTail(sb); 286 return sb.toString(); 235 return PlaceholderExpression.insertArguments(matchingSelector, s, p); 287 236 } 288 237 … … 329 278 return String.valueOf( 330 279 val instanceof Expression 331 ? ((Expression) val).evaluate(new Environment(p)) 280 ? ((Expression) val).evaluate(new Environment(p).withSelector(p == null ? null : whichSelectorMatchesPrimitive(p))) 332 281 : val 333 282 );
Note:
See TracChangeset
for help on using the changeset viewer.