Browse Source

PropertyResource will effectively proxy another Resource if ${name} evaluates to a Resource object.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@916121 13f79535-47bb-0310-9956-ffa450edef68
master
Matthew Jason Benson 15 years ago
parent
commit
0a8c19b47e
3 changed files with 92 additions and 19 deletions
  1. +3
    -0
      WHATSNEW
  2. +76
    -18
      src/main/org/apache/tools/ant/types/resources/PropertyResource.java
  3. +13
    -1
      src/tests/antunit/types/resources/test.xml

+ 3
- 0
WHATSNEW View File

@@ -58,6 +58,9 @@ Other changes:

* 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
======================================



+ 76
- 18
src/main/org/apache/tools/ant/types/resources/PropertyResource.java View File

@@ -24,6 +24,7 @@ import java.io.OutputStream;
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 @@ public class PropertyResource extends Resource {
* @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 @@ public class PropertyResource extends Resource {
* 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 @@ public class PropertyResource extends Resource {
* @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 @@ public class PropertyResource extends Resource {
* 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 @@ public class PropertyResource extends Resource {
* 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 @@ public class PropertyResource extends Resource {
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");
}
}

+ 13
- 1
src/tests/antunit/types/resources/test.xml View File

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


Loading…
Cancel
Save