KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > ownhelpers > OptionalExecuteHelper


1 /**
2  * $Id: OptionalExecuteHelper.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2004-2005 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 (LGPL) as published
8  * by the Free Software Foundation; either version 2.1 of the License, or (at your option)
9  * any 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 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 GNU 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.ownhelpers;
30
31 import java.util.Iterator JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import org.apache.tools.ant.Project;
35 import org.apache.tools.ant.Task;
36
37 import com.idaremedia.antx.AntX;
38 import com.idaremedia.antx.Iteration;
39 import com.idaremedia.antx.go.Go;
40 import com.idaremedia.antx.go.Iff;
41 import com.idaremedia.antx.go.IffAll;
42 import com.idaremedia.antx.go.IffAnt;
43 import com.idaremedia.antx.go.IffOs;
44 import com.idaremedia.antx.go.IffValue;
45 import com.idaremedia.antx.parameters.Conditional;
46 import com.idaremedia.antx.parameters.FlexConditional;
47
48 /**
49  * Common implementation of the {@linkplain FlexConditional} interface. Public-facing
50  * items can implement all or part of an AntX conditional interface by delegating to
51  * an instance of this class. Note that the owning element must synchronize this helper's
52  * project reference with its own. This class is <em>not</em> synchronized against
53  * concurrent modification.
54  *
55  * @since JWare/AntX 0.4
56  * @author ssmc, &copy;2004-2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
57  * @version 0.5
58  * @.safety single
59  * @.group impl,helper
60  * @.caveat If your task wants to track why it did not execute (ala logs) it must
61  * stop testing on the first failed test because subsequent tests can
62  * erase the "failure reason string" from the helper's memory.
63  **/

64
65 public class OptionalExecuteHelper extends ProjectDependentSkeleton
66     implements FlexConditional
67 {
68     private static final String JavaDoc IAM_=
69         AntX.AntX+"OptionalExecuteHelper:";
70
71
72     /**
73      * Initializes a new helper.
74      **/

75     public OptionalExecuteHelper()
76     {
77     }
78
79
80     /**
81      * Initializes a new helper bound to a particular
82      * project.
83      * @param P the project
84      **/

85     public OptionalExecuteHelper(Project P)
86     {
87         setProject(P);
88     }
89
90
91     /**
92      * Initializes a new helper based on a placeholder item's
93      * execution attributes. The attribute names must be normalized
94      * to all lowercase (US locale).
95      * @param attrs collection of controller's parameters (non-null)
96      * @.impl Only 'Conditional' and 'PlatformConditional' and "true|false".
97      **/

98     public OptionalExecuteHelper(Map JavaDoc attrs)
99     {
100         AntX.require_(attrs!=null,IAM_,"ctor- nonzro attrs");
101         String JavaDoc v;
102
103         v = (String JavaDoc)attrs.get("if");
104         if (v!=null) { setIf(v); }
105         v = (String JavaDoc)attrs.get("unless");
106         if (v!=null) { setUnless(v); }
107
108         v = (String JavaDoc)attrs.get("true");
109         if (v!=null) { setTrue(v); }
110         v = (String JavaDoc)attrs.get("false");
111         if (v!=null) { setFalse(v); }
112
113         v = (String JavaDoc)attrs.get("ifos");
114         if (v!=null) { setIfOS(v); }
115         v = (String JavaDoc)attrs.get("unlessos");
116         if (v!=null) { setUnlessOS(v); }
117
118         v = (String JavaDoc)attrs.get("ifantlike");
119         if (v!=null) { setIfAntLike(v); }
120         v = (String JavaDoc)attrs.get("unlessantlike");
121         if (v!=null) { setUnlessAntLike(v); }
122    }
123
124
125     /**
126      * Adds a custom if-condition to this helper.
127      * @param t the test (non-null)
128      **/

129     public void addIfTest(Go.Test t)
130     {
131         m_ifTests.add(t);
132     }
133
134
135     /**
136      * Adds a custom unless-condition to this helper.
137      * @param t the test (non-null)
138      **/

139     public void addUnlessTest(Go.Test t)
140     {
141         m_unlessTests.add(t);
142     }
143
144
145     /**
146      * Clears <em>all</em> of this helper's tests.
147      **/

148     public final void clearTests()
149     {
150         m_ifTests.clear();
151         m_unlessTests.clear();
152     }
153
154
155     public void setIf(String JavaDoc property)
156     {
157         addIfTest(new Iff.Exists(property));
158     }
159
160
161     public void setUnless(String JavaDoc property)
162     {
163         addUnlessTest(new Iff.NotExists(property));
164     }
165
166
167     public void setTrue(String JavaDoc booleanString)
168     {
169         addIfTest(new IffValue.IsTrue(booleanString));
170     }
171
172     public void setFalse(String JavaDoc booleanString)
173     {
174         addIfTest(new IffValue.IsNotTrue(booleanString));
175     }
176
177
178     public void setIfTrue(String JavaDoc property)
179     {
180         addIfTest(new Iff.IsTrue(property));
181     }
182
183
184     public void setUnlessTrue(String JavaDoc property)
185     {
186         addUnlessTest(new Iff.IsNotTrue(property));
187     }
188
189
190     public void setIfAll(String JavaDoc properties)
191     {
192         addIfTest(new IffAll.Exists(properties));
193     }
194
195
196     public void setUnlessAll(String JavaDoc properties)
197     {
198         addUnlessTest(new IffAll.NotExists(properties));
199     }
200
201
202     public void setIfAllTrue(String JavaDoc properties)
203     {
204         addIfTest(new IffAll.IsTrue(properties));
205     }
206
207
208     public void setUnlessAllTrue(String JavaDoc properties)
209     {
210         addUnlessTest(new IffAll.IsNotTrue(properties));
211     }
212
213
214     public void setIfOS(String JavaDoc choice)
215     {
216         addIfTest(new IffOs.Is(choice));
217     }
218
219
220     public void setUnlessOS(String JavaDoc choice)
221     {
222         addUnlessTest(new IffOs.IsNot(choice));
223     }
224
225
226     public void setIfAntLike(String JavaDoc versionRE)
227     {
228         addIfTest(new IffAnt.IsLike(versionRE));
229     }
230
231
232     public void setUnlessAntLike(String JavaDoc versionRE)
233     {
234         addUnlessTest(new IffAnt.IsNotLike(versionRE));
235     }
236
237
238     public String JavaDoc getIfProperty()
239     {
240         String JavaDoc p= "";
241         int N= m_ifTests.size();
242         if (N>0) {
243             if (N==1) {
244                 p= m_ifTests.get(0).getParameter();
245             }
246             //Else look for Conditional API's tests bits(?!)
247
else {
248                 Go.Test t= m_ifTests.get(Iff.Exists.class);
249                 if (t==null) {
250                     t= m_ifTests.get(Iff.IsTrue.class);
251                 }
252                 if (t!=null) {
253                     p = t.getParameter();
254                 }
255             }
256         }
257         return p;
258     }
259
260
261     public String JavaDoc getUnlessProperty()
262     {
263         String JavaDoc p= "";
264         int N= m_unlessTests.size();
265         if (N>0) {
266             if (N==1) {
267                 p= m_unlessTests.get(0).getParameter();
268             }
269             //Else look for Conditional API's tests bits(?!)
270
else {
271                 Go.Test t= m_unlessTests.get(Iff.NotExists.class);
272                 if (t==null) {
273                     t= m_unlessTests.get(Iff.IsNotTrue.class);
274                 }
275                 if (t!=null) {
276                     p = t.getParameter();
277                 }
278             }
279         }
280         return p;
281     }
282
283
284     /**
285      * Returns <i>null</i> if <em>all</em> of the given "go"
286      * tests are <i>true</i>; otherwise, will return a descriptive
287      * string of the first test that failed.
288      * @param P project under test (non-null)
289      * @param tests list of <span class="src">Go.Test</span>s.
290      * @see #getLastFailure
291      **/

292     public static String JavaDoc testTests(Project P, Go.TestList tests)
293     {
294         AntX.require_(P!=null && tests!=null,IAM_,"test- nonzro proj+list");
295         if (!tests.isEmpty()) {
296             Iterator JavaDoc itr= tests.iterator();
297             while (itr.hasNext()) {
298                 Go.Test t= (Go.Test)itr.next();
299                 if (!t.pass(P)) {
300                     return t.toString();
301                 }
302             }
303             itr = null;
304         }
305         return null;
306     }
307
308
309
310     /**
311      * Returns the last failed test's descriptive string.
312      * Will return <i>null</i> if this helper's test
313      * {@linkplain #testTests method} was never called or
314      * it did not fail (on last call).
315      **/

316     public final String JavaDoc getLastFailure()
317     {
318         return m_lastFailure;
319     }
320
321
322
323     /**
324      * Manually sets the last failure reason for this helper.
325      * @param lastFailure last failure string.
326      * @since JWare/AntX 0.5
327      **/

328     public final void setLastFailure(String JavaDoc lastFailure)
329     {
330         m_lastFailure = lastFailure==null ? "" : lastFailure;
331     }
332
333
334
335     /**
336      * Returns <i>true</i> if <em>all</em> of this helper's
337      * if-conditions are <i>true</i>. Returns <i>true</i> if
338      * no if-conditions exist.
339      * @.sideeffect If failure, updates this helper's last
340      * {@linkplain #getLastFailure failure} information.
341      **/

342     public boolean testIfCondition()
343     {
344         String JavaDoc failure = testTests(getProjectNoNull(), m_ifTests);
345         if (failure!=null) {
346             m_lastFailure = failure;
347         }
348         return failure==null;
349     }
350
351
352     /**
353      * Returns <i>true</i> if <em>all</em> of this helper's
354      * unless-conditions are <i>true</i>. Returns <i>true</i>
355      * if no unless-conditions exist.
356      * @.sideeffect If failure, updates this helper's last
357      * {@linkplain #getLastFailure failure} information.
358      **/

359     public boolean testUnlessCondition()
360     {
361         String JavaDoc failure = testTests(getProjectNoNull(), m_unlessTests);
362         if (failure!=null) {
363             m_lastFailure = failure;
364         }
365         return failure==null;
366     }
367
368
369     /**
370      * Returns <i>true</i> if this helper represents a simple
371      * (zero or single condition) if/unless test.
372      **/

373     public final boolean isSimple()
374     {
375         return m_ifTests.size()<=1 && m_unlessTests.size()<=1;
376     }
377
378
379
380     /**
381      * Returns <i>true</i> if this helper contains no tests.
382      * @since JWare/AntX 0.5
383      **/

384     public final boolean isEmpty()
385     {
386         return m_ifTests.isEmpty() && m_unlessTests.isEmpty();
387     }
388
389
390
391     /**
392      * Helper that shows why this helper said "no" (if/unless).
393      * Message is logged at requested level.
394      * @param from enclosing task (non-null)
395      * @param passedIf <i>true</i> if the 'if' bits passed
396      * @param lvl preferred log level
397      * @.impl Uses this helper's last recorded failure's
398      * information.
399      **/

400     public void logSkippedBecause(Task from,
401                                   boolean passedIf, int lvl)
402     {
403         Project P= getProject();
404
405         if (!passedIf) {
406             P.log(from, Iteration.uistrs().get
407                 ("task.skipped.if.failed", from.getTaskName(), getLastFailure()),
408                   lvl);
409         }
410         else {
411             P.log(from, Iteration.uistrs().get
412                 ("task.skipped.unless.failed", from.getTaskName(), getLastFailure()),
413                   lvl);
414         }
415     }
416
417
418
419     /**
420      * Based on current setting of a parameter's constraints,
421      * determines if caller should include parameter. This utility
422      * method works for any InnerNameValuePair that implements the
423      * standard AntX <span class="src">Conditional</span> interface.
424      * @param P caller's project (used to resolve constraint properties)
425      * @param param source of if/unless condition
426      * @return <i>true</i> if pass if/unless test; otherwise <i>false</i>
427      * @since JWare/AntX 0.5
428      **/

429     public static boolean include(final Project P, Conditional param)
430     {
431         boolean can = true;
432         String JavaDoc ifProp = param.getIfProperty();
433         String JavaDoc unProp = param.getUnlessProperty();
434
435         if (ifProp!=null || unProp!=null) {
436             OptionalExecuteHelper go = new OptionalExecuteHelper(P);
437             if (ifProp!=null) {
438                 go.setIf(ifProp);
439             }
440             if (unProp!=null) {
441                 go.setUnless(unProp);
442             }
443             can = go.testIfCondition() && go.testUnlessCondition();
444         }
445         return can;
446     }
447
448
449
450     private Go.TestList m_ifTests= Go.TestList.newList(4);
451     private Go.TestList m_unlessTests= Go.TestList.newList(4);
452     private String JavaDoc m_lastFailure;//None
453
}
454
455 /* end-of-OptionalExecuteHelper.java */
456
Popular Tags