Browse Source

use the same logic as target for xslt's param element's if/unless attribute

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@822118 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 16 years ago
parent
commit
48468d8995
2 changed files with 86 additions and 18 deletions
  1. +38
    -18
      src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java
  2. +48
    -0
      src/tests/antunit/taskdefs/xslt-test.xml

+ 38
- 18
src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java View File

@@ -26,6 +26,7 @@ import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.DynamicConfigurator;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.PropertyHelper;
import org.apache.tools.ant.types.CommandlineJava;
import org.apache.tools.ant.types.Environment;
import org.apache.tools.ant.types.Mapper;
@@ -947,8 +948,8 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger {
/** The parameter's value */
private String expression = null;

private String ifProperty;
private String unlessProperty;
private Object ifCond;
private Object unlessCond;
private Project project;

/**
@@ -1005,22 +1006,45 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger {
}

/**
* Set whether this param should be used. It will be
* used if the property has been set, otherwise it won't.
* @param ifProperty name of property
* Set whether this param should be used. It will be used if
* the expression evalutes to true or the name of a property
* which has been set, otherwise it won't.
* @param ifCond evaluated expression
* @since Ant 1.8.0
*/
public void setIf(Object ifCond) {
this.ifCond = ifCond;
}

/**
* Set whether this param should be used. It will be used if
* the expression evalutes to true or the name of a property
* which has been set, otherwise it won't.
* @param ifProperty evaluated expression
*/
public void setIf(String ifProperty) {
this.ifProperty = ifProperty;
setIf((Object) ifProperty);
}

/**
* Set whether this param should NOT be used. It will not be
* used if the expression evaluates to true or the name of a
* property which has been set, otherwise it will be used.
* @param unlessCond evaluated expression
* @since Ant 1.8.0
*/
public void setUnless(Object unlessCond) {
this.unlessCond = unlessCond;
}

/**
* Set whether this param should NOT be used. It
* will not be used if the property has been set, otherwise it
* will be used.
* @param unlessProperty name of property
* Set whether this param should NOT be used. It will not be
* used if the expression evaluates to true or the name of a
* property which has been set, otherwise it will be used.
* @param unlessProperty evaluated expression
*/
public void setUnless(String unlessProperty) {
this.unlessProperty = unlessProperty;
setUnless((Object) unlessProperty);
}

/**
@@ -1029,13 +1053,9 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger {
* @return true if the task passes the "if" and "unless" parameters
*/
public boolean shouldUse() {
if (ifProperty != null && project.getProperty(ifProperty) == null) {
return false;
}
if (unlessProperty != null && project.getProperty(unlessProperty) != null) {
return false;
}
return true;
PropertyHelper ph = PropertyHelper.getPropertyHelper(project);
return ph.testIfCondition(ifCond)
&& ph.testUnlessCondition(unlessCond);
}
} // Param



+ 48
- 0
src/tests/antunit/taskdefs/xslt-test.xml View File

@@ -170,4 +170,52 @@ undefined='<xsl:value-of select="$undefined"/>'
<au:assertLogDoesntContain text="Failed to enable tracing"/>
</target>

<target name="setUpIfUnlessTests" depends="setUp">
<macrodef name="xs">
<sequential>
<xslt in="${legacy.dir}/data.xml" out="${output}/out.xml"
style="${legacy.dir}/printParams.xsl">
<param name="set" expression="if-value" if="${if}"/>
<param name="set" expression="unless-value" unless="${if}"/>
<param name="empty" expression="if-value" if="if"/>
<param name="empty" expression="unless-value" unless="if"/>
</xslt>
</sequential>
</macrodef>
</target>

<target name="testPropertiesNotSet" depends="setUpIfUnlessTests">
<xs/>
<au:assertResourceContains resource="${output}/out.xml"
value="set='unless-value'"/>
<au:assertResourceContains resource="${output}/out.xml"
value="empty='unless-value'"/>
</target>

<target name="testPropertiesSet" depends="setUpIfUnlessTests">
<property name="if" value="whatever"/>
<xs/>
<au:assertResourceContains resource="${output}/out.xml"
value="set='unless-value'"/>
<au:assertResourceContains resource="${output}/out.xml"
value="empty='if-value'"/>
</target>

<target name="testIfTrue" depends="setUpIfUnlessTests">
<property name="if" value="true"/>
<xs/>
<au:assertResourceContains resource="${output}/out.xml"
value="set='if-value'"/>
<au:assertResourceContains resource="${output}/out.xml"
value="empty='if-value'"/>
</target>

<target name="testIfFalse" depends="setUpIfUnlessTests">
<property name="if" value="false"/>
<xs/>
<au:assertResourceContains resource="${output}/out.xml"
value="set='unless-value'"/>
<au:assertResourceContains resource="${output}/out.xml"
value="empty='if-value'"/>
</target>
</project>

Loading…
Cancel
Save