KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > josql > functions > regexp > OroApacheRegExpWrapper


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

15 package org.josql.functions.regexp;
16
17 import java.util.Map JavaDoc;
18 import java.util.HashMap JavaDoc;
19
20 import org.josql.QueryExecutionException;
21
22 import java.lang.reflect.Method JavaDoc;
23 import java.lang.reflect.Constructor JavaDoc;
24
25 /**
26  * The wrapper implementation for the Apache ORO implementation of regular expression matching.
27  * See: <a HREF="http://jakarta.apache.org/oro/">http://jakarta.apache.org/oro/</a> for details.
28  * <p>
29  * Last Modified By: $Author: barrygently $<br />
30  * Last Modified On: $Date: 2005/01/07 17:10:41 $<br />
31  * Current Revision: $Revision: 1.1 $<br />
32  */

33 public class OroApacheRegExpWrapper extends AbstractRegExpWrapper implements RegExp
34 {
35
36     public static final String JavaDoc SUPPORTED_VERSION = "2.0.8";
37
38     private final String JavaDoc compilerClassName = "org.apache.oro.text.regex.Perl5Compiler";
39     private final String JavaDoc matcherClassName = "org.apache.oro.text.regex.Perl5Matcher";
40     private final String JavaDoc patternClassName = "org.apache.oro.text.regex.Pattern";
41
42     private final String JavaDoc matchesMethName = "matches";
43     private final String JavaDoc compileMethName = "compile";
44
45     private Method JavaDoc compileMeth = null;
46     private Method JavaDoc matchesMeth = null;
47     
48     private Object JavaDoc compiler = null;
49     private Object JavaDoc matcher = null;
50     
51     private Map JavaDoc patterns = new HashMap JavaDoc ();
52
53     public OroApacheRegExpWrapper ()
54     {
55
56     }
57
58     public String JavaDoc getSupportedVersion ()
59     {
60
61     return OroApacheRegExpWrapper.SUPPORTED_VERSION;
62
63     }
64
65     public boolean isAvailable ()
66     {
67
68     try
69     {
70
71         Class.forName (this.patternClassName);
72
73         return true;
74
75     } catch (Exception JavaDoc e) {
76
77         return false;
78
79     }
80
81     }
82
83     public boolean match (String JavaDoc pattern,
84               String JavaDoc val)
85                           throws QueryExecutionException
86     {
87
88     try
89     {
90
91         // See if we already have the pattern.
92
Object JavaDoc o = this.patterns.get (pattern);
93         
94         if (o == null)
95         {
96
97         Object JavaDoc args[] = {pattern};
98         
99         // Create a new Pattern.
100
o = this.compileMeth.invoke (this.compiler,
101                          args);
102         
103         this.patterns.put (pattern,
104                    o);
105         
106         }
107         
108         Object JavaDoc args[] = {val, o};
109         
110         // Match them!
111
return ((Boolean JavaDoc) this.matchesMeth.invoke (this.matcher,
112                                args)).booleanValue ();
113
114     } catch (Exception JavaDoc e) {
115
116         throw new QueryExecutionException ("Unable to match value: " +
117                            val +
118                            " against pattern: " +
119                            pattern,
120                            e);
121         
122     }
123     
124     }
125     
126     public void init ()
127                       throws QueryExecutionException
128     {
129
130     try
131     {
132
133         Class JavaDoc compClass = Class.forName (this.compilerClassName);
134         
135         // Create a new Compiler instance.
136
this.compiler = compClass.newInstance ();
137         
138         // Get the compile(String) method.
139
Class JavaDoc argTypes[] = {String JavaDoc.class};
140         
141         this.compileMeth = compClass.getMethod (this.compileMethName,
142                             argTypes);
143         
144         Class JavaDoc matchClass = Class.forName (this.matcherClassName);
145         
146         // Create a new matcher.
147
this.matcher = matchClass.newInstance ();
148         
149         Class JavaDoc patternClass = Class.forName (this.patternClassName);
150         
151         Class JavaDoc argTypes2[] = {String JavaDoc.class, patternClass};
152         
153         // Get the matches(String,Pattern) method...
154
this.matchesMeth = matchClass.getMethod (this.matchesMethName,
155                              argTypes2);
156         
157     } catch (Exception JavaDoc e) {
158         
159         throw new QueryExecutionException ("Unable to init",
160                            e);
161         
162     }
163     
164     }
165     
166 }
167
Popular Tags