Changeset 15315 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2019-08-23T22:53:24+02:00 (5 years ago)
Author:
Don-vip
Message:

fix #18057 - add tag_regex mapcss method to get keys by regex for comparison functions (patch by taylor.smock)

File:
1 edited

Legend:

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

    r15279 r15315  
    1010import java.util.List;
    1111import java.util.Locale;
     12import java.util.Map.Entry;
    1213import java.util.TreeSet;
    1314import java.util.regex.Matcher;
    1415import java.util.regex.Pattern;
     16import java.util.stream.Collectors;
    1517import java.util.zip.CRC32;
    1618
     
    387389
    388390    /**
     391     * Get keys that follow a regex
     392     * @param env the environment
     393     * @param key_regex the pattern that the key must match
     394     * @return the values for the keys that match the pattern
     395     * @see Functions#tag_regex(Environment, String, String)
     396     * @since 15315
     397     */
     398    public static List<String> tag_regex(final Environment env, String key_regex) { // NO_UCD (unused code)
     399        return tag_regex(env, key_regex, "");
     400    }
     401
     402    /**
     403     * Get keys that follow a regex
     404     * @param env the environment
     405     * @param key_regex the pattern that the key must match
     406     * @return the values for the keys that match the pattern
     407     * @param flags a string that may contain "i" (case insensitive), "m" (multiline) and "s" ("dot all")
     408     * @see Pattern#CASE_INSENSITIVE
     409     * @see Pattern#DOTALL
     410     * @see Pattern#MULTILINE
     411     * @since 15315
     412     */
     413    public static List<String> tag_regex(final Environment env, String key_regex, String flags) { // NO_UCD (unused code)
     414        int f = parse_regex_flags(flags);
     415        Pattern compiled = Pattern.compile(key_regex, f);
     416        return env.osm.getKeys().entrySet().stream()
     417                .filter(object -> compiled.matcher(object.getKey()).find())
     418                .map(Entry::getValue).collect(Collectors.toList());
     419    }
     420
     421    /**
     422     * Parse flags for regex usage. Shouldn't be used in mapcss
     423     * @param flags a string that may contain "i" (case insensitive), "m" (multiline) and "s" ("dot all")
     424     * @see Pattern#CASE_INSENSITIVE
     425     * @see Pattern#DOTALL
     426     * @see Pattern#MULTILINE
     427     * @return An int that can be used by a {@link Pattern} object
     428     */
     429    private static final int parse_regex_flags(String flags) {
     430        int f = 0;
     431        if (flags.contains("i")) {
     432            f |= Pattern.CASE_INSENSITIVE;
     433        }
     434        if (flags.contains("s")) {
     435            f |= Pattern.DOTALL;
     436        }
     437        if (flags.contains("m")) {
     438            f |= Pattern.MULTILINE;
     439        }
     440        return f;
     441    }
     442
     443    /**
    389444     * Gets the first non-null value of the key {@code key} from the object's parent(s).
    390445     * @param env the environment
     
    725780     */
    726781    public static boolean regexp_test(String pattern, String target, String flags) { // NO_UCD (unused code)
    727         int f = 0;
    728         if (flags.contains("i")) {
    729             f |= Pattern.CASE_INSENSITIVE;
    730         }
    731         if (flags.contains("s")) {
    732             f |= Pattern.DOTALL;
    733         }
    734         if (flags.contains("m")) {
    735             f |= Pattern.MULTILINE;
    736         }
     782        int f = parse_regex_flags(flags);
    737783        return Pattern.compile(pattern, f).matcher(target).matches();
    738784    }
     
    752798     */
    753799    public static List<String> regexp_match(String pattern, String target, String flags) { // NO_UCD (unused code)
    754         int f = 0;
    755         if (flags.contains("i")) {
    756             f |= Pattern.CASE_INSENSITIVE;
    757         }
    758         if (flags.contains("s")) {
    759             f |= Pattern.DOTALL;
    760         }
    761         if (flags.contains("m")) {
    762             f |= Pattern.MULTILINE;
    763         }
     800        int f = parse_regex_flags(flags);
    764801        return Utils.getMatches(Pattern.compile(pattern, f).matcher(target));
    765802    }
Note: See TracChangeset for help on using the changeset viewer.