Browse Source

make resourcecontains handle refids

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@537899 13f79535-47bb-0310-9956-ffa450edef68
master
Matthew Jason Benson 18 years ago
parent
commit
bc0012c1a3
2 changed files with 107 additions and 7 deletions
  1. +50
    -7
      src/main/org/apache/tools/ant/taskdefs/condition/ResourceContains.java
  2. +57
    -0
      src/tests/antunit/taskdefs/condition/resourcecontains-test.xml

+ 50
- 7
src/main/org/apache/tools/ant/taskdefs/condition/ResourceContains.java View File

@@ -25,6 +25,7 @@ import java.io.InputStreamReader;
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 @@ public class ResourceContains implements Condition {
private Project project;
private String substring;
private Resource resource;
private String refid;
private boolean casesensitive = true;
/**
@@ -64,6 +66,38 @@ public class ResourceContains implements Condition {
this.resource = new FileResource(new File(r));
}
/**
* 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 @@ public class ResourceContains implements Condition {
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;
}


+ 57
- 0
src/tests/antunit/taskdefs/condition/resourcecontains-test.xml View File

@@ -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>

Loading…
Cancel
Save