KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > util > regexp > RegexpMatcherFactory


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.util.regexp;
20
21 import org.apache.tools.ant.Project;
22 import org.apache.tools.ant.MagicNames;
23 import org.apache.tools.ant.BuildException;
24 import org.apache.tools.ant.util.ClasspathUtils;
25 import org.apache.tools.ant.util.JavaEnvUtils;
26
27 /**
28  * Simple Factory Class that produces an implementation of
29  * RegexpMatcher based on the system property
30  * <code>ant.regexp.regexpimpl</code> and the classes
31  * available.
32  *
33  * <p>In a more general framework this class would be abstract and
34  * have a static newInstance method.</p>
35  *
36  */

37 public class RegexpMatcherFactory {
38
39     /** Constructor for RegexpMatcherFactory. */
40     public RegexpMatcherFactory() {
41     }
42
43     /***
44      * Create a new regular expression instance.
45      * @return the matcher
46      * @throws BuildException on error
47      */

48     public RegexpMatcher newRegexpMatcher() throws BuildException {
49         return newRegexpMatcher(null);
50     }
51
52     /***
53      * Create a new regular expression instance.
54      *
55      * @param p Project whose ant.regexp.regexpimpl property will be used.
56      * @return the matcher
57      * @throws BuildException on error
58      */

59     public RegexpMatcher newRegexpMatcher(Project p)
60         throws BuildException {
61         String JavaDoc systemDefault = null;
62         if (p == null) {
63             systemDefault = System.getProperty(MagicNames.REGEXP_IMPL);
64         } else {
65             systemDefault = p.getProperty(MagicNames.REGEXP_IMPL);
66         }
67
68         if (systemDefault != null) {
69             return createInstance(systemDefault);
70             // XXX should we silently catch possible exceptions and try to
71
// load a different implementation?
72
}
73
74         Throwable JavaDoc cause = null;
75
76         try {
77             testAvailability("java.util.regex.Matcher");
78             return createInstance("org.apache.tools.ant.util.regexp.Jdk14RegexpMatcher");
79         } catch (BuildException be) {
80             cause = orCause(cause, be, JavaEnvUtils.getJavaVersionNumber() < 14);
81         }
82
83         try {
84             testAvailability("org.apache.oro.text.regex.Pattern");
85             return createInstance("org.apache.tools.ant.util.regexp.JakartaOroMatcher");
86         } catch (BuildException be) {
87             cause = orCause(cause, be, true);
88         }
89
90         try {
91             testAvailability("org.apache.regexp.RE");
92             return createInstance("org.apache.tools.ant.util.regexp.JakartaRegexpMatcher");
93         } catch (BuildException be) {
94             cause = orCause(cause, be, true);
95         }
96
97         throw new BuildException(
98             "No supported regular expression matcher found"
99             + (cause != null ? ": " + cause : ""), cause);
100    }
101
102     static Throwable JavaDoc orCause(Throwable JavaDoc deflt, BuildException be, boolean ignoreCnfe) {
103         if (deflt != null) {
104             return deflt;
105         }
106         Throwable JavaDoc t = be.getException();
107         return ignoreCnfe && t instanceof ClassNotFoundException JavaDoc ? null : t;
108     }
109
110     /**
111      * Create an instance of a matcher from a classname.
112      *
113      * @param className a <code>String</code> value
114      * @return a <code>RegexpMatcher</code> value
115      * @exception BuildException if an error occurs
116      */

117     protected RegexpMatcher createInstance(String JavaDoc className)
118         throws BuildException {
119         return (RegexpMatcher) ClasspathUtils.newInstance(className,
120                 RegexpMatcherFactory.class.getClassLoader(), RegexpMatcher.class);
121     }
122
123     /**
124      * Test if a particular class is available to be used.
125      *
126      * @param className a <code>String</code> value
127      * @exception BuildException if an error occurs
128      */

129     protected void testAvailability(String JavaDoc className) throws BuildException {
130         try {
131             Class.forName(className);
132         } catch (Throwable JavaDoc t) {
133             throw new BuildException(t);
134         }
135     }
136 }
137
Popular Tags