source: release-kits/lirk3/ant-scripts/tasks/antelope/src/ise/antelope/tasks/IfTask.java@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

File size: 9.1 KB
Line 
1/*
2* The Apache Software License, Version 1.1
3*
4* Copyright (c) 2001-2002 The Apache Software Foundation. All rights
5* reserved.
6*
7* Redistribution and use in source and binary forms, with or without
8* modification, are permitted provided that the following conditions
9* are met:
10*
11* 1. Redistributions of source code must retain the above copyright
12* notice, this list of conditions and the following disclaimer.
13*
14* 2. Redistributions in binary form must reproduce the above copyright
15* notice, this list of conditions and the following disclaimer in
16* the documentation and/or other materials provided with the
17* distribution.
18*
19* 3. The end-user documentation included with the redistribution, if
20* any, must include the following acknowlegement:
21* "This product includes software developed by the
22* Apache Software Foundation (http://www.apache.org/)."
23* Alternately, this acknowlegement may appear in the software itself,
24* if and wherever such third-party acknowlegements normally appear.
25*
26* 4. The names "The Jakarta Project", "Ant", and "Apache Software
27* Foundation" must not be used to endorse or promote products derived
28* from this software without prior written permission. For written
29* permission, please contact [email protected].
30*
31* 5. Products derived from this software may not be called "Apache"
32* nor may "Apache" appear in their names without prior written
33* permission of the Apache Group.
34*
35* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46* SUCH DAMAGE.
47* ====================================================================
48*
49* This software consists of voluntary contributions made by many
50* individuals on behalf of the Apache Software Foundation. For more
51* information on the Apache Software Foundation, please see
52* <http://www.apache.org/>.
53*/
54package ise.antelope.tasks;
55import java.util.Enumeration;
56
57import java.util.Vector;
58import org.apache.tools.ant.BuildException;
59
60import org.apache.tools.ant.Task;
61import org.apache.tools.ant.TaskAdapter;
62import org.apache.tools.ant.TaskContainer;
63import org.apache.tools.ant.taskdefs.Available;
64import org.apache.tools.ant.taskdefs.Checksum;
65import org.apache.tools.ant.taskdefs.Sequential;
66import org.apache.tools.ant.taskdefs.UpToDate;
67import org.apache.tools.ant.taskdefs.condition.*;
68
69
70/**
71 * Similar to Java's 'if' keyword, verifies that a given property has a given
72 * value and executes embedded tasks if it does. Does nothing if the property
73 * value is not as expected or the property does not exist. <p>
74 *
75 * <p>
76 *
77 * Can hold other tasks including IfTask, in particular, an ElseTask and a
78 * Break.
79 *
80 * @author Dale Anson, [email protected]
81 * @since Ant 1.5
82 */
83public class IfTask extends Task implements TaskContainer, Breakable {
84
85 // attribute storage
86 private boolean exists = true;
87 private String value = null;
88 private String name = null;
89
90 private Task else_task = null;
91 private Task condition_task = null;
92
93 // vector to hold any nested tasks
94 private Vector tasks = new Vector();
95
96 // break condition
97 private boolean doBreak = false;
98
99
100 /** Automatically define dependent tasks. */
101 public void init() {
102 // may need to do this differently for Ant 1.6, need to check when 1.6
103 // is final
104 getProject().addTaskDefinition( "else", ElseTask.class );
105 getProject().addTaskDefinition( "bool", BooleanConditionTask.class );
106 getProject().addTaskDefinition( "break", Break.class );
107 }
108
109
110 /**
111 * Set the name of the property to test. Required unless nested condition is
112 * used.
113 *
114 * @param name the name of the property to test.
115 */
116 public void setName( String name ) {
117 this.name = name;
118 }
119
120
121 /**
122 * Set the expected value of the property. Implies 'exists'. <code>execute</code>
123 * method throws BuildException if the actual value is not the same as this value.
124 * Optional.
125 *
126 * @param value the expected value of the property.
127 */
128 public void setValue( String value ) {
129 this.value = value;
130 }
131
132
133 /**
134 * Set the 'exists' attribute. If true, throws BuildException if the property
135 * does not exist. Optional, default is true.
136 *
137 * @param exists Ant boolean, whether the value must exist.
138 */
139 public void setExists( String exists ) {
140 this.exists = getProject().toBoolean( exists );
141 }
142
143
144 /**
145 * Required by Breakable.
146 *
147 * @param b The new break value
148 */
149 public void setBreak( boolean b ) {
150 doBreak = b;
151 }
152
153
154 /**
155 * Required by Breakable.
156 *
157 * @param b The feature to be added to the Break attribute
158 */
159 public void addBreak( Break b ) {
160 // does nothing for Ant 1.5, next line is for Ant 1.6
161 addTask( b );
162 }
163
164 public void addElse( ElseTask elseTask ) {
165 addTask( elseTask );
166 }
167
168 public void addBool( BooleanConditionTask boolTask ) {
169 addTask( boolTask );
170 }
171
172 /**
173 * Required by Breakable.
174 *
175 * @return Description of the Return Value
176 */
177 public boolean doBreak() {
178 return doBreak;
179 }
180
181
182 /**
183 * Override {@link org.apache.tools.ant.Task#maybeConfigure maybeConfigure}
184 * in a way that leaves the nested tasks unconfigured until they get
185 * executed.
186 *
187 * @exception BuildException Description of the Exception
188 * @since Ant 1.5
189 */
190 public void maybeConfigure() throws BuildException {
191 if ( isInvalid() ) {
192 super.maybeConfigure();
193 }
194 else {
195 getRuntimeConfigurableWrapper().maybeConfigure( getProject(), false );
196 }
197 }
198
199
200 /**
201 * Add a nested task to execute. <p>
202 *
203 * @param task Nested task to execute. <p>
204 */
205 public void addTask( Task task ) {
206 if ( task instanceof ElseTask ) {
207 if ( else_task == null ) {
208 else_task = task;
209 return ;
210 }
211 else {
212 throw new BuildException( "Only one <else> allowed per If." );
213 }
214 }
215 else if ( task instanceof BooleanConditionTask ) {
216 if ( condition_task == null ) {
217 condition_task = task;
218 return ;
219 }
220 else {
221 throw new BuildException( "Only one <bool> allowed per If." );
222 }
223 }
224 tasks.addElement( task );
225 }
226
227
228 /**
229 * Execute this task and all nested Tasks, checking for Breaks and
230 * Breakables.
231 *
232 * @exception BuildException Description of the Exception
233 */
234 public void execute() throws BuildException {
235 if ( condition_task == null ) {
236 // no conditions, so property name is required
237 if ( name == null || name.equals( "" ) ) {
238 throw new BuildException( "The 'name' attribute is required." );
239 }
240
241 // get the property value from the project
242 String prop_value = getProject().getProperty( name );
243
244 // check if the property exists
245 if ( exists && prop_value == null ) {
246 doElse();
247 return ;
248 }
249 else if ( !exists && prop_value != null ) {
250 doElse();
251 return ;
252 }
253
254 // check that the property has the right value
255 if ( value != null ) {
256 if ( prop_value == null ) {
257 doElse();
258 return ;
259 }
260 else if ( prop_value != null && !prop_value.equals( value ) ) {
261 doElse();
262 return ;
263 }
264 }
265
266 // all is well, so do the if
267 doIf();
268 }
269 else {
270 // have nested condition
271 if ( ( ( BooleanConditionTask ) condition_task ).eval() ) {
272 doIf();
273 }
274 else {
275 doElse();
276 }
277 }
278 }
279
280
281 /**
282 * Do the 'if' part of the if/else.
283 *
284 * @exception BuildException Description of the Exception
285 */
286 private void doIf() throws BuildException {
287 // execute all nested tasks
288 for ( Enumeration e = tasks.elements(); e.hasMoreElements(); ) {
289 Task task = ( Task ) e.nextElement();
290 if ( task instanceof Breakable ) {
291 task.perform();
292 if ( ( ( Breakable ) task ).doBreak() ) {
293 setBreak( true );
294 return ;
295 }
296 }
297 else {
298 task.perform();
299 }
300 }
301 }
302
303
304 /**
305 * Do the 'else' part of the if/else.
306 *
307 * @exception BuildException Description of the Exception
308 */
309 private void doElse() throws BuildException {
310 if ( else_task == null ) {
311 return ;
312 }
313 else_task.perform();
314 }
315
316}
Note: See TracBrowser for help on using the repository browser.