From 6c9e00f717f85949fa52914e1203785bb7c22182 Mon Sep 17 00:00:00 2001 From: Matthew Jason Benson Date: Fri, 22 Sep 2006 22:13:40 +0000 Subject: [PATCH] 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 --- docs/manual/CoreTypes/resources.html | 2 ++ docs/manual/CoreTypes/selectors.html | 4 +++ .../selectors/ContainsRegexpSelector.java | 33 ++++++++++++++----- .../ant/types/resources/selectors/antlib.xml | 2 ++ .../types/resources/selectors/test.xml | 21 +++++++++--- 5 files changed, 50 insertions(+), 12 deletions(-) diff --git a/docs/manual/CoreTypes/resources.html b/docs/manual/CoreTypes/resources.html index ff3ee4a5d..f2b7b0cc4 100644 --- a/docs/manual/CoreTypes/resources.html +++ b/docs/manual/CoreTypes/resources.html @@ -459,6 +459,8 @@ platforms. content has changed.
  • contains - select resources containing a particular text string.
  • +
  • containsregexp - select + resources whose contents match a particular regular expression.
  • name

    diff --git a/docs/manual/CoreTypes/selectors.html b/docs/manual/CoreTypes/selectors.html index 651858c22..762111ad6 100755 --- a/docs/manual/CoreTypes/selectors.html +++ b/docs/manual/CoreTypes/selectors.html @@ -527,6 +527,10 @@ the files defined by that fileset to only those which contain a match to the regular expression specified by the expression attribute.

    +

    The <containsregexp> selector can be used as a + ResourceSelector (see the + <restrict> + ResourceCollection).

    diff --git a/src/main/org/apache/tools/ant/types/selectors/ContainsRegexpSelector.java b/src/main/org/apache/tools/ant/types/selectors/ContainsRegexpSelector.java index 976dd5312..4633e8616 100644 --- a/src/main/org/apache/tools/ant/types/selectors/ContainsRegexpSelector.java +++ b/src/main/org/apache/tools/ant/types/selectors/ContainsRegexpSelector.java @@ -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()); } } } diff --git a/src/resources/org/apache/tools/ant/types/resources/selectors/antlib.xml b/src/resources/org/apache/tools/ant/types/resources/selectors/antlib.xml index 56df8e8ea..ebf3d4779 100755 --- a/src/resources/org/apache/tools/ant/types/resources/selectors/antlib.xml +++ b/src/resources/org/apache/tools/ant/types/resources/selectors/antlib.xml @@ -3,6 +3,8 @@ classname="org.apache.tools.ant.types.resources.selectors.And" /> + + + + + + + + + + + + + + + + @@ -349,14 +365,11 @@ depends="testand,testor,testnone,testnot,majority" /> - - + depends="name,testexists,instanceof,testtype,testdate,testsize,testcontains,testcontainsregexp,logical" /> -