You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by cz...@apache.org on 2006/06/01 12:02:30 UTC

svn commit: r410821 - in /cocoon/trunk/core: cocoon-bootstrap/src/main/java/org/apache/cocoon/classloader/ cocoon-core/src/main/java/org/apache/cocoon/matching/helpers/ cocoon-core/src/main/java/org/apache/cocoon/util/ cocoon-core/src/test/java/org/apa...

Author: cziegeler
Date: Thu Jun  1 03:02:29 2006
New Revision: 410821

URL: http://svn.apache.org/viewvc?rev=410821&view=rev
Log:
Fix bug in wildcard matcher where a uri containing a pattern twice did not always match. For example,
the uri "hallo.xml.xml" did not match "*.xml".

Added:
    cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/util/test/WildcardHelperTestCase.java   (with props)
Modified:
    cocoon/trunk/core/cocoon-bootstrap/src/main/java/org/apache/cocoon/classloader/WildcardHelper.java
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/matching/helpers/WildcardHelper.java
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/util/WildcardHelper.java

Modified: cocoon/trunk/core/cocoon-bootstrap/src/main/java/org/apache/cocoon/classloader/WildcardHelper.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-bootstrap/src/main/java/org/apache/cocoon/classloader/WildcardHelper.java?rev=410821&r1=410820&r2=410821&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-bootstrap/src/main/java/org/apache/cocoon/classloader/WildcardHelper.java (original)
+++ cocoon/trunk/core/cocoon-bootstrap/src/main/java/org/apache/cocoon/classloader/WildcardHelper.java Thu Jun  1 03:02:29 2006
@@ -15,7 +15,7 @@
  */
 package org.apache.cocoon.classloader;
 
-import java.util.HashMap;
+import java.util.Map;
 
 /**
  * This class is an utility class that perform wilcard-patterns matching and
@@ -32,9 +32,7 @@
     /** The int representing begin in the pattern <code>int []</code>. */
     protected static final int MATCH_BEGIN = -4;
     /** The int representing end in pattern <code>int []</code>. */
-    protected static final int MATCH_THEEND = -5;
-    /** The int value that terminates the pattern <code>int []</code>. */
-    protected static final int MATCH_END = -3;
+    protected static final int MATCH_END = -5;
 
     /**
      * Translate the given <code>String</code> into a <code>int []</code>
@@ -119,7 +117,7 @@
         }
 
         // Must match end at the end
-        expr[y] = MATCH_THEEND;
+        expr[y] = MATCH_END;
         return expr;
     }
 
@@ -127,7 +125,7 @@
      * match a pattern agains a string and isolates wildcard replacement into a
      * <code>Stack</code>.
      */
-    public static boolean match (HashMap map, String data, int[] expr) 
+    public static boolean match (Map map, String data, int[] expr) 
     throws NullPointerException {
         if (data == null) {
             throw new NullPointerException ("No data provided");
@@ -149,7 +147,6 @@
         // The position in the expression, input, translation and result arrays
         int exprpos = 0;
         int buffpos = 0;
-        int rsltpos = 0;
         int offset = -1;
 
         // The matching count
@@ -178,39 +175,50 @@
             // Check if the data in the expression array before the current
             // expression character matches the data in the input buffer
             if (matchBegin) {
-                if (!matchArray(expr, exprpos, charpos, buff, buffpos))
-                    return (false);
+                if (!matchArray(expr, exprpos, charpos, buff, buffpos)) {
+                    return false;
+                }
                 matchBegin = false;
             } else {
-                offset = indexOfArray (expr, exprpos, charpos, buff,
-                        buffpos);
-                if (offset < 0)
-                    return (false);
+                offset = indexOfArray (expr, exprpos, charpos, buff, buffpos);
+                if (offset < 0) {
+                    return false;
+                }
             }
 
+            // This code can never be reached, so it's commented out now!
             // Check for MATCH_BEGIN
-            if (matchBegin) {
-                if (offset != 0)
-                    return (false);
-                matchBegin = false;
-            }
+            //if (matchBegin) {
+            //    if (offset != 0)
+            //        return false;
+            //    matchBegin = false;
+            //}
 
             // Advance buffpos
             buffpos += (charpos - exprpos);
 
             // Check for END's
             if (exprchr == MATCH_END) {
-                if (rsltpos > 0 && map != null) {
-                    map.put(Integer.toString(++mcount),new String(rslt, 0, rsltpos));
-                }
-                // Don't care about rest of input buffer
-                return (true);
-            } else if (exprchr == MATCH_THEEND) {
-                if (rsltpos > 0 && map != null ) {
-                    map.put (Integer.toString(++mcount),new String(rslt, 0, rsltpos));
+                // Check that we reached buffer's end
+                // if not, we'll search again!
+                if ( buffpos != buff.length ) {
+                    int startpos = buffpos - (charpos - exprpos);
+                    while ( buffpos != buff.length ) {
+                        buffpos -= (charpos - exprpos);
+                        buffpos++;
+                        offset = indexOfArray (expr, exprpos, charpos, buff, buffpos);
+                        if (offset < 0) {
+                            return false;
+                        }
+                        buffpos = offset + (charpos - exprpos);    
+                    }
+                    // replace value in result map
+                    if (map != null ) {
+                        String oldValue = (String)map.get(Integer.toString(mcount));
+                        map.put (Integer.toString(mcount), oldValue + new String(buff, startpos, offset - startpos));
+                    }
                 }
-                // Check that we reach buffer's end
-                return (buffpos == buff.length);
+                return true;
             }
 
             // Search the next expression character
@@ -226,19 +234,23 @@
                     lastIndexOfArray (expr, exprpos, charpos, buff,
                     buffpos);
 
-            if (offset < 0)
-                return (false);
+            if (offset < 0) {
+                return false;
+            }
 
             // Copy the data from the source buffer into the result buffer
             // to substitute the expression character
+            int rsltpos = 0;
             if (prevchr == MATCH_PATH) {
-                while (buffpos < offset)
+                while (buffpos < offset) {
                     rslt[rsltpos++] = buff[buffpos++];
+                }
             } else {
                 // Matching file, don't copy '/'
                 while (buffpos < offset) {
-                    if (buff[buffpos] == '/')
-                        return (false);
+                    if (buff[buffpos] == '/') {
+                        return false;
+                    }
                     rslt[rsltpos++] = buff[buffpos++];
                 }
             }
@@ -246,7 +258,6 @@
             if ( map != null ) {
                 map.put(Integer.toString(++mcount),new String (rslt, 0, rsltpos));
             }
-            rsltpos = 0;
         }
     }
 

Modified: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/matching/helpers/WildcardHelper.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/matching/helpers/WildcardHelper.java?rev=410821&r1=410820&r2=410821&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/matching/helpers/WildcardHelper.java (original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/matching/helpers/WildcardHelper.java Thu Jun  1 03:02:29 2006
@@ -15,7 +15,7 @@
  */
 package org.apache.cocoon.matching.helpers;
 
-import java.util.HashMap;
+import java.util.Map;
 
 /**
  * This class is an utility class that perform wilcard-patterns matching and
@@ -34,9 +34,9 @@
     }
 
     /**
-     * @see org.apache.cocoon.util.WildcardHelper#match(HashMap, String, int[])
+     * @see org.apache.cocoon.util.WildcardHelper#match(Map, String, int[])
      */
-    public static boolean match(HashMap map, String data, int[] expr) 
+    public static boolean match(Map map, String data, int[] expr) 
     throws NullPointerException {
         return org.apache.cocoon.util.WildcardHelper.match(map, data, expr);
     }

Modified: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/util/WildcardHelper.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/util/WildcardHelper.java?rev=410821&r1=410820&r2=410821&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/util/WildcardHelper.java (original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/util/WildcardHelper.java Thu Jun  1 03:02:29 2006
@@ -15,7 +15,7 @@
  */
 package org.apache.cocoon.util;
 
-import java.util.HashMap;
+import java.util.Map;
 
 /**
  * This class is an utility class that perform wilcard-patterns matching and
@@ -32,9 +32,7 @@
     /** The int representing begin in the pattern <code>int []</code>. */
     protected static final int MATCH_BEGIN = -4;
     /** The int representing end in pattern <code>int []</code>. */
-    protected static final int MATCH_THEEND = -5;
-    /** The int value that terminates the pattern <code>int []</code>. */
-    protected static final int MATCH_END = -3;
+    protected static final int MATCH_END = -5;
 
     /**
      * Translate the given <code>String</code> into a <code>int []</code>
@@ -119,7 +117,7 @@
         }
 
         // Must match end at the end
-        expr[y] = MATCH_THEEND;
+        expr[y] = MATCH_END;
         return expr;
     }
 
@@ -127,7 +125,7 @@
      * match a pattern agains a string and isolates wildcard replacement into a
      * <code>Stack</code>.
      */
-    public static boolean match (HashMap map, String data, int[] expr) 
+    public static boolean match (Map map, String data, int[] expr) 
     throws NullPointerException {
         if (data == null) {
             throw new NullPointerException ("No data provided");
@@ -149,7 +147,6 @@
         // The position in the expression, input, translation and result arrays
         int exprpos = 0;
         int buffpos = 0;
-        int rsltpos = 0;
         int offset = -1;
 
         // The matching count
@@ -178,39 +175,50 @@
             // Check if the data in the expression array before the current
             // expression character matches the data in the input buffer
             if (matchBegin) {
-                if (!matchArray(expr, exprpos, charpos, buff, buffpos))
-                    return (false);
+                if (!matchArray(expr, exprpos, charpos, buff, buffpos)) {
+                    return false;
+                }
                 matchBegin = false;
             } else {
-                offset = indexOfArray (expr, exprpos, charpos, buff,
-                        buffpos);
-                if (offset < 0)
-                    return (false);
+                offset = indexOfArray (expr, exprpos, charpos, buff, buffpos);
+                if (offset < 0) {
+                    return false;
+                }
             }
 
+            // This code can never be reached, so it's commented out now!
             // Check for MATCH_BEGIN
-            if (matchBegin) {
-                if (offset != 0)
-                    return (false);
-                matchBegin = false;
-            }
+            //if (matchBegin) {
+            //    if (offset != 0)
+            //        return false;
+            //    matchBegin = false;
+            //}
 
             // Advance buffpos
             buffpos += (charpos - exprpos);
 
             // Check for END's
             if (exprchr == MATCH_END) {
-                if (rsltpos > 0 && map != null) {
-                    map.put(Integer.toString(++mcount),new String(rslt, 0, rsltpos));
-                }
-                // Don't care about rest of input buffer
-                return (true);
-            } else if (exprchr == MATCH_THEEND) {
-                if (rsltpos > 0 && map != null ) {
-                    map.put (Integer.toString(++mcount),new String(rslt, 0, rsltpos));
+                // Check that we reached buffer's end
+                // if not, we'll search again!
+                if ( buffpos != buff.length ) {
+                    int startpos = buffpos - (charpos - exprpos);
+                    while ( buffpos != buff.length ) {
+                        buffpos -= (charpos - exprpos);
+                        buffpos++;
+                        offset = indexOfArray (expr, exprpos, charpos, buff, buffpos);
+                        if (offset < 0) {
+                            return false;
+                        }
+                        buffpos = offset + (charpos - exprpos);    
+                    }
+                    // replace value in result map
+                    if (map != null ) {
+                        String oldValue = (String)map.get(Integer.toString(mcount));
+                        map.put (Integer.toString(mcount), oldValue + new String(buff, startpos, offset - startpos));
+                    }
                 }
-                // Check that we reach buffer's end
-                return (buffpos == buff.length);
+                return true;
             }
 
             // Search the next expression character
@@ -226,19 +234,23 @@
                     lastIndexOfArray (expr, exprpos, charpos, buff,
                     buffpos);
 
-            if (offset < 0)
-                return (false);
+            if (offset < 0) {
+                return false;
+            }
 
             // Copy the data from the source buffer into the result buffer
             // to substitute the expression character
+            int rsltpos = 0;
             if (prevchr == MATCH_PATH) {
-                while (buffpos < offset)
+                while (buffpos < offset) {
                     rslt[rsltpos++] = buff[buffpos++];
+                }
             } else {
                 // Matching file, don't copy '/'
                 while (buffpos < offset) {
-                    if (buff[buffpos] == '/')
-                        return (false);
+                    if (buff[buffpos] == '/') {
+                        return false;
+                    }
                     rslt[rsltpos++] = buff[buffpos++];
                 }
             }
@@ -246,7 +258,6 @@
             if ( map != null ) {
                 map.put(Integer.toString(++mcount),new String (rslt, 0, rsltpos));
             }
-            rsltpos = 0;
         }
     }
 

Added: cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/util/test/WildcardHelperTestCase.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/util/test/WildcardHelperTestCase.java?rev=410821&view=auto
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/util/test/WildcardHelperTestCase.java (added)
+++ cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/util/test/WildcardHelperTestCase.java Thu Jun  1 03:02:29 2006
@@ -0,0 +1,83 @@
+/*
+* Copyright 1999-2006 The Apache Software Foundation
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.apache.cocoon.util.test;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.cocoon.util.WildcardHelper;
+
+import junit.framework.TestCase;
+
+/**
+ * Testcase for the WildcardHelper class.
+ * @version $Id$
+ */
+public class WildcardHelperTestCase extends TestCase {
+
+    public void testWildcardURIMatch() throws Exception {
+        final Map resultMap = new HashMap();
+        final String uri = "test/foo/bla/end";
+        final String pattern = "**";
+        int[] expr = WildcardHelper.compilePattern(pattern);
+        boolean result = WildcardHelper.match(resultMap, uri, expr);
+        assertTrue("Test if url matches: " + uri + " - " + pattern, result);
+        assertEquals("Test if result matches for {0}", uri, resultMap.get("0"));
+        assertEquals("Test if result matches for {1}", uri, resultMap.get("1"));
+
+        resultMap.clear();
+        final String pattern2 = "**/bla/*";
+        int[] expr2 = WildcardHelper.compilePattern(pattern2);
+        boolean result2 = WildcardHelper.match(resultMap, uri, expr2);
+        assertTrue("Test if url matches: " + uri + " - " + pattern2, result2);
+        assertEquals("Test if result matches for {0}", uri, resultMap.get("0"));
+        assertEquals("Test if result matches for {1}", "test/foo", resultMap.get("1"));
+        assertEquals("Test if result matches for {2}", "end", resultMap.get("2"));
+    }
+
+    public void testWildcardURIMatchSimplePattern() throws Exception {
+        final Map resultMap = new HashMap();
+        final String uri = "test";
+        final String pattern = "*";
+        int[] expr = WildcardHelper.compilePattern(pattern);
+        boolean result = WildcardHelper.match(resultMap, uri, expr);
+        assertTrue("Test if url matches: " + uri + " - " + pattern, result);
+        assertEquals("Test if result matches for {0}", uri, resultMap.get("0"));
+        assertEquals("Test if result matches for {1}", uri, resultMap.get("1"));
+    }
+
+    public void testWildcardURIMatchDoublePattern() throws Exception {
+        final Map resultMap = new HashMap();
+        final String uri = "test/something.xml";
+        final String pattern = "*/*.xml";
+        int[] expr = WildcardHelper.compilePattern(pattern);
+        boolean result = WildcardHelper.match(resultMap, uri, expr);
+        assertTrue("Test if url matches: " + uri + " - " + pattern, result);
+        assertEquals("Test if result matches for {0}", uri, resultMap.get("0"));
+        assertEquals("Test if result matches for {1}", "test", resultMap.get("1"));
+        assertEquals("Test if result matches for {2}", "something", resultMap.get("2"));
+
+        final Map resultMap2 = new HashMap();
+        final String uri2 = "test/something.xmlbla.xml";
+        final String pattern2 = "*/*.xml";
+        int[] expr2 = WildcardHelper.compilePattern(pattern2);
+        boolean result2 = WildcardHelper.match(resultMap2, uri2, expr2);
+        assertTrue("Test if url matches: " + uri2 + " - " + pattern2, result2);
+        assertEquals("Test if result matches for {0}", uri2, resultMap2.get("0"));
+        assertEquals("Test if result matches for {1}", "test", resultMap2.get("1"));
+        assertEquals("Test if result matches for {2}", "something.xmlbla", resultMap2.get("2"));
+    }
+}

Propchange: cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/util/test/WildcardHelperTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/util/test/WildcardHelperTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Id