KickJava   Java API By Example, From Geeks To Geeks.

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


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
24 /**
25  * The wrapper implementation for the Java 1.4 regular expression matching (java.util.regex).
26  * See: {@link java.util.regex}.
27  * <p>
28  * Last Modified By: $Author: barrygently $<br />
29  * Last Modified On: $Date: 2005/01/07 17:10:41 $<br />
30  * Current Revision: $Revision: 1.1 $<br />
31  */

32 public class StandardJavaRegExpWrapper extends AbstractRegExpWrapper implements RegExp
33 {
34     
35     public static final String JavaDoc SUPPORTED_VERSION = "1.4";
36
37     private final String JavaDoc compileMethName = "compile";
38     private final String JavaDoc matcherMethName = "matcher";
39     private final String JavaDoc matchesMethName = "matches";
40     private final String JavaDoc matcherClassName = "java.util.regex.Matcher";
41     private final String JavaDoc patternClassName = "java.util.regex.Pattern";
42     
43     private Method JavaDoc compileMeth = null;
44     private Method JavaDoc matcherMeth = null;
45     private Method JavaDoc matchesMeth = null;
46     
47     private Map JavaDoc patterns = new HashMap JavaDoc ();
48     
49     public StandardJavaRegExpWrapper ()
50     {
51     
52     }
53
54     public String JavaDoc getSupportedVersion ()
55     {
56
57     return StandardJavaRegExpWrapper.SUPPORTED_VERSION;
58
59     }
60
61     public boolean isAvailable ()
62     {
63
64     try
65     {
66
67         Class.forName (this.patternClassName);
68
69         return true;
70
71     } catch (Exception JavaDoc e) {
72
73         return false;
74
75     }
76
77     }
78
79     public boolean match (String JavaDoc pattern,
80               String JavaDoc val)
81                       throws QueryExecutionException
82     {
83
84     try
85     {
86
87         // See if we already have a Pattern for this pattern.
88
Object JavaDoc o = this.patterns.get (pattern);
89         
90         if (o == null)
91         {
92
93         Object JavaDoc args[] = {pattern};
94         
95         // Create a new one. Static method.
96
o = this.compileMeth.invoke (null,
97                          args);
98         
99         this.patterns.put (pattern,
100                    o);
101         
102         }
103         
104         Object JavaDoc args[] = {val};
105         
106         // Now create the matcher.
107
Object JavaDoc matcher = this.matcherMeth.invoke (o,
108                               args);
109         
110         // Now invoke the "matches" method.
111
return ((Boolean JavaDoc) this.matchesMeth.invoke (matcher,
112                                null)).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 pc = Class.forName (this.patternClassName);
134         
135         Class JavaDoc argTypes[] = {String JavaDoc.class};
136         
137         // Get the "compile" method.
138
this.compileMeth = pc.getMethod (this.compileMethName,
139                          argTypes);
140         
141         Class JavaDoc argTypes2[] = {CharSequence JavaDoc.class};
142         
143         // Get the "matcher" method.
144
this.matcherMeth = pc.getMethod (this.matcherMethName,
145                          argTypes2);
146         
147         Class JavaDoc mc = Class.forName (this.matcherClassName);
148         
149         // Get the matches method.
150
this.matchesMeth = mc.getMethod (this.matchesMethName,
151                          null);
152         
153     } catch (Exception JavaDoc e) {
154         
155         throw new QueryExecutionException ("Unable to init",
156                            e);
157
158     }
159     
160     }
161
162 }
163
Popular Tags