Ignore:
Timestamp:
2011-02-09T12:02:41+01:00 (14 years ago)
Author:
bastiK
Message:

mapcss: support for more exotic constructs; make parser more robust: if parsing as an expression fails, read everything between colon and semicolon as string, e.g. 'icon-image: images/img.png;' where the path should be in quotes, but it isn't

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Condition.java

    r3860 r3876  
    22package org.openstreetmap.josm.gui.mappaint.mapcss;
    33
     4import static org.openstreetmap.josm.tools.Utils.equal;
     5
     6import java.util.EnumSet;
     7import java.util.regex.Matcher;
     8import java.util.regex.Pattern;
     9
     10import org.openstreetmap.josm.data.osm.Node;
    411import org.openstreetmap.josm.data.osm.Relation;
    512import org.openstreetmap.josm.data.osm.Way;
    613import org.openstreetmap.josm.gui.mappaint.Environment;
    7 import org.openstreetmap.josm.tools.Utils;
     14import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Op;
    815
    916abstract public class Condition {
     
    1118    abstract public boolean applies(Environment e);
    1219
    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);
    1425
    1526    public static class KeyValueCondition extends Condition {
     
    1829        public String v;
    1930        public Op op;
     31        private float v_float;
    2032
    2133        public KeyValueCondition(String k, String v, Op op) {
    22 
    2334            this.k = k;
    2435            this.v = v;
    2536            this.op = op;
     37            if (COMPARISON_OPERATERS.contains(op)) {
     38                v_float = Float.parseFloat(v);
     39            }
    2640        }
    2741
    2842        @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;
    3047            switch (op) {
    3148                case EQ:
    32                     return Utils.equal(e.osm.get(k), v);
     49                    return equal(val, v);
    3350                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;
    3585                default:
    3686                    throw new AssertionError();
     
    4090        @Override
    4191        public String toString() {
    42             return "[" + k + (op == Op.EQ ? "=" : "!=") + v + "]";
     92            return "[" + k + "'" + op + "'" + v + "]";
    4393        }
    4494    }
     
    77127        @Override
    78128        public boolean applies(Environment e) {
    79             if ("closed".equals(id)) {
     129            if (equal(id, "closed")) {
    80130                if (e.osm instanceof Way && ((Way) e.osm).isClosed())
    81131                    return true;
     
    83133                    return true;
    84134                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();
    85141            }
    86142            return true;
Note: See TracChangeset for help on using the changeset viewer.