You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by gi...@apache.org on 2006/06/28 09:50:29 UTC

svn commit: r417687 - in /cocoon/trunk/core: cocoon-bootstrap/src/main/java/org/apache/cocoon/classloader/ cocoon-core/src/main/java/org/apache/cocoon/util/ cocoon-core/src/test/java/org/apache/cocoon/util/

Author: giacomo
Date: Wed Jun 28 00:50:29 2006
New Revision: 417687

URL: http://svn.apache.org/viewvc?rev=417687&view=rev
Log:
made the '**' wildcard greedy

Modified:
    cocoon/trunk/core/cocoon-bootstrap/src/main/java/org/apache/cocoon/classloader/WildcardMatcherHelper.java
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/util/WildcardMatcherHelper.java
    cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/util/WildcardMatcherHelperTestCase.java

Modified: cocoon/trunk/core/cocoon-bootstrap/src/main/java/org/apache/cocoon/classloader/WildcardMatcherHelper.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-bootstrap/src/main/java/org/apache/cocoon/classloader/WildcardMatcherHelper.java?rev=417687&r1=417686&r2=417687&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-bootstrap/src/main/java/org/apache/cocoon/classloader/WildcardMatcherHelper.java (original)
+++ cocoon/trunk/core/cocoon-bootstrap/src/main/java/org/apache/cocoon/classloader/WildcardMatcherHelper.java Wed Jun 28 00:50:29 2006
@@ -56,10 +56,22 @@
      *     The '\*' sequence is honored as a litteral '*' character, not a wildcard
      *   </li>
      * </ul>
-     *
+     * <br>
      * When more than two '*' characters, not separated by another character, are found their value is
-     * considered as '**'.
-     *
+     * considered as '**' and immediate succeeding '*' are skipped.
+     * <br>
+     * The '**' wildcard is greedy and thus the following sample cannot match:
+     * <dl>
+     *   <dt>pattern</dt>
+     *   <dd>STAR,STAR,PATHSEP,STAR,PATHSEP,STAR,STAR (why can't I express it litterally?)</pre></dt>
+     *   <dt>string</dt>
+     *   <dd>foo/bar/baz/bug</dt>
+     * </dl>
+     * The first '**' in the pattern will suck up until the last '/' in string and thus will not match.
+     * <br>
+     * A more advance algorithm could cerainly check whether there is an other literal later in the pattern to ev. match in string
+     * but I'll leave this exercise to someone else ATM if one needs such.
+     * 
      * @param pat The pattern string.
      * @param str The string to math agains the pattern
      *
@@ -222,14 +234,15 @@
                 // Now we need to check whether the litteral substring of the pattern 
                 // is contained in the string somewhere
                 final int l = ipat - sipat;
-                final int sistr = istr;
+                int eistr = lstr - l;
 
-                while(istr < lstr && ! strncmp(apat, sipat, astr, istr, l)) istr++;
+                // beause the '**' wildcard need to be greedy we scan from the end of the string for a match
+                while(istr < eistr && ! strncmp(apat, sipat, astr, eistr, l)) eistr--;
 
-                if(istr >= lstr) return false;
+                if(istr >= eistr) return false;
 
-                add(new String(astr, sistr, istr - sistr));
-                istr += l;
+                add(new String(astr, istr, eistr - istr));
+                istr = eistr + l;
             } else {// if it is a single star pattern
                 // skip the star
                 ++ipat;

Modified: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/util/WildcardMatcherHelper.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/util/WildcardMatcherHelper.java?rev=417687&r1=417686&r2=417687&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/util/WildcardMatcherHelper.java (original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/util/WildcardMatcherHelper.java Wed Jun 28 00:50:29 2006
@@ -56,10 +56,22 @@
      *     The '\*' sequence is honored as a litteral '*' character, not a wildcard
      *   </li>
      * </ul>
-     *
+     * <br>
      * When more than two '*' characters, not separated by another character, are found their value is
-     * considered as '**'.
-     *
+     * considered as '**' and immediate succeeding '*' are skipped.
+     * <br>
+     * The '**' wildcard is greedy and thus the following sample cannot match:
+     * <dl>
+     *   <dt>pattern</dt>
+     *   <dd>STAR,STAR,PATHSEP,STAR,PATHSEP,STAR,STAR (why can't I express it litterally?)</pre></dt>
+     *   <dt>string</dt>
+     *   <dd>foo/bar/baz/bug</dt>
+     * </dl>
+     * The first '**' in the pattern will suck up until the last '/' in string and thus will not match.
+     * <br>
+     * A more advance algorithm could cerainly check whether there is an other literal later in the pattern to ev. match in string
+     * but I'll leave this exercise to someone else ATM if one needs such.
+     * 
      * @param pat The pattern string.
      * @param str The string to math agains the pattern
      *
@@ -222,14 +234,15 @@
                 // Now we need to check whether the litteral substring of the pattern 
                 // is contained in the string somewhere
                 final int l = ipat - sipat;
-                final int sistr = istr;
+                int eistr = lstr - l;
 
-                while(istr < lstr && ! strncmp(apat, sipat, astr, istr, l)) istr++;
+                // beause the '**' wildcard need to be greedy we scan from the end of the string for a match
+                while(istr < eistr && ! strncmp(apat, sipat, astr, eistr, l)) eistr--;
 
-                if(istr >= lstr) return false;
+                if(istr >= eistr) return false;
 
-                add(new String(astr, sistr, istr - sistr));
-                istr += l;
+                add(new String(astr, istr, eistr - istr));
+                istr = eistr + l;
             } else {// if it is a single star pattern
                 // skip the star
                 ++ipat;

Modified: cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/util/WildcardMatcherHelperTestCase.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/util/WildcardMatcherHelperTestCase.java?rev=417687&r1=417686&r2=417687&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/util/WildcardMatcherHelperTestCase.java (original)
+++ cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/util/WildcardMatcherHelperTestCase.java Wed Jun 28 00:50:29 2006
@@ -209,11 +209,9 @@
 
     public void test26WildcardURIMatch()
     throws Exception {
+        // greedy '**' patter swallows all until last '/' and thus doesn't match
         Map result = WildcardMatcherHelper.match("**/*/**", "foo/bar/baz/bug");
-        assertNotNull(result);
-        assertEquals("foo", result.get("1"));
-        assertEquals("bar", result.get("2"));
-        assertEquals("baz/bug", result.get("3"));
+        assertNull(result);
     }
 
     public void test27WildcardURIMatch()
@@ -236,20 +234,30 @@
 
     public void test29WildcardURIMatch()
     throws Exception {
+        // greedy '**' patter swallows all until last '/' and thus doesn't match
         Map result = WildcardMatcherHelper.match("end**end**end**end", "endXXendYendend");
-        assertNotNull(result);
-        assertEquals("XX", result.get("1"));
-        assertEquals("Y", result.get("2"));
-        assertEquals("", result.get("3"));
+        assertNull(result);
     }
 
     public void test30WildcardURIMatch()
     throws Exception {
+        // greedy '**' patter swallows all until last '/' and thus doesn't match
         Map result = WildcardMatcherHelper.match("end**end**end**end", "endendendend");
+        assertNull(result);
+    }
+
+    public void test31WildcardURIMatch()
+    throws Exception {
+        Map result = WildcardMatcherHelper.match("*/", "test/foo/bar");
+        assertNull(result);
+    }
+
+    public void test32WildcardURIMatch()
+    throws Exception {
+        Map result = WildcardMatcherHelper.match("**/*.html", "foo/bar/baz.html");
         assertNotNull(result);
-        assertEquals("", result.get("1"));
-        assertEquals("", result.get("2"));
-        assertEquals("", result.get("3"));
+        assertEquals("baz", result.get("2"));
+        assertEquals("foo/bar", result.get("1"));
     }
 
     public void testEmptyPattern() throws Exception {