giacomo 00/08/31 05:20:18
Modified: src/org/apache/cocoon/matching Tag: xml-cocoon2
WildcardURIMatcherFactory.java
src/org/apache/cocoon/matching/helpers Tag: xml-cocoon2
WildcardURIMatcher.java
Log:
Extenden WildcarURIMatcher to allways match the beginning and the end of a
pattern to the URL given. This patch was contributed by Vadim Gritsenko.
Revision Changes Path
No revision
No revision
1.1.2.10 +14 -7
xml-cocoon/src/org/apache/cocoon/matching/Attic/WildcardURIMatcherFactory.java
Index: WildcardURIMatcherFactory.java
===================================================================
RCS file:
/home/cvs/xml-cocoon/src/org/apache/cocoon/matching/Attic/WildcardURIMatcherFactory.java,v
retrieving revision 1.1.2.9
retrieving revision 1.1.2.10
diff -u -r1.1.2.9 -r1.1.2.10
--- WildcardURIMatcherFactory.java 2000/08/21 17:36:55 1.1.2.9
+++ WildcardURIMatcherFactory.java 2000/08/31 12:20:16 1.1.2.10
@@ -18,17 +18,21 @@
*
* @author <a href="mailto:[EMAIL PROTECTED]">Giacomo Pati</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Stefano Mazzocchi</a>
- * @version CVS $Revision: 1.1.2.9 $ $Date: 2000/08/21 17:36:55 $
+ * @version CVS $Revision: 1.1.2.10 $ $Date: 2000/08/31 12:20:16 $
*/
public class WildcardURIMatcherFactory implements MatcherFactory {
/** The int representing '*' in the pattern <code>int []</code>. */
- protected static final int MATCH_FILE = -1;
+ protected static final int MATCH_FILE = -1;
/** The int representing '**' in the pattern <code>int []</code>. */
- protected static final int MATCH_PATH = -2;
+ protected static final int MATCH_PATH = -2;
+ /** 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 = -3;
/** The <code>int []</code> identifying the pattern to match. */
protected int[] sourcePattern = null;
@@ -119,12 +123,15 @@
throws NullPointerException {
// Prepare the arrays
- int expr[] = new int[data.length() + 1];
+ int expr[] = new int[data.length() + 2];
char buff[] = data.toCharArray();
// Prepare variables for the translation loop
int y = 0;
boolean slash = false;
+
+ // Must start from beginning
+ expr[y++] = MATCH_BEGIN;
if (buff[0]=='\\') {
slash = true;
@@ -160,8 +167,8 @@
}
}
- // Declare the end of the array and return it
- expr[y] = MATCH_END;
+ // Must match end at the end
+ expr[y] = MATCH_THEEND;
return expr;
}
}
No revision
No revision
1.1.2.5 +186 -59
xml-cocoon/src/org/apache/cocoon/matching/helpers/Attic/WildcardURIMatcher.java
Index: WildcardURIMatcher.java
===================================================================
RCS file:
/home/cvs/xml-cocoon/src/org/apache/cocoon/matching/helpers/Attic/WildcardURIMatcher.java,v
retrieving revision 1.1.2.4
retrieving revision 1.1.2.5
diff -u -r1.1.2.4 -r1.1.2.5
--- WildcardURIMatcher.java 2000/08/21 17:37:13 1.1.2.4
+++ WildcardURIMatcher.java 2000/08/31 12:20:17 1.1.2.5
@@ -13,12 +13,12 @@
/**
* This class is an utility class that perform wilcard-patterns matching and
* isolation.
- *
+ *
* @author <a href="mailto:[EMAIL PROTECTED]">Pierpaolo Fumagalli</a>
* (Apache Software Foundation, Exoffice Technologies)
* @author <a href="mailto:[EMAIL PROTECTED]">Giacomo Pati</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Stefano Mazzocchi</a>
- * @version CVS $Revision: 1.1.2.4 $ $Date: 2000/08/21 17:37:13 $
+ * @version CVS $Revision: 1.1.2.5 $ $Date: 2000/08/31 12:20:17 $
*/
public class WildcardURIMatcher {
@@ -26,108 +26,158 @@
protected static final int MATCH_FILE = -1;
/** The int representing '**' in the pattern <code>int []</code>. */
protected static final int MATCH_PATH = -2;
+ /** 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;
/**
- * match a pattern agains a string and isolates wildcard replacement
into a
+ * match a pattern agains a string and isolates wildcard replacement
into a
* <code>Stack</code>.
*/
- public static boolean match (List list, String data, int[] expr)
- throws NullPointerException {
- if (list == null) throw new NullPointerException ("No list
provided");
- if (data == null) throw new NullPointerException ("No data
provided");
- if (expr == null) throw new NullPointerException ("No pattern
expression provided");
-
+ public static boolean match (List list, String data,
+ int[] expr) throws NullPointerException {
+ if (list == null)
+ throw new NullPointerException ("No list provided");
+ if (data == null)
+ throw new NullPointerException ("No data provided");
+ if (expr == null)
+ throw new NullPointerException ("No pattern expression
provided");
+
+
char buff[] = data.toCharArray();
// Allocate the result buffer
char rslt[] = new char[expr.length + buff.length];
-
+
+
// The previous and current position of the expression character
// (MATCH_*)
int charpos = 0;
- // Search the fist expression character
- while (expr[charpos] >= 0) charpos++;
- // The expression charater (MATCH_*)
- int exprchr = expr[charpos];
// The position in the expression, input, translation and result
arrays
int exprpos = 0;
int buffpos = 0;
- int trnspos = 0;
int rsltpos = 0;
int offset = -1;
-
+
+
+ // First check for MATCH_BEGIN
+ boolean matchBegin = false;
+ if (expr[charpos] == MATCH_BEGIN) {
+ matchBegin = true;
+ exprpos = ++charpos;
+ }
+
+ // Search the fist expression character (except MATCH_BEGIN -
already skipped)
+ while (expr[charpos] >= 0)
+ charpos++;
+
+ // The expression charater (MATCH_*)
+ int exprchr = expr[charpos];
+
while (true) {
// Check if the data in the expression array before the current
// expression character matches the data in the input buffer
- offset = matchArray (expr, exprpos, charpos, buff, buffpos);
- if (offset < 0) return(false);
+ if (matchBegin) {
+ if (!matchArray(expr, exprpos, charpos, buff, buffpos))
+ return (false);
+ matchBegin = false;
+ } else {
+ offset = indexOfArray (expr, exprpos, charpos, buff,
+ buffpos);
+ if (offset < 0)
+ return (false);
+ }
- // Copy the data from the translation buffer into the result
buffer
- // up to the next expression character (arriving to the current
in the
- // expression buffer)
- // while (trns[trnspos] >= 0) rslt[rsltpos++] =
(char)trns[trnspos++];
- // trnspos++;
-
+ // Check for MATCH_BEGIN
+ 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)
list.add (new String(rslt, 0, rsltpos));
+ // Don't care about rest of input buffer
return (true);
+ } else if (exprchr == MATCH_THEEND) {
+ if (rsltpos > 0)
+ list.add (new String(rslt, 0, rsltpos));
+ // Check that we reach buffer's end
+ return (buffpos == buff.length);
}
-
+
// Search the next expression character
- buffpos += (charpos-exprpos);
exprpos = ++charpos;
- while (expr[charpos] >= 0) charpos++;
+ while (expr[charpos] >= 0)
+ charpos++;
int prevchr = exprchr;
exprchr = expr[charpos];
-
- offset = matchArray (expr, exprpos, charpos, buff, buffpos);
- if (offset < 0) return (false);
-
+
+ // We have here prevchr == * or **.
+ offset = (prevchr == MATCH_FILE) ?
+ indexOfArray (expr, exprpos, charpos, buff, buffpos) :
+ lastIndexOfArray (expr, exprpos, charpos, buff,
+ buffpos);
+
+ if (offset < 0)
+ return (false);
+
// Copy the data from the source buffer into the result buffer
// to substitute the expression character
if (prevchr == MATCH_PATH) {
- while (buffpos < offset) rslt[rsltpos++] = buff[buffpos++];
+ 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++];
}
-
- list.add (new String (rslt, 0, rsltpos));
- rsltpos = 0;
}
+
+ list.add (new String (rslt, 0, rsltpos));
+ rsltpos = 0;
}
}
/**
- * Get the offset of a part of an int array within a char array.
- * <br>
- * This method return the index in d of the first occurrence after dpos
of
- * that part of array specified by r, starting at rpos and terminating at
- * rend.
- *
- * @param r The array containing the data that need to be matched in d.
- * @param rpos The index of the first character in r to look for.
- * @param rend The index of the last character in r to look for plus 1.
- * @param d The array of char that should contain a part of r.
- * @param dpos The starting offset in d for the matching.
- * @return The offset in d of the part of r matched in d or -1 if that
was
- * not found.
- */
- protected static int matchArray (int r[], int rpos, int rend, char d[],
int dpos) {
+ * Get the offset of a part of an int array within a char array.
+ * <br>
+ * This method return the index in d of the first occurrence after dpos
of
+ * that part of array specified by r, starting at rpos and terminating
at
+ * rend.
+ *
+ * @param r The array containing the data that need to be matched in d.
+ * @param rpos The index of the first character in r to look for.
+ * @param rend The index of the last character in r to look for plus 1.
+ * @param d The array of char that should contain a part of r.
+ * @param dpos The starting offset in d for the matching.
+ * @return The offset in d of the part of r matched in d or -1 if that
was
+ * not found.
+ */
+ protected static int indexOfArray (int r[], int rpos, int rend,
+ char d[], int dpos) {
// Check if pos and len are legal
- if (rend < rpos) throw new IllegalArgumentException ("rend < rpos");
+ if (rend < rpos)
+ throw new IllegalArgumentException ("rend < rpos");
// If we need to match a zero length string return current dpos
- if (rend == rpos) return (d.length); //?? dpos?
+ if (rend == rpos)
+ return (d.length); //?? dpos?
// If we need to match a 1 char length string do it simply
if ((rend - rpos) == 1) {
// Search for the specified character
- for (int x = dpos; x < d.length; x++)
- if (r[rpos] == d[x]) return (x);
+ for (int x = dpos; x < d.length; x++)
+ if (r[rpos] == d[x])
+ return (x);
}
// Main string matching loop. It gets executed if the characters to
// match are less then the characters left in the d buffer
@@ -137,15 +187,92 @@
// Check every character in d for equity. If the string is
matched
// return dpos
for (int x = rpos; x <= rend; x++) {
- if (x == rend) return (dpos);
- if (r[x] == d[y]) y++;
- else break;
+ if (x == rend)
+ return (dpos);
+ if (r[x] != d[y++])
+ break;
}
// Increase dpos to search for the same string at next offset
dpos++;
+ }
+ // The remaining chars in d buffer were not enough or the string
+ // wasn't matched
+ return (-1);
+ }
+
+ /**
+ * Get the offset of a last occurance of an int array within a char
array.
+ * <br>
+ * This method return the index in d of the last occurrence after dpos
of
+ * that part of array specified by r, starting at rpos and terminating
at
+ * rend.
+ *
+ * @param r The array containing the data that need to be matched in d.
+ * @param rpos The index of the first character in r to look for.
+ * @param rend The index of the last character in r to look for plus 1.
+ * @param d The array of char that should contain a part of r.
+ * @param dpos The starting offset in d for the matching.
+ * @return The offset in d of the last part of r matched in d or -1 if
that was
+ * not found.
+ */
+ protected static int lastIndexOfArray (int r[], int rpos, int rend,
+ char d[], int dpos) {
+ // Check if pos and len are legal
+ if (rend < rpos)
+ throw new IllegalArgumentException ("rend < rpos");
+ // If we need to match a zero length string return current dpos
+ if (rend == rpos)
+ return (d.length); //?? dpos?
+
+ // If we need to match a 1 char length string do it simply
+ if ((rend - rpos) == 1) {
+ // Search for the specified character
+ for (int x = d.length - 1; x > dpos; x--)
+ if (r[rpos] == d[x])
+ return (x);
}
- // The remaining chars in d buffer were not enough or the string
+
+ // Main string matching loop. It gets executed if the characters to
+ // match are less then the characters left in the d buffer
+ int l = d.length - (rend - rpos);
+ while (l >= dpos) {
+ // Set current startpoint in d
+ int y = l;
+ // Check every character in d for equity. If the string is
matched
+ // return dpos
+ for (int x = rpos; x <= rend; x++) {
+ if (x == rend)
+ return (l);
+ if (r[x] != d[y++])
+ break;
+ }
+ // Decrease l to search for the same string at next offset
+ l--;
+ }
+ // The remaining chars in d buffer were not enough or the string
// wasn't matched
return (-1);
+ }
+
+ /**
+ * Matches elements of array r from rpos to rend with array d, starting
from dpos.
+ * <br>
+ * This method return true if elements of array r from rpos to rend
+ * equals elements of array d starting from dpos to dpos+(rend-rpos).
+ *
+ * @param r The array containing the data that need to be matched in d.
+ * @param rpos The index of the first character in r to look for.
+ * @param d The array of char that should start from a part of r.
+ * @param dpos The starting offset in d for the matching.
+ * @return true if array d starts from portion of array r.
+ */
+ protected static boolean matchArray (int r[], int rpos, int rend,
+ char d[], int dpos) {
+ if (d.length - dpos < rend - rpos)
+ return (false);
+ for (int i = rpos; i < rend; i++)
+ if (r[i] != d[dpos++])
+ return (false);
+ return (true);
}
}