Browse Source

containsregexp does double-duty as FileSelector + ResourceSelector.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@449108 13f79535-47bb-0310-9956-ffa450edef68
master
Matthew Jason Benson 19 years ago
parent
commit
6c9e00f717
5 changed files with 50 additions and 12 deletions
  1. +2
    -0
      docs/manual/CoreTypes/resources.html
  2. +4
    -0
      docs/manual/CoreTypes/selectors.html
  3. +25
    -8
      src/main/org/apache/tools/ant/types/selectors/ContainsRegexpSelector.java
  4. +2
    -0
      src/resources/org/apache/tools/ant/types/resources/selectors/antlib.xml
  5. +17
    -4
      src/tests/antunit/types/resources/selectors/test.xml

+ 2
- 0
docs/manual/CoreTypes/resources.html View File

@@ -459,6 +459,8 @@ platforms.
content has changed.</li>
<li><a href="selectors.html#containsselect">contains</a> - select resources
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>

<h4><a name="rsel.name">name</a></h4>


+ 4
- 0
docs/manual/CoreTypes/selectors.html View File

@@ -527,6 +527,10 @@
the files defined by that fileset to only those which contain a
match to the regular expression specified by the <code>expression</code> attribute.
</p>
<p>The <code>&lt;containsregexp&gt;</code> selector can be used as a
ResourceSelector (see the
<a href="resources.html#restrict">&lt;restrict&gt;</a>
ResourceCollection).</p>

<table border="1" cellpadding="2" cellspacing="0">
<tr>


+ 25
- 8
src/main/org/apache/tools/ant/types/selectors/ContainsRegexpSelector.java View File

@@ -27,6 +27,9 @@ import java.io.InputStreamReader;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.types.Parameter;
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;

/**
@@ -34,7 +37,8 @@ import org.apache.tools.ant.util.regexp.Regexp;
*
* @since Ant 1.6
*/
public class ContainsRegexpSelector extends BaseExtendSelector {
public class ContainsRegexpSelector extends BaseExtendSelector
implements ResourceSelector {

private String userProvidedExpression = null;
private RegularExpression myRegExp = null;
@@ -107,6 +111,16 @@ public class ContainsRegexpSelector extends BaseExtendSelector {
* @return whether the file should be selected or not
*/
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;
BufferedReader in = null;

@@ -114,7 +128,7 @@ public class ContainsRegexpSelector extends BaseExtendSelector {

validate();

if (file.isDirectory()) {
if (r.isDirectory()) {
return true;
}

@@ -125,9 +139,12 @@ public class ContainsRegexpSelector extends BaseExtendSelector {
}

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();

while (teststr != null) {
@@ -140,14 +157,14 @@ public class ContainsRegexpSelector extends BaseExtendSelector {

return false;
} catch (IOException ioe) {
throw new BuildException("Could not read file " + filename);
throw new BuildException("Could not read " + r.toLongString());
} finally {
if (in != null) {
try {
in.close();
} catch (Exception e) {
throw new BuildException("Could not close file "
+ filename);
throw new BuildException("Could not close "
+ r.toLongString());
}
}
}


+ 2
- 0
src/resources/org/apache/tools/ant/types/resources/selectors/antlib.xml View File

@@ -3,6 +3,8 @@
classname="org.apache.tools.ant.types.resources.selectors.And" />
<typedef name="contains"
classname="org.apache.tools.ant.types.selectors.ContainsSelector" />
<typedef name="containsregexp"
classname="org.apache.tools.ant.types.selectors.ContainsRegexpSelector" />
<typedef name="date"
classname="org.apache.tools.ant.types.resources.selectors.Date" />
<typedef name="exists"


+ 17
- 4
src/tests/antunit/types/resources/selectors/test.xml View File

@@ -342,6 +342,22 @@
</au:assertTrue>
</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"
depends="testmajority1,testmajority2,testmajority3,testmajority4" />

@@ -349,14 +365,11 @@
depends="testand,testor,testnone,testnot,majority" />

<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
ResourceSelector are in its test-buildfile src\etc\testcases\types\selectors.xml.
-->


</project>

Loading…
Cancel
Save