| 1 | /* |
| 2 | * Copyright (c) 2001-2004 Ant-Contrib project. All rights reserved. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package net.sf.antcontrib.logic; |
| 18 | |
| 19 | import java.util.Vector; |
| 20 | |
| 21 | import org.apache.tools.ant.BuildException; |
| 22 | import org.apache.tools.ant.taskdefs.Sequential; |
| 23 | import org.apache.tools.ant.taskdefs.condition.Condition; |
| 24 | import org.apache.tools.ant.taskdefs.condition.ConditionBase; |
| 25 | |
| 26 | /** |
| 27 | * Perform some tasks based on whether a given condition holds true or |
| 28 | * not. |
| 29 | * |
| 30 | * <p>This task is heavily based on the Condition framework that can |
| 31 | * be found in Ant 1.4 and later, therefore it cannot be used in |
| 32 | * conjunction with versions of Ant prior to 1.4.</p> |
| 33 | * |
| 34 | * <p>This task doesn't have any attributes, the condition to test is |
| 35 | * specified by a nested element - see the documentation of your |
| 36 | * <code><condition></code> task (see |
| 37 | * <a href="http://jakarta.apache.org/ant/manual/CoreTasks/condition.html">the |
| 38 | * online documentation</a> for example) for a complete list of nested |
| 39 | * elements.</p> |
| 40 | * |
| 41 | * <p>Just like the <code><condition></code> task, only a single |
| 42 | * condition can be specified - you combine them using |
| 43 | * <code><and></code> or <code><or></code> conditions.</p> |
| 44 | * |
| 45 | * <p>In addition to the condition, you can specify three different |
| 46 | * child elements, <code><elseif></code>, <code><then></code> and |
| 47 | * <code><else></code>. All three subelements are optional. |
| 48 | * |
| 49 | * Both <code><then></code> and <code><else></code> must not be |
| 50 | * used more than once inside the if task. Both are |
| 51 | * containers for Ant tasks, just like Ant's |
| 52 | * <code><parallel></code> and <code><sequential></code> |
| 53 | * tasks - in fact they are implemented using the same class as Ant's |
| 54 | * <code><sequential></code> task.</p> |
| 55 | * |
| 56 | * The <code><elseif></code> behaves exactly like an <code><if></code> |
| 57 | * except that it cannot contain the <code><else></code> element |
| 58 | * inside of it. You may specify as may of these as you like, and the |
| 59 | * order they are specified is the order they are evaluated in. If the |
| 60 | * condition on the <code><if></code> is false, then the first |
| 61 | * <code><elseif></code> who's conditional evaluates to true |
| 62 | * will be executed. The <code><else></code> will be executed |
| 63 | * only if the <code><if></code> and all <code><elseif></code> |
| 64 | * conditions are false. |
| 65 | * |
| 66 | * <p>Use the following task to define the <code><if></code> |
| 67 | * task before you use it the first time:</p> |
| 68 | * |
| 69 | * <pre><code> |
| 70 | * <taskdef name="if" classname="net.sf.antcontrib.logic.IfTask" /> |
| 71 | * </code></pre> |
| 72 | * |
| 73 | * <h3>Crude Example</h3> |
| 74 | * |
| 75 | * <pre><code> |
| 76 | * <if> |
| 77 | * <equals arg1="${foo}" arg2="bar" /> |
| 78 | * <then> |
| 79 | * <echo message="The value of property foo is bar" /> |
| 80 | * </then> |
| 81 | * <else> |
| 82 | * <echo message="The value of property foo is not bar" /> |
| 83 | * </else> |
| 84 | * </if> |
| 85 | * </code> |
| 86 | * |
| 87 | * <code> |
| 88 | * <if> |
| 89 | * <equals arg1="${foo}" arg2="bar" /> |
| 90 | * <then> |
| 91 | * <echo message="The value of property foo is 'bar'" /> |
| 92 | * </then> |
| 93 | * |
| 94 | * <elseif> |
| 95 | * <equals arg1="${foo}" arg2="foo" /> |
| 96 | * <then> |
| 97 | * <echo message="The value of property foo is 'foo'" /> |
| 98 | * </then> |
| 99 | * </elseif> |
| 100 | * |
| 101 | * <else> |
| 102 | * <echo message="The value of property foo is not 'foo' or 'bar'" /> |
| 103 | * </else> |
| 104 | * </if> |
| 105 | * </code></pre> |
| 106 | * |
| 107 | * @author <a href="mailto:[email protected]">Stefan Bodewig</a> |
| 108 | */ |
| 109 | public class IfTask extends ConditionBase { |
| 110 | |
| 111 | public static final class ElseIf |
| 112 | extends ConditionBase |
| 113 | { |
| 114 | private Sequential thenTasks = null; |
| 115 | |
| 116 | public void addThen(Sequential t) |
| 117 | { |
| 118 | if (thenTasks != null) |
| 119 | { |
| 120 | throw new BuildException("You must not nest more than one <then> into <elseif>"); |
| 121 | } |
| 122 | thenTasks = t; |
| 123 | } |
| 124 | |
| 125 | public boolean eval() |
| 126 | throws BuildException |
| 127 | { |
| 128 | if (countConditions() > 1) { |
| 129 | throw new BuildException("You must not nest more than one condition into <elseif>"); |
| 130 | } |
| 131 | if (countConditions() < 1) { |
| 132 | throw new BuildException("You must nest a condition into <elseif>"); |
| 133 | } |
| 134 | Condition c = (Condition) getConditions().nextElement(); |
| 135 | |
| 136 | return c.eval(); |
| 137 | } |
| 138 | |
| 139 | public void execute() |
| 140 | throws BuildException |
| 141 | { |
| 142 | if (thenTasks != null) |
| 143 | { |
| 144 | thenTasks.execute(); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | private Sequential thenTasks = null; |
| 150 | private Vector elseIfTasks = new Vector(); |
| 151 | private Sequential elseTasks = null; |
| 152 | |
| 153 | /*** |
| 154 | * A nested Else if task |
| 155 | */ |
| 156 | public void addElseIf(ElseIf ei) |
| 157 | { |
| 158 | elseIfTasks.addElement(ei); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * A nested <then> element - a container of tasks that will |
| 163 | * be run if the condition holds true. |
| 164 | * |
| 165 | * <p>Not required.</p> |
| 166 | */ |
| 167 | public void addThen(Sequential t) { |
| 168 | if (thenTasks != null) { |
| 169 | throw new BuildException("You must not nest more than one <then> into <if>"); |
| 170 | } |
| 171 | thenTasks = t; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * A nested <else> element - a container of tasks that will |
| 176 | * be run if the condition doesn't hold true. |
| 177 | * |
| 178 | * <p>Not required.</p> |
| 179 | */ |
| 180 | public void addElse(Sequential e) { |
| 181 | if (elseTasks != null) { |
| 182 | throw new BuildException("You must not nest more than one <else> into <if>"); |
| 183 | } |
| 184 | elseTasks = e; |
| 185 | } |
| 186 | |
| 187 | public void execute() throws BuildException { |
| 188 | if (countConditions() > 1) { |
| 189 | throw new BuildException("You must not nest more than one condition into <if>"); |
| 190 | } |
| 191 | if (countConditions() < 1) { |
| 192 | throw new BuildException("You must nest a condition into <if>"); |
| 193 | } |
| 194 | Condition c = (Condition) getConditions().nextElement(); |
| 195 | if (c.eval()) { |
| 196 | if (thenTasks != null) { |
| 197 | thenTasks.execute(); |
| 198 | } |
| 199 | } |
| 200 | else |
| 201 | { |
| 202 | boolean done = false; |
| 203 | int sz = elseIfTasks.size(); |
| 204 | for (int i=0;i<sz && ! done;i++) |
| 205 | { |
| 206 | |
| 207 | ElseIf ei = (ElseIf)(elseIfTasks.elementAt(i)); |
| 208 | if (ei.eval()) |
| 209 | { |
| 210 | done = true; |
| 211 | ei.execute(); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | if (!done && elseTasks != null) |
| 216 | { |
| 217 | elseTasks.execute(); |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | } |