Ignore:
Timestamp:
2013-02-07T18:57:30+01:00 (11 years ago)
Author:
bastiK
Message:

add a few more functions for mapcss eval expression

File:
1 edited

Legend:

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

    r5206 r5699  
    1111import java.util.Arrays;
    1212import java.util.List;
     13import java.util.regex.Matcher;
     14import java.util.regex.Pattern;
    1315
    1416import org.openstreetmap.josm.Main;
     
    107109            }
    108110
     111            public static Object get(List objects, float index) {
     112                int idx = Math.round(index);
     113                if (idx >=0 && idx < objects.size())
     114                    return objects.get(idx);
     115                return null;
     116            }
     117
     118            public List split(String sep, String toSplit) {
     119                return Arrays.asList(toSplit.split(Pattern.quote(sep)));
     120            }
     121           
    109122            public Color rgb(float r, float g, float b) {
    110123                Color c = null;
     
    229242            public int length(String s) {
    230243                return s.length();
     244            }
     245
     246            public int length(List l) {
     247                return l.size();
    231248            }
    232249
     
    263280                Color res = Main.pref.getColor(s, null);
    264281                return res != null ? res : def;
     282            }
     283
     284            public static boolean regexp_test(String pattern, String target) {
     285                return Pattern.matches(pattern, target);
     286            }
     287
     288            public static boolean regexp_test(String pattern, String target, String flags) {
     289                int f = 0;
     290                if (flags.contains("i")) {
     291                    f |= Pattern.CASE_INSENSITIVE;
     292                }
     293                if (flags.contains("s")) {
     294                    f |= Pattern.DOTALL;
     295                }
     296                if (flags.contains("m")) {
     297                    f |= Pattern.MULTILINE;
     298                }
     299                return Pattern.compile(pattern, f).matcher(target).matches();
     300            }
     301
     302            public static List regexp_match(String pattern, String target, String flags) {
     303                int f = 0;
     304                if (flags.contains("i")) {
     305                    f |= Pattern.CASE_INSENSITIVE;
     306                }
     307                if (flags.contains("s")) {
     308                    f |= Pattern.DOTALL;
     309                }
     310                if (flags.contains("m")) {
     311                    f |= Pattern.MULTILINE;
     312                }
     313                Matcher m = Pattern.compile(pattern, f).matcher(target);
     314                if (m.matches()) {
     315                    List result = new ArrayList(m.groupCount());
     316                    for (int i=1; i<=m.groupCount(); i++) {
     317                        result.add(m.group(i));
     318                    }
     319                    return result;
     320                } else
     321                    return null;
     322            }
     323
     324            public static List regexp_match(String pattern, String target) {
     325                Matcher m = Pattern.compile(pattern).matcher(target);
     326                if (m.matches()) {
     327                    List result = new ArrayList(m.groupCount());
     328                    for (int i=1; i<=m.groupCount(); i++) {
     329                        result.add(m.group(i));
     330                    }
     331                    return result;
     332                } else
     333                    return null;
     334            }
     335
     336            public long osm_id() {
     337                return env.osm.getUniqueId();
    265338            }
    266339        }
Note: See TracChangeset for help on using the changeset viewer.