KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > xpath > regex > BMPattern


1 /*
2  * Copyright 1999-2002,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
17 package org.apache.xerces.impl.xpath.regex;
18
19 import java.text.CharacterIterator JavaDoc;
20
21 /**
22  * Boyer-Moore searcher.
23  *
24  * @xerces.internal
25  *
26  * @version $Id: BMPattern.java,v 1.5 2004/10/04 22:07:40 mrglavas Exp $
27  */

28 public class BMPattern {
29     char[] pattern;
30     int[] shiftTable;
31     boolean ignoreCase;
32
33     public BMPattern(String JavaDoc pat, boolean ignoreCase) {
34         this(pat, 256, ignoreCase);
35     }
36
37     public BMPattern(String JavaDoc pat, int tableSize, boolean ignoreCase) {
38         this.pattern = pat.toCharArray();
39         this.shiftTable = new int[tableSize];
40         this.ignoreCase = ignoreCase;
41
42         int length = pattern.length;
43         for (int i = 0; i < this.shiftTable.length; i ++)
44             this.shiftTable[i] = length;
45
46         for (int i = 0; i < length; i ++) {
47             char ch = this.pattern[i];
48             int diff = length-i-1;
49             int index = ch % this.shiftTable.length;
50             if (diff < this.shiftTable[index])
51                 this.shiftTable[index] = diff;
52             if (this.ignoreCase) {
53                 ch = Character.toUpperCase(ch);
54                 index = ch % this.shiftTable.length;
55                 if (diff < this.shiftTable[index])
56                     this.shiftTable[index] = diff;
57                 ch = Character.toLowerCase(ch);
58                 index = ch % this.shiftTable.length;
59                 if (diff < this.shiftTable[index])
60                     this.shiftTable[index] = diff;
61             }
62         }
63     }
64
65     /**
66      *
67      * @return -1 if <var>iterator</var> does not contain this pattern.
68      */

69     public int matches(CharacterIterator JavaDoc iterator, int start, int limit) {
70         if (this.ignoreCase) return this.matchesIgnoreCase(iterator, start, limit);
71         int plength = this.pattern.length;
72         if (plength == 0) return start;
73         int index = start+plength;
74         while (index <= limit) {
75             int pindex = plength;
76             int nindex = index+1;
77             char ch;
78             do {
79                 if ((ch = iterator.setIndex(--index)) != this.pattern[--pindex])
80                     break;
81                 if (pindex == 0)
82                     return index;
83             } while (pindex > 0);
84             index += this.shiftTable[ch % this.shiftTable.length]+1;
85             if (index < nindex) index = nindex;
86         }
87         return -1;
88     }
89
90     /**
91      *
92      * @return -1 if <var>str</var> does not contain this pattern.
93      */

94     public int matches(String JavaDoc str, int start, int limit) {
95         if (this.ignoreCase) return this.matchesIgnoreCase(str, start, limit);
96         int plength = this.pattern.length;
97         if (plength == 0) return start;
98         int index = start+plength;
99         while (index <= limit) {
100             //System.err.println("Starts at "+index);
101
int pindex = plength;
102             int nindex = index+1;
103             char ch;
104             do {
105                 if ((ch = str.charAt(--index)) != this.pattern[--pindex])
106                     break;
107                 if (pindex == 0)
108                     return index;
109             } while (pindex > 0);
110             index += this.shiftTable[ch % this.shiftTable.length]+1;
111             if (index < nindex) index = nindex;
112         }
113         return -1;
114     }
115     /**
116      *
117      * @return -1 if <var>chars</char> does not contain this pattern.
118      */

119     public int matches(char[] chars, int start, int limit) {
120         if (this.ignoreCase) return this.matchesIgnoreCase(chars, start, limit);
121         int plength = this.pattern.length;
122         if (plength == 0) return start;
123         int index = start+plength;
124         while (index <= limit) {
125             //System.err.println("Starts at "+index);
126
int pindex = plength;
127             int nindex = index+1;
128             char ch;
129             do {
130                 if ((ch = chars[--index]) != this.pattern[--pindex])
131                     break;
132                 if (pindex == 0)
133                     return index;
134             } while (pindex > 0);
135             index += this.shiftTable[ch % this.shiftTable.length]+1;
136             if (index < nindex) index = nindex;
137         }
138         return -1;
139     }
140
141     int matchesIgnoreCase(CharacterIterator JavaDoc iterator, int start, int limit) {
142         int plength = this.pattern.length;
143         if (plength == 0) return start;
144         int index = start+plength;
145         while (index <= limit) {
146             int pindex = plength;
147             int nindex = index+1;
148             char ch;
149             do {
150                 char ch1 = ch = iterator.setIndex(--index);
151                 char ch2 = this.pattern[--pindex];
152                 if (ch1 != ch2) {
153                     ch1 = Character.toUpperCase(ch1);
154                     ch2 = Character.toUpperCase(ch2);
155                     if (ch1 != ch2 && Character.toLowerCase(ch1) != Character.toLowerCase(ch2))
156                         break;
157                 }
158                 if (pindex == 0)
159                     return index;
160             } while (pindex > 0);
161             index += this.shiftTable[ch % this.shiftTable.length]+1;
162             if (index < nindex) index = nindex;
163         }
164         return -1;
165     }
166     
167     int matchesIgnoreCase(String JavaDoc text, int start, int limit) {
168         int plength = this.pattern.length;
169         if (plength == 0) return start;
170         int index = start+plength;
171         while (index <= limit) {
172             int pindex = plength;
173             int nindex = index+1;
174             char ch;
175             do {
176                 char ch1 = ch = text.charAt(--index);
177                 char ch2 = this.pattern[--pindex];
178                 if (ch1 != ch2) {
179                     ch1 = Character.toUpperCase(ch1);
180                     ch2 = Character.toUpperCase(ch2);
181                     if (ch1 != ch2 && Character.toLowerCase(ch1) != Character.toLowerCase(ch2))
182                         break;
183                 }
184                 if (pindex == 0)
185                     return index;
186             } while (pindex > 0);
187             index += this.shiftTable[ch % this.shiftTable.length]+1;
188             if (index < nindex) index = nindex;
189         }
190         return -1;
191     }
192     int matchesIgnoreCase(char[] chars, int start, int limit) {
193         int plength = this.pattern.length;
194         if (plength == 0) return start;
195         int index = start+plength;
196         while (index <= limit) {
197             int pindex = plength;
198             int nindex = index+1;
199             char ch;
200             do {
201                 char ch1 = ch = chars[--index];
202                 char ch2 = this.pattern[--pindex];
203                 if (ch1 != ch2) {
204                     ch1 = Character.toUpperCase(ch1);
205                     ch2 = Character.toUpperCase(ch2);
206                     if (ch1 != ch2 && Character.toLowerCase(ch1) != Character.toLowerCase(ch2))
207                         break;
208                 }
209                 if (pindex == 0)
210                     return index;
211             } while (pindex > 0);
212             index += this.shiftTable[ch % this.shiftTable.length]+1;
213             if (index < nindex) index = nindex;
214         }
215         return -1;
216     }
217
218     /*
219     public static void main(String[] argv) {
220         try {
221             int[] shiftTable = new int[256];
222             initializeBoyerMoore(argv[0], shiftTable, true);
223             int o = -1;
224             CharacterIterator ite = new java.text.StringCharacterIterator(argv[1]);
225             long start = System.currentTimeMillis();
226             //for (int i = 0; i < 10000; i ++)
227                 o = searchIgnoreCasesWithBoyerMoore(ite, 0, argv[0], shiftTable);
228             start = System.currentTimeMillis()-start;
229             System.out.println("Result: "+o+", Elapsed: "+start);
230         } catch (Exception ex) {
231             ex.printStackTrace();
232         }
233     }*/

234 }
235
236
Popular Tags