Author: mbenson Date: Mon May 14 09:14:43 2007 New Revision: 537899 URL: http://svn.apache.org/viewvc?view=rev&rev=537899 Log: make resourcecontains handle refids
Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/condition/ResourceContains.java ant/core/trunk/src/tests/antunit/taskdefs/condition/resourcecontains-test.xml Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/condition/ResourceContains.java URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/condition/ResourceContains.java?view=diff&rev=537899&r1=537898&r2=537899 ============================================================================== --- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/condition/ResourceContains.java (original) +++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/condition/ResourceContains.java Mon May 14 09:14:43 2007 @@ -25,6 +25,7 @@ import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; import org.apache.tools.ant.types.Resource; +import org.apache.tools.ant.types.ResourceCollection; import org.apache.tools.ant.types.resources.FileResource; import org.apache.tools.ant.util.FileUtils; @@ -38,6 +39,7 @@ private Project project; private String substring; private Resource resource; + private String refid; private boolean casesensitive = true; /** @@ -65,6 +67,38 @@ } /** + * Sets the refid to search; should indicate a resource directly + * or by way of a single-element ResourceCollection. + * @param refid + */ + public void setRefid(String refid) { + this.refid = refid; + } + + private void resolveRefid() { + try { + if (getProject() == null) { + throw new BuildException("Cannot retrieve refid; project unset"); + } + Object o = getProject().getReference(refid); + if (!(o instanceof Resource)) { + if (o instanceof ResourceCollection) { + ResourceCollection rc = (ResourceCollection) o; + if (rc.size() == 1) { + o = rc.iterator().next(); + } + } else { + throw new BuildException("Illegal value at '" + refid +"': " + + String.valueOf(o)); + } + } + this.resource = (Resource) o; + } finally { + refid = null; + } + } + + /** * Sets the substring to look for * @param substring */ @@ -80,23 +114,32 @@ this.casesensitive = casesensitive; } - /** - * Evaluates - * Returns true if the substring is contained in the resource - */ - public boolean eval() throws BuildException { + private void validate() { + if (resource != null && refid != null) { + throw new BuildException("Cannot set both resource and refid"); + } + if (resource == null && refid != null) { + resolveRefid(); + } if (resource == null || substring == null) { throw new BuildException("both resource and substring are required " + "in <resourcecontains>"); } + } + + /** + * Evaluates + * Returns true if the substring is contained in the resource + */ + public synchronized boolean eval() throws BuildException { + validate(); if (substring.length() == 0) { if (getProject() != null) { - getProject().log("ResourceContains: substring is empty; returning true", Project.MSG_VERBOSE); + getProject().log("Substring is empty; returning true", Project.MSG_VERBOSE); } return true; } - if (resource.getSize() == 0) { return false; } Modified: ant/core/trunk/src/tests/antunit/taskdefs/condition/resourcecontains-test.xml URL: http://svn.apache.org/viewvc/ant/core/trunk/src/tests/antunit/taskdefs/condition/resourcecontains-test.xml?view=diff&rev=537899&r1=537898&r2=537899 ============================================================================== --- ant/core/trunk/src/tests/antunit/taskdefs/condition/resourcecontains-test.xml (original) +++ ant/core/trunk/src/tests/antunit/taskdefs/condition/resourcecontains-test.xml Mon May 14 09:14:43 2007 @@ -66,4 +66,61 @@ <resourcecontains resource="${file}" substring="futurama"/> </au:assertFalse> </target> + + <target name="testFileRefContains"> + <file id="file" file="${file}" /> + <au:assertTrue message="Should have found the text in the resource"> + <resourcecontains refid="file" substring="text"/> + </au:assertTrue> + </target> + + <target name="testStringRefContains"> + <string id="string">loads of text!</string> + <au:assertTrue message="Should have found the text in the resource"> + <resourcecontains refid="string" substring="text"/> + </au:assertTrue> + </target> + + <target name="testTextConcatRefContains"> + <resources id="concat"> + <concat>loads of text!</concat> + </resources> + <au:assertTrue message="Should have found the text in the resource"> + <resourcecontains refid="concat" substring="text"/> + </au:assertTrue> + </target> + + <target name="testFileConcatRefContains"> + <resources id="concat"> + <concat><file file="${file}" /></concat> + </resources> + <au:assertTrue message="Should have found the text in the resource"> + <resourcecontains refid="concat" substring="text"/> + </au:assertTrue> + </target> + + <target name="testMultiConcatRefContains"> + <resources id="concat"> + <concat> + <header>HEADER</header> + <footer>FOOTER</footer> + <string>foo</string> + <file file="${file}" /> + <string>bar</string> + </concat> + </resources> + <au:assertTrue message="Should have found the text in the resource"> + <resourcecontains refid="concat" substring="text"/> + </au:assertTrue> + </target> + + <target name="testFirstRefContains"> + <first id="first"> + <fileset dir="${basedir}" includes="*-test.xml" /> + </first> + <au:assertTrue message="Should have found the text in the resource"> + <resourcecontains refid="first" substring="project"/> + </au:assertTrue> + </target> + </project> --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]