KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: TallySet.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 java.util.Iterator JavaDoc;
32 import java.util.Stack JavaDoc;
33
34 import org.apache.tools.ant.BuildException;
35 import org.apache.tools.ant.Project;
36 import org.apache.tools.ant.taskdefs.Available;
37 import org.apache.tools.ant.taskdefs.Checksum;
38 import org.apache.tools.ant.taskdefs.UpToDate;
39 import org.apache.tools.ant.taskdefs.condition.Condition;
40 import org.apache.tools.ant.types.Reference;
41
42 import com.idaremedia.antx.AntX;
43 import com.idaremedia.antx.NoiseLevel;
44 import com.idaremedia.antx.apis.Requester;
45 import com.idaremedia.antx.condition.AllSet;
46 import com.idaremedia.antx.condition.AllSetTrue;
47 import com.idaremedia.antx.condition.AnySet;
48 import com.idaremedia.antx.condition.AnySetTrue;
49 import com.idaremedia.antx.condition.FileNotEmpty;
50 import com.idaremedia.antx.condition.IsAntVersion;
51 import com.idaremedia.antx.condition.IsClass;
52 import com.idaremedia.antx.condition.IsResource;
53 import com.idaremedia.antx.condition.NoneSet;
54
55 /**
56  * A shareable set of availability checks. A TallySet evaluates all of its nested checks
57  * regardless of their individual results.
58  * <p>
59  * <b>Example Usage:</b><pre>
60  * &lt;<b>tallyset</b> id="tally.j2se.libs"&gt;
61  * &lt;isclass name="java.lang.StrictMath" trueproperty="atleast-j2se12-present"/&gt;
62  * &lt;isclass name="java.sql.Clob" trueproperty="atleast-j2se13-present"/&gt;
63  * &lt;isclass name="java.nio.ReadOnlyBufferException" trueproperty="atleast-j2se14-present"/&gt;
64  * &lt;isclass name="java.lang.Enum" trueproperty="atleast-j2se15-present"/&gt;
65  * &lt;/tallyset&gt;
66  *
67  * &lt;<b>tallyset</b> id="tally.testing.thirdparty.libs"&gt;
68  * &lt;isclass name="junit.framework.Assert" trueproperty="junit-present"/&gt;
69  * &lt;isclass name="org.apache.log4j.Logger" trueproperty="log4j-present"/&gt;
70  * &lt;/tallyset&gt;
71  *
72  * &lt;<b>tallyset</b> id="tally.libs"&gt;
73  * &lt;<b>tallyset</b> refid="tally.j2se.libs"/&gt;
74  * &lt;<b>tallyset</b> refid="tally.testing.thirdparty.libs"/&gt;
75  * &lt;/tallyset&gt;
76  *
77  * &lt;<b>tallyset</b> id="tally.test.env"&gt;
78  * &lt;<b>tallyset</b> refid="tally.j2se.libs"/&gt;
79  * &lt;<b>tallyset</b> refid="tally.testing.thirdparty.libs"/&gt;
80  * &lt;allset trueproperty="base-test-env-present"&gt;
81  * &lt;property name="atleast-j2se13-present"/&gt;
82  * &lt;property name="junit-present"/&gt;
83  * &lt;/allset&gt;
84  * &lt;/tallyset&gt;
85  * </pre>
86  *
87  * @since JWare/AntX 0.3
88  * @author ssmc, &copy;2002-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
89  * @version 0.5
90  * @.safety single
91  * @.group api,helper
92  * @.expects Ant 1.6 or later for automatic freeform
93  * @see TallyTask
94  **/

95
96 public class TallySet extends RuleType implements FreeformRule
97 {
98     /**
99      * Initializes a new tallyset.
100      **/

101     public TallySet()
102     {
103         super(AntX.rules+"tally");
104         setStopQuickEvaluation(false);
105     }
106
107
108     /**
109      * Initializes a new CV-labeled tallyset.
110      * @param iam CV-label (non-null)
111      **/

112     public TallySet(String JavaDoc iam)
113     {
114         super(iam);
115         setStopQuickEvaluation(false);
116     }
117
118
119     /**
120      * Returns a clone of this tallyset. If this tallyset is a reference
121      * to another set, that object's clone method is invoked.
122      **/

123     public Object JavaDoc clone()
124     {
125         if (isReference()) {
126             return getReference().clone();
127         }
128         return super.clone();
129     }
130
131
132     /**
133      * Set this tallyset to evaluate as its own definition another tallyset.
134      * Saves the reference for conversion to a livin' tallyset when needed.
135      * @param r reference to other tallyset (non-null)
136      * @throws BuildException if this tallyset already has own conditions
137      **/

138     public void setRefId(Reference r)
139     {
140         require_(r!=null,"setRefId- nonzro ref");
141         checkModify("setRefId");
142         if (!isEmpty()) {
143             throw new BuildException(uistrs().get("task.too.many.attrs",getId()));
144         }
145         m_ref = r;
146     }
147
148
149     /**
150      * Returns this tallyset's reference. Returns <i>null</i> if no
151      * reference has been set.
152      **/

153     public final Reference getRefId()
154     {
155         return m_ref;
156     }
157
158
159     /**
160      * Returns <i>true</i> if this tallyset is setup as a reference to
161      * another tallyset.
162      */

163     public boolean isReference()
164     {
165         return getRefId()!=null;
166     }
167
168
169     /**
170      * Returns the effect if this a tallyset; by default returns
171      * WARNING (no build stop).
172      **/

173     public NoiseLevel getFailureEffect()
174     {
175         return NoiseLevel.WARNING;
176     }
177
178
179 // ---------------------------------------------------------------------------------------
180
// Nestable Elements:
181
// ---------------------------------------------------------------------------------------
182

183     /**
184      * Adds an arbitrary application-defined condition to
185      * this tallyset.
186      * @param c custom condition definition (non-null)
187      * @since JWare/AntX 0.3
188      **/

189     public void addConfigured(Condition c)
190     {
191         require_(c!=null,"add- nonzro condition");
192         xaddCondition(c);
193     }
194
195
196     /**
197      * Adds a sub-tally &lt;tallyset&gt; to this tallyset.
198      **/

199     public void addTallyset(TallySet tt)
200     {
201         xaddCondition(tt);
202     }
203
204
205     /**
206      * Adds an &lt;isclass&gt; condition to this tallyset.
207      **/

208     public void addIsClass(IsClass isc)
209     {
210         xaddCondition(isc);
211     }
212
213
214     /**
215      * Adds an &lt;isresource&gt; condition to this tallyset.
216      **/

217     public void addIsResource(IsResource isr)
218     {
219         xaddCondition(isr);
220     }
221
222
223     /**
224      * Adds a &lt;filenotempty&gt; condition to this tallyset.
225      **/

226     public void addFileNotEmpty(FileNotEmpty fne)
227     {
228         xaddCondition(fne);
229     }
230
231
232     /**
233      * Adds an &lt;allset&gt; condition to this tallyset.
234      **/

235     public void addAllSet(AllSet allset)
236     {
237         xaddCondition(allset);
238     }
239
240
241     /**
242      * Adds an &lt;alltrue&gt; condition to this tallyset.
243      * @since JWare/AntX 0.5
244      **/

245     public void addAllSetTrue(AllSetTrue allset)
246     {
247         xaddCondition(allset);
248     }
249
250
251     /**
252      * Adds an &lt;anyset&gt; condition to this tallyset.
253      **/

254     public void addAnySet(AnySet anyset)
255     {
256         xaddCondition(anyset);
257     }
258
259
260     /**
261      * Adds an &lt;anytrue&gt; condition to this tallyset.
262      * @since JWare/AntX 0.5
263      **/

264     public void addAnySetTrue(AnySetTrue anyset)
265     {
266         xaddCondition(anyset);
267     }
268
269
270     /**
271      * Adds a &lt;noneset&gt; condition to this tallyset.
272      **/

273     public void addNoneSet(NoneSet noneset)
274     {
275         xaddCondition(noneset);
276     }
277
278
279     /**
280      * Adds an &lt;ismatch&gt; (task) condition to this rule.
281      * @since JWare/AntX 0.3
282      **/

283     public void addIsMatch(MatchesTask matches)
284     {
285         xaddCondition(matches);
286     }
287
288
289     /**
290      * Adds an &lt;available&gt; condition to this tallyset.
291      **/

292     public void addAvailable(Available av)
293     {
294         xaddCondition(av);
295     }
296
297
298     /**
299      * Adds a &lt;checksum&gt; condition to this tallyset.
300      **/

301     public void addChecksum(Checksum cs)
302     {
303         xaddCondition(cs);
304     }
305
306
307     /**
308      * Adds an &lt;uptodate&gt; condition to this tallyset.
309      **/

310     public void addUpToDate(UpToDate up)
311     {
312         xaddCondition(up);
313     }
314
315
316     /**
317      * Adds an &lt;antversion&gt; condition to this tallyset.
318      * @since JWare/AntX 0.4
319      **/

320     public void addAntVersion(IsAntVersion vc)
321     {
322         xaddCondition(vc);
323     }
324
325 // ---------------------------------------------------------------------------------------
326
// Managing Referrals:
327
// ---------------------------------------------------------------------------------------
328

329     /**
330      * Appends new condition to this tallyset's conditions sequence if
331      * its not a reference.
332      * @throws BuildException if tallyset is a reference or is frozen
333      **/

334     public void xaddCondition(Condition c)
335     {
336         if (isReference()) {
337             throw new BuildException(uistrs().get("task.too.many.attrs",getId()));
338         }
339         super.xaddCondition(c);
340     }
341
342
343     /**
344      * Appends the only condition allowed for this tallyset.
345      * @param c new root conditon (non-null)
346      * @return <i>true</i> if could add as only condition
347      * @throws BuildException if tallyset is a reference or is frozen
348      **/

349     public boolean xaddRootCondition(Condition c)
350     {
351         if (isReference()) {
352             throw new BuildException(uistrs().get("task.too.many.attrs",getId()));
353         }
354         return super.xaddRootCondition(c);
355     }
356
357
358     /**
359      * Returns the strongly-typed tallyset to which we refer. Must be
360      * a reference. Does not verify reference other than to check its type.
361      **/

362     protected final TallySet getReference()
363     {
364         verify_(m_ref!=null,"getReference- has refid");
365         Object JavaDoc robj = m_ref.getReferencedObject(getProject());
366
367         if (!(robj instanceof TallySet)) {
368             String JavaDoc ermsg = uistrs().get("brul.bad.ruleid",m_ref.getRefId());
369             log(ermsg,Project.MSG_ERR);
370             throw new BuildException(ermsg);
371         }
372         return (TallySet)robj;
373     }
374
375
376 // ---------------------------------------------------------------------------------------
377
// Evaluation:
378
// ---------------------------------------------------------------------------------------
379

380
381     /**
382      * Ensure this tallyset does not contain any circular references
383      * in its definition. Checks references as well as tallyset's
384      * nested elements.
385      * @param stk stack of referred-to (or used) rules (non-null)
386      * @param clnt call controls (non-null)
387      * @throws BuildException if circular dependency discovered
388      * @.impl sets latch after 1st successful call
389      **/

390     public void verifyNoCircularDependency(Stack JavaDoc stk, Requester clnt)
391     {
392         if (!m_circularityChecked) {
393             if (isReference()) {
394                 Object JavaDoc robj = m_ref.getReferencedObject(getProject());
395                 if (stk.contains(robj)) {
396                     String JavaDoc ermsg = uistrs().get("brul.circular.referals",m_ref.getRefId());
397                     clnt.problem(ermsg,Project.MSG_ERR);
398                     throw new BuildException(ermsg,clnt.getLocation());
399                 }
400                 if (robj instanceof BooleanRule) {
401                     stk.push(robj);
402                     ((BooleanRule)robj).verifyNoCircularDependency(stk,clnt);
403                     stk.pop();
404                 }
405                 m_circularityChecked = true;
406             }
407             else if (!isEmpty()) {
408                 Stack JavaDoc itemstk= new Stack JavaDoc();
409                 Iterator JavaDoc itr= getConditions().iterator();
410                 int I=0;
411                 while (itr.hasNext()) {
412                     Object JavaDoc oc = itr.next();
413                     I++;
414                     if (oc instanceof BooleanRule) {
415                         if (stk.contains(oc)) {
416                             String JavaDoc posI = getId()+"["+String.valueOf(I)+"]";
417                             String JavaDoc ermsg = uistrs().get("brul.circular.referals",posI);
418                             clnt.problem(ermsg,Project.MSG_ERR);
419                             throw new BuildException(ermsg,clnt.getLocation());
420                         }
421                         itemstk.addAll(stk);
422                         itemstk.push(oc);
423                         ((BooleanRule)oc).verifyNoCircularDependency(itemstk,clnt);
424                         itemstk.clear();
425                     }
426                 }
427                 m_circularityChecked = true;
428             }
429         }//!checked
430
}
431
432
433     /**
434      * Evaluates this tallyset as a root instance. If is reference
435      * calls referred-to-tallyset's evaluation method.
436      * @throws BuildException if an error occurs during evaluation
437      **/

438     public boolean eval(ShareableConditionUser calr)
439         throws BuildException
440     {
441         if (isReference()) {
442             return getReference().eval(calr);
443         }
444         return super.eval(calr);
445     }
446
447
448     /**
449      * Evaluates this tallyset as a nested component of another
450      * tallyset. If is reference calls referred-to-tallyset's eval
451      * method.
452      * @throws BuildException if an error occurs during evaluation
453      **/

454     public boolean eval()
455         throws BuildException
456     {
457         if (isReference()) {
458             return getReference().eval();
459         }
460         return super.eval();
461     }
462
463
464     /**
465      * Default evaluation result handler method does nothing except
466      * log diagnostics information.
467      **/

468     protected void setEvalResult(boolean istrue, String JavaDoc listing)
469     {
470         if (istrue) {
471             log("TallySet ("+getId()+") evaluates TRUE",Project.MSG_DEBUG);
472         } else {
473             log("TallySet ("+getId()+") evaluates FALSE; condition in order "+listing,
474                 Project.MSG_DEBUG);
475         }
476     }
477
478
479     private Reference m_ref;
480 }
481
482 /* end-of-TallySet.java */
483
Popular Tags