KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > flowcontrol > ConditionalTaskSet


1 /**
2  * $Id: ConditionalTaskSet.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2002-2004 iDare Media, Inc. All rights reserved.
4  *
5  * Originally written by iDare Media, Inc. for release into the public domain. This
6  * library, source form and binary form, is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2.1 of the License, or (at your option) any
9  * later version.<p>
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU LGPL (GNU Lesser General Public License) for more details.<p>
14  *
15  * You should have received a copy of the GNU Lesser General Public License along with this
16  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite
17  * 330, Boston, MA 02111-1307 USA. The LGPL can be found online at
18  * http://www.fsf.org/copyleft/lesser.html<p>
19  *
20  * This product has been influenced by several projects within the open-source community.
21  * The JWare developers wish to acknowledge the open-source community's support. For more
22  * information regarding the open-source products used within JWare, please visit the
23  * JWare website.
24  *----------------------------------------------------------------------------------------*
25  * WEBSITE- http://www.jware.info EMAIL- inquiries@jware.info
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.flowcontrol;
30
31 import org.apache.tools.ant.BuildException;
32 import org.apache.tools.ant.Project;
33 import org.apache.tools.ant.types.Reference;
34
35 import com.idaremedia.antx.apis.Requester;
36 import com.idaremedia.antx.condition.solo.RulesTk;
37 import com.idaremedia.antx.parameters.FlexConditional;
38
39 /**
40  * Taskset that has builtin support for conditional execution. Implements basic support
41  * for both the standard '<i>if</i>' and '<i>unless</i>' control options as well as
42  * several other AntX-specific variants like '<i>ifAnt</i>'. See the
43  * {@linkplain FlexConditional} interface for the complete list of supported
44  * execution conditions.
45  * <p/>
46  * <b>Example Usage:</b><pre>
47  * &lt;do test="some-condition-id"&gt;
48  * <i>[Your tasks here&#8230;]</i>
49  * &lt;/do&gt;
50  *
51  * &lt;do ifAntLike=".*\1\.6.*"&gt;
52  * <i>[Your tasks here&#8230;]</i>
53  * &lt;/do&gt;
54  *
55  * &lt;do ifAllTrue="a,b,c" unless="no-alpabet"&gt;
56  * <i>[Your tasks here&#8230;]</i>
57  * &lt;/do&gt;
58  * </pre>
59  *
60  * @since JWare/AntX 0.5 (Pushed down from antx.starters.ConditionalTaskSet)
61  * @author ssmc, &copy;2002-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
62  * @version 0.5
63  * @.safety single
64  * @.group impl,infra
65  **/

66
67 public class ConditionalTaskSet extends com.idaremedia.antx.starters.ConditionalTaskSet
68     implements FlexConditional
69 {
70     /**
71      * Initializes a new empty conditional taskset.
72      **/

73     public ConditionalTaskSet()
74     {
75         super();
76     }
77
78
79     /**
80      * Initializes a new CV-labeled empty conditional taskset.
81      * @param iam CV-label (non-null)
82      **/

83     protected ConditionalTaskSet(String JavaDoc iam)
84     {
85         super(iam);
86     }
87
88
89
90     /**
91      * Initializes new configuration-delaying taskset.
92      * @param iam CV-label (non-null)
93      * @param delayConfigure <i>true</i> if nested tasks configured
94      * when they're actually executed
95      **/

96     protected ConditionalTaskSet(String JavaDoc iam, boolean delayConfigure)
97     {
98         super(iam,delayConfigure);
99     }
100
101
102
103     /**
104      * Adds the if-condition-is-true to this taskset.
105      * A property value is considered <i>true</i> if it
106      * is one of: "true","on", or "yes".
107      **/

108     public void setIfTrue(String JavaDoc property)
109     {
110         m_guard.setIfTrue(property);
111     }
112
113     public void setTrue(String JavaDoc booleanString)
114     {
115         m_guard.setTrue(booleanString);
116     }
117
118     public void setFalse(String JavaDoc booleanString)
119     {
120         m_guard.setFalse(booleanString);
121     }
122
123
124     public void setIfAll(String JavaDoc properties)
125     {
126         m_guard.setIfAll(properties);
127     }
128
129     public void setIfAllTrue(String JavaDoc properties)
130     {
131         m_guard.setIfAllTrue(properties);
132     }
133
134     public void setIfOS(String JavaDoc choice)
135     {
136         m_guard.setIfOS(choice);
137     }
138
139     public void setIfAntLike(String JavaDoc version)
140     {
141         m_guard.setIfAntLike(version);
142     }
143
144
145
146     /**
147      * Adds the unless-condition-is-true to this taskset.
148      * A property value is considered <i>true</i> if it is
149      * one of: "true", "on", or "yes".
150      **/

151     public final void setUnlessTrue(String JavaDoc property)
152     {
153         m_guard.setUnlessTrue(property);
154     }
155
156
157     public void setUnlessAll(String JavaDoc properties)
158     {
159         m_guard.setUnlessAll(properties);
160     }
161
162     public void setUnlessAllTrue(String JavaDoc properties)
163     {
164         m_guard.setUnlessAllTrue(properties);
165     }
166
167     public void setUnlessOS(String JavaDoc choice)
168     {
169         m_guard.setUnlessOS(choice);
170     }
171
172     public void setUnlessAntLike(String JavaDoc version)
173     {
174         m_guard.setUnlessAntLike(version);
175     }
176
177
178     /**
179      * Assigns this taskset a named condition execution guard. If the
180      * referenced condition evaluates <i>true</i> this taskset's
181      * contents will be performed.
182      * @param testRef reference to test definition (non-null)
183      * @since JWare/AntX 0.5
184      **/

185     public void setCriteria(Reference testRef)
186     {
187         require_(testRef!=null,"setTest: nonzro test refid");
188         m_conditionId = testRef;
189     }
190
191
192
193     /**
194      * Synonym for {@linkplain #setCriteria(Reference) setCriteria}.
195      * @param testRef reference to test definition (non-null)
196      * @since JWare/AntX 0.5
197      **/

198     public final void setTest(Reference testRef)
199     {
200         setCriteria(testRef);
201     }
202
203
204
205     /**
206      * Returns <i>true</i> if either this taskset's named condition test
207      * passes or all of its individual if tests pass.
208      * @return <i>true</i> if all execution conditions met
209      * @since JWare/AntX 0.5
210      **/

211     public boolean testIfCondition()
212     {
213         if (m_conditionId!=null) {
214             String JavaDoc testId = m_conditionId.getRefId();
215             boolean passIf = true;
216             if (!RulesTk.evalTest(testId, m_rqlink)) {
217                 m_guard.setLastFailure("$test:"+testId);
218                 passIf = false;
219             }
220             log("Conditional execution (test="+testId+") is "+passIf, Project.MSG_DEBUG);
221             return passIf;
222         }
223         return m_guard.testIfCondition();
224     }
225
226
227
228     /**
229      * Ensures that for conditional execution either a single named
230      * condition has been defined or a combination of if/unless options.
231      * @throws BuildException if a test condition has been named well
232      * as other if/unless conditions.
233      * @throws BuildExceptionif a referred-to test object is not a
234      * valid condition object.
235      * @since JWare/AntX 0.5
236      */

237     protected void verifyCanExecute_(String JavaDoc calr)
238     {
239         super.verifyCanExecute_(calr);
240
241         if (m_conditionId!=null) {
242             if (!m_guard.isEmpty()) {
243                 String JavaDoc e = getAntXMsg("flow.only.test.param",m_conditionId.getRefId());
244                 log(e, Project.MSG_ERR);
245                 throw new BuildException(e, getLocation());
246             }
247             RulesTk.verifyTest(m_conditionId.getRefId(), m_rqlink);
248         }
249     }
250
251
252
253     private Requester m_rqlink = new Requester.ForComponent(this);
254     private Reference m_conditionId;
255 }
256
257 /* end-of-ConditionalTaskSet.java */
258
Popular Tags