[jboss-cvs] JBossAS SVN: r102291 - projects/vfs/trunk/src/main/java/org/jboss/vfs.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Mar 11 10:11:18 EST 2010


Author: david.lloyd at jboss.com
Date: 2010-03-11 10:11:18 -0500 (Thu, 11 Mar 2010)
New Revision: 102291

Modified:
   projects/vfs/trunk/src/main/java/org/jboss/vfs/VFSUtils.java
Log:
Add a glob pattern generator, which should make JAR matching easier

Modified: projects/vfs/trunk/src/main/java/org/jboss/vfs/VFSUtils.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/vfs/VFSUtils.java	2010-03-11 14:56:13 UTC (rev 102290)
+++ projects/vfs/trunk/src/main/java/org/jboss/vfs/VFSUtils.java	2010-03-11 15:11:18 UTC (rev 102291)
@@ -43,6 +43,8 @@
 import java.util.Map;
 import java.util.Set;
 import java.util.StringTokenizer;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipFile;
 import java.util.jar.Attributes;
@@ -809,4 +811,61 @@
             VFSUtils.safeClose(zip);
         }
     }
+
+    private static final Pattern GLOB_PATTERN = Pattern.compile("(\\*\\*?)|(\\?)|(\\\\.)|(/+)|([^*?]+)");
+
+    /**
+     * Get a regular expression pattern which matches any path names which match the given glob.  The glob patterns
+     * function similarly to {@code ant} file patterns.  Valid metacharacters in the glob pattern include:
+     * <ul>
+     * <li><code>"\"</code> - escape the next character (treat it literally, even if it is itself a recognized metacharacter)</li>
+     * <li><code>"?"</code> - match any non-slash character</li>
+     * <li><code>"*"</code> - match zero or more non-slash characters</li>
+     * <li><code>"**"</code> - match zero or more characters, including slashes</li>
+     * <li><code>"/"</code> - match one or more slash characters.  Consecutive {@code /} characters are collapsed down into one.</li>
+     * </ul>
+     * In addition, like {@code ant}, if the pattern ends with a {@code /}, then an implicit <code>"**"</code> will be appended.
+     * <p/>
+     * <b>See also:</b> <a href="http://ant.apache.org/manual/dirtasks.html#patterns">"Patterns" in the Ant Manual</a>
+     *
+     * @param glob the glob to match
+     *
+     * @return the pattern
+     */
+    public static Pattern getGlobPattern(final String glob) {
+        StringBuilder patternBuilder = new StringBuilder();
+        patternBuilder.append("^");
+        final Matcher m = GLOB_PATTERN.matcher(glob);
+        while (m.find()) {
+            String grp;
+            if ((grp = m.group(1)) != null) {
+                // match a * or **
+                if (grp.length() == 2) {
+                    // it's a **
+                    patternBuilder.append(".*");
+                } else {
+                    // it's a *
+                    patternBuilder.append("[^/]*");
+                }
+            } else if ((grp = m.group(2)) != null) {
+                // match a '?' glob pattern; any non-slash character
+                patternBuilder.append("[^/]");
+            } else if ((grp = m.group(3)) != null) {
+                // backslash-escaped value
+                patternBuilder.append(grp.charAt(1));
+            } else if ((grp = m.group(4)) != null) {
+                // match any number of / chars
+                patternBuilder.append("/+");
+            } else {
+                // some other string
+                patternBuilder.append(Pattern.quote(m.group()));
+            }
+        }
+        if (patternBuilder.charAt(patternBuilder.length() - 1) == '/') {
+            // ends in /, append **
+            patternBuilder.append(".*");
+        }
+        patternBuilder.append("$");
+        return Pattern.compile(patternBuilder.toString());
+    }
 }




More information about the jboss-cvs-commits mailing list