From bc0012c1a3b49f75baf100584cc92869a9325432 Mon Sep 17 00:00:00 2001 From: Matthew Jason Benson Date: Mon, 14 May 2007 16:14:43 +0000 Subject: [PATCH] make resourcecontains handle refids git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@537899 13f79535-47bb-0310-9956-ffa450edef68 --- .../taskdefs/condition/ResourceContains.java | 57 ++++++++++++++++--- .../condition/resourcecontains-test.xml | 57 +++++++++++++++++++ 2 files changed, 107 insertions(+), 7 deletions(-) diff --git a/src/main/org/apache/tools/ant/taskdefs/condition/ResourceContains.java b/src/main/org/apache/tools/ant/taskdefs/condition/ResourceContains.java index b7e70bf20..d4e5d10a6 100644 --- a/src/main/org/apache/tools/ant/taskdefs/condition/ResourceContains.java +++ b/src/main/org/apache/tools/ant/taskdefs/condition/ResourceContains.java @@ -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 "); } + } + + /** + * 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; } diff --git a/src/tests/antunit/taskdefs/condition/resourcecontains-test.xml b/src/tests/antunit/taskdefs/condition/resourcecontains-test.xml index 2fe404952..b418c24a7 100644 --- a/src/tests/antunit/taskdefs/condition/resourcecontains-test.xml +++ b/src/tests/antunit/taskdefs/condition/resourcecontains-test.xml @@ -66,4 +66,61 @@ + + + + + + + + + + loads of text! + + + + + + + + loads of text! + + + + + + + + + + + + + + + + + + +
HEADER
+
FOOTER
+ foo + + bar +
+
+ + + +
+ + + + + + + + + +