KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.apache.tools.ant.util.regexp;
19
20 import org.apache.tools.ant.BuildException;
21 import org.apache.tools.ant.Project;
22 import org.apache.tools.ant.util.JavaEnvUtils;
23
24 /***
25  * Regular expression factory, which will create Regexp objects. The
26  * actual implementation class depends on the System or Ant Property:
27  * <code>ant.regexp.regexpimpl</code>.
28  *
29  */

30 public class RegexpFactory extends RegexpMatcherFactory {
31
32     /** Constructor for RegexpFactory */
33     public RegexpFactory() {
34     }
35
36     /***
37      * Create a new regular expression matcher instance.
38      * @return the matcher instance
39      * @throws BuildException on error
40      */

41     public Regexp newRegexp() throws BuildException {
42         return (Regexp) newRegexp(null);
43     }
44
45     /***
46      * Create a new regular expression matcher instance.
47      *
48      * @param p Project whose ant.regexp.regexpimpl property will be used.
49      * @return the matcher instance
50      * @throws BuildException on error
51      */

52     public Regexp newRegexp(Project p) throws BuildException {
53         String JavaDoc systemDefault = null;
54         if (p == null) {
55             systemDefault = System.getProperty("ant.regexp.regexpimpl");
56         } else {
57             systemDefault = p.getProperty("ant.regexp.regexpimpl");
58         }
59
60         if (systemDefault != null) {
61             return createRegexpInstance(systemDefault);
62             // XXX should we silently catch possible exceptions and try to
63
// load a different implementation?
64
}
65
66         Throwable JavaDoc cause = null;
67
68         try {
69             testAvailability("java.util.regex.Matcher");
70             return createRegexpInstance("org.apache.tools.ant.util.regexp.Jdk14RegexpRegexp");
71         } catch (BuildException be) {
72             cause = orCause(cause, be, JavaEnvUtils.getJavaVersionNumber() < 14);
73         }
74
75         try {
76             testAvailability("org.apache.oro.text.regex.Pattern");
77             return createRegexpInstance("org.apache.tools.ant.util.regexp.JakartaOroRegexp");
78         } catch (BuildException be) {
79             cause = orCause(cause, be, true);
80         }
81
82         try {
83             testAvailability("org.apache.regexp.RE");
84             return createRegexpInstance("org.apache.tools.ant.util.regexp.JakartaRegexpRegexp");
85         } catch (BuildException be) {
86             cause = orCause(cause, be, true);
87         }
88
89         throw new BuildException(
90             "No supported regular expression matcher found"
91             + (cause != null ? ": " + cause : ""), cause);
92     }
93
94     /**
95      * Wrapper over RegexpMatcherFactory.createInstance that ensures that
96      * we are dealing with a Regexp implementation.
97      * @param classname the name of the class to use.
98      * @return the instance.
99      * @throws BuildException if there is a problem.
100      * @since 1.3
101      *
102      * @see RegexpMatcherFactory#createInstance(String)
103      */

104     protected Regexp createRegexpInstance(String JavaDoc classname)
105         throws BuildException {
106
107         RegexpMatcher m = createInstance(classname);
108         if (m instanceof Regexp) {
109             return (Regexp) m;
110         } else {
111             throw new BuildException(classname + " doesn't implement the Regexp interface");
112         }
113     }
114
115 }
116
Popular Tags