1 18 19 package org.apache.tools.ant.types.selectors; 20 21 import java.io.BufferedReader ; 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.InputStreamReader ; 25 26 import org.apache.tools.ant.BuildException; 27 import org.apache.tools.ant.types.Parameter; 28 import org.apache.tools.ant.types.RegularExpression; 29 import org.apache.tools.ant.types.Resource; 30 import org.apache.tools.ant.types.resources.FileResource; 31 import org.apache.tools.ant.types.resources.selectors.ResourceSelector; 32 import org.apache.tools.ant.util.regexp.Regexp; 33 34 39 public class ContainsRegexpSelector extends BaseExtendSelector 40 implements ResourceSelector { 41 42 private String userProvidedExpression = null; 43 private RegularExpression myRegExp = null; 44 private Regexp myExpression = null; 45 46 public static final String EXPRESSION_KEY = "expression"; 47 48 51 public ContainsRegexpSelector() { 52 } 53 54 57 public String toString() { 58 StringBuffer buf = new StringBuffer ( 59 "{containsregexpselector expression: "); 60 buf.append(userProvidedExpression); 61 buf.append("}"); 62 return buf.toString(); 63 } 64 65 70 public void setExpression(String theexpression) { 71 this.userProvidedExpression = theexpression; 72 } 73 74 80 public void setParameters(Parameter[] parameters) { 81 super.setParameters(parameters); 82 if (parameters != null) { 83 for (int i = 0; i < parameters.length; i++) { 84 String paramname = parameters[i].getName(); 85 if (EXPRESSION_KEY.equalsIgnoreCase(paramname)) { 86 setExpression(parameters[i].getValue()); 87 } else { 88 setError("Invalid parameter " + paramname); 89 } 90 } 91 } 92 } 93 94 98 public void verifySettings() { 99 if (userProvidedExpression == null) { 100 setError("The expression attribute is required"); 101 } 102 } 103 104 112 public boolean isSelected(File basedir, String filename, File file) { 113 return isSelected(new FileResource(file)); 114 } 115 116 122 public boolean isSelected(Resource r) { 123 String teststr = null; 124 BufferedReader in = null; 125 126 128 validate(); 129 130 if (r.isDirectory()) { 131 return true; 132 } 133 134 if (myRegExp == null) { 135 myRegExp = new RegularExpression(); 136 myRegExp.setPattern(userProvidedExpression); 137 myExpression = myRegExp.getRegexp(getProject()); 138 } 139 140 try { 141 in = new BufferedReader (new InputStreamReader (r.getInputStream())); 142 } catch (Exception e) { 143 throw new BuildException("Could not get InputStream from " 144 + r.toLongString(), e); 145 } 146 try { 147 teststr = in.readLine(); 148 149 while (teststr != null) { 150 151 if (myExpression.matches(teststr)) { 152 return true; 153 } 154 teststr = in.readLine(); 155 } 156 157 return false; 158 } catch (IOException ioe) { 159 throw new BuildException("Could not read " + r.toLongString()); 160 } finally { 161 if (in != null) { 162 try { 163 in.close(); 164 } catch (Exception e) { 165 throw new BuildException("Could not close " 166 + r.toLongString()); 167 } 168 } 169 } 170 } 171 } 172 173 | Popular Tags |