001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.util.regexp;
019:
020: import org.apache.tools.ant.BuildException;
021: import org.apache.tools.ant.Project;
022: import org.apache.tools.ant.util.JavaEnvUtils;
023:
024: /***
025: * Regular expression factory, which will create Regexp objects. The
026: * actual implementation class depends on the System or Ant Property:
027: * <code>ant.regexp.regexpimpl</code>.
028: *
029: */
030: public class RegexpFactory extends RegexpMatcherFactory {
031:
032: /** Constructor for RegexpFactory */
033: public RegexpFactory() {
034: }
035:
036: /***
037: * Create a new regular expression matcher instance.
038: * @return the matcher instance
039: * @throws BuildException on error
040: */
041: public Regexp newRegexp() throws BuildException {
042: return (Regexp) newRegexp(null);
043: }
044:
045: /***
046: * Create a new regular expression matcher instance.
047: *
048: * @param p Project whose ant.regexp.regexpimpl property will be used.
049: * @return the matcher instance
050: * @throws BuildException on error
051: */
052: public Regexp newRegexp(Project p) throws BuildException {
053: String systemDefault = null;
054: if (p == null) {
055: systemDefault = System.getProperty("ant.regexp.regexpimpl");
056: } else {
057: systemDefault = p.getProperty("ant.regexp.regexpimpl");
058: }
059:
060: if (systemDefault != null) {
061: return createRegexpInstance(systemDefault);
062: // XXX should we silently catch possible exceptions and try to
063: // load a different implementation?
064: }
065:
066: Throwable cause = null;
067:
068: try {
069: testAvailability("java.util.regex.Matcher");
070: return createRegexpInstance("org.apache.tools.ant.util.regexp.Jdk14RegexpRegexp");
071: } catch (BuildException be) {
072: cause = orCause(cause, be, JavaEnvUtils
073: .getJavaVersionNumber() < 14);
074: }
075:
076: try {
077: testAvailability("org.apache.oro.text.regex.Pattern");
078: return createRegexpInstance("org.apache.tools.ant.util.regexp.JakartaOroRegexp");
079: } catch (BuildException be) {
080: cause = orCause(cause, be, true);
081: }
082:
083: try {
084: testAvailability("org.apache.regexp.RE");
085: return createRegexpInstance("org.apache.tools.ant.util.regexp.JakartaRegexpRegexp");
086: } catch (BuildException be) {
087: cause = orCause(cause, be, true);
088: }
089:
090: throw new BuildException(
091: "No supported regular expression matcher found"
092: + (cause != null ? ": " + cause : ""), cause);
093: }
094:
095: /**
096: * Wrapper over RegexpMatcherFactory.createInstance that ensures that
097: * we are dealing with a Regexp implementation.
098: * @param classname the name of the class to use.
099: * @return the instance.
100: * @throws BuildException if there is a problem.
101: * @since 1.3
102: *
103: * @see RegexpMatcherFactory#createInstance(String)
104: */
105: protected Regexp createRegexpInstance(String classname)
106: throws BuildException {
107:
108: RegexpMatcher m = createInstance(classname);
109: if (m instanceof Regexp) {
110: return (Regexp) m;
111: } else {
112: throw new BuildException(classname
113: + " doesn't implement the Regexp interface");
114: }
115: }
116:
117: }
|