KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > antcontrib > logic > IfTask


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2002 Ant-Contrib project. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution, if
19  * any, must include the following acknowlegement:
20  * "This product includes software developed by the
21  * Ant-Contrib project (http://sourceforge.net/projects/ant-contrib)."
22  * Alternately, this acknowlegement may appear in the software itself,
23  * if and wherever such third-party acknowlegements normally appear.
24  *
25  * 4. The name Ant-Contrib must not be used to endorse or promote products
26  * derived from this software without prior written permission. For
27  * written permission, please contact
28  * ant-contrib-developers@lists.sourceforge.net.
29  *
30  * 5. Products derived from this software may not be called "Ant-Contrib"
31  * nor may "Ant-Contrib" appear in their names without prior written
32  * permission of the Ant-Contrib project.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37  * DISCLAIMED. IN NO EVENT SHALL THE ANT-CONTRIB PROJECT OR ITS
38  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
41  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
42  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
43  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
44  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45  * SUCH DAMAGE.
46  * ====================================================================
47  */

48
49 package net.sf.antcontrib.logic;
50
51 import org.apache.tools.ant.BuildException;
52 import org.apache.tools.ant.taskdefs.Sequential;
53 import org.apache.tools.ant.taskdefs.condition.Condition;
54 import org.apache.tools.ant.taskdefs.condition.ConditionBase;
55
56 /**
57  * Perform some tasks based on whether a given condition holds true or
58  * not.
59  *
60  * <p>This task is heavily based on the Condition framework that can
61  * be found in Ant 1.4 and later, therefore it cannot be used in
62  * conjunction with versions of Ant prior to 1.4.</p>
63  *
64  * <p>This task doesn't have any attributes, the condition to test is
65  * specified by a nested element - see the documentation of your
66  * <code>&lt;condition&gt;</code> task (see
67  * <a HREF="http://jakarta.apache.org/ant/manual/CoreTasks/condition.html">the
68  * online documentation</a> for example) for a complete list of nested
69  * elements.</p>
70  *
71  * <p>Just like the <code>&lt;condition&gt;</code> task, only a single
72  * condition can be specified - you combine them using
73  * <code>&lt;and&gt;</code> or <code>&lt;or&gt;</code> conditions.</p>
74  *
75  * <p>In addition to the condition, you can specify two different
76  * child elements, <code>&lt;then&gt;</code> and
77  * <code>&lt;else&gt;</code>. Both are optional and both must not be
78  * used more than once inside the same task. Both elements are
79  * containers for Ant tasks, just like Ant's
80  * <code>&lt;parallel&gt;</code> and <code>&lt;sequential&gt;</code>
81  * tasks - in fact they are implemented using the same class as Ant's
82  * <code>&lt;sequential&gt;</code> task.</p>
83  *
84  * <p>Use the following task to define the <code>&lt;if&gt;</code>
85  * task before you use it the first time:</p>
86  *
87  * <pre><code>
88  * &lt;taskdef name="if" classname="net.sf.antcontrib.logic.IfTask" /&gt;
89  * </code></pre>
90  *
91  * <h3>Crude Example</h3>
92  *
93  * <pre><code>
94  * &lt;if&gt;
95  * &lt;equals arg1=&quot;${foo}&quot; arg2=&quot;bar&quot; /&gt;
96  * &lt;then&gt;
97  * &lt;echo message=&quot;The value of property foo is bar&quot; /&gt;
98  * &lt;/then&gt;
99  * &lt;else&gt;
100  * &lt;echo message=&quot;The value of property foo is not bar&quot; /&gt;
101  * &lt;/else&gt;
102  * &lt;/if&gt;
103  * </code></pre>
104  *
105  * @author <a HREF="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
106  */

107 public class IfTask extends ConditionBase {
108
109     private Sequential thenTasks = null;
110     private Sequential elseTasks = null;
111
112     /**
113      * A nested &lt;then&gt; element - a container of tasks that will
114      * be run if the condition holds true.
115      *
116      * <p>Not required.</p>
117      */

118     public void addThen(Sequential t) {
119         if (thenTasks != null) {
120             throw new BuildException("You must not nest more than one <then> into <if>");
121         }
122         thenTasks = t;
123     }
124
125     /**
126      * A nested &lt;else&gt; element - a container of tasks that will
127      * be run if the condition doesn't hold true.
128      *
129      * <p>Not required.</p>
130      */

131     public void addElse(Sequential e) {
132         if (elseTasks != null) {
133             throw new BuildException("You must not nest more than one <else> into <if>");
134         }
135         elseTasks = e;
136     }
137
138     public void execute() throws BuildException {
139         if (countConditions() > 1) {
140             throw new BuildException("You must not nest more than one condition into <if>");
141         }
142         if (countConditions() < 1) {
143             throw new BuildException("You must nest a condition into <if>");
144         }
145         Condition c = (Condition) getConditions().nextElement();
146         if (c.eval()) {
147             if (thenTasks != null) {
148                 thenTasks.perform();
149             }
150         } else {
151             if (elseTasks != null) {
152                 elseTasks.perform();
153             }
154         }
155     }
156 }
157
Popular Tags