Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/acmsl/regexpplugin/RegexpManager.java


1   /*
2                   Java Regular Expressions Plugin API
3   
4       Copyright (C) 2002  Jose San Leandro Armendáriz
5                           jsanleandro@yahoo.es
6                           chousz@yahoo.com
7   
8       This library is free software; you can redistribute it and/or
9       modify it under the terms of the GNU Lesser General Public
10      License as published by the Free Software Foundation; either
11      version 2.1 of the License, or (at your option) any later version.
12  
13      This library is distributed in the hope that it will be useful,
14      but WITHOUT ANY WARRANTY; without even the implied warranty of
15      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16      Lesser General Public License for more details.
17  
18      You should have received a copy of the GNU Lesser General Public
19      License along with this library; if not, write to the Free Software
20      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  
22      Thanks to ACM S.L. for distributing this library under the LGPL license.
23      Contact info: jsr000@terra.es
24      Postal Address: c/Playa de Lagoa, 1
25                      Urb. Valdecabañas
26                      Boadilla del monte
27                      28660 Madrid
28                      Spain
29  
30      This library uses some external APIs. So far I haven't released such
31      APIs as projects themselves, but you should be able
32      to download them from the web page where you got this source code.
33  
34   ******************************************************************************
35   *
36   * Filename: $RCSfile: RegexpManager.java,v $
37   *
38   * Author: Jose San Leandro Armendáriz
39   *
40   * Description: Generic central class that manages which regexp engine
41   *              to use.
42   *
43   * Last modified by: $Author: chous $ at $Date: 2003/06/21 21:07:25 $
44   *
45   * File version: $Revision: 1.12 $
46   *
47   * Project version: $Name:  $
48   *                  ("Name" means no concrete version has been checked out)
49   *
50   * $Id: RegexpManager.java,v 1.12 2003/06/21 21:07:25 chous Exp $
51   *
52   */
53  package org.acmsl.regexpplugin;
54  
55  /*
56   * Importing project-specific classes.
57   */
58  import org.acmsl.regexpplugin.Compiler;
59  import org.acmsl.regexpplugin.Matcher;
60  import org.acmsl.regexpplugin.RegexpEngineNotFoundException;
61  
62  /*
63   * Importing some ACM classes.
64   */
65  import org.acmsl.version.Version;
66  import org.acmsl.version.Versionable;
67  import org.acmsl.version.VersionFactory;
68  
69  /**
70   * Generic class that manages which regexp engine to use.
71   * @author <a href="mailto:jsanleandro@yahoo.es"
72             >Jose San Leandro Armendáriz</a>
73   * @version $Revision: 1.12 $
74   */
75  public final class RegexpManager
76      implements  Versionable
77  {
78      /**
79       * Engine name position in the array object.
80       */
81      protected static final int NAME = 0;
82  
83      /**
84       * Engine version position in the array object.
85       */
86      protected static final int ENGINE_VERSION = 1;
87  
88      /**
89       * Engine class package position in the array object.
90       */
91      protected static final int PACKAGE = 2;
92  
93      /**
94       * Compiler position in the array object.
95       */
96      protected static final int COMPILER = 3;
97  
98      /**
99       * Match result position in the array object.
100      */
101     protected static final int MATCHER = 4;
102 
103     /**
104      * Helper result position in the array object.
105      */
106     protected static final int HELPER = 5;
107 
108     /*
109      * Engine list Jakarta Oro.
110      */
111     /**
112      * Jakarta Oro Perl5
113      */
114     private static final String[] JAKARTA_ORO_PERL5 =
115         new String[]
116         {
117             "Jakarta Oro Perl 5",
118             "2.0.6",
119             "org.apache.oro.text.perl",
120             "org.acmsl.regexpplugin.jakartaoro.Perl5CompilerOROAdapter",
121             "org.acmsl.regexpplugin.jakartaoro.Perl5MatcherOROAdapter",
122             "org.acmsl.regexpplugin.jakartaoro.HelperOROAdapter"
123         };
124 
125     /**
126      * Jakarta Oro Awk
127      * @deprecated Awk engine is not working correctly!
128      */
129     private static final String[] JAKARTA_ORO_AWK =
130         new String[]
131         {
132             "Jakarta Oro Awk",
133             "2.0.6",
134             "org.apache.oro.text.awk",
135             "org.acmsl.regexpplugin.jakartaoro.AwkCompilerOROAdapter",
136             "org.acmsl.regexpplugin.jakartaoro.AwkMatcherOROAdapter",
137             "org.acmsl.regexpplugin.jakartaoro.HelperOROAdapter"
138         };
139 
140     /**
141      * Jakarta Regexp
142      */
143     private static final String[] JAKARTA_REGEXP =
144         new String[]
145         {
146             "Jakarta Regexp",
147             "1.2",
148             "org.apache.regexp",
149             "org.acmsl.regexpplugin.jakartaregexp.CompilerRegexpAdapter",
150             "org.acmsl.regexpplugin.jakartaregexp.MatcherRegexpAdapter",
151             "org.acmsl.regexpplugin.jakartaregexp.HelperRegexpAdapter"
152         };
153 
154     /**
155      * JDK1.4
156      */
157     private static final String[] JDK14_REGEXP =
158         new String[]
159         {
160             "JDK1.4 Regular Expresions",
161             "1.4",
162             "java.util.regex",
163             "org.acmsl.regexpplugin.jdk14regexp.CompilerJDKAdapter",
164             "org.acmsl.regexpplugin.jdk14regexp.MatcherJDKAdapter",
165             "org.acmsl.regexpplugin.jdk14regexp.HelperJDKAdapter"
166         };
167 
168     /**
169      * GNU Regexp
170      */
171     private static final String[] GNU_REGEXP_114 =
172         new String[]
173         {
174             "GNU Regexp",
175             "1.1.4",
176             "gnu.regexp",
177             "org.acmsl.regexpplugin.gnuregexp.CompilerGNUAdapter",
178             "org.acmsl.regexpplugin.gnuregexp.MatcherGNUAdapter",
179             "org.acmsl.regexpplugin.gnuregexp.HelperGNUAdapter"
180         };
181 
182     /**
183      * Singleton instance.
184      */
185     private static RegexpManager m__Singleton;
186 
187     /**
188      * Private reference to the engine type.
189      */
190     private String[] m__astrEngine = JAKARTA_ORO_PERL5;
191 
192     /**
193      * Private constructor to avoid accidental instantiation.
194      */
195     private RegexpManager()  {};
196 
197     /**
198      * Common method to retrieve the singleton object.
199      * @return the singleton instance.
200      */
201     private static RegexpManager getInstance()
202     {
203         RegexpManager result = m__Singleton;
204 
205         if  (m__Singleton == null)
206         {
207             m__Singleton = new RegexpManager();
208 
209             result = m__Singleton;
210         }
211 
212         return result;
213     }
214 
215     /**
216      * Retrieves current engine information.
217      * @return the engine information.
218      */
219     protected String[] getEngine()
220     {
221         return m__astrEngine;
222     }
223 
224     /**
225      * Sets the current engine.
226      * @param engine the engine information.
227      */
228     protected void setEngine(String[] engine)
229     {
230         m__astrEngine = engine;
231     }
232 
233     /**
234      * Sets Jakarta ORO Perl5 implementation to be the engine used.
235      */
236     public static void useJakartaOroPerl5()
237     {
238         getInstance().useJakartaOroPerl5Engine();
239     }
240 
241     /**
242      * Sets Jakarta ORO Perl5 implementation to be the engine used.
243      */
244     private void useJakartaOroPerl5Engine()
245     {
246         setEngine(JAKARTA_ORO_PERL5);
247     }
248 
249     /**
250      * Sets Jakarta ORO Awk implementation to be the engine used.
251      * @deprecated Awk engine is not working correctly!
252      */
253     public static void useJakartaOroAwk()
254     {
255         getInstance().useJakartaOroAwkEngine();
256     }
257 
258     /**
259      * Sets Jakarta ORO Awk implementation to be the engine used.
260      * @deprecated Awk engine is not working correctly!
261      */
262     private void useJakartaOroAwkEngine()
263     {
264         setEngine(JAKARTA_ORO_AWK);
265     }
266 
267     /**
268      * Sets Jakarta Regexp implementation to be the engine used.
269      */
270     public static void useJakartaRegexp()
271     {
272         getInstance().useJakartaRegexpEngine();
273     }
274 
275     /**
276      * Sets Jakarta Regexp implementation to be the engine used.
277      */
278     private void useJakartaRegexpEngine()
279     {
280         setEngine(JAKARTA_REGEXP);
281     }
282 
283     /**
284      * Sets JDK1.4 regexp implementation to be the engine used.
285      */
286     public static void useJDK14Regexp()
287     {
288         getInstance().useJDK14RegexpEngine();
289     }
290 
291     /**
292      * Sets JDK1.4 regexp implementation to be the engine used.
293      */
294     private void useJDK14RegexpEngine()
295     {
296         setEngine(JDK14_REGEXP);
297     }
298 
299     /**
300      * Sets GNU Regexp implementation to be the engine used.
301      */
302     public static void useGNURegexp()
303     {
304         getInstance().useGNURegexpEngine();
305     }
306 
307     /**
308      * Sets GNU Regexp implementation to be the engine used.
309      */
310     private void useGNURegexpEngine()
311     {
312         setEngine(GNU_REGEXP_114);
313     }
314 
315     /**
316      * Creates a new compiler.
317      * @return the new compiler.
318      * @throws RegexpEngineNotFoundException whenever the instantiation
319      * of the engine classes fails.
320      */
321     public static Compiler createCompiler()
322         throws  RegexpEngineNotFoundException
323     {
324         return createSpecificCompiler(getInstance().getEngine());
325     }
326 
327     /**
328      * Creates a new helper instance.
329      * @return such kind of object.
330      * @throws RegexpEngineNotFoundException whenever the instantiation
331      * of the engine classes fails.
332      */
333     public static Helper createHelper()
334         throws  RegexpEngineNotFoundException
335     {
336         return createSpecificHelper(getInstance().getEngine());
337     }
338 
339     /**
340      * Creates a compiler determined by given class name.
341      * @param className the class name of the compiler.
342      * @return the compiler object, or null if the class name is incorrect.
343      * @throws RegexpEngineNotFoundException whenever the instantiation
344      * of the engine classes fails.
345      */
346     protected static Compiler createSpecificCompiler(String[] engine)
347         throws  RegexpEngineNotFoundException
348     {
349         Compiler result = null;
350 
351         try
352         {
353             result = (Compiler) Class.forName(engine[COMPILER]).newInstance();
354         }
355         catch  (Exception exception)
356         {
357             System.err.println("Compiler instantiation error.");
358 
359             exception.printStackTrace(System.err);
360 
361             throw
362                 new RegexpEngineNotFoundException(
363                     engine[NAME],
364                     engine[ENGINE_VERSION],
365                     engine[PACKAGE],
366                     engine[COMPILER],
367                     engine[MATCHER],
368                     engine[HELPER]);
369         }
370 
371         return result;
372     }
373 
374     /**
375      * Creates a compiler determined by given class name.
376      * @param className the class name of the compiler.
377      * @return the compiler object, or null if the class name is incorrect.
378      * @throws RegexpEngineNotFoundException whenever the instantiation
379      * of the engine classes fails.
380      */
381     protected static Helper createSpecificHelper(String[] engine)
382         throws  RegexpEngineNotFoundException
383     {
384         Helper result = null;
385 
386         try
387         {
388             result = (Helper) Class.forName(engine[HELPER]).newInstance();
389         }
390         catch  (Exception exception)
391         {
392             System.err.println("Compiler instantiation error.");
393 
394             exception.printStackTrace(System.err);
395 
396             throw
397                 new RegexpEngineNotFoundException(
398                     engine[NAME],
399                     engine[ENGINE_VERSION],
400                     engine[PACKAGE],
401                     engine[COMPILER],
402                     engine[MATCHER],
403                     engine[HELPER]);
404         }
405 
406         return result;
407     }
408 
409     /**
410      * Creates a new pattern matcher.
411      * @return the new matcher.
412      * @throws RegexpEngineNotFoundException whenever the instantiation
413      * of the engine classes fails.
414      */
415     public static Matcher createMatcher()
416         throws  RegexpEngineNotFoundException
417     {
418         return createSpecificMatcher(getInstance().getEngine());
419     }
420 
421     /**
422      * Creates a matcher determined by given class name.
423      * @param className the class name of the matcher.
424      * @return the matcher object, or null if the class name is incorrect.
425      * @throws RegexpEngineNotFoundException whenever the instantiation
426      * of the engine classes fails.
427      */
428     private static Matcher createSpecificMatcher(String[] engine)
429         throws  RegexpEngineNotFoundException
430     {
431         Matcher result = null;
432 
433         try
434         {
435             result = (Matcher) Class.forName(engine[MATCHER]).newInstance();
436         }
437         catch  (Exception exception)
438         {
439             System.out.println(exception.toString());
440 
441             exception.printStackTrace(System.err);
442 
443             throw
444                 new RegexpEngineNotFoundException(
445                     engine[NAME],
446                     engine[ENGINE_VERSION],
447                     engine[PACKAGE],
448                     engine[COMPILER],
449                     engine[MATCHER],
450                     engine[HELPER]);
451         }
452 
453         return result;
454     }
455 
456     /**
457      * Concrete version object updated everytime it's checked-in in a CVS
458      * repository.
459      */
460     public static final Version VERSION =
461         VersionFactory.createVersion("$Revision: 1.12 $");
462 
463     /**
464      * Retrieves the current version of this object.
465      * @return the version object with such information.
466      */
467     public Version getVersion()
468     {
469         return VERSION;
470     }
471 
472     /**
473      * Retrieves the current version of this class.
474      * @return the object with class version information.
475      */
476     public static Version getClassVersion()
477     {
478         return VERSION;
479     }
480 }