From f67b8649e8b40f489ea4091276cfffb010beae99 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Tue, 29 Jun 2010 16:16:41 +0000 Subject: [PATCH] reinstate prefix handling of property task at the expense of even more state in a thread-unfriendly class and with a new attribute on two tasks to make everybody happy. PR 49373. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@959019 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 8 +++ docs/manual/Tasks/loadproperties.html | 16 +++-- docs/manual/Tasks/property.html | 18 ++++- src/main/org/apache/tools/ant/Main.java | 2 +- .../ant/property/ResolvePropertyMap.java | 70 ++++++++++++++----- .../tools/ant/taskdefs/LoadProperties.java | 12 ++++ .../apache/tools/ant/taskdefs/Property.java | 23 +++++- src/tests/antunit/taskdefs/property-test.xml | 6 +- 8 files changed, 127 insertions(+), 28 deletions(-) diff --git a/WHATSNEW b/WHATSNEW index 81be81546..e265afec3 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -32,6 +32,14 @@ Changes that could break older environments: onMissingExtensionPoint attribute. Bugzilla Report 49473. + * When using properties defined + inside the same file will only get used in expansions if the ${} + reference uses the same prefix. This is different from Ant 1.8.1 + but is the same behavior Ant 1.8.0 and earlier exhibited. + A new attribute prefixValues can be used to re-enable the behavior + of Ant 1.8.1. + Bugzilla Report 49373. + Fixed bugs: ----------- diff --git a/docs/manual/Tasks/loadproperties.html b/docs/manual/Tasks/loadproperties.html index 0c115dfc9..8b8ca5c99 100644 --- a/docs/manual/Tasks/loadproperties.html +++ b/docs/manual/Tasks/loadproperties.html @@ -33,10 +33,11 @@ Also if the file is missing, the build is halted with an error, rather than a warning being printed.

-

If you want to simulate property's -prefix attribute, please use prefixlines -filter.

+

Note: the default value of this +task's prefixValues attribute is different from the +default value of the same attribute in +the <property> +task.

Parameters

@@ -78,6 +79,13 @@ filter.

a "." is appended to the prefix if not specified. Since Ant 1.8.1 + + + + +
No
prefixValuesWhether to apply the prefix when expanding the + right hand side of the properties. + Since Ant 1.8.2No (default=true)

Parameters specified as nested elements

diff --git a/docs/manual/Tasks/property.html b/docs/manual/Tasks/property.html index 74bf7e550..b89844f94 100644 --- a/docs/manual/Tasks/property.html +++ b/docs/manual/Tasks/property.html @@ -154,6 +154,14 @@ SYSTEM). A "." is appended to the prefix if not specified. No + + prefixValues + Whether to apply the prefix when expanding the + right hand side of properties loaded using file, + resource, or url. + Since Ant 1.8.2 + No (default=false) + relative If set to true the relative path @@ -198,7 +206,7 @@ to be your home directory. Where the "user.home" property resolves to the file system depends on the operating system version and the JVM implementation. On Unix based systems, this will map to the user's home directory. On modern Windows variants, this will most likely resolve to the user's directory in the "Documents -and Settings" folder. Older windows variants such as Windows 98/ME are less +and Settings" or "Users" folder. Older windows variants such as Windows 98/ME are less predictable, as are other operating system/JVM combinations.

@@ -247,8 +255,12 @@ project, that team members can customize.
 by Sun.
 This makes it hard for Team Ant to field bug reports about it.
 
  • Trailing spaces are not stripped. It may have been what you wanted. -
  • Want unusual characters? Escape them \u0456 or \" style. -
  • Ant Properties are expanded in the file. +
  • Want unusual characters? Escape them \u0456 or \" style. +
  • Ant Properties are expanded in the file +
  • If you want to expand properties defined inside the same file and + you use the prefix attribute of the task, you must use the same + prefix when expanding the properties or + set prefixValues to true.
  • In-file property expansion is very cool. Learn to use it.

    diff --git a/src/main/org/apache/tools/ant/Main.java b/src/main/org/apache/tools/ant/Main.java index 9ffb2629e..ec50d96e6 100644 --- a/src/main/org/apache/tools/ant/Main.java +++ b/src/main/org/apache/tools/ant/Main.java @@ -769,7 +769,7 @@ public class Main implements AntMain { HashMap props = new HashMap(definedProps); new ResolvePropertyMap(project, propertyHelper, propertyHelper.getExpanders()) - .resolveAllProperties(props, null); + .resolveAllProperties(props, null, false); // set user-define properties for (Iterator e = props.entrySet().iterator(); e.hasNext(); ) { diff --git a/src/main/org/apache/tools/ant/property/ResolvePropertyMap.java b/src/main/org/apache/tools/ant/property/ResolvePropertyMap.java index ea0caf6e9..c9fdecbc8 100644 --- a/src/main/org/apache/tools/ant/property/ResolvePropertyMap.java +++ b/src/main/org/apache/tools/ant/property/ResolvePropertyMap.java @@ -36,6 +36,12 @@ public class ResolvePropertyMap implements GetProperty { private final GetProperty master; private Map map; private String prefix; + // whether properties of the value side of the map should be + // expanded + private boolean prefixValues = false; + // whether the current getProperty call is expanding the key side + // of the map + private boolean expandingLHS = true; /** * Constructor with a master getproperty and a collection of expanders. @@ -59,19 +65,20 @@ public class ResolvePropertyMap implements GetProperty { "Property " + name + " was circularly " + "defined."); } - // if the property has already been set to the name it will - // have in the end, then return the existing value to ensure - // properties remain immutable - String masterPropertyName = name; - if (prefix != null) { - masterPropertyName = prefix + name; - } - Object masterProperty = master.getProperty(masterPropertyName); - if (masterProperty != null) { - return masterProperty; - } - try { + String fullKey = name; + if (prefix != null && prefixValues) { + fullKey = prefix + name; + } + Object masterValue = expandingLHS + ? null : master.getProperty(fullKey); + // if the property is defined outside of this map don't + // consult the map at all. + if (masterValue != null) { + return masterValue; + } + + expandingLHS = false; seen.add(name); return parseProperties.parseProperties((String) map.get(name)); } finally { @@ -82,10 +89,10 @@ public class ResolvePropertyMap implements GetProperty { /** * The action method - resolves all the properties in a map. * @param map the map to resolve properties in. - * @deprecated since Ant 1.8.1, use the two-arg method instead. + * @deprecated since Ant 1.8.2, use the three-arg method instead. */ public void resolveAllProperties(Map map) { - resolveAllProperties(map, null); + resolveAllProperties(map, null, false); } /** @@ -93,13 +100,44 @@ public class ResolvePropertyMap implements GetProperty { * @param map the map to resolve properties in. * @param prefix the prefix the properties defined inside the map * will finally receive - may be null. + * @deprecated since Ant 1.8.2, use the three-arg method instead. */ public void resolveAllProperties(Map map, String prefix) { - this.map = map; // The map gets used in the getProperty callback + resolveAllProperties(map, null, false); + } + + /** + * The action method - resolves all the properties in a map. + * @param map the map to resolve properties in. + * @param prefix the prefix the properties defined inside the map + * will finally receive - may be null. + * @param prefixValues - whether the prefix will be applied + * to properties on the value side of the map as well. + */ + public void resolveAllProperties(Map map, String prefix, + boolean prefixValues) { + // The map, prefix and prefixValues flag get used in the + // getProperty callback + this.map = map; this.prefix = prefix; + this.prefixValues = prefixValues; + for (Iterator i = map.keySet().iterator(); i.hasNext();) { + expandingLHS = true; String key = (String) i.next(); - Object result = getProperty(key); + + // if the property has already been set to the name it + // will have in the end, then return the existing value to + // ensure properties remain immutable + String fullKey = key; + if (prefix != null) { + fullKey = prefix + key; + } + Object result = master.getProperty(fullKey); + + if (result == null) { + result = getProperty(key); + } String value = result == null ? "" : result.toString(); map.put(key, value); } diff --git a/src/main/org/apache/tools/ant/taskdefs/LoadProperties.java b/src/main/org/apache/tools/ant/taskdefs/LoadProperties.java index 044cc0f87..65092c71f 100644 --- a/src/main/org/apache/tools/ant/taskdefs/LoadProperties.java +++ b/src/main/org/apache/tools/ant/taskdefs/LoadProperties.java @@ -66,6 +66,7 @@ public class LoadProperties extends Task { * Prefix for loaded properties. */ private String prefix = null; + private boolean prefixValues = true; /** * Set the file to load. @@ -141,6 +142,16 @@ public class LoadProperties extends Task { this.prefix = prefix; } + /** + * Whether to apply the prefix when expanding properties on the + * right hand side of a properties file as well. + * + * @since Ant 1.8.2 + */ + public void setPrefixValues(boolean b) { + prefixValues = b; + } + /** * load Ant properties from the source file or resource * @@ -189,6 +200,7 @@ public class LoadProperties extends Task { Property propertyTask = new Property(); propertyTask.bindToOwner(this); propertyTask.setPrefix(prefix); + propertyTask.setPrefixValues(prefixValues); propertyTask.addProperties(props); } } catch (final IOException ioe) { diff --git a/src/main/org/apache/tools/ant/taskdefs/Property.java b/src/main/org/apache/tools/ant/taskdefs/Property.java index b0363781b..67dada022 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Property.java +++ b/src/main/org/apache/tools/ant/taskdefs/Property.java @@ -94,6 +94,7 @@ public class Property extends Task { private boolean valueAttributeUsed = false; private boolean relative = false; private File basedir; + private boolean prefixValues = false; protected boolean userProperty; // set read-only properties // CheckStyle:VisibilityModifier ON @@ -293,6 +294,26 @@ public class Property extends Task { return prefix; } + /** + * Whether to apply the prefix when expanding properties on the + * right hand side of a properties file as well. + * + * @since Ant 1.8.2 + */ + public void setPrefixValues(boolean b) { + prefixValues = b; + } + + /** + * Whether to apply the prefix when expanding properties on the + * right hand side of a properties file as well. + * + * @since Ant 1.8.2 + */ + public boolean getPrefixValues() { + return prefixValues; + } + /** * Sets a reference to an Ant datatype * declared elsewhere. @@ -716,7 +737,7 @@ public class Property extends Task { getProject(), propertyHelper, propertyHelper.getExpanders()) - .resolveAllProperties(props, prefix); + .resolveAllProperties(props, getPrefix(), getPrefixValues()); } } diff --git a/src/tests/antunit/taskdefs/property-test.xml b/src/tests/antunit/taskdefs/property-test.xml index e23a9f1af..1697e7776 100644 --- a/src/tests/antunit/taskdefs/property-test.xml +++ b/src/tests/antunit/taskdefs/property-test.xml @@ -93,7 +93,7 @@ y=$${x} ]]> - + - - +