From e25dc381ca8cd1fa1bb9548e7e21aad143e9a9a0 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Thu, 17 Sep 2009 11:59:26 +0000 Subject: [PATCH] implement a new ${ant.refid:} syntax to expand project references as properties git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@816153 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 17 +++ docs/manual/using.html | 25 +++++ .../org/apache/tools/ant/PropertyHelper.java | 15 +++ .../antunit/core/ref-propertyhelper-test.xml | 104 ++++++++++++++++++ 4 files changed, 161 insertions(+) create mode 100644 src/tests/antunit/core/ref-propertyhelper-test.xml diff --git a/WHATSNEW b/WHATSNEW index 8375ecf8c..abf634bd4 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -156,6 +156,23 @@ Changes that could break older environments: adapt (by overriding the new two-arg processFile method). Bugzilla Report 23243. + * A new property syntax can be used to set attributes from + references: ${ant.ref:some-reference} + + In most cases this will yield the exact same result as + ${toString:some-reference} - only when an attribute setter method + accepts an object type other than string and the project's + reference is an Object of matching type the new syntax will pass in + that object. + + If your build file already contains properties whose name starts + with "ant.ref:" there is a potential for collision. If your + property has been set, normal property expansion will take + precedence over the new syntax. If the property has not been set + and a reference with the postfix of your property name exists + (i.e. in a very unlikely event) then the new syntax would yield a + different result (an expanded property) than Ant 1.7.1 did. + Fixed bugs: ----------- diff --git a/docs/manual/using.html b/docs/manual/using.html index c58832cc5..a8f87e785 100644 --- a/docs/manual/using.html +++ b/docs/manual/using.html @@ -804,6 +804,31 @@ For example, here is how to get a listing of the files in a fileset, There is no guarantee that external types provide meaningful information in such a situation

+

Getting the value of a Reference with + ${ant.refid:}

+ +

Any Ant type which has been declared with a reference can also be + used as a property by using the ${ant.refid:} + operation, with the name of the reference listed after + the ant.refid: text. The difference between this + operation and ${toString:} is + that ${ant.refid:} will expand to the referenced object + itself. In most circumstances the toString method will be invoked + anyway, for example if the ${ant.refid:} is surrounded + by other text.

+ +

This syntax is most useful when using a task with attribute setters + that accept objects other than String. For example if the setter + accepts a Resource object as in

+
+public void setAttr(Resource r) { ... }
+
+

then the syntax can be used to pass in resource subclasses + preciously defined as references like

+
+  <url url="http://ant.apache.org/" id="anturl"/>
+  <my:task attr="${ant.refid:anturl}"/>
+

Use of external tasks

Ant supports a plugin mechanism for using third party tasks. For using them you diff --git a/src/main/org/apache/tools/ant/PropertyHelper.java b/src/main/org/apache/tools/ant/PropertyHelper.java index 133475bc8..c436657e9 100644 --- a/src/main/org/apache/tools/ant/PropertyHelper.java +++ b/src/main/org/apache/tools/ant/PropertyHelper.java @@ -220,6 +220,20 @@ public class PropertyHelper implements GetProperty { } }; + /** + * @since Ant 1.8.0 + */ + private static final PropertyEvaluator FROM_REF = new PropertyEvaluator() { + private final String PREFIX = "ant.refid:"; + private final int PREFIX_LEN = PREFIX.length(); + + public Object evaluate(String prop, PropertyHelper helper) { + return prop.startsWith(PREFIX) && helper.getProject() != null + ? helper.getProject().getReference(prop.substring(PREFIX_LEN)) + : null; + } + }; + private Project project; private PropertyHelper next; private Hashtable delegates = new Hashtable(); @@ -245,6 +259,7 @@ public class PropertyHelper implements GetProperty { * Default constructor. */ protected PropertyHelper() { + add(FROM_REF); add(TO_STRING); add(SKIP_DOUBLE_DOLLAR); add(DEFAULT_EXPANDER); diff --git a/src/tests/antunit/core/ref-propertyhelper-test.xml b/src/tests/antunit/core/ref-propertyhelper-test.xml new file mode 100644 index 000000000..25f398e41 --- /dev/null +++ b/src/tests/antunit/core/ref-propertyhelper-test.xml @@ -0,0 +1,104 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +