Author: mbenson Date: Fri Sep 22 15:13:40 2006 New Revision: 449108 URL: http://svn.apache.org/viewvc?view=rev&rev=449108 Log: containsregexp does double-duty as FileSelector + ResourceSelector.
Modified: ant/core/trunk/docs/manual/CoreTypes/resources.html ant/core/trunk/docs/manual/CoreTypes/selectors.html ant/core/trunk/src/main/org/apache/tools/ant/types/selectors/ContainsRegexpSelector.java ant/core/trunk/src/resources/org/apache/tools/ant/types/resources/selectors/antlib.xml ant/core/trunk/src/tests/antunit/types/resources/selectors/test.xml Modified: ant/core/trunk/docs/manual/CoreTypes/resources.html URL: http://svn.apache.org/viewvc/ant/core/trunk/docs/manual/CoreTypes/resources.html?view=diff&rev=449108&r1=449107&r2=449108 ============================================================================== --- ant/core/trunk/docs/manual/CoreTypes/resources.html (original) +++ ant/core/trunk/docs/manual/CoreTypes/resources.html Fri Sep 22 15:13:40 2006 @@ -459,6 +459,8 @@ 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> Modified: ant/core/trunk/docs/manual/CoreTypes/selectors.html URL: http://svn.apache.org/viewvc/ant/core/trunk/docs/manual/CoreTypes/selectors.html?view=diff&rev=449108&r1=449107&r2=449108 ============================================================================== --- ant/core/trunk/docs/manual/CoreTypes/selectors.html (original) +++ ant/core/trunk/docs/manual/CoreTypes/selectors.html Fri Sep 22 15:13:40 2006 @@ -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><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"> <tr> Modified: ant/core/trunk/src/main/org/apache/tools/ant/types/selectors/ContainsRegexpSelector.java URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/types/selectors/ContainsRegexpSelector.java?view=diff&rev=449108&r1=449107&r2=449108 ============================================================================== --- ant/core/trunk/src/main/org/apache/tools/ant/types/selectors/ContainsRegexpSelector.java (original) +++ ant/core/trunk/src/main/org/apache/tools/ant/types/selectors/ContainsRegexpSelector.java Fri Sep 22 15:13:40 2006 @@ -27,6 +27,9 @@ 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 @@ * * @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 @@ * @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 @@ validate(); - if (file.isDirectory()) { + if (r.isDirectory()) { return true; } @@ -125,9 +139,12 @@ } 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 @@ 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()); } } } Modified: ant/core/trunk/src/resources/org/apache/tools/ant/types/resources/selectors/antlib.xml URL: http://svn.apache.org/viewvc/ant/core/trunk/src/resources/org/apache/tools/ant/types/resources/selectors/antlib.xml?view=diff&rev=449108&r1=449107&r2=449108 ============================================================================== --- ant/core/trunk/src/resources/org/apache/tools/ant/types/resources/selectors/antlib.xml (original) +++ ant/core/trunk/src/resources/org/apache/tools/ant/types/resources/selectors/antlib.xml Fri Sep 22 15:13:40 2006 @@ -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" Modified: ant/core/trunk/src/tests/antunit/types/resources/selectors/test.xml URL: http://svn.apache.org/viewvc/ant/core/trunk/src/tests/antunit/types/resources/selectors/test.xml?view=diff&rev=449108&r1=449107&r2=449108 ============================================================================== --- ant/core/trunk/src/tests/antunit/types/resources/selectors/test.xml (original) +++ ant/core/trunk/src/tests/antunit/types/resources/selectors/test.xml Fri Sep 22 15:13:40 2006 @@ -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> --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]