KickJava   Java API By Example, From Geeks To Geeks.

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


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 public class RegExpFactory
9 {
10
11     /**
12      * The instance name to use for the Java 1.4 (java.util.regex) regular expression library.
13      * Whilst this is the default it will not be available on version of Java < 1.4.
14      */

15     public static final String JavaDoc JAVA_INST = "java";
16
17     /**
18      * The instance name to use for the ORO Apache regular expression library.
19      */

20     public static final String JavaDoc ORO_INST = "oro";
21
22     /**
23      * The instance name to use for the GNU regular expression library.
24      */

25     public static final String JavaDoc GNU_INST = "gnu";
26
27     /**
28      * The instance name to use for the Apache RegExp regular expression library.
29      */

30     public static final String JavaDoc APACHE_REGEXP_INST = "apache.regexp";
31
32     private static String JavaDoc defInst = RegExpFactory.JAVA_INST;
33
34     private static Map JavaDoc mappings = new HashMap JavaDoc ();
35     private static Map JavaDoc versions = new HashMap JavaDoc ();
36
37     static
38     {
39
40     StandardJavaRegExpWrapper j = new StandardJavaRegExpWrapper ();
41
42     if (j.isAvailable ())
43     {
44
45         RegExpFactory.mappings.put (RegExpFactory.JAVA_INST,
46                     StandardJavaRegExpWrapper.class);
47         RegExpFactory.versions.put (RegExpFactory.JAVA_INST,
48                     j.getSupportedVersion ());
49
50     }
51
52     OroApacheRegExpWrapper o = new OroApacheRegExpWrapper ();
53
54     if (o.isAvailable ())
55     {
56
57         RegExpFactory.mappings.put (RegExpFactory.ORO_INST,
58                     OroApacheRegExpWrapper.class);
59         RegExpFactory.versions.put (RegExpFactory.ORO_INST,
60                     o.getSupportedVersion ());
61
62     }
63
64     GNURegExpWrapper g = new GNURegExpWrapper ();
65
66     if (g.isAvailable ())
67     {
68
69         RegExpFactory.mappings.put (RegExpFactory.GNU_INST,
70                     GNURegExpWrapper.class);
71         RegExpFactory.versions.put (RegExpFactory.GNU_INST,
72                     g.getSupportedVersion ());
73
74     }
75
76     ApacheRegExpWrapper a = new ApacheRegExpWrapper ();
77
78     if (a.isAvailable ())
79     {
80
81         RegExpFactory.mappings.put (RegExpFactory.APACHE_REGEXP_INST,
82                     ApacheRegExpWrapper.class);
83         RegExpFactory.versions.put (RegExpFactory.APACHE_REGEXP_INST,
84                     a.getSupportedVersion ());
85
86     }
87
88     }
89
90     private RegExpFactory ()
91     {
92
93     }
94
95     public static String JavaDoc getSupportedVersion (String JavaDoc instName)
96     {
97
98     return (String JavaDoc) RegExpFactory.versions.get (instName);
99
100     }
101
102     public static String JavaDoc getDefaultInstanceName ()
103     {
104
105     return RegExpFactory.defInst;
106
107     }
108
109     public static void addInstance (String JavaDoc name,
110                     RegExp re,
111                     boolean def)
112     {
113
114     RegExpFactory.mappings.put (name,
115                     re);
116
117     if (def)
118     {
119         
120         RegExpFactory.defInst = name;
121
122     }
123
124     }
125
126     public static void setDefaultInstanceName (String JavaDoc n)
127     {
128
129     if (!RegExpFactory.mappings.containsKey (n))
130     {
131
132         throw new IllegalArgumentException JavaDoc ("No appropriate wrapper class found for instance name: " +
133                         n);
134
135     }
136
137     RegExpFactory.defInst = n;
138
139     }
140
141     public static RegExp getDefaultInstance ()
142                                          throws QueryExecutionException
143     {
144
145     return RegExpFactory.getInstance (RegExpFactory.defInst);
146
147     }
148
149     public static RegExp getInstance (String JavaDoc type)
150                                   throws QueryExecutionException
151     {
152
153     Object JavaDoc o = RegExpFactory.mappings.get (type);
154
155     if (o == null)
156     {
157
158         return null;
159
160     }
161
162     if (o instanceof RegExp)
163     {
164
165         // Already inited...
166
return (RegExp) o;
167
168     }
169
170     Class JavaDoc c = (Class JavaDoc) o;
171
172     try
173     {
174
175         Object JavaDoc oo = c.newInstance ();
176
177         RegExp re = (RegExp) c.newInstance ();
178
179         re.init ();
180
181         RegExpFactory.mappings.put (type,
182                     re);
183
184         return re;
185
186     } catch (Exception JavaDoc e) {
187
188         throw new QueryExecutionException ("Unable to init RegExp instance: " +
189                            c.getName (),
190                            e);
191
192     }
193
194     }
195
196 }
197
Popular Tags