Changeset 3876 in josm for trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Condition.java
- Timestamp:
- 2011-02-09T12:02:41+01:00 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Condition.java
r3860 r3876 2 2 package org.openstreetmap.josm.gui.mappaint.mapcss; 3 3 4 import static org.openstreetmap.josm.tools.Utils.equal; 5 6 import java.util.EnumSet; 7 import java.util.regex.Matcher; 8 import java.util.regex.Pattern; 9 10 import org.openstreetmap.josm.data.osm.Node; 4 11 import org.openstreetmap.josm.data.osm.Relation; 5 12 import org.openstreetmap.josm.data.osm.Way; 6 13 import org.openstreetmap.josm.gui.mappaint.Environment; 7 import org.openstreetmap.josm. tools.Utils;14 import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Op; 8 15 9 16 abstract public class Condition { … … 11 18 abstract public boolean applies(Environment e); 12 19 13 public static enum Op {EQ, NEQ} 20 public static enum Op { EQ, NEQ, GREATER_OR_EQUAL, GREATER, LESS_OR_EQUAL, LESS, 21 REGEX, ONE_OF, BEGINS_WITH, ENDS_WITH, CONTAINS } 22 23 public final static EnumSet<Op> COMPARISON_OPERATERS = 24 EnumSet.of(Op.GREATER_OR_EQUAL, Op.GREATER, Op.LESS_OR_EQUAL, Op.LESS); 14 25 15 26 public static class KeyValueCondition extends Condition { … … 18 29 public String v; 19 30 public Op op; 31 private float v_float; 20 32 21 33 public KeyValueCondition(String k, String v, Op op) { 22 23 34 this.k = k; 24 35 this.v = v; 25 36 this.op = op; 37 if (COMPARISON_OPERATERS.contains(op)) { 38 v_float = Float.parseFloat(v); 39 } 26 40 } 27 41 28 42 @Override 29 public boolean applies(Environment e) { 43 public boolean applies(Environment env) { 44 String val = env.osm.get(k); 45 if (val == null) 46 return false; 30 47 switch (op) { 31 48 case EQ: 32 return Utils.equal(e.osm.get(k), v);49 return equal(val, v); 33 50 case NEQ: 34 return !Utils.equal(e.osm.get(k), v); 51 return !equal(val, v); 52 case REGEX: 53 Pattern p = Pattern.compile(v); 54 Matcher m = p.matcher(val); 55 return m.find(); 56 case ONE_OF: 57 String[] parts = val.split(";"); 58 for (String part : parts) { 59 if (equal(v, part.trim())) 60 return true; 61 } 62 return false; 63 case BEGINS_WITH: 64 return val.startsWith(v); 65 case ENDS_WITH: 66 return val.endsWith(v); 67 case CONTAINS: 68 return val.contains(v); 69 } 70 float val_float; 71 try { 72 val_float = Float.parseFloat(val); 73 } catch (NumberFormatException e) { 74 return false; 75 } 76 switch (op) { 77 case GREATER_OR_EQUAL: 78 return val_float >= v_float; 79 case GREATER: 80 return val_float > v_float; 81 case LESS_OR_EQUAL: 82 return val_float <= v_float; 83 case LESS: 84 return val_float < v_float; 35 85 default: 36 86 throw new AssertionError(); … … 40 90 @Override 41 91 public String toString() { 42 return "[" + k + (op == Op.EQ ? "=" : "!=")+ v + "]";92 return "[" + k + "'" + op + "'" + v + "]"; 43 93 } 44 94 } … … 77 127 @Override 78 128 public boolean applies(Environment e) { 79 if ("closed" .equals(id)) {129 if (equal(id, "closed")) { 80 130 if (e.osm instanceof Way && ((Way) e.osm).isClosed()) 81 131 return true; … … 83 133 return true; 84 134 return false; 135 } else if (equal(id, "modified")) { 136 return e.osm.isModified() || e.osm.isNewOrUndeleted(); 137 } else if (equal(id, "new")) { 138 return e.osm.isNew(); 139 } else if (equal(id, "connection") && (e.osm instanceof Node)) { 140 return ((Node) e.osm).isConnectionNode(); 85 141 } 86 142 return true;
Note:
See TracChangeset
for help on using the changeset viewer.