Browse Source

Allow Condition as if and unless attributes of targets and extension points (Java API only)

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@1367306 13f79535-47bb-0310-9956-ffa450edef68
master
Nicolas Lalevee 13 years ago
parent
commit
1885612e5d
1 changed files with 47 additions and 6 deletions
  1. +47
    -6
      src/main/org/apache/tools/ant/Target.java

+ 47
- 6
src/main/org/apache/tools/ant/Target.java View File

@@ -26,6 +26,7 @@ import java.util.List;
import java.util.StringTokenizer;

import org.apache.tools.ant.property.LocalProperties;
import org.apache.tools.ant.taskdefs.condition.Condition;

/**
* Class to implement a target object with required parameters.
@@ -45,6 +46,10 @@ public class Target implements TaskContainer {
/** The "unless" condition to test on execution. */
private String unlessCondition = "";

private Condition if_;

private Condition unless;

/** List of targets this target is dependent on. */
private List/*<String>*/ dependencies = null;

@@ -73,6 +78,8 @@ public class Target implements TaskContainer {
this.name = other.name;
this.ifCondition = other.ifCondition;
this.unlessCondition = other.unlessCondition;
this.if_ = other.if_;
this.unless = other.unless;
this.dependencies = other.dependencies;
this.location = other.location;
this.project = other.project;
@@ -292,6 +299,15 @@ public class Target implements TaskContainer {
return "".equals(ifCondition) ? null : ifCondition;
}

/**
* Same as {@link #setIf(String)} but requires a {@link Condition} instance
*
* @since 1.9
*/
public void setIf(Condition if_) {
this.if_ = if_;
}

/**
* Sets the "unless" condition to test on execution. This is the
* name of a property to test for existence - if the property
@@ -320,6 +336,15 @@ public class Target implements TaskContainer {
return "".equals(unlessCondition) ? null : unlessCondition;
}

/**
* Same as {@link #setUnless(String)} but requires a {@link Condition} instance
*
* @since 1.9
*/
public void setUnless(Condition unless) {
this.unless = unless;
}

/**
* Sets the description of this target.
*
@@ -450,32 +475,48 @@ public class Target implements TaskContainer {
}

/**
* Tests whether or not the "if" condition allows the execution of this target.
* Tests whether or not the "if" conditions (via String AND Condition)
* allows the execution of this target.
*
* @return whether or not the "if" condition is satisfied. If no
* @return whether or not both "if" conditions are satisfied. If no
* condition (or an empty condition) has been set,
* <code>true</code> is returned.
*
* @see #setIf(String)
* @see #setIf(Condition)
*/
private boolean testIfAllows() {
PropertyHelper propertyHelper = PropertyHelper.getPropertyHelper(getProject());
Object o = propertyHelper.parseProperties(ifCondition);
return propertyHelper.testIfCondition(o);
if (!propertyHelper.testIfCondition(o)) {
return false;
}
if (if_ != null && !if_.eval()) {
return false;
}
return true;
}

/**
* Tests whether or not the "unless" condition allows the execution of this target.
* Tests whether or not the "unless" conditions (via String AND Condition)
* allows the execution of this target.
*
* @return whether or not the "unless" condition is satisfied. If no
* @return whether or not both "unless" condition are satisfied. If no
* condition (or an empty condition) has been set,
* <code>true</code> is returned.
*
* @see #setUnless(String)
* @see #setUnless(Condition)
*/
private boolean testUnlessAllows() {
PropertyHelper propertyHelper = PropertyHelper.getPropertyHelper(getProject());
Object o = propertyHelper.parseProperties(unlessCondition);
return propertyHelper.testUnlessCondition(o);
if (!propertyHelper.testUnlessCondition(o)) {
return false;
}
if (unless != null && unless.eval()) {
return false;
}
return true;
}
}

Loading…
Cancel
Save