KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > soto > util > matcher > UriPatternHelper


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.sapia.soto.util.matcher;
17
18 import java.util.Map JavaDoc;
19
20 /**
21  *
22  * NOTE: THE SOURCE FROM THIS CLASS HAS BEEN COPIED FROM APACHE COCOON'S
23  * org.apache.cocoon.matching.helpers.WildcardHelper CLASS. THIS ALLOWS NOT
24  * HAVING DEPENCIES ON COCOON AT COMPILE-TIME/RUNTIME, IF COCOON IS NOT USED.
25  * <p>
26  * This class is an utility class that perform wilcard-patterns matching and
27  * isolation.
28  *
29  * @author <a HREF="mailto:pier@apache.org">Pierpaolo Fumagalli</a>
30  * (Apache Software Foundation)
31  * @author <a HREF="mailto:Giacomo.Pati@pwr.ch">Giacomo Pati</a>
32  * @author <a HREF="mailto:stefano@apache.org">Stefano Mazzocchi</a>
33  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
34   */

35 public class UriPatternHelper {
36     
37     /** The int representing '*' in the pattern <code>int []</code>. */
38     protected static final int MATCH_FILE = -1;
39     /** The int representing '**' in the pattern <code>int []</code>. */
40     protected static final int MATCH_PATH = -2;
41     /** The int representing begin in the pattern <code>int []</code>. */
42     protected static final int MATCH_BEGIN = -4;
43     /** The int representing end in pattern <code>int []</code>. */
44     protected static final int MATCH_THEEND = -5;
45     /** The int value that terminates the pattern <code>int []</code>. */
46     protected static final int MATCH_END = -3;
47
48
49     /**
50      * Translate the given <code>String</code> into a <code>int []</code>
51      * representing the pattern matchable by this class.
52      * <br>
53      * This function translates a <code>String</code> into an int array
54      * converting the special '*' and '\' characters.
55      * <br>
56      * Here is how the conversion algorithm works:
57      * <ul>
58      * <li>The '*' character is converted to MATCH_FILE, meaning that zero
59      * or more characters (excluding the path separator '/') are to
60      * be matched.</li>
61      * <li>The '**' sequence is converted to MATCH_PATH, meaning that zero
62      * or more characters (including the path separator '/') are to
63      * be matched.</li>
64      * <li>The '\' character is used as an escape sequence ('\*' is
65      * translated in '*', not in MATCH_FILE). If an exact '\' character
66      * is to be matched the source string must contain a '\\'.
67      * sequence.</li>
68      * </ul>
69      * When more than two '*' characters, not separated by another character,
70      * are found their value is considered as '**' (MATCH_PATH).
71      * <br>
72      * The array is always terminated by a special value (MATCH_END).
73      * <br>
74      * All MATCH* values are less than zero, while normal characters are equal
75      * or greater.
76      *
77      * @param data The string to translate.
78      * @return The encoded string as an int array, terminated by the MATCH_END
79      * value (don't consider the array length).
80      * @exception NullPointerException If data is null.
81      */

82     static int[] compilePattern(String JavaDoc data)
83     throws NullPointerException JavaDoc {
84
85             // Prepare the arrays
86
int expr[] = new int[data.length() + 2];
87             char buff[] = data.toCharArray();
88
89             // Prepare variables for the translation loop
90
int y = 0;
91             boolean slash = false;
92
93             // Must start from beginning
94
expr[y++] = MATCH_BEGIN;
95
96             if (buff.length > 0) {
97                     if (buff[0]=='\\') {
98                             slash = true;
99                     } else if (buff[0] == '*') {
100                             expr[y++] = MATCH_FILE;
101                     } else {
102                             expr[y++] = buff[0];
103                     }
104
105                     // Main translation loop
106
for (int x = 1; x < buff.length; x++) {
107                             // If the previous char was '\' simply copy this char.
108
if (slash) {
109                                     expr[y++] = buff[x];
110                                     slash = false;
111                             // If the previous char was not '\' we have to do a bunch of checks
112
} else {
113                                     // If this char is '\' declare that and continue
114
if (buff[x] == '\\') {
115                                             slash = true;
116                                     // If this char is '*' check the previous one
117
} else if (buff[x] == '*') {
118                                             // If the previous character als was '*' match a path
119
if (expr[y-1] <= MATCH_FILE) {
120                                                     expr[y-1] = MATCH_PATH;
121                                             } else {
122                                                     expr[y++] = MATCH_FILE;
123                                             }
124                                     } else {
125                                             expr[y++]=buff[x];
126                                     }
127                             }
128                     }
129             }
130
131             // Must match end at the end
132
expr[y] = MATCH_THEEND;
133             return expr;
134     }
135
136     /**
137      * match a pattern agains a string and isolates wildcard replacement into a
138      * <code>Stack</code>.
139      */

140     static boolean match (Map JavaDoc map, String JavaDoc data,
141                     int[] expr) throws NullPointerException JavaDoc {
142             if (map == null)
143                     throw new NullPointerException JavaDoc ("No map provided");
144             if (data == null)
145                     throw new NullPointerException JavaDoc ("No data provided");
146             if (expr == null)
147                     throw new NullPointerException JavaDoc ("No pattern expression provided");
148
149
150             char buff[] = data.toCharArray();
151             // Allocate the result buffer
152
char rslt[] = new char[expr.length + buff.length];
153
154
155             // The previous and current position of the expression character
156
// (MATCH_*)
157
int charpos = 0;
158
159             // The position in the expression, input, translation and result arrays
160
int exprpos = 0;
161             int buffpos = 0;
162             int rsltpos = 0;
163             int offset = -1;
164
165             // The matching count
166
int mcount = 0;
167
168             // We want the complete data be in {0}
169
map.put(Integer.toString(mcount),data);
170
171             // First check for MATCH_BEGIN
172
boolean matchBegin = false;
173             if (expr[charpos] == MATCH_BEGIN) {
174                     matchBegin = true;
175                     exprpos = ++charpos;
176             }
177
178             // Search the fist expression character (except MATCH_BEGIN - already skipped)
179
while (expr[charpos] >= 0)
180                     charpos++;
181
182             // The expression charater (MATCH_*)
183
int exprchr = expr[charpos];
184
185             while (true) {
186                     // Check if the data in the expression array before the current
187
// expression character matches the data in the input buffer
188
if (matchBegin) {
189                             if (!matchArray(expr, exprpos, charpos, buff, buffpos))
190                                     return (false);
191                             matchBegin = false;
192                     } else {
193                             offset = indexOfArray (expr, exprpos, charpos, buff,
194                                             buffpos);
195                             if (offset < 0)
196                                     return (false);
197                     }
198
199                     // Check for MATCH_BEGIN
200
if (matchBegin) {
201                             if (offset != 0)
202                                     return (false);
203                             matchBegin = false;
204                     }
205
206                     // Advance buffpos
207
buffpos += (charpos - exprpos);
208
209                     // Check for END's
210
if (exprchr == MATCH_END) {
211                             if (rsltpos > 0)
212                                     map.put(Integer.toString(++mcount),new String JavaDoc(rslt, 0, rsltpos));
213                             // Don't care about rest of input buffer
214
return (true);
215                     } else if (exprchr == MATCH_THEEND) {
216                             if (rsltpos > 0)
217                                     map.put (Integer.toString(++mcount),new String JavaDoc(rslt, 0, rsltpos));
218                             // Check that we reach buffer's end
219
return (buffpos == buff.length);
220                     }
221
222                     // Search the next expression character
223
exprpos = ++charpos;
224                     while (expr[charpos] >= 0)
225                             charpos++;
226                     int prevchr = exprchr;
227                     exprchr = expr[charpos];
228
229                     // We have here prevchr == * or **.
230
offset = (prevchr == MATCH_FILE) ?
231                                     indexOfArray (expr, exprpos, charpos, buff, buffpos) :
232                                     lastIndexOfArray (expr, exprpos, charpos, buff,
233                                     buffpos);
234
235                     if (offset < 0)
236                             return (false);
237
238                     // Copy the data from the source buffer into the result buffer
239
// to substitute the expression character
240
if (prevchr == MATCH_PATH) {
241                             while (buffpos < offset)
242                                     rslt[rsltpos++] = buff[buffpos++];
243                     } else {
244                             // Matching file, don't copy '/'
245
while (buffpos < offset) {
246                                     if (buff[buffpos] == '/')
247                                             return (false);
248                                     rslt[rsltpos++] = buff[buffpos++];
249                             }
250                     }
251
252                     map.put(Integer.toString(++mcount),new String JavaDoc (rslt, 0, rsltpos));
253                     rsltpos = 0;
254             }
255     }
256
257     /**
258         * Get the offset of a part of an int array within a char array.
259         * <br>
260         * This method return the index in d of the first occurrence after dpos of
261         * that part of array specified by r, starting at rpos and terminating at
262         * rend.
263         *
264         * @param r The array containing the data that need to be matched in d.
265         * @param rpos The index of the first character in r to look for.
266         * @param rend The index of the last character in r to look for plus 1.
267         * @param d The array of char that should contain a part of r.
268         * @param dpos The starting offset in d for the matching.
269         * @return The offset in d of the part of r matched in d or -1 if that was
270         * not found.
271         */

272     protected static int indexOfArray (int r[], int rpos, int rend,
273                     char d[], int dpos) {
274             // Check if pos and len are legal
275
if (rend < rpos)
276                     throw new IllegalArgumentException JavaDoc ("rend < rpos");
277             // If we need to match a zero length string return current dpos
278
if (rend == rpos)
279                     return (d.length); //?? dpos?
280
// If we need to match a 1 char length string do it simply
281
if ((rend - rpos) == 1) {
282                     // Search for the specified character
283
for (int x = dpos; x < d.length; x++)
284                             if (r[rpos] == d[x])
285                                     return (x);
286             }
287             // Main string matching loop. It gets executed if the characters to
288
// match are less then the characters left in the d buffer
289
while ((dpos + rend - rpos) <= d.length) {
290                     // Set current startpoint in d
291
int y = dpos;
292                     // Check every character in d for equity. If the string is matched
293
// return dpos
294
for (int x = rpos; x <= rend; x++) {
295                             if (x == rend)
296                                     return (dpos);
297                             if (r[x] != d[y++])
298                                     break;
299                     }
300                     // Increase dpos to search for the same string at next offset
301
dpos++;
302             }
303             // The remaining chars in d buffer were not enough or the string
304
// wasn't matched
305
return (-1);
306     }
307
308     /**
309         * Get the offset of a last occurance of an int array within a char array.
310         * <br>
311         * This method return the index in d of the last occurrence after dpos of
312         * that part of array specified by r, starting at rpos and terminating at
313         * rend.
314         *
315         * @param r The array containing the data that need to be matched in d.
316         * @param rpos The index of the first character in r to look for.
317         * @param rend The index of the last character in r to look for plus 1.
318         * @param d The array of char that should contain a part of r.
319         * @param dpos The starting offset in d for the matching.
320         * @return The offset in d of the last part of r matched in d or -1 if that was
321         * not found.
322         */

323     protected static int lastIndexOfArray (int r[], int rpos, int rend,
324                     char d[], int dpos) {
325             // Check if pos and len are legal
326
if (rend < rpos)
327                     throw new IllegalArgumentException JavaDoc ("rend < rpos");
328             // If we need to match a zero length string return current dpos
329
if (rend == rpos)
330                     return (d.length); //?? dpos?
331

332             // If we need to match a 1 char length string do it simply
333
if ((rend - rpos) == 1) {
334                     // Search for the specified character
335
for (int x = d.length - 1; x > dpos; x--)
336                             if (r[rpos] == d[x])
337                                     return (x);
338             }
339
340             // Main string matching loop. It gets executed if the characters to
341
// match are less then the characters left in the d buffer
342
int l = d.length - (rend - rpos);
343             while (l >= dpos) {
344                     // Set current startpoint in d
345
int y = l;
346                     // Check every character in d for equity. If the string is matched
347
// return dpos
348
for (int x = rpos; x <= rend; x++) {
349                             if (x == rend)
350                                     return (l);
351                             if (r[x] != d[y++])
352                                     break;
353                     }
354                     // Decrease l to search for the same string at next offset
355
l--;
356             }
357             // The remaining chars in d buffer were not enough or the string
358
// wasn't matched
359
return (-1);
360     }
361
362     /**
363         * Matches elements of array r from rpos to rend with array d, starting from dpos.
364         * <br>
365         * This method return true if elements of array r from rpos to rend
366         * equals elements of array d starting from dpos to dpos+(rend-rpos).
367         *
368         * @param r The array containing the data that need to be matched in d.
369         * @param rpos The index of the first character in r to look for.
370         * @param d The array of char that should start from a part of r.
371         * @param dpos The starting offset in d for the matching.
372         * @return true if array d starts from portion of array r.
373         */

374     protected static boolean matchArray (int r[], int rpos, int rend,
375                     char d[], int dpos) {
376             if (d.length - dpos < rend - rpos)
377                     return (false);
378             for (int i = rpos; i < rend; i++)
379                     if (r[i] != d[dpos++])
380                             return (false);
381             return (true);
382     }
383
384 }
385
Popular Tags