KickJava   Java API By Example, From Geeks To Geeks.

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


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 GNU implementation of regular expression matching.
27  * See: <a HREF="http://www.cacas.org/java/gnu/regexp/">http://www.cacas.org/java/gnu/regexp/</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 GNURegExpWrapper extends AbstractRegExpWrapper implements RegExp
34 {
35
36     public static final String JavaDoc SUPPORTED_VERSION = "1.1.4";
37
38     private final String JavaDoc reClassName = "gnu.regexp.RE";
39     private final String JavaDoc isMatchMethName = "isMatch";
40
41     private Map JavaDoc patterns = new HashMap JavaDoc ();
42     
43     private Constructor JavaDoc cons = null;
44     private Method JavaDoc isMatchMeth = null;
45
46     public GNURegExpWrapper ()
47     {
48
49     }
50
51     public String JavaDoc getSupportedVersion ()
52     {
53
54     return GNURegExpWrapper.SUPPORTED_VERSION;
55
56     }
57
58     public boolean isAvailable ()
59     {
60
61     try
62     {
63
64         Class.forName (this.reClassName);
65
66         return true;
67
68     } catch (Exception JavaDoc e) {
69
70         return false;
71
72     }
73
74     }
75
76     public boolean match (String JavaDoc pattern,
77               String JavaDoc val)
78                       throws QueryExecutionException
79     {
80         
81     try
82     {
83
84         Object JavaDoc o = this.patterns.get (pattern);
85         
86         if (o == null)
87         {
88
89         Object JavaDoc args[] = {pattern};
90         
91         o = this.cons.newInstance (args);
92         
93         this.patterns.put (pattern,
94                    o);
95         
96         }
97         
98         Object JavaDoc args[] = {val};
99         
100         return ((Boolean JavaDoc) this.isMatchMeth.invoke (o,
101                                args)).booleanValue ();
102         
103     } catch (Exception JavaDoc e) {
104
105         throw new QueryExecutionException ("Unable to match value: " +
106                            val +
107                            " against pattern: " +
108                            pattern,
109                            e);
110
111     }
112
113     }
114
115     public void init ()
116                       throws QueryExecutionException
117     {
118         
119     try
120     {
121
122         // Bit easier this one!
123
Class JavaDoc reClass = Class.forName (reClassName);
124         
125         Class JavaDoc argTypes[] = {Object JavaDoc.class};
126         
127         this.cons = reClass.getConstructor (argTypes);
128         
129         this.isMatchMeth = reClass.getMethod (this.isMatchMethName,
130                           argTypes);
131         
132     } catch (Exception JavaDoc e) {
133         
134         throw new QueryExecutionException ("Unable to init",
135                            e);
136         
137     }
138     
139     }
140     
141 }
142
Popular Tags