/* * Copyright (c) 2001-2004 Ant-Contrib project. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.antcontrib.logic; import java.util.Vector; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.taskdefs.Sequential; import org.apache.tools.ant.taskdefs.condition.Condition; import org.apache.tools.ant.taskdefs.condition.ConditionBase; /** * Perform some tasks based on whether a given condition holds true or * not. * *

This task is heavily based on the Condition framework that can * be found in Ant 1.4 and later, therefore it cannot be used in * conjunction with versions of Ant prior to 1.4.

* *

This task doesn't have any attributes, the condition to test is * specified by a nested element - see the documentation of your * <condition> task (see * the * online documentation for example) for a complete list of nested * elements.

* *

Just like the <condition> task, only a single * condition can be specified - you combine them using * <and> or <or> conditions.

* *

In addition to the condition, you can specify three different * child elements, <elseif>, <then> and * <else>. All three subelements are optional. * * Both <then> and <else> must not be * used more than once inside the if task. Both are * containers for Ant tasks, just like Ant's * <parallel> and <sequential> * tasks - in fact they are implemented using the same class as Ant's * <sequential> task.

* * The <elseif> behaves exactly like an <if> * except that it cannot contain the <else> element * inside of it. You may specify as may of these as you like, and the * order they are specified is the order they are evaluated in. If the * condition on the <if> is false, then the first * <elseif> who's conditional evaluates to true * will be executed. The <else> will be executed * only if the <if> and all <elseif> * conditions are false. * *

Use the following task to define the <if> * task before you use it the first time:

* *

 *   <taskdef name="if" classname="net.sf.antcontrib.logic.IfTask" />
 * 
* *

Crude Example

* *

 * <if>
 *  <equals arg1="${foo}" arg2="bar" />
 *  <then>
 *    <echo message="The value of property foo is bar" />
 *  </then>
 *  <else>
 *    <echo message="The value of property foo is not bar" />
 *  </else>
 * </if>
 * 
 *
 * 
 * <if>
 *  <equals arg1="${foo}" arg2="bar" />
 *  <then>
 *   <echo message="The value of property foo is 'bar'" />
 *  </then>
 *
 *  <elseif>
 *   <equals arg1="${foo}" arg2="foo" />
 *   <then>
 *    <echo message="The value of property foo is 'foo'" />
 *   </then>
 *  </elseif>
 *
 *  <else>
 *   <echo message="The value of property foo is not 'foo' or 'bar'" />
 *  </else>
 * </if>
 * 
* * @author Stefan Bodewig */ public class IfTask extends ConditionBase { public static final class ElseIf extends ConditionBase { private Sequential thenTasks = null; public void addThen(Sequential t) { if (thenTasks != null) { throw new BuildException("You must not nest more than one into "); } thenTasks = t; } public boolean eval() throws BuildException { if (countConditions() > 1) { throw new BuildException("You must not nest more than one condition into "); } if (countConditions() < 1) { throw new BuildException("You must nest a condition into "); } Condition c = (Condition) getConditions().nextElement(); return c.eval(); } public void execute() throws BuildException { if (thenTasks != null) { thenTasks.execute(); } } } private Sequential thenTasks = null; private Vector elseIfTasks = new Vector(); private Sequential elseTasks = null; /*** * A nested Else if task */ public void addElseIf(ElseIf ei) { elseIfTasks.addElement(ei); } /** * A nested <then> element - a container of tasks that will * be run if the condition holds true. * *

Not required.

*/ public void addThen(Sequential t) { if (thenTasks != null) { throw new BuildException("You must not nest more than one into "); } thenTasks = t; } /** * A nested <else> element - a container of tasks that will * be run if the condition doesn't hold true. * *

Not required.

*/ public void addElse(Sequential e) { if (elseTasks != null) { throw new BuildException("You must not nest more than one into "); } elseTasks = e; } public void execute() throws BuildException { if (countConditions() > 1) { throw new BuildException("You must not nest more than one condition into "); } if (countConditions() < 1) { throw new BuildException("You must nest a condition into "); } Condition c = (Condition) getConditions().nextElement(); if (c.eval()) { if (thenTasks != null) { thenTasks.execute(); } } else { boolean done = false; int sz = elseIfTasks.size(); for (int i=0;i