FileDocCategorySizeDatePackage
RegexpMatcherFactory.javaAPI DocApache Ant 1.704684Wed Dec 13 06:16:22 GMT 2006org.apache.tools.ant.util.regexp

RegexpMatcherFactory

public class RegexpMatcherFactory extends Object
Simple Factory Class that produces an implementation of RegexpMatcher based on the system property ant.regexp.regexpimpl and the classes available.

In a more general framework this class would be abstract and have a static newInstance method.

Fields Summary
Constructors Summary
public RegexpMatcherFactory()
Constructor for RegexpMatcherFactory.

    
Methods Summary
protected RegexpMatchercreateInstance(java.lang.String className)
Create an instance of a matcher from a classname.

param
className a String value
return
a RegexpMatcher value
exception
BuildException if an error occurs

        return (RegexpMatcher) ClasspathUtils.newInstance(className,
                RegexpMatcherFactory.class.getClassLoader(), RegexpMatcher.class);
    
public RegexpMatchernewRegexpMatcher()
Create a new regular expression instance.

return
the matcher
throws
BuildException on error

        return newRegexpMatcher(null);
    
public RegexpMatchernewRegexpMatcher(org.apache.tools.ant.Project p)
Create a new regular expression instance.

param
p Project whose ant.regexp.regexpimpl property will be used.
return
the matcher
throws
BuildException on error

        String systemDefault = null;
        if (p == null) {
            systemDefault = System.getProperty(MagicNames.REGEXP_IMPL);
        } else {
            systemDefault = p.getProperty(MagicNames.REGEXP_IMPL);
        }

        if (systemDefault != null) {
            return createInstance(systemDefault);
            // XXX     should we silently catch possible exceptions and try to
            //         load a different implementation?
        }

        Throwable cause = null;

        try {
            testAvailability("java.util.regex.Matcher");
            return createInstance("org.apache.tools.ant.util.regexp.Jdk14RegexpMatcher");
        } catch (BuildException be) {
            cause = orCause(cause, be, JavaEnvUtils.getJavaVersionNumber() < 14);
        }

        try {
            testAvailability("org.apache.oro.text.regex.Pattern");
            return createInstance("org.apache.tools.ant.util.regexp.JakartaOroMatcher");
        } catch (BuildException be) {
            cause = orCause(cause, be, true);
        }

        try {
            testAvailability("org.apache.regexp.RE");
            return createInstance("org.apache.tools.ant.util.regexp.JakartaRegexpMatcher");
        } catch (BuildException be) {
            cause = orCause(cause, be, true);
        }

        throw new BuildException(
            "No supported regular expression matcher found"
            + (cause != null ? ": " + cause : ""), cause);
   
static java.lang.ThrowableorCause(java.lang.Throwable deflt, org.apache.tools.ant.BuildException be, boolean ignoreCnfe)

        if (deflt != null) {
            return deflt;
        }
        Throwable t = be.getException();
        return ignoreCnfe && t instanceof ClassNotFoundException ? null : t;
    
protected voidtestAvailability(java.lang.String className)
Test if a particular class is available to be used.

param
className a String value
exception
BuildException if an error occurs

        try {
            Class.forName(className);
        } catch (Throwable t) {
            throw new BuildException(t);
        }