git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@449108 13f79535-47bb-0310-9956-ffa450edef68master
@@ -459,6 +459,8 @@ platforms. | |||||
content has changed.</li> | content has changed.</li> | ||||
<li><a href="selectors.html#containsselect">contains</a> - select resources | <li><a href="selectors.html#containsselect">contains</a> - select resources | ||||
containing a particular text string.</li> | containing a particular text string.</li> | ||||
<li><a href="selectors.html#regexpselect">containsregexp</a> - select | |||||
resources whose contents match a particular regular expression.</li> | |||||
</ul> | </ul> | ||||
<h4><a name="rsel.name">name</a></h4> | <h4><a name="rsel.name">name</a></h4> | ||||
@@ -527,6 +527,10 @@ | |||||
the files defined by that fileset to only those which contain a | the files defined by that fileset to only those which contain a | ||||
match to the regular expression specified by the <code>expression</code> attribute. | match to the regular expression specified by the <code>expression</code> attribute. | ||||
</p> | </p> | ||||
<p>The <code><containsregexp></code> selector can be used as a | |||||
ResourceSelector (see the | |||||
<a href="resources.html#restrict"><restrict></a> | |||||
ResourceCollection).</p> | |||||
<table border="1" cellpadding="2" cellspacing="0"> | <table border="1" cellpadding="2" cellspacing="0"> | ||||
<tr> | <tr> | ||||
@@ -27,6 +27,9 @@ import java.io.InputStreamReader; | |||||
import org.apache.tools.ant.BuildException; | import org.apache.tools.ant.BuildException; | ||||
import org.apache.tools.ant.types.Parameter; | import org.apache.tools.ant.types.Parameter; | ||||
import org.apache.tools.ant.types.RegularExpression; | import org.apache.tools.ant.types.RegularExpression; | ||||
import org.apache.tools.ant.types.Resource; | |||||
import org.apache.tools.ant.types.resources.FileResource; | |||||
import org.apache.tools.ant.types.resources.selectors.ResourceSelector; | |||||
import org.apache.tools.ant.util.regexp.Regexp; | import org.apache.tools.ant.util.regexp.Regexp; | ||||
/** | /** | ||||
@@ -34,7 +37,8 @@ import org.apache.tools.ant.util.regexp.Regexp; | |||||
* | * | ||||
* @since Ant 1.6 | * @since Ant 1.6 | ||||
*/ | */ | ||||
public class ContainsRegexpSelector extends BaseExtendSelector { | |||||
public class ContainsRegexpSelector extends BaseExtendSelector | |||||
implements ResourceSelector { | |||||
private String userProvidedExpression = null; | private String userProvidedExpression = null; | ||||
private RegularExpression myRegExp = null; | private RegularExpression myRegExp = null; | ||||
@@ -107,6 +111,16 @@ public class ContainsRegexpSelector extends BaseExtendSelector { | |||||
* @return whether the file should be selected or not | * @return whether the file should be selected or not | ||||
*/ | */ | ||||
public boolean isSelected(File basedir, String filename, File file) { | public boolean isSelected(File basedir, String filename, File file) { | ||||
return isSelected(new FileResource(file)); | |||||
} | |||||
/** | |||||
* Tests a regular expression against each line of text in a Resource. | |||||
* | |||||
* @param r the Resource to check. | |||||
* @return whether the Resource is selected or not | |||||
*/ | |||||
public boolean isSelected(Resource r) { | |||||
String teststr = null; | String teststr = null; | ||||
BufferedReader in = null; | BufferedReader in = null; | ||||
@@ -114,7 +128,7 @@ public class ContainsRegexpSelector extends BaseExtendSelector { | |||||
validate(); | validate(); | ||||
if (file.isDirectory()) { | |||||
if (r.isDirectory()) { | |||||
return true; | return true; | ||||
} | } | ||||
@@ -125,9 +139,12 @@ public class ContainsRegexpSelector extends BaseExtendSelector { | |||||
} | } | ||||
try { | try { | ||||
in = new BufferedReader(new InputStreamReader( | |||||
new FileInputStream(file))); | |||||
in = new BufferedReader(new InputStreamReader(r.getInputStream())); | |||||
} catch (Exception e) { | |||||
throw new BuildException("Could not get InputStream from " | |||||
+ r.toLongString(), e); | |||||
} | |||||
try { | |||||
teststr = in.readLine(); | teststr = in.readLine(); | ||||
while (teststr != null) { | while (teststr != null) { | ||||
@@ -140,14 +157,14 @@ public class ContainsRegexpSelector extends BaseExtendSelector { | |||||
return false; | return false; | ||||
} catch (IOException ioe) { | } catch (IOException ioe) { | ||||
throw new BuildException("Could not read file " + filename); | |||||
throw new BuildException("Could not read " + r.toLongString()); | |||||
} finally { | } finally { | ||||
if (in != null) { | if (in != null) { | ||||
try { | try { | ||||
in.close(); | in.close(); | ||||
} catch (Exception e) { | } catch (Exception e) { | ||||
throw new BuildException("Could not close file " | |||||
+ filename); | |||||
throw new BuildException("Could not close " | |||||
+ r.toLongString()); | |||||
} | } | ||||
} | } | ||||
} | } | ||||
@@ -3,6 +3,8 @@ | |||||
classname="org.apache.tools.ant.types.resources.selectors.And" /> | classname="org.apache.tools.ant.types.resources.selectors.And" /> | ||||
<typedef name="contains" | <typedef name="contains" | ||||
classname="org.apache.tools.ant.types.selectors.ContainsSelector" /> | classname="org.apache.tools.ant.types.selectors.ContainsSelector" /> | ||||
<typedef name="containsregexp" | |||||
classname="org.apache.tools.ant.types.selectors.ContainsRegexpSelector" /> | |||||
<typedef name="date" | <typedef name="date" | ||||
classname="org.apache.tools.ant.types.resources.selectors.Date" /> | classname="org.apache.tools.ant.types.resources.selectors.Date" /> | ||||
<typedef name="exists" | <typedef name="exists" | ||||
@@ -342,6 +342,22 @@ | |||||
</au:assertTrue> | </au:assertTrue> | ||||
</target> | </target> | ||||
<target name="testcontainsregexp"> | |||||
<au:assertTrue> | |||||
<resourcecount when="equal" count="2"> | |||||
<restrict> | |||||
<resources> | |||||
<string value="foo" /> | |||||
<string value="bar" /> | |||||
<string value="baz" /> | |||||
</resources> | |||||
<containsregexp expression="^b..$" | |||||
xmlns="antlib:org.apache.tools.ant.types.resources.selectors" /> | |||||
</restrict> | |||||
</resourcecount> | |||||
</au:assertTrue> | |||||
</target> | |||||
<target name="majority" | <target name="majority" | ||||
depends="testmajority1,testmajority2,testmajority3,testmajority4" /> | depends="testmajority1,testmajority2,testmajority3,testmajority4" /> | ||||
@@ -349,14 +365,11 @@ | |||||
depends="testand,testor,testnone,testnot,majority" /> | depends="testand,testor,testnone,testnot,majority" /> | ||||
<target name="all" | <target name="all" | ||||
depends="name,testexists,instanceof,testtype,testdate,testsize,testcontains,logical" /> | |||||
depends="name,testexists,instanceof,testtype,testdate,testsize,testcontains,testcontainsregexp,logical" /> | |||||
<!-- | <!-- | ||||
The tests for oata.types.selectors.ModifiedSelectorTest as | The tests for oata.types.selectors.ModifiedSelectorTest as | ||||
ResourceSelector are in its test-buildfile src\etc\testcases\types\selectors.xml. | ResourceSelector are in its test-buildfile src\etc\testcases\types\selectors.xml. | ||||
--> | --> | ||||
</project> | </project> |