source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/util/regexp/RegexpMatcherFactory.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 4.0 KB
Line 
1/*
2 * Copyright 2000-2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18package org.apache.tools.ant.util.regexp;
19
20import org.apache.tools.ant.BuildException;
21import org.apache.tools.ant.Project;
22
23/**
24 * Simple Factory Class that produces an implementation of
25 * RegexpMatcher based on the system property
26 * <code>ant.regexp.matcherimpl</code> and the classes
27 * available.
28 *
29 * <p>In a more general framework this class would be abstract and
30 * have a static newInstance method.</p>
31 *
32 */
33public class RegexpMatcherFactory {
34
35 /** Constructor for RegexpMatcherFactory. */
36 public RegexpMatcherFactory() {
37 }
38
39 /***
40 * Create a new regular expression instance.
41 * @return the matcher
42 * @throws BuildException on error
43 */
44 public RegexpMatcher newRegexpMatcher() throws BuildException {
45 return newRegexpMatcher(null);
46 }
47
48 /***
49 * Create a new regular expression instance.
50 *
51 * @param p Project whose ant.regexp.regexpimpl property will be used.
52 * @return the matcher
53 * @throws BuildException on error
54 */
55 public RegexpMatcher newRegexpMatcher(Project p)
56 throws BuildException {
57 String systemDefault = null;
58 if (p == null) {
59 systemDefault = System.getProperty("ant.regexp.regexpimpl");
60 } else {
61 systemDefault = p.getProperty("ant.regexp.regexpimpl");
62 }
63
64 if (systemDefault != null) {
65 return createInstance(systemDefault);
66 // XXX should we silently catch possible exceptions and try to
67 // load a different implementation?
68 }
69
70 try {
71 testAvailability("java.util.regex.Matcher");
72 return createInstance("org.apache.tools.ant.util.regexp.Jdk14RegexpMatcher");
73 } catch (BuildException be) {
74 // ignore
75 }
76
77 try {
78 testAvailability("org.apache.oro.text.regex.Pattern");
79 return createInstance("org.apache.tools.ant.util.regexp.JakartaOroMatcher");
80 } catch (BuildException be) {
81 // ignore
82 }
83
84 try {
85 testAvailability("org.apache.regexp.RE");
86 return createInstance("org.apache.tools.ant.util.regexp.JakartaRegexpMatcher");
87 } catch (BuildException be) {
88 // ignore
89 }
90
91 throw new BuildException("No supported regular expression matcher found");
92 }
93
94 /**
95 * Create an instance of a matcher from a classname.
96 *
97 * @param className a <code>String</code> value
98 * @return a <code>RegexpMatcher</code> value
99 * @exception BuildException if an error occurs
100 */
101 protected RegexpMatcher createInstance(String className)
102 throws BuildException {
103 try {
104 Class implClass = Class.forName(className);
105 return (RegexpMatcher) implClass.newInstance();
106 } catch (Throwable t) {
107 throw new BuildException(t);
108 }
109 }
110
111 /**
112 * Test if a particular class is available to be used.
113 *
114 * @param className a <code>String</code> value
115 * @exception BuildException if an error occurs
116 */
117 protected void testAvailability(String className) throws BuildException {
118 try {
119 Class.forName(className);
120 } catch (Throwable t) {
121 throw new BuildException(t);
122 }
123 }
124}
Note: See TracBrowser for help on using the repository browser.