KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > matching > helpers > WildcardURIMatcher


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.apache.cocoon.matching.helpers;
17
18 import java.util.HashMap JavaDoc;
19
20 /**
21  * This class is an utility class that perform wilcard-patterns matching and
22  * isolation.
23  *
24  * @author <a HREF="mailto:pier@apache.org">Pierpaolo Fumagalli</a>
25  * (Apache Software Foundation)
26  * @author <a HREF="mailto:Giacomo.Pati@pwr.ch">Giacomo Pati</a>
27  * @author <a HREF="mailto:stefano@apache.org">Stefano Mazzocchi</a>
28  * @version CVS $Id: WildcardURIMatcher.java 30932 2004-07-29 17:35:38Z vgritsenko $
29  * @deprecated renamed to WildcardHelper
30  */

31 public class WildcardURIMatcher {
32
33     /** The int representing '*' in the pattern <code>int []</code>. */
34     protected static final int MATCH_FILE = -1;
35     /** The int representing '**' in the pattern <code>int []</code>. */
36     protected static final int MATCH_PATH = -2;
37     /** The int representing begin in the pattern <code>int []</code>. */
38     protected static final int MATCH_BEGIN = -4;
39     /** The int representing end in pattern <code>int []</code>. */
40     protected static final int MATCH_THEEND = -5;
41     /** The int value that terminates the pattern <code>int []</code>. */
42     protected static final int MATCH_END = -3;
43
44     /**
45      * match a pattern agains a string and isolates wildcard replacement into a
46      * <code>Stack</code>.
47      */

48     public static boolean match (HashMap JavaDoc map, String JavaDoc data,
49             int[] expr) throws NullPointerException JavaDoc {
50         if (map == null)
51             throw new NullPointerException JavaDoc ("No map provided");
52         if (data == null)
53             throw new NullPointerException JavaDoc ("No data provided");
54         if (expr == null)
55             throw new NullPointerException JavaDoc ("No pattern expression provided");
56
57
58         char buff[] = data.toCharArray();
59         // Allocate the result buffer
60
char rslt[] = new char[expr.length + buff.length];
61
62
63         // The previous and current position of the expression character
64
// (MATCH_*)
65
int charpos = 0;
66
67         // The position in the expression, input, translation and result arrays
68
int exprpos = 0;
69         int buffpos = 0;
70         int rsltpos = 0;
71         int offset = -1;
72
73         // The matching count
74
int mcount = 0;
75
76         // We want the complete data be in {0}
77
map.put(Integer.toString(mcount),data);
78
79         // First check for MATCH_BEGIN
80
boolean matchBegin = false;
81         if (expr[charpos] == MATCH_BEGIN) {
82             matchBegin = true;
83             exprpos = ++charpos;
84         }
85
86         // Search the fist expression character (except MATCH_BEGIN - already skipped)
87
while (expr[charpos] >= 0)
88             charpos++;
89
90         // The expression charater (MATCH_*)
91
int exprchr = expr[charpos];
92
93         while (true) {
94             // Check if the data in the expression array before the current
95
// expression character matches the data in the input buffer
96
if (matchBegin) {
97                 if (!matchArray(expr, exprpos, charpos, buff, buffpos))
98                     return (false);
99                 matchBegin = false;
100             } else {
101                 offset = indexOfArray (expr, exprpos, charpos, buff,
102                         buffpos);
103                 if (offset < 0)
104                     return (false);
105             }
106
107             // Check for MATCH_BEGIN
108
if (matchBegin) {
109                 if (offset != 0)
110                     return (false);
111                 matchBegin = false;
112             }
113
114             // Advance buffpos
115
buffpos += (charpos - exprpos);
116
117             // Check for END's
118
if (exprchr == MATCH_END) {
119                 if (rsltpos > 0)
120                     map.put(Integer.toString(++mcount),new String JavaDoc(rslt, 0, rsltpos));
121                 // Don't care about rest of input buffer
122
return (true);
123             } else if (exprchr == MATCH_THEEND) {
124                 if (rsltpos > 0)
125                     map.put (Integer.toString(++mcount),new String JavaDoc(rslt, 0, rsltpos));
126                 // Check that we reach buffer's end
127
return (buffpos == buff.length);
128             }
129
130             // Search the next expression character
131
exprpos = ++charpos;
132             while (expr[charpos] >= 0)
133                 charpos++;
134             int prevchr = exprchr;
135             exprchr = expr[charpos];
136
137             // We have here prevchr == * or **.
138
offset = (prevchr == MATCH_FILE) ?
139                     indexOfArray (expr, exprpos, charpos, buff, buffpos) :
140                     lastIndexOfArray (expr, exprpos, charpos, buff,
141                     buffpos);
142
143             if (offset < 0)
144                 return (false);
145
146             // Copy the data from the source buffer into the result buffer
147
// to substitute the expression character
148
if (prevchr == MATCH_PATH) {
149                 while (buffpos < offset)
150                     rslt[rsltpos++] = buff[buffpos++];
151             } else {
152                 // Matching file, don't copy '/'
153
while (buffpos < offset) {
154                     if (buff[buffpos] == '/')
155                         return (false);
156                     rslt[rsltpos++] = buff[buffpos++];
157                 }
158             }
159
160             map.put(Integer.toString(++mcount),new String JavaDoc (rslt, 0, rsltpos));
161             rsltpos = 0;
162         }
163     }
164
165     /**
166       * Get the offset of a part of an int array within a char array.
167       * <br>
168       * This method return the index in d of the first occurrence after dpos of
169       * that part of array specified by r, starting at rpos and terminating at
170       * rend.
171       *
172       * @param r The array containing the data that need to be matched in d.
173       * @param rpos The index of the first character in r to look for.
174       * @param rend The index of the last character in r to look for plus 1.
175       * @param d The array of char that should contain a part of r.
176       * @param dpos The starting offset in d for the matching.
177       * @return The offset in d of the part of r matched in d or -1 if that was
178       * not found.
179       */

180     protected static int indexOfArray (int r[], int rpos, int rend,
181             char d[], int dpos) {
182         // Check if pos and len are legal
183
if (rend < rpos)
184             throw new IllegalArgumentException JavaDoc ("rend < rpos");
185         // If we need to match a zero length string return current dpos
186
if (rend == rpos)
187             return (d.length); //?? dpos?
188
// If we need to match a 1 char length string do it simply
189
if ((rend - rpos) == 1) {
190             // Search for the specified character
191
for (int x = dpos; x < d.length; x++)
192                 if (r[rpos] == d[x])
193                     return (x);
194         }
195         // Main string matching loop. It gets executed if the characters to
196
// match are less then the characters left in the d buffer
197
while ((dpos + rend - rpos) <= d.length) {
198             // Set current startpoint in d
199
int y = dpos;
200             // Check every character in d for equity. If the string is matched
201
// return dpos
202
for (int x = rpos; x <= rend; x++) {
203                 if (x == rend)
204                     return (dpos);
205                 if (r[x] != d[y++])
206                     break;
207             }
208             // Increase dpos to search for the same string at next offset
209
dpos++;
210         }
211         // The remaining chars in d buffer were not enough or the string
212
// wasn't matched
213
return (-1);
214     }
215
216     /**
217       * Get the offset of a last occurance of an int array within a char array.
218       * <br>
219       * This method return the index in d of the last occurrence after dpos of
220       * that part of array specified by r, starting at rpos and terminating at
221       * rend.
222       *
223       * @param r The array containing the data that need to be matched in d.
224       * @param rpos The index of the first character in r to look for.
225       * @param rend The index of the last character in r to look for plus 1.
226       * @param d The array of char that should contain a part of r.
227       * @param dpos The starting offset in d for the matching.
228       * @return The offset in d of the last part of r matched in d or -1 if that was
229       * not found.
230       */

231     protected static int lastIndexOfArray (int r[], int rpos, int rend,
232             char d[], int dpos) {
233         // Check if pos and len are legal
234
if (rend < rpos)
235             throw new IllegalArgumentException JavaDoc ("rend < rpos");
236         // If we need to match a zero length string return current dpos
237
if (rend == rpos)
238             return (d.length); //?? dpos?
239

240         // If we need to match a 1 char length string do it simply
241
if ((rend - rpos) == 1) {
242             // Search for the specified character
243
for (int x = d.length - 1; x > dpos; x--)
244                 if (r[rpos] == d[x])
245                     return (x);
246         }
247
248         // Main string matching loop. It gets executed if the characters to
249
// match are less then the characters left in the d buffer
250
int l = d.length - (rend - rpos);
251         while (l >= dpos) {
252             // Set current startpoint in d
253
int y = l;
254             // Check every character in d for equity. If the string is matched
255
// return dpos
256
for (int x = rpos; x <= rend; x++) {
257                 if (x == rend)
258                     return (l);
259                 if (r[x] != d[y++])
260                     break;
261             }
262             // Decrease l to search for the same string at next offset
263
l--;
264         }
265         // The remaining chars in d buffer were not enough or the string
266
// wasn't matched
267
return (-1);
268     }
269
270     /**
271       * Matches elements of array r from rpos to rend with array d, starting from dpos.
272       * <br>
273       * This method return true if elements of array r from rpos to rend
274       * equals elements of array d starting from dpos to dpos+(rend-rpos).
275       *
276       * @param r The array containing the data that need to be matched in d.
277       * @param rpos The index of the first character in r to look for.
278       * @param d The array of char that should start from a part of r.
279       * @param dpos The starting offset in d for the matching.
280       * @return true if array d starts from portion of array r.
281       */

282     protected static boolean matchArray (int r[], int rpos, int rend,
283             char d[], int dpos) {
284         if (d.length - dpos < rend - rpos)
285             return (false);
286         for (int i = rpos; i < rend; i++)
287             if (r[i] != d[dpos++])
288                 return (false);
289         return (true);
290     }
291 }
292
Popular Tags