Author: mbenson
Date: Thu Feb 25 04:36:20 2010
New Revision: 916121

URL: http://svn.apache.org/viewvc?rev=916121&view=rev
Log:
PropertyResource will effectively proxy another Resource if ${name} evaluates 
to a Resource object.

Modified:
    ant/core/trunk/WHATSNEW
    
ant/core/trunk/src/main/org/apache/tools/ant/types/resources/PropertyResource.java
    ant/core/trunk/src/tests/antunit/types/resources/test.xml

Modified: ant/core/trunk/WHATSNEW
URL: 
http://svn.apache.org/viewvc/ant/core/trunk/WHATSNEW?rev=916121&r1=916120&r2=916121&view=diff
==============================================================================
--- ant/core/trunk/WHATSNEW (original)
+++ ant/core/trunk/WHATSNEW Thu Feb 25 04:36:20 2010
@@ -58,6 +58,9 @@
 
  * Add resource attribute to length task.
 
+ * PropertyResource will effectively proxy another Resource if ${name}
+   evaluates to a Resource object.
+
 Changes from Ant 1.8.0RC1 TO Ant 1.8.0
 ======================================
 

Modified: 
ant/core/trunk/src/main/org/apache/tools/ant/types/resources/PropertyResource.java
URL: 
http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/types/resources/PropertyResource.java?rev=916121&r1=916120&r2=916121&view=diff
==============================================================================
--- 
ant/core/trunk/src/main/org/apache/tools/ant/types/resources/PropertyResource.java
 (original)
+++ 
ant/core/trunk/src/main/org/apache/tools/ant/types/resources/PropertyResource.java
 Thu Feb 25 04:36:20 2010
@@ -24,6 +24,7 @@
 import java.io.ByteArrayInputStream;
 
 import org.apache.tools.ant.Project;
+import org.apache.tools.ant.PropertyHelper;
 import org.apache.tools.ant.types.Resource;
 import org.apache.tools.ant.util.PropertyOutputStream;
 
@@ -64,16 +65,35 @@
      * @return the value of the specified Property.
      */
     public String getValue() {
+        if (isReference()) {
+            return ((PropertyResource) getCheckedRef()).getValue();
+        }
         Project p = getProject();
         return p == null ? null : p.getProperty(getName());
     }
 
     /**
+     * Get the Object value of this PropertyResource.
+     * @return the Object value of the specified Property.
+     * @since Ant 1.8.1
+     */
+    public Object getObjectValue() {
+        if (isReference()) {
+            return ((PropertyResource) getCheckedRef()).getObjectValue();
+        }
+        Project p = getProject();
+        return p == null ? null : PropertyHelper.getProperty(p, getName());
+    }
+
+    /**
      * Find out whether this Resource exists.
      * @return true if the Property is set, false otherwise.
      */
     public boolean isExists() {
-        return getValue() != null;
+        if (isReferenceOrProxy()) {
+            return getReferencedOrProxied().isExists();
+        }
+        return getObjectValue() != null;
     }
 
     /**
@@ -82,10 +102,24 @@
      *         compatibility with java.io.File), or UNKNOWN_SIZE if not known.
      */
     public long getSize() {
-        if (isReference()) {
-            return ((Resource) getCheckedRef()).getSize();
+        if (isReferenceOrProxy()) {
+            return getReferencedOrProxied().getSize();
+        }
+        Object o = getObjectValue();
+        return o == null ? 0L : (long) String.valueOf(o).length();
+    }
+
+    /**
+     * Override to implement equality with equivalent Resources,
+     * since we are capable of proxying them.
+     * @param o object to compare
+     * @return true if equal to o
+     */
+    public boolean equals(Object o) {
+        if (super.equals(o)) {
+            return true;
         }
-        return isExists() ? (long) getValue().length() : 0L;
+        return isReferenceOrProxy() && getReferencedOrProxied().equals(o);
     }
 
     /**
@@ -93,23 +127,20 @@
      * @return hash code as int.
      */
     public int hashCode() {
-        if (isReference()) {
-            return getCheckedRef().hashCode();
+        if (isReferenceOrProxy()) {
+            return getReferencedOrProxied().hashCode();
         }
         return super.hashCode() * PROPERTY_MAGIC;
     }
 
     /**
-     * Get the string.
-     *
-     * @return the string contents of the resource.
-     * @since Ant 1.7
+     * {...@inheritdoc}
      */
     public String toString() {
-        if (isReference()) {
-            return getCheckedRef().toString();
+        if (isReferenceOrProxy()) {
+            return getReferencedOrProxied().toString();
         }
-        return String.valueOf(getValue());
+        return getValue();
     }
 
     /**
@@ -121,10 +152,11 @@
      *         supported for this Resource type.
      */
     public InputStream getInputStream() throws IOException {
-        if (isReference()) {
-            return ((Resource) getCheckedRef()).getInputStream();
+        if (isReferenceOrProxy()) {
+            return getReferencedOrProxied().getInputStream();
         }
-        return isExists() ? new ByteArrayInputStream(getValue().getBytes()) : 
UNSET;
+        Object o = getObjectValue();
+        return o == null ? UNSET : new 
ByteArrayInputStream(String.valueOf(o).getBytes());
     }
 
     /**
@@ -136,8 +168,8 @@
      *         supported for this Resource type.
      */
     public OutputStream getOutputStream() throws IOException {
-        if (isReference()) {
-            return ((Resource) getCheckedRef()).getOutputStream();
+        if (isReferenceOrProxy()) {
+            return getReferencedOrProxied().getOutputStream();
         }
         if (isExists()) {
             throw new ImmutableResourceException();
@@ -145,4 +177,30 @@
         return new PropertyOutputStream(getProject(), getName());
     }
 
+    /**
+     * Learn whether this PropertyResource either refers to another Resource
+     * or proxies another Resource due to its object property value being said 
Resource.
+     * @return boolean
+     */
+    protected boolean isReferenceOrProxy() {
+        return isReference() || getObjectValue() instanceof Resource;
+    }
+
+    /**
+     * Get the referenced or proxied Resource, if applicable.
+     * @return Resource
+     * @throws IllegalStateException if this PropertyResource neither proxies 
nor
+     *                               references another Resource.
+     */
+    protected Resource getReferencedOrProxied() {
+        if (isReference()) {
+            return (Resource) getCheckedRef(Resource.class, "resource");
+        }
+        Object o = getObjectValue();
+        if (o instanceof Resource) {
+            return (Resource) o;
+        }
+        throw new IllegalStateException(
+                "This PropertyResource does not reference or proxy another 
Resource");
+    }
 }

Modified: ant/core/trunk/src/tests/antunit/types/resources/test.xml
URL: 
http://svn.apache.org/viewvc/ant/core/trunk/src/tests/antunit/types/resources/test.xml?rev=916121&r1=916120&r2=916121&view=diff
==============================================================================
--- ant/core/trunk/src/tests/antunit/types/resources/test.xml (original)
+++ ant/core/trunk/src/tests/antunit/types/resources/test.xml Thu Feb 25 
04:36:20 2010
@@ -15,9 +15,11 @@
   See the License for the specific language governing permissions and
   limitations under the License.
 -->
-<project default="all" xmlns:au="antlib:org.apache.ant.antunit"
+<project default="antunit" xmlns:au="antlib:org.apache.ant.antunit"
          xmlns:rsel="antlib:org.apache.tools.ant.types.resources.selectors">
 
+  <import file="../../antunit-base.xml" />
+
   <property name="dirname" value="work" />
   <property name="dir" location="${dirname}" />
   <property name="zip" location="${dirname}.zip" />
@@ -351,6 +353,16 @@
     </au:assertTrue>
   </target>
 
+  <target name="testPropertyResolvedAsResource">
+    <string id="s" value="abcdefghij" />
+    <au:assertTrue>
+      <resourcesmatch>
+        <resource refid="s" />
+        <propertyresource name="ant.refid:s" />
+      </resourcesmatch>
+    </au:assertTrue>
+  </target>
+
   <target name="testfirst0">
     <au:assertTrue>
       <resourcecount count="0">


Reply via email to