Changeset 8654 in josm
- Timestamp:
- 2015-08-09T23:22:36+02:00 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Condition.java
r8612 r8654 16 16 import org.openstreetmap.josm.data.osm.Node; 17 17 import org.openstreetmap.josm.data.osm.OsmPrimitive; 18 import org.openstreetmap.josm.data.osm.OsmUtils; 18 19 import org.openstreetmap.josm.data.osm.Relation; 19 20 import org.openstreetmap.josm.data.osm.Tag; … … 83 84 } 84 85 86 /** 87 * This is the operation that {@link KeyValueCondition} uses to match. 88 */ 85 89 public static enum Op { 86 EQ, NEQ, GREATER_OR_EQUAL, GREATER, LESS_OR_EQUAL, LESS, 87 REGEX, NREGEX, ONE_OF, BEGINS_WITH, ENDS_WITH, CONTAINS; 90 /** The value equals the given reference. */ 91 EQ, 92 /** The value does not equal the reference. */ 93 NEQ, 94 /** The value is greater than or equal to the given reference value (as float). */ 95 GREATER_OR_EQUAL, 96 /** The value is greater than the given reference value (as float). */ 97 GREATER, 98 /** The value is less than or equal to the given reference value (as float). */ 99 LESS_OR_EQUAL, 100 /** The value is less than the given reference value (as float). */ 101 LESS, 102 /** The reference is treated as regular expression and the value needs to match it. */ 103 REGEX, 104 /** The reference is treated as regular expression and the value needs to not match it. */ 105 NREGEX, 106 /** The reference is treated as a list separated by ';'. Spaces around the ; are ignored. 107 * The value needs to be equal one of the list elements. */ 108 ONE_OF, 109 /** The value needs to begin with the reference string. */ 110 BEGINS_WITH, 111 /** The value needs to end with the reference string. */ 112 ENDS_WITH, 113 /** The value needs to contain the reference string. */ 114 CONTAINS; 88 115 89 116 public static final Set<Op> NEGATED_OPS = EnumSet.of(NEQ, NREGEX); 90 117 118 /** 119 * Evaluates a value against a reference string. 120 * @param testString The value. May be <code>null</code> 121 * @param prototypeString The reference string- 122 * @return <code>true</code> if and only if this operation matches for the given value/reference pair. 123 */ 91 124 public boolean eval(String testString, String prototypeString) { 92 125 if (testString == null && !NEGATED_OPS.contains(this)) … … 150 183 151 184 /** 152 * Most common case of a KeyValueCondition .185 * Most common case of a KeyValueCondition, this is the basic key=value case. 153 186 * 154 187 * Extra class for performance reasons. 155 188 */ 156 189 public static class SimpleKeyValueCondition extends Condition { 190 /** 191 * The key to search for. 192 */ 157 193 public final String k; 194 /** 195 * The value to search for. 196 */ 158 197 public final String v; 159 198 199 /** 200 * Create a new SimpleKeyValueCondition. 201 * @param k The key 202 * @param v The value. 203 */ 160 204 public SimpleKeyValueCondition(String k, String v) { 161 205 this.k = k; … … 184 228 */ 185 229 public static class KeyValueCondition extends Condition { 186 230 /** 231 * The key to search for. 232 */ 187 233 public final String k; 234 /** 235 * The value to search for. 236 */ 188 237 public final String v; 238 /** 239 * The key/value match operation. 240 */ 189 241 public final Op op; 242 /** 243 * If this flag is set, {@link #v} is treated as a key and the value is the value set for that key. 244 */ 190 245 public boolean considerValAsKey; 191 246 … … 282 337 } 283 338 339 /** 340 * This defines how {@link KeyCondition} matches a given key. 341 */ 284 342 public static enum KeyMatchType { 285 EQ, TRUE, FALSE, REGEX 343 /** 344 * The key needs to be equal to the given label. 345 */ 346 EQ, 347 /** 348 * The key needs to have a true value (yes, ...) 349 * @see OsmUtils#isTrue(String) 350 */ 351 TRUE, 352 /** 353 * The key needs to have a false value (no, ...) 354 * @see OsmUtils#isFalse(String) 355 */ 356 FALSE, 357 /** 358 * The key needs to match the given regular expression. 359 */ 360 REGEX 286 361 } 287 362 … … 307 382 public static class KeyCondition extends Condition { 308 383 384 /** 385 * The key name. 386 */ 309 387 public final String label; 388 /** 389 * If we should negate the result of the match. 390 */ 310 391 public final boolean negateResult; 392 /** 393 * Describes how to match the label against the key. 394 * @see KeyMatchType 395 */ 311 396 public final KeyMatchType matchType; 312 public Predicate<String> containsPattern; 313 397 /** 398 * A predicate used to match a the regexp against the key. Only used if the match type is regexp. 399 */ 400 public final Predicate<String> containsPattern; 401 402 /** 403 * Creates a new KeyCondition 404 * @param label The key name (or regexp) to use. 405 * @param negateResult If we should negate the result., 406 * @param matchType The match type. 407 */ 314 408 public KeyCondition(String label, boolean negateResult, KeyMatchType matchType) { 315 409 this.label = label; 316 410 this.negateResult = negateResult; 317 this.matchType = matchType ;411 this.matchType = matchType == null ? KeyMatchType.EQ : matchType; 318 412 this.containsPattern = KeyMatchType.REGEX.equals(matchType) 319 413 ? Predicates.stringContainsPattern(Pattern.compile(label)) … … 325 419 switch(e.getContext()) { 326 420 case PRIMITIVE: 327 if (KeyMatchType.TRUE.equals(matchType)) 421 switch (matchType) { 422 case TRUE: 328 423 return e.osm.isKeyTrue(label) ^ negateResult; 329 else if (KeyMatchType.FALSE.equals(matchType))424 case FALSE: 330 425 return e.osm.isKeyFalse(label) ^ negateResult; 331 else if (KeyMatchType.REGEX.equals(matchType)) {426 case REGEX: 332 427 return Utils.exists(e.osm.keySet(), containsPattern) ^ negateResult; 333 } else {428 default: 334 429 return e.osm.hasKey(label) ^ negateResult; 335 430 } … … 341 436 } 342 437 438 /** 439 * Get the matched key and the corresponding value. 440 * <p> 441 * WARNING: This ignores {@link #negateResult}. 442 * <p> 443 * WARNING: For regexp, the regular expression is returned instead of a key if the match failed. 444 * @param p The primitive to get the value from. 445 * @return The tag. 446 */ 343 447 public Tag asTag(OsmPrimitive p) { 344 448 String key = label;
Note:
See TracChangeset
for help on using the changeset viewer.