1 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 37 public class RegexpMatcherFactory { 38 39 40 public RegexpMatcherFactory() { 41 } 42 43 48 public RegexpMatcher newRegexpMatcher() throws BuildException { 49 return newRegexpMatcher(null); 50 } 51 52 59 public RegexpMatcher newRegexpMatcher(Project p) 60 throws BuildException { 61 String 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 } 73 74 Throwable 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 orCause(Throwable deflt, BuildException be, boolean ignoreCnfe) { 103 if (deflt != null) { 104 return deflt; 105 } 106 Throwable t = be.getException(); 107 return ignoreCnfe && t instanceof ClassNotFoundException ? null : t; 108 } 109 110 117 protected RegexpMatcher createInstance(String className) 118 throws BuildException { 119 return (RegexpMatcher) ClasspathUtils.newInstance(className, 120 RegexpMatcherFactory.class.getClassLoader(), RegexpMatcher.class); 121 } 122 123 129 protected void testAvailability(String className) throws BuildException { 130 try { 131 Class.forName(className); 132 } catch (Throwable t) { 133 throw new BuildException(t); 134 } 135 } 136 } 137 | Popular Tags |