Ignore:
Timestamp:
2013-04-11T20:29:44+02:00 (11 years ago)
Author:
Don-vip
Message:

fix #8593 - MapCSS: robustness to invalid regex patterns

File:
1 edited

Legend:

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

    r4318 r5841  
    33
    44import java.util.List;
     5import java.util.regex.PatternSyntaxException;
    56
    67import org.openstreetmap.josm.data.osm.Node;
    78import org.openstreetmap.josm.data.osm.OsmPrimitive;
    8 import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
    99import org.openstreetmap.josm.data.osm.Relation;
    1010import org.openstreetmap.josm.data.osm.RelationMember;
     
    196196        }
    197197    }
    198 
    199     public static class LinkSelector implements Selector {
    200         protected List<Condition> conditions;
     198   
     199    /**
     200     * Super class of {@link GeneralSelector} and {@link LinkSelector}
     201     * @since 5841
     202     */
     203    public static abstract class AbstractSelector implements Selector {
     204
     205        protected final List<Condition> conds;
     206       
     207        protected AbstractSelector(List<Condition> conditions) {
     208            if (conditions == null || conditions.isEmpty()) {
     209                this.conds = null;
     210            } else {
     211                this.conds = conditions;
     212            }
     213        }
     214
     215        /**
     216         * Determines if all conditions match the given environment.
     217         * @param env The environment to check
     218         * @return {@code true} if all conditions apply, false otherwise.
     219         */
     220        public final boolean matchesConditions(Environment env) {
     221            if (conds == null) return true;
     222            for (Condition c : conds) {
     223                try {
     224                    if (!c.applies(env)) return false;
     225                } catch (PatternSyntaxException e) {
     226                    System.err.println("PatternSyntaxException while applying condition" + c +": "+e.getMessage());
     227                    return false;
     228                }
     229            }
     230            return true;
     231        }
     232    }
     233
     234    public static class LinkSelector extends AbstractSelector {
    201235
    202236        public LinkSelector(List<Condition> conditions) {
    203             this.conditions = conditions;
     237            super(conditions);
    204238        }
    205239
     
    207241        public boolean matches(Environment env) {
    208242            Utils.ensure(env.isLinkContext(), "Requires LINK context in environment, got ''{0}''", env.getContext());
    209             for (Condition c: conditions) {
    210                 if (!c.applies(env)) return false;
    211             }
    212             return true;
     243            return matchesConditions(env);
    213244        }
    214245
     
    225256        @Override
    226257        public String toString() {
    227             return "LinkSelector{" + "conditions=" + conditions + '}';
     258            return "LinkSelector{" + "conditions=" + conds + '}';
    228259        }
    229260    }
    230261
    231     public static class GeneralSelector implements Selector {
     262    public static class GeneralSelector extends AbstractSelector {
    232263        private String base;
    233264        public Range range;
    234         private List<Condition> conds;
    235265        private String subpart;
    236266
    237267        public GeneralSelector(String base, Pair<Integer, Integer> zoom, List<Condition> conds, String subpart) {
     268            super(conds);
    238269            this.base = base;
    239270            if (zoom != null) {
     
    246277            if (range == null) {
    247278                range = new Range();
    248             }
    249             if (conds == null || conds.isEmpty()) {
    250                 this.conds = null;
    251             } else {
    252                 this.conds = conds;
    253279            }
    254280            this.subpart = subpart;
     
    281307        }
    282308
    283         public boolean matchesConditions(Environment e){
    284             if (conds == null) return true;
    285             for (Condition c : conds) {
    286                 if (!c.applies(e))
    287                     return false;
    288             }
    289             return true;
    290         }
    291 
    292309        @Override
    293310        public boolean matches(Environment e) {
Note: See TracChangeset for help on using the changeset viewer.