KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > condition > solo > TallyTask


1 /**
2  * $Id: TallyTask.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.condition.solo;
30
31 import org.apache.tools.ant.Project;
32 import org.apache.tools.ant.taskdefs.condition.Condition;
33 import org.apache.tools.ant.types.Reference;
34
35 import com.idaremedia.antx.AntX;
36 import com.idaremedia.antx.ExportedProperties;
37 import com.idaremedia.antx.helpers.Strings;
38 import com.idaremedia.antx.parameters.TrueFalsePropertySetter;
39
40 /**
41  * Supplement for standard ConditionTask that allows the AntX extra conditions to also be
42  * nested. Doesn't really <em>do</em> anything explicitly. Effective action is the
43  * side-effects of nested conditions (like property-setting, etc.). Usually defined as
44  * &lt;tally&gt; or &lt;check&gt;.
45  * <p>
46  * <b>Example Usage:</b><pre>
47  * &lt;<b>tally</b>&gt;
48  * &lt;isallset trueproperty="all.required.present"&gt;
49  * &lt;property value="junit.present"/&gt;
50  * &lt;property value="log4j.present"/&gt;
51  * &lt;reference value="tests.classpath"/&gt;
52  * &lt;/isallset&gt;
53  * &lt;/tally&gt;
54  * OR
55  * &lt;<b>tally</b>&gt;
56  * &lt;tally ruleid="preferred.thirdparty.libs"/&gt;
57  * &lt;isnoneset trueproperty="no.test.engines"&gt;
58  * &lt;property value="junit.present"/&gt;
59  * &lt;property value="jware.pet.present"/&gt;
60  * &lt;/isnoneset&gt;
61  * &lt;/tally&gt;
62  *
63  * <i><b>For Ant 1.6 or later:</b></i>
64  * &lt;typedef name="moonisright" classname="..."/&gt; <i>[your condition impl]</i>
65  * &#46;&#46;&#46;
66  * &lt;<b>tally</b>&gt;
67  * &lt;<b>moonisright</b> date=${DSTAMP}.../&gt;
68  * &lt;/tally&gt;
69  * </pre>
70  *
71  * <b>Implementation Notes</b>:<ul>
72  * <li>The tally sets write-once properties (its 'true' and 'false' properties); however,
73  * if a tally executes a rule that triggers a false-preference it will update the current
74  * setting of the preference's update property to whatever the default value is. The
75  * scope of this update is the current project <em>and any child projects created
76  * when using 'ant,' 'antcall', 'do', 'callforeach' and 'foreach.'</em>
77  * </ul>
78  *
79  * @since JWare/AntX 0.2
80  * @author ssmc, &copy;2002-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
81  * @version 0.5
82  * @.safety single
83  * @.group api,infra
84  * @.expects Ant 1.6 or later for automatic freeform
85  * @see AssertTask
86  * @see PreferTask
87  **/

88
89 public class TallyTask extends CompoundRuleTask
90     implements FreeformRule, TrueFalsePropertySetter
91 {
92     /**
93      * Initialize a new TallyTask.
94      **/

95     public TallyTask()
96     {
97         super(AntX.rules+"tally");
98     }
99
100
101     /**
102      * Initializes a new CV-labeled TallyTask.
103      **/

104     public TallyTask(String JavaDoc iam)
105     {
106         super(iam);
107     }
108
109
110     /**
111      * Initializes a new optionally embedded CV-labeled TallyTask
112      * subclass.
113      **/

114     protected TallyTask(String JavaDoc iam, boolean embedded)
115     {
116         super(iam,embedded);
117     }
118
119
120 // ---------------------------------------------------------------------------------------
121
// Parameters, Nested Elements:
122
// ---------------------------------------------------------------------------------------
123

124     /**
125      * Sets the default condition combining logic operator for
126      * this tally. Defaults to 'anding' multiple conditions. Example
127      * usage: <pre>
128      * &lt;tally logic="or" trueproperty="tests.required"&gt;
129      * &lt;isset property="is.debug.build"/&gt;
130      * &lt;equals arg1="debug" arg2="${build.type}"/&gt;
131      * &lt;/tally&gt;
132      * </pre>
133      * @since JWare/AntX 0.3
134      **/

135     public final void setLogic(BooleanRule.Operator logic)
136     {
137         setDefaultCombineOperator(logic);
138     }
139
140
141     /**
142      * Returns the combine logic of this task. Defaults to <i>AND</i>
143      * if never set.
144      * @since JWare/AntX 0.3
145      **/

146     public final BooleanRule.Operator getLogic()
147     {
148         return getDefaultCombineOperator();
149     }
150
151
152     /**
153      * Sets whether or not this tally will evaluate all nested
154      * conditions before returning.
155      * @param evaluateAll <i>true</i> to force tally to evaluate
156      * all conditions
157      * @since JWare/AntX 0.3
158      **/

159     public void setAll(boolean evaluateAll)
160     {
161         setStopQuickEvaluation(!evaluateAll);
162     }
163
164
165     /**
166      * Returns <i>true</i> if this tally will evaluate all
167      * nested conditions even if final result can be determined
168      * before.
169      * @since JWare/AntX 0.3
170      **/

171     public final boolean willEvaluateAll()
172     {
173         return !isStopQuickEvaluation();
174     }
175
176
177     /**
178      * Sets the property to be created on a negative evaluation.
179      * Property will be set to the string "<i>true</i>."
180      * @param property the property to create (non-null)
181      **/

182     public void setFalseProperty(String JavaDoc property)
183     {
184         require_(property!=null,"setFalsP- nonzro nam");
185         checkModify("setFalsProp");
186         m_falseProperty = property;
187         m_varMask &= ~F_VAR;
188     }
189
190
191     /**
192      * Returns the property to be created/set on a negative
193      * evaluation. Returns <i>null</i> if never set.
194      **/

195     public final String JavaDoc getFalseProperty()
196     {
197         return (m_varMask&F_VAR)==F_VAR ? null : m_falseProperty;
198     }
199
200
201     /**
202      * Sets the variable to be created on a negative evaluation.
203      * Variable will be set to the string "<i>true</i>."
204      * @param variable the variable to create (non-null)
205      * @since JWare/AntX 0.5
206      **/

207     public void setFalseVariable(String JavaDoc variable)
208     {
209         require_(variable!=null,"setFalsV- nonzro nam");
210         checkModify("setFalsVar");
211         m_falseProperty = variable;
212         m_varMask |= F_VAR;
213     }
214
215
216     /**
217      * Returns the variable to be created/set on a negative
218      * evaluation. Returns <i>null</i> if never set.
219      * @since JWare/AntX 0.5
220      **/

221     public final String JavaDoc getFalseVariable()
222     {
223         return (m_varMask&F_VAR)==F_VAR ? m_falseProperty : null;
224     }
225
226
227     /**
228      * Sets the property to be created on a positive evaluation.
229      * Property will be set to the string "<i>true</i>."
230      * @param property the property to create (non-null)
231      **/

232     public void setTrueProperty(String JavaDoc property)
233     {
234         require_(property!=null,"setTrueP- nonzro nam");
235         checkModify("setTrueProp");
236         m_trueProperty = property;
237     }
238
239
240     /**
241      * Returns the property to be created/set on a positive
242      * evaluation. Returns <i>null</i> if never set.
243      **/

244     public final String JavaDoc getTrueProperty()
245     {
246         return m_trueProperty;
247     }
248
249
250     /**
251      * Sets the variable to be created on a positive evaluation.
252      * Variable will be set to the string "<i>true</i>."
253      * @param variable the variable to create (non-null)
254      * @since JWare/AntX 0.5
255      **/

256     public void setTrueVariable(String JavaDoc variable)
257     {
258         require_(variable!=null,"setTrueV- nonzro nam");
259         checkModify("setTrueVar");
260         m_trueProperty = variable;
261         m_varMask |= T_VAR;
262     }
263
264
265     /**
266      * Returns the variable to be created/set on a positive
267      * evaluation. Returns <i>null</i> if never set.
268      * @since JWare/AntX 0.5
269      **/

270     public final String JavaDoc getTrueVariable()
271     {
272         return (m_varMask&T_VAR)==T_VAR ? m_trueProperty : null;
273     }
274
275
276     /**
277      * Sets this tally task to use an existing build rule as its
278      * definition. The reference will be evaluated (and verified)
279      * the first time this task is evaluated.
280      * @param ruleId build rule reference id (non-null)
281      * @throws BuildException if referral's effect is incompatible
282      * @see #isIncompatibleReferral
283      **/

284     public void setRuleId(Reference ruleId)
285     {
286         setReferralReference(ruleId);
287     }
288
289
290     /**
291      * Capture our identifier for feedback if ever used to refer
292      * to self in circular manner.
293      **/

294     public void setId(String JavaDoc id)
295     {
296         m_Id= id;
297     }
298
299
300     /**
301      * Tries to return an identifier for this rule.
302      **/

303     public final String JavaDoc getId()
304     {
305         if (m_Id!=null) {
306             return m_Id;
307         }
308         return super.getId();
309     }
310
311
312     /**
313      * Adds a sub-tally to this tally task.
314      **/

315     public void addTally(TallyTask tt)
316     {
317         xaddCondition(tt);
318     }
319
320
321     /**
322      * Adds a preference condition to this tally task.
323      **/

324     public final void addPrefer(PreferTask pt)
325     {
326         xaddCondition(pt);
327     }
328
329
330     /**
331      * Adds an assertion to this tally task; diagnostic aid.
332      **/

333     public final void addAssert(AssertTask at)
334     {
335         xaddCondition(at);
336     }
337
338 // ---------------------------------------------------------------------------------------
339
// Free-form (Application) Nested Elements:
340
// ---------------------------------------------------------------------------------------
341

342     /**
343      * Adds an arbitrary application-defined condition to
344      * this tally.
345      * @param c custom condition definition (non-null)
346      * @since JWare/AntX 0.4
347      **/

348     public void addConfigured(Condition c)
349     {
350         require_(c!=null,"add- nonzro condition");
351         xaddCondition(c);
352     }
353
354 // ---------------------------------------------------------------------------------------
355
// Evaluation:
356
// ---------------------------------------------------------------------------------------
357

358     /**
359      * Returns evaluation result for nested conditions.
360      **/

361     public boolean eval()
362     {
363         boolean istrue;
364
365         if (runReferral()) {
366             istrue= getReferral().eval(new Referee());
367         } else {
368             verifyCanExecute_("eval");
369             istrue= defaultMultipleConditionEval();
370         }
371
372         // Update regular write-once properties instead of the inherited
373
// preference-like velcro effect.
374

375         if (istrue && getTrueProperty()!=null) {
376             String JavaDoc prop = getTrueProperty();
377             log("Tally was true; setting true-property '"+prop+"' property",
378                 Project.MSG_DEBUG);
379             if ((m_varMask&T_VAR)==T_VAR) {
380                 ExportedProperties.set(prop,Strings.TRUE);
381             } else {
382                 checkIfProperty_(prop,true);
383                 getProject().setNewProperty(prop,Strings.TRUE);
384             }
385         }
386         if (!istrue && getFalseProperty()!=null) {
387             String JavaDoc prop = getFalseProperty();
388             log("Tally was false; setting false-property '"+prop+"' property",
389                 Project.MSG_DEBUG);
390             if ((m_varMask&F_VAR)==F_VAR) {
391                 ExportedProperties.set(prop,Strings.TRUE);
392             } else {
393                 checkIfProperty_(prop,true);
394                 getProject().setNewProperty(prop,Strings.TRUE);
395             }
396         }
397
398         return istrue;
399     }
400
401
402     /**
403      * Returns <i>false</i> since tallys don't care much what kind-of
404      * conditions are nested in 'em.
405      **/

406     protected boolean isIncompatibleReferral(ShareableCondition rule)
407     {
408         return false;
409     }
410
411
412     private static final int T_VAR=0x02;
413     private static final int F_VAR=0x04;
414
415     private String JavaDoc m_Id;
416     private String JavaDoc m_falseProperty;
417     private String JavaDoc m_trueProperty;
418     private int m_varMask;
419 }
420
421 /* end-of-TallyTask.java */
422
Popular Tags