Ticket #18057: 18057.patch

File 18057.patch, 5.3 KB (added by taylor.smock, 5 years ago)

Add tag_regex method and test, reduce duplicated code for parsing flags (adds parse_regex_flags function, currently private)

  • src/org/openstreetmap/josm/gui/mappaint/mapcss/Functions.java

     
    99import java.util.Collections;
    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
    1719import org.openstreetmap.josm.data.coor.LatLon;
     
    386388    }
    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 xxx
     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 xxx
     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     * @since xxx
     429     */
     430    private static final int parse_regex_flags(String flags) {
     431        int f = 0;
     432        if (flags.contains("i")) {
     433            f |= Pattern.CASE_INSENSITIVE;
     434        }
     435        if (flags.contains("s")) {
     436            f |= Pattern.DOTALL;
     437        }
     438        if (flags.contains("m")) {
     439            f |= Pattern.MULTILINE;
     440        }
     441        return f;
     442    }
     443
     444    /**
    389445     * Gets the first non-null value of the key {@code key} from the object's parent(s).
    390446     * @param env the environment
    391447     * @param key the OSM key
     
    724799     * @since 5699
    725800     */
    726801    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         }
     802        int f = parse_regex_flags(flags);
    737803        return Pattern.compile(pattern, f).matcher(target).matches();
    738804    }
    739805
     
    751817     * @since 5701
    752818     */
    753819    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         }
     820        int f = parse_regex_flags(flags);
    764821        return Utils.getMatches(Pattern.compile(pattern, f).matcher(target));
    765822    }
    766823
  • test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSParserTest.java

     
    263263    }
    264264
    265265    @Test
     266    public void testTagRegex() throws Exception {
     267        Selector selector = getParser("way[ref][count(tag_regex(\"ref\")) > 1] {}").selector();
     268        DataSet ds = new DataSet();
     269        Way way1 = TestUtils.newWay("old_ref=A1 ref=A2", new Node(new LatLon(1, 1)), new Node(new LatLon(2, 2)));
     270        for (Node node : way1.getNodes()) {
     271            ds.addPrimitive(node);
     272        }
     273        ds.addPrimitive(way1);
     274        assertTrue(selector.matches(new Environment(way1)));
     275        way1.put("old_ref", null);
     276        assertFalse(selector.matches(new Environment(way1)));
     277        way1.put("no_match_tag", "false");
     278        assertFalse(selector.matches(new Environment(way1)));
     279        way1.put("old_ref", "A22");
     280        assertTrue(selector.matches(new Environment(way1)));
     281    }
     282
     283    @Test
    266284    public void testParentTag() throws Exception {
    267285        Selector c1 = getParser("way[foo] > node[tag(\"foo\")=parent_tag(\"foo\")] {}").child_selector();
    268286        DataSet ds = new DataSet();