KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > walend > somnifugi > sql92 > RegularExpression


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
9  * See the License for the specific language governing
10  * permissions and limitations under the License.
11  *
12  * When distributing Covered Code, include this CDDL
13  * HEADER in each file and include the License file at
14  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
15  *
16  * If applicable add the following below this CDDL HEADER,
17  * with the fields enclosed by brackets "[]" replaced with
18  * your own identifying information: Portions Copyright
19  * [year] [name of copyright owner]
20  */

21
22 /*
23  * @(#)RegularExpression.java 1.2 05/02/06
24  *
25  * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package net.walend.somnifugi.sql92;
29
30 /**
31  * A simple RegularExpression handler to handle the JMS Selector "LIKE"
32  * operation.
33  *
34  */

35 public class RegularExpression
36 {
37     
38     String JavaDoc expression = null;
39     Character JavaDoc escape = null;
40     
41     public RegularExpression(String JavaDoc expression, String JavaDoc escape)
42     {
43         this.expression = expression;
44         
45         if (escape != null)
46         {
47             this.escape = new Character JavaDoc(escape.charAt(0));
48         }
49     }
50     
51     public boolean equals(Object JavaDoc o)
52     {
53         if (this == o) return true;
54         
55         if (!(o instanceof RegularExpression))
56         {
57             return false;
58         }
59         RegularExpression obj = (RegularExpression)o;
60         return (expression.equals(obj.expression) &&
61                 escape.equals(obj.escape));
62     }
63     
64     public String JavaDoc toString()
65     {
66         return ("{re=" + expression + ", esc=" + escape + "}");
67     }
68     
69     public String JavaDoc getExpression()
70     {
71         return expression;
72     }
73     
74     public Character JavaDoc getEscape()
75     {
76         return escape;
77     }
78     
79     public int hashCode()
80     {
81         return expression.hashCode();
82     }
83     
84     public boolean match(String JavaDoc string)
85     {
86         return match(expression, 0, string, 0);
87     }
88     
89     private boolean match(String JavaDoc re, int reStart, String JavaDoc value, int valStart)
90     {
91         
92         int reLen = re.length();
93         int vLen = value.length();
94         
95         int i = reStart;
96         int j = valStart;
97         
98         char esc = 0;
99         if (escape != null)
100         {
101             esc = escape.charValue();
102         }
103         char c;
104         
105         boolean escaped = false;
106         
107         do
108         {
109             
110             c = re.charAt(i);
111             
112             // Detect escape character
113
if (escape != null && c == esc)
114             {
115                 escaped = true;
116                 i++;
117                 continue;
118             }
119             
120             switch (c)
121             {
122                 
123                 // Match any single character
124
case '_':
125                     if (escaped)
126                     {
127                         escaped = false;
128                         // Just a normal character
129
if (c == value.charAt(j))
130                         {
131                             // Two characters match. Move past them
132
i++;
133                             j++;
134                         }
135                         else
136                         {
137                             // No match
138
return false;
139                         }
140                     }
141                     else
142                     {
143                         // Anything matches. Move on
144
i++;
145                         j++;
146                     }
147                     break;
148                     
149                 case '%':
150                     if (escaped)
151                     {
152                         escaped = false;
153                         // Just a normal character
154
if (c == value.charAt(j))
155                         {
156                             // Two characters match. Move past them
157
i++;
158                             j++;
159                         }
160                         else
161                         {
162                             // No match
163
return false;
164                         }
165                     }
166                     else
167                     {
168                         // Wildcard
169
// Skip %
170
i++;
171                         if (i == reLen)
172                         {
173                             // % was at end of re. By definition we mach the rest
174
// of the string.
175
return true;
176                         }
177                         do
178                         {
179                             // Match substring against re starting after %
180
if (match(re, i, value, j))
181                             {
182                                 return true;
183                             }
184                             // No match starting here. Skip character in string
185
// and try again.
186
j++;
187                         } while (j < vLen);
188                         // Ran out of string with no match.
189
return false;
190                     }
191                     break;
192                     
193                 default:
194                     if (c == value.charAt(j))
195                     {
196                         // Two characters match. Move past them
197
i++;
198                         j++;
199                         escaped = false;
200                     }
201                     else
202                     {
203                         // No match
204
return false;
205                     }
206                     break;
207             }
208         } while (j < vLen && i < reLen);
209         
210         // Skip any trailing % since they match 0 or more
211
while (i < reLen && re.charAt(i) == '%')
212         {
213             i++;
214         }
215         
216         if (j == vLen && i == reLen)
217         {
218             return true;
219         }
220         else
221         {
222             return false;
223         }
224         
225     }
226     
227 }
228
Popular Tags