git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@557097 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -22,6 +22,7 @@ import java.io.File; | |||||
| import org.apache.tools.ant.AntClassLoader; | import org.apache.tools.ant.AntClassLoader; | ||||
| import org.apache.tools.ant.BuildException; | import org.apache.tools.ant.BuildException; | ||||
| import org.apache.tools.ant.Project; | import org.apache.tools.ant.Project; | ||||
| import org.apache.tools.ant.PropertyHelper; | |||||
| import org.apache.tools.ant.Task; | import org.apache.tools.ant.Task; | ||||
| import org.apache.tools.ant.taskdefs.condition.Condition; | import org.apache.tools.ant.taskdefs.condition.Condition; | ||||
| import org.apache.tools.ant.types.EnumeratedAttribute; | import org.apache.tools.ant.types.EnumeratedAttribute; | ||||
| @@ -50,7 +51,7 @@ public class Available extends Task implements Condition { | |||||
| private FileDir type; | private FileDir type; | ||||
| private Path classpath; | private Path classpath; | ||||
| private AntClassLoader loader; | private AntClassLoader loader; | ||||
| private String value = "true"; | |||||
| private Object value = "true"; | |||||
| private boolean isTask = false; | private boolean isTask = false; | ||||
| private boolean ignoreSystemclasses = false; | private boolean ignoreSystemclasses = false; | ||||
| private boolean searchParents = false; | private boolean searchParents = false; | ||||
| @@ -136,10 +137,20 @@ public class Available extends Task implements Condition { | |||||
| * | * | ||||
| * @param value the value to be given. | * @param value the value to be given. | ||||
| */ | */ | ||||
| public void setValue(String value) { | |||||
| public void setValue(Object value) { | |||||
| this.value = value; | this.value = value; | ||||
| } | } | ||||
| /** | |||||
| * Set the value to be given to the property if the desired resource is | |||||
| * available. | |||||
| * | |||||
| * @param value the value to be given. | |||||
| */ | |||||
| public void setValue(String value) { | |||||
| setValue((Object) value); | |||||
| } | |||||
| /** | /** | ||||
| * Set a classname of a class which must be available to set the given | * Set a classname of a class which must be available to set the given | ||||
| * property. | * property. | ||||
| @@ -223,7 +234,8 @@ public class Available extends Task implements Condition { | |||||
| isTask = true; | isTask = true; | ||||
| try { | try { | ||||
| if (eval()) { | if (eval()) { | ||||
| String oldvalue = getProject().getProperty(property); | |||||
| PropertyHelper ph = PropertyHelper.getPropertyHelper(getProject()); | |||||
| Object oldvalue = ph.getProperty(property); | |||||
| if (null != oldvalue && !oldvalue.equals(value)) { | if (null != oldvalue && !oldvalue.equals(value)) { | ||||
| log("DEPRECATED - <available> used to override an existing" | log("DEPRECATED - <available> used to override an existing" | ||||
| + " property." | + " property." | ||||
| @@ -234,7 +246,7 @@ public class Available extends Task implements Condition { | |||||
| } | } | ||||
| // NB: this makes use of Project#setProperty rather than Project#setNewProperty | // NB: this makes use of Project#setProperty rather than Project#setNewProperty | ||||
| // due to backwards compatiblity reasons | // due to backwards compatiblity reasons | ||||
| getProject().setProperty(property, value); | |||||
| ph.setProperty(property, value, true); | |||||
| } | } | ||||
| } finally { | } finally { | ||||
| isTask = false; | isTask = false; | ||||
| @@ -15,11 +15,11 @@ | |||||
| * limitations under the License. | * limitations under the License. | ||||
| * | * | ||||
| */ | */ | ||||
| package org.apache.tools.ant.taskdefs; | package org.apache.tools.ant.taskdefs; | ||||
| import org.apache.tools.ant.BuildException; | import org.apache.tools.ant.BuildException; | ||||
| import org.apache.tools.ant.Project; | import org.apache.tools.ant.Project; | ||||
| import org.apache.tools.ant.PropertyHelper; | |||||
| import org.apache.tools.ant.taskdefs.condition.Condition; | import org.apache.tools.ant.taskdefs.condition.Condition; | ||||
| import org.apache.tools.ant.taskdefs.condition.ConditionBase; | import org.apache.tools.ant.taskdefs.condition.ConditionBase; | ||||
| @@ -40,8 +40,8 @@ import org.apache.tools.ant.taskdefs.condition.ConditionBase; | |||||
| public class ConditionTask extends ConditionBase { | public class ConditionTask extends ConditionBase { | ||||
| private String property = null; | private String property = null; | ||||
| private String value = "true"; | |||||
| private String alternative = null; | |||||
| private Object value = "true"; | |||||
| private Object alternative = null; | |||||
| /** | /** | ||||
| * Constructor, names this task "condition". | * Constructor, names this task "condition". | ||||
| @@ -59,6 +59,16 @@ public class ConditionTask extends ConditionBase { | |||||
| property = p; | property = p; | ||||
| } | } | ||||
| /** | |||||
| * The value for the property to set, if condition evaluates to true. | |||||
| * Defaults to "true". | |||||
| * @param v the (Object) value of the property | |||||
| * @since Ant 1.8 | |||||
| */ | |||||
| public void setValue(Object value) { | |||||
| this.value = value; | |||||
| } | |||||
| /** | /** | ||||
| * The value for the property to set, if condition evaluates to true. | * The value for the property to set, if condition evaluates to true. | ||||
| * Defaults to "true". | * Defaults to "true". | ||||
| @@ -66,7 +76,17 @@ public class ConditionTask extends ConditionBase { | |||||
| * @since Ant 1.4 | * @since Ant 1.4 | ||||
| */ | */ | ||||
| public void setValue(String v) { | public void setValue(String v) { | ||||
| value = v; | |||||
| setValue((Object) v); | |||||
| } | |||||
| /** | |||||
| * The value for the property to set, if condition evaluates to false. | |||||
| * If this attribute is not specified, the property will not be set. | |||||
| * @param e the alternate value of the property. | |||||
| * @since Ant 1.8 | |||||
| */ | |||||
| public void setElse(Object alt) { | |||||
| alternative = alt; | |||||
| } | } | ||||
| /** | /** | ||||
| @@ -76,7 +96,7 @@ public class ConditionTask extends ConditionBase { | |||||
| * @since Ant 1.6.3 | * @since Ant 1.6.3 | ||||
| */ | */ | ||||
| public void setElse(String e) { | public void setElse(String e) { | ||||
| alternative = e; | |||||
| setElse((Object) e); | |||||
| } | } | ||||
| /** | /** | ||||
| @@ -87,29 +107,24 @@ public class ConditionTask extends ConditionBase { | |||||
| */ | */ | ||||
| public void execute() throws BuildException { | public void execute() throws BuildException { | ||||
| if (countConditions() > 1) { | if (countConditions() > 1) { | ||||
| throw new BuildException("You must not nest more than one " | |||||
| + "condition into <" | |||||
| + getTaskName() + ">"); | |||||
| throw new BuildException("You must not nest more than one condition into <" | |||||
| + getTaskName() + ">"); | |||||
| } | } | ||||
| if (countConditions() < 1) { | if (countConditions() < 1) { | ||||
| throw new BuildException("You must nest a condition into <" | |||||
| + getTaskName() + ">"); | |||||
| throw new BuildException("You must nest a condition into <" + getTaskName() + ">"); | |||||
| } | } | ||||
| if (property == null) { | if (property == null) { | ||||
| throw new BuildException("The property attribute is required."); | throw new BuildException("The property attribute is required."); | ||||
| } | } | ||||
| Condition c = (Condition) getConditions().nextElement(); | Condition c = (Condition) getConditions().nextElement(); | ||||
| if (c.eval()) { | if (c.eval()) { | ||||
| log("Condition true; setting " + property + " to " + value, | |||||
| Project.MSG_DEBUG); | |||||
| getProject().setNewProperty(property, value); | |||||
| log("Condition true; setting " + property + " to " + value, Project.MSG_DEBUG); | |||||
| PropertyHelper.getPropertyHelper(getProject()).setNewProperty(property, value); | |||||
| } else if (alternative != null) { | } else if (alternative != null) { | ||||
| log("Condition false; setting " + property + " to " + alternative, | |||||
| Project.MSG_DEBUG); | |||||
| getProject().setNewProperty(property, alternative); | |||||
| log("Condition false; setting " + property + " to " + alternative, Project.MSG_DEBUG); | |||||
| PropertyHelper.getPropertyHelper(getProject()).setNewProperty(property, alternative); | |||||
| } else { | } else { | ||||
| log("Condition false; not setting " + property, | |||||
| Project.MSG_DEBUG); | |||||
| log("Condition false; not setting " + property, Project.MSG_DEBUG); | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| @@ -21,15 +21,27 @@ package org.apache.tools.ant.taskdefs.condition; | |||||
| import org.apache.tools.ant.BuildException; | import org.apache.tools.ant.BuildException; | ||||
| /** | /** | ||||
| * Simple String comparison condition. | |||||
| * Simple comparison condition. | |||||
| * | * | ||||
| * @since Ant 1.4 | * @since Ant 1.4 | ||||
| */ | */ | ||||
| public class Equals implements Condition { | public class Equals implements Condition { | ||||
| private static final int REQUIRED = 1 | 2; | |||||
| private String arg1, arg2; | |||||
| private Object arg1, arg2; | |||||
| private boolean trim = false; | private boolean trim = false; | ||||
| private boolean caseSensitive = true; | private boolean caseSensitive = true; | ||||
| private int args; | |||||
| /** | |||||
| * Set the first argument | |||||
| * @param arg1 | |||||
| * @since Ant 1.8 | |||||
| */ | |||||
| public void setArg1(Object arg1) { | |||||
| this.arg1 = arg1; | |||||
| args |= 1; | |||||
| } | |||||
| /** | /** | ||||
| * Set the first string | * Set the first string | ||||
| @@ -37,7 +49,17 @@ public class Equals implements Condition { | |||||
| * @param a1 the first string | * @param a1 the first string | ||||
| */ | */ | ||||
| public void setArg1(String a1) { | public void setArg1(String a1) { | ||||
| arg1 = a1; | |||||
| setArg1((Object) a1); | |||||
| } | |||||
| /** | |||||
| * Set the second argument | |||||
| * @param arg2 | |||||
| * @since Ant 1.8 | |||||
| */ | |||||
| public void setArg2(Object arg2) { | |||||
| this.arg2 = arg2; | |||||
| args |= 2; | |||||
| } | } | ||||
| /** | /** | ||||
| @@ -46,7 +68,7 @@ public class Equals implements Condition { | |||||
| * @param a2 the second string | * @param a2 the second string | ||||
| */ | */ | ||||
| public void setArg2(String a2) { | public void setArg2(String a2) { | ||||
| arg2 = a2; | |||||
| setArg2((Object) a2); | |||||
| } | } | ||||
| /** | /** | ||||
| @@ -73,16 +95,19 @@ public class Equals implements Condition { | |||||
| * @exception BuildException if the attributes are not set correctly | * @exception BuildException if the attributes are not set correctly | ||||
| */ | */ | ||||
| public boolean eval() throws BuildException { | public boolean eval() throws BuildException { | ||||
| if (arg1 == null || arg2 == null) { | |||||
| throw new BuildException("both arg1 and arg2 are required in " | |||||
| + "equals"); | |||||
| if ((args & REQUIRED) != REQUIRED) { | |||||
| throw new BuildException("both arg1 and arg2 are required in equals"); | |||||
| } | } | ||||
| if (trim) { | |||||
| arg1 = arg1.trim(); | |||||
| arg2 = arg2.trim(); | |||||
| if (arg1 instanceof String && arg2 instanceof String) { | |||||
| String s1 = (String) arg1; | |||||
| String s2 = (String) arg2; | |||||
| if (trim) { | |||||
| s1 = s1.trim(); | |||||
| s2 = s2.trim(); | |||||
| } | |||||
| return caseSensitive ? s1.equals(s2) : s1.equalsIgnoreCase(s2); | |||||
| } | } | ||||
| return caseSensitive ? arg1.equals(arg2) : arg1.equalsIgnoreCase(arg2); | |||||
| return arg1 == arg2 || arg1 != null && arg1.equals(arg2); | |||||
| } | } | ||||
| } | } | ||||