The Unified and Autonomous Development Platform
Support patterns match for string and path.
robin shine97795a665ef6
diff --git a/commons.util/src/main/java/com/pmease/commons/util/PathUtils.java b/commons.util/src/main/java/com/pmease/commons/util/PathUtils.javaindex c1480e8bcb..40e1ab4861 100644--- a/commons.util/src/main/java/com/pmease/commons/util/PathUtils.java+++ b/commons.util/src/main/java/com/pmease/commons/util/PathUtils.java@@ -80,18 +80,53 @@ public class PathUtils {}/**- * Tests whether or not a given path matches a given pattern.- *- * @param pattern- * the pattern to match against. Must not be null+ * Tests whether or not a given path matches a given patterns.+ * <p>+ * Patterns are separated by comma or line separator, with each pattern using syntax+ * of <a href="http://ant.apache.org/manual/dirtasks.html">Ant path pattern</a>.+ * <p>+ * If a pattern is prefixed with <code>-</code>, it is considered as a negative pattern;+ * if a pattern is prefixed with <code>+</code>, or is not prefixed with both signs,+ * it is considered as positive pattern.+ * <p>+ * When determine if a string matches the patterns, the string will be matched against+ * each pattern in order, until finding a matching negative or positive pattern. If a+ * negative pattern is matched first, the string is considered not matched for the whole+ * patterns, and if a positive pattern is matched first, the string is considered matched+ * for the whole patterns. If no any pattern is matched, the string is considered not+ * matched for the whole patterns.+ * <p>+ * Some examples:+ * <ul>+ * <li>assert(match("-**/*.java, **", "Test.java") == false);+ * <li>assert(match("-**/*.java, **", "test.c") == true);+ * </ul>+ *+ * @param patterns+ * the patterns to match against. Must not be null* @param str* the path to match, as a String. Must not be null** @return- * true if the pattern matches against the string, or false otherwise+ * true if the patterns matches against the string, or false otherwise+ **/- public static boolean match(String pathPattern, String path) {- return new AntPathMatcher().match(pathPattern, path);+ public static boolean match(String patterns, String path) {+ AntPathMatcher matcher = new AntPathMatcher();++ for (String pattern: StringUtils.splitAndTrim(patterns)) {+ if (pattern.startsWith("+"))+ pattern = pattern.substring(1);+ if (pattern.startsWith("-")) {+ pattern = pattern.substring(1);+ if (matcher.match(pattern, path))+ return false;+ } else {+ if (matcher.match(pattern, path))+ return true;+ }+ }+ return false;}}diff --git a/commons.util/src/main/java/com/pmease/commons/util/StringUtils.java b/commons.util/src/main/java/com/pmease/commons/util/StringUtils.javaindex a45f35c53d..b57e69f445 100644--- a/commons.util/src/main/java/com/pmease/commons/util/StringUtils.java+++ b/commons.util/src/main/java/com/pmease/commons/util/StringUtils.java@@ -15,21 +15,35 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {/*** Split specified string with specified separator and trim the result fields.+ ** @param str* @param separator* @return- * Modifiable collection of splitted fields. Leading and trailing white spaces will be trimmed+ * modifiable collection of split fields. Leading and trailing white spaces will be trimmed* from these fields. Element of the resulting collection will never be null or- * empty string.+ * empty string*/- public static Collection<String> splitAndTrim(String str, String separator) {- Collection<String> fields = new ArrayList<String>();+ public static List<String> splitAndTrim(String str, String separator) {+ List<String> fields = new ArrayList<String>();for (String each: StringUtils.split(str, separator)) {if (each != null && each.trim().length() != 0)fields.add(each.trim());}return fields;}++ /**+ * Split specified string with comma and line separator.+ *+ * @param str+ * string to be split+ * @return+ * modifiable collection of split fields. Leading and trailing white spaces will be trimmed+ * from these fields. Element of resulting collection will never be null or empty string+ */+ public static List<String> splitAndTrim(String str) {+ return splitAndTrim(str, ",\n");+ }/*** Parse specified string into tokens. Content surrounded with " character@@ -77,10 +91,29 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {}/**- * Tests whether or not a string matches against a pattern.- * The pattern may contain two special characters:<br>+ * Tests whether or not a string matches against specified patterns.+ * <p>+ * Patterns are separated with comma or line separator, with each pattern may contain+ * two special characters:<br>* '*' means zero or more characters<br>* '?' means one and only one character+ * <p>+ * If a pattern is prefixed with <code>-</code>, it is considered as a negative pattern;+ * if a pattern is prefixed with <code>+</code>, or is not prefixed with both signs,+ * it is considered as positive pattern.+ * <p>+ * When determine if a string matches the patterns, the string will be matched against+ * each pattern in order, until finding a matching negative or positive pattern. If a+ * negative pattern is matched first, the string is considered not matched for the whole+ * patterns, and if a positive pattern is matched first, the string is considered matched+ * for the whole patterns. If no any pattern is matched, the string is considered not+ * matched for the whole patterns.+ * <p>+ * Some examples:+ * <ul>+ * <li>assert(wildcardMatch("-Core.java, *.java", "Core.java") == false);+ * <li>assert(wildcardMatch("-Core.java, *.java", "Client.java") == true);+ * </ul>** @param pattern* the pattern to match against. Must not be null.@@ -89,8 +122,20 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {* @return* true if the string matches against the pattern, or false otherwise.*/- public static boolean wildcardMatch(String pattern, String str) {- return SelectorUtils.match(pattern, str);+ public static boolean wildcardMatch(String patterns, String str) {+ for (String pattern: splitAndTrim(patterns)) {+ if (pattern.startsWith("+"))+ pattern = pattern.substring(1);+ if (pattern.startsWith("-")) {+ pattern = pattern.substring(1);+ if (SelectorUtils.match(pattern, str))+ return false;+ } else {+ if (SelectorUtils.match(pattern, str))+ return true;+ }+ }+ return false;}/**diff --git a/commons.util/src/test/java/com/pmease/commons/util/PathUtilsTest.java b/commons.util/src/test/java/com/pmease/commons/util/PathUtilsTest.javaindex 7773632796..177c5902fb 100644--- a/commons.util/src/test/java/com/pmease/commons/util/PathUtilsTest.java+++ b/commons.util/src/test/java/com/pmease/commons/util/PathUtilsTest.java@@ -4,6 +4,7 @@ import static org.junit.Assert.assertEquals;import static org.junit.Assert.assertFalse;import static org.junit.Assert.assertNull;import static org.junit.Assert.assertTrue;+import static com.pmease.commons.util.PathUtils.*;import java.util.Collection;@@ -13,38 +14,44 @@ public class PathUtilsTest {@Testpublic void shouldParseRelative() {- assertEquals(PathUtils.parseRelative("/path1/path2", "/path1"), "/path2");- assertEquals(PathUtils.parseRelative("\\path1/path2", "/path1\\"), "/path2");- assertEquals(PathUtils.parseRelative("path1\\path2/", "path1"), "/path2");- assertEquals(PathUtils.parseRelative("/path1/path2", "/path1/path2"), "");- assertNull(PathUtils.parseRelative("/path1/path2", "/path1/path3"));- assertNull(PathUtils.parseRelative("/path1/path23", "/path1/path2"));+ assertEquals(parseRelative("/path1/path2", "/path1"), "/path2");+ assertEquals(parseRelative("\\path1/path2", "/path1\\"), "/path2");+ assertEquals(parseRelative("path1\\path2/", "path1"), "/path2");+ assertEquals(parseRelative("/path1/path2", "/path1/path2"), "");+ assertNull(parseRelative("/path1/path2", "/path1/path3"));+ assertNull(parseRelative("/path1/path23", "/path1/path2"));}@Testpublic void shouldMatchLongest() {Collection<String> basePaths = EasyList.of("/path1/path2", "/path1/path2/path3");- assertEquals(PathUtils.matchLongest(basePaths, "/path1/path2/path3/path4"), "/path1/path2/path3");- assertNull(PathUtils.matchLongest(basePaths, "/path1/path23"));- assertNull(PathUtils.matchLongest(basePaths, "/path1/path3"));+ assertEquals(matchLongest(basePaths, "/path1/path2/path3/path4"), "/path1/path2/path3");+ assertNull(matchLongest(basePaths, "/path1/path23"));+ assertNull(matchLongest(basePaths, "/path1/path3"));basePaths = EasyList.of("/path1/path2", "/path1\\");- assertEquals(PathUtils.matchLongest(basePaths, "path1\\path234"), "/path1\\");+ assertEquals(matchLongest(basePaths, "path1\\path234"), "/path1\\");basePaths = EasyList.of("/asset1/asset2", "/asset1");- assertEquals(PathUtils.matchLongest(basePaths, "/asset1/asset2/test.html"), "/asset1/asset2");+ assertEquals(matchLongest(basePaths, "/asset1/asset2/test.html"), "/asset1/asset2");}@Testpublic void shouldMatchPath() {- assertTrue(PathUtils.match("**//*.java", "com/example/Test.java"));- assertFalse(PathUtils.match("**/*.java", "com/example/test.c"));- assertFalse(PathUtils.match("com/*.java", "com/example/Test.java"));- assertTrue(PathUtils.match("com/**//*.java", "com/example/Test.java"));- assertTrue(PathUtils.match("com/*.java", "com/Test.java"));- assertTrue(PathUtils.match("src///**/*.java", "src/com/example/Test.java"));- assertFalse(PathUtils.match("/src/**/*.java", "src/com/example/Test.java"));- assertFalse(PathUtils.match("src/**////*.java", "resource/com/example/Test.java"));+ assertTrue(match("**//*.java", "com/example/Test.java"));+ assertFalse(match("**/*.java", "com/example/test.c"));+ assertFalse(match("com/*.java", "com/example/Test.java"));+ assertTrue(match("com/**//*.java", "com/example/Test.java"));+ assertTrue(match("com/*.java", "com/Test.java"));+ assertTrue(match("src///**/*.java", "src/com/example/Test.java"));+ assertFalse(match("/src/**/*.java", "src/com/example/Test.java"));+ assertFalse(match("src/**////*.java", "resource/com/example/Test.java"));++ assertTrue(match("-**/*.java, **", "test/hello.c"));+ assertFalse(match("-**/*.java, **", "test/hello.java"));+ assertFalse(match("-**/*.java, **/*.java, **/*.c", "test/hello.txt"));+ assertFalse(match("-**/*.java, **/*.java, **/*.c", "test/hello.java"));+ assertTrue(match("+**/*.java, -**/*.java, **/*.c", "test/hello.java"));}}diff --git a/commons.util/src/test/java/com/pmease/commons/util/StringUtilsTest.java b/commons.util/src/test/java/com/pmease/commons/util/StringUtilsTest.javaindex 7a20f2fa73..3e527a8b90 100644--- a/commons.util/src/test/java/com/pmease/commons/util/StringUtilsTest.java+++ b/commons.util/src/test/java/com/pmease/commons/util/StringUtilsTest.java@@ -1,6 +1,7 @@package com.pmease.commons.util;import static org.junit.Assert.*;+import static com.pmease.commons.util.StringUtils.wildcardMatch;import org.junit.Test;@@ -8,16 +9,22 @@ public class StringUtilsTest {@Testpublic void shouldMatchWildcard() {- assertTrue(StringUtils.wildcardMatch("hello?world", "hello world"));- assertFalse(StringUtils.wildcardMatch("hello?world", "hello world"));- assertTrue(StringUtils.wildcardMatch("hello*world", "hello world"));- assertTrue(StringUtils.wildcardMatch("**/*.java", "com/example/Test.java"));- assertFalse(StringUtils.wildcardMatch("**/*.java", "com/example/test.c"));- assertTrue(StringUtils.wildcardMatch("com/*.java", "com/example/Test.java"));- assertTrue(StringUtils.wildcardMatch("com/**/*.java", "com/example/Test.java"));- assertTrue(StringUtils.wildcardMatch("com/*.java", "com/Test.java"));- assertTrue(StringUtils.wildcardMatch("src/**/*.java", "src/com/example/Test.java"));- assertFalse(StringUtils.wildcardMatch("src/**/*.java", "resource/com/example/Test.java"));+ assertTrue(wildcardMatch("hello?world", "hello world"));+ assertFalse(wildcardMatch("hello?world", "hello world"));+ assertTrue(wildcardMatch("hello*world", "hello world"));+ assertTrue(wildcardMatch("**/*.java", "com/example/Test.java"));+ assertFalse(wildcardMatch("**/*.java", "com/example/test.c"));+ assertTrue(wildcardMatch("com/*.java", "com/example/Test.java"));+ assertTrue(wildcardMatch("com/**/*.java", "com/example/Test.java"));+ assertTrue(wildcardMatch("com/*.java", "com/Test.java"));+ assertTrue(wildcardMatch("src/**/*.java", "src/com/example/Test.java"));+ assertFalse(wildcardMatch("src/**/*.java", "resource/com/example/Test.java"));++ assertTrue(wildcardMatch("-*.java, *", "hello.c"));+ assertFalse(wildcardMatch("-*.java, *", "hello.java"));+ assertFalse(wildcardMatch("-*.java, *.java, *.c", "hello.txt"));+ assertFalse(wildcardMatch("-*.java, *.java, *.c", "hello.java"));+ assertTrue(wildcardMatch("+*.java, -*.java, *.c", "hello.java"));}}