KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.josql.functions.regexp;
2
3 import java.util.Map JavaDoc;
4 import java.util.HashMap JavaDoc;
5
6 import org.josql.QueryExecutionException;
7
8 import java.lang.reflect.Method JavaDoc;
9
10 public class ApacheRegExpWrapper extends AbstractRegExpWrapper implements RegExp
11 {
12
13     public static final String JavaDoc SUPPORTED_VERSION = "1.3";
14
15     private final String JavaDoc compilerClassName = "org.apache.regexp.RECompiler";
16     private final String JavaDoc matcherClassName = "org.apache.regexp.RE";
17     private final String JavaDoc programClassName = "org.apache.regexp.REProgram";
18     private final String JavaDoc compileMethName = "compile";
19     private final String JavaDoc setProgramMethName = "setProgram";
20     private final String JavaDoc matchMethName = "match";
21     
22     private Method JavaDoc compileMeth = null;
23     private Method JavaDoc setProgramMeth = null;
24     private Method JavaDoc matchMeth = null;
25     private Class JavaDoc compilerClass = null;
26     private Class JavaDoc matcherClass = null;
27
28     private Map JavaDoc patterns = new HashMap JavaDoc ();
29
30     public ApacheRegExpWrapper ()
31     {
32
33     }
34
35     public String JavaDoc getSupportedVersion ()
36     {
37
38     return ApacheRegExpWrapper.SUPPORTED_VERSION;
39
40     }
41
42     public boolean isAvailable ()
43     {
44
45     try
46     {
47
48         Class.forName (this.compilerClassName);
49
50         return true;
51
52     } catch (Exception JavaDoc e) {
53
54         return false;
55
56     }
57
58     }
59
60     public void init ()
61                   throws QueryExecutionException
62     {
63
64     try
65     {
66
67         this.compilerClass = Class.forName (this.compilerClassName);
68         
69         Class JavaDoc argTypes[] = {String JavaDoc.class};
70         
71         this.compileMeth = this.compilerClass.getMethod (this.compileMethName,
72                                  argTypes);
73         
74         this.matcherClass = Class.forName (this.matcherClassName);
75         
76         Class JavaDoc pc = Class.forName (this.programClassName);
77         
78         Class JavaDoc argTypes2[] = {pc};
79         
80         this.setProgramMeth = this.matcherClass.getMethod (this.setProgramMethName,
81                                    argTypes2);
82         
83         this.matchMeth = this.matcherClass.getMethod (this.matchMethName,
84                               argTypes);
85         
86     } catch (Exception JavaDoc e) {
87         
88         throw new QueryExecutionException ("Unable to init",
89                            e);
90         
91     }
92     
93     }
94
95     public boolean match (String JavaDoc pattern,
96               String JavaDoc val)
97                           throws QueryExecutionException
98     {
99
100     try
101     {
102
103         // See if we already have the pattern.
104
Object JavaDoc o = this.patterns.get (pattern);
105         
106         if (o == null)
107         {
108
109         // Create a new compiler.
110
Object JavaDoc c = this.compilerClass.newInstance ();
111         
112         Object JavaDoc args[] = {pattern};
113         
114         // Compile the pattern.
115
Object JavaDoc program = this.compileMeth.invoke (c,
116                               args);
117         
118         // Create a new RE.
119
Object JavaDoc re = this.matcherClass.newInstance ();
120         
121         Object JavaDoc args2[] = {program};
122         
123         // Apply the program.
124
this.setProgramMeth.invoke (re,
125                         args2);
126         
127         this.patterns.put (pattern,
128                    re);
129         
130         o = re;
131         
132         }
133         
134         Object JavaDoc args[] = {val};
135         
136         // Now try and match the value.
137
return ((Boolean JavaDoc) this.matchMeth.invoke (o,
138                              args)).booleanValue ();
139         
140     } catch (Exception JavaDoc e) {
141         
142         throw new QueryExecutionException ("Unable to match value: " +
143                            val +
144                            " against pattern: " +
145                            pattern,
146                            e);
147         
148     }
149         
150     }
151     
152 }
153
Popular Tags