Changeset 18757 in josm for trunk/src/org
- Timestamp:
- 2023-06-14T18:01:00+02:00 (18 months ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 6 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 ); -
trunk/src/org/openstreetmap/josm/gui/mappaint/Environment.java
r18275 r18757 13 13 import org.openstreetmap.josm.data.osm.WaySegment; 14 14 import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Context; 15 import org.openstreetmap.josm.gui.mappaint.mapcss.Selector; 15 16 import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.LinkSelector; 16 17 import org.openstreetmap.josm.tools.CheckParameterUtil; … … 42 43 private Context context = Context.PRIMITIVE; 43 44 45 /** The selector that is currently being evaluated */ 46 private final Selector selector; 47 44 48 /** 45 49 * The name of the default layer. It is used if no layer is specified in the MapCSS rule … … 98 102 public Environment() { 99 103 // environment can be initialized later through with* methods 104 this.selector = null; 100 105 } 101 106 … … 107 112 */ 108 113 public Environment(IPrimitive osm) { 109 this .osm = osm;114 this(osm, null, null, null); 110 115 } 111 116 … … 123 128 this.layer = layer; 124 129 this.source = source; 130 this.selector = null; 125 131 } 126 132 … … 132 138 */ 133 139 public Environment(Environment other) { 140 this(other, other.selector); 141 } 142 143 /** 144 * Creates a clone of the environment {@code other}. 145 * 146 * @param other the other environment. Must not be null. 147 * @param selector the selector for this environment. May be null. 148 * @throws IllegalArgumentException if {@code param} is {@code null} 149 */ 150 private Environment(Environment other, Selector selector) { 134 151 CheckParameterUtil.ensureParameterNotNull(other); 135 152 this.osm = other.osm; … … 147 164 this.mpAreaCache = other.mpAreaCache; 148 165 this.toMatchForSurrounding = other.toMatchForSurrounding; 166 this.selector = selector; 149 167 } 150 168 … … 264 282 265 283 /** 284 * Creates a clone of this environment, with the selector set 285 * @param selector The selector to use 286 * @return A clone of this environment, with the specified selector 287 * @since xxx 288 */ 289 public Environment withSelector(Selector selector) { 290 return new Environment(this, selector); 291 } 292 293 /** 266 294 * Determines if the context of this environment is {@link Context#LINK}. 267 295 * @return {@code true} if the context of this environment is {@code Context#LINK}, {@code false} otherwise … … 302 330 return ((Relation) osm).getMember(index).getRole(); 303 331 return null; 332 } 333 334 /** 335 * Get the selector for this environment 336 * @return The selector. May be {@code null}. 337 * @since xxx 338 */ 339 public Selector selector() { 340 return this.selector; 304 341 } 305 342 -
trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSParser.jj
r18712 r18757 32 32 import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSRule; 33 33 import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource; 34 import org.openstreetmap.josm.gui.mappaint.mapcss.PlaceholderExpression; 34 35 import org.openstreetmap.josm.gui.mappaint.mapcss.Selector; 35 36 import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.ChildOrParentSelector; … … 1053 1054 if (lit == null) 1054 1055 return NullExpression.INSTANCE; 1056 else if (lit instanceof String && PlaceholderExpression.PATTERN_PLACEHOLDER.matcher((String) lit).find()) { 1057 return new PlaceholderExpression((String) lit); 1058 } 1055 1059 return new LiteralExpression(lit); 1056 1060 }
Note:
See TracChangeset
for help on using the changeset viewer.