Debian Codegraph @

src/main/java/net/sf/antcontrib/logic/IfTask.java

raw symbols SCIP
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>&lt;condition&gt;</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>&lt;condition&gt;</code> task, only a single
42 * condition can be specified - you combine them using
43 * <code>&lt;and&gt;</code> or <code>&lt;or&gt;</code> conditions.</p>
44 *
45 * <p>In addition to the condition, you can specify three different
46 * child elements, <code>&lt;elseif&gt;</code>, <code>&lt;then&gt;</code> and
47 * <code>&lt;else&gt;</code>. All three subelements are optional.
48 *
49 * Both <code>&lt;then&gt;</code> and <code>&lt;else&gt;</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>&lt;parallel&gt;</code> and <code>&lt;sequential&gt;</code>
53 * tasks - in fact they are implemented using the same class as Ant's
54 * <code>&lt;sequential&gt;</code> task.</p>
55 *
56 * The <code>&lt;elseif&gt;</code> behaves exactly like an <code>&lt;if&gt;</code>
57 * except that it cannot contain the <code>&lt;else&gt;</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>&lt;if&gt;</code> is false, then the first
61 * <code>&lt;elseif&gt;</code> who's conditional evaluates to true
62 * will be executed. The <code>&lt;else&gt;</code> will be executed
63 * only if the <code>&lt;if&gt;</code> and all <code>&lt;elseif&gt;</code>
64 * conditions are false.
65 *
66 * <p>Use the following task to define the <code>&lt;if&gt;</code>
67 * task before you use it the first time:</p>
68 *
69 * <pre><code>
70 * &lt;taskdef name="if" classname="net.sf.antcontrib.logic.IfTask" /&gt;
71 * </code></pre>
72 *
73 * <h3>Crude Example</h3>
74 *
75 * <pre><code>
76 * &lt;if&gt;
77 * &lt;equals arg1=&quot;${foo}&quot; arg2=&quot;bar&quot; /&gt;
78 * &lt;then&gt;
79 * &lt;echo message=&quot;The value of property foo is bar&quot; /&gt;
80 * &lt;/then&gt;
81 * &lt;else&gt;
82 * &lt;echo message=&quot;The value of property foo is not bar&quot; /&gt;
83 * &lt;/else&gt;
84 * &lt;/if&gt;
85 * </code>
86 *
87 * <code>
88 * &lt;if&gt;
89 * &lt;equals arg1=&quot;${foo}&quot; arg2=&quot;bar&quot; /&gt;
90 * &lt;then&gt;
91 * &lt;echo message=&quot;The value of property foo is 'bar'&quot; /&gt;
92 * &lt;/then&gt;
93 *
94 * &lt;elseif&gt;
95 * &lt;equals arg1=&quot;${foo}&quot; arg2=&quot;foo&quot; /&gt;
96 * &lt;then&gt;
97 * &lt;echo message=&quot;The value of property foo is 'foo'&quot; /&gt;
98 * &lt;/then&gt;
99 * &lt;/elseif&gt;
100 *
101 * &lt;else&gt;
102 * &lt;echo message=&quot;The value of property foo is not 'foo' or 'bar'&quot; /&gt;
103 * &lt;/else&gt;
104 * &lt;/if&gt;
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 &lt;then&gt; 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 &lt;else&gt; 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 }