KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > condition > CheckSetCondition


1 /**
2  * $Id: CheckSetCondition.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2002-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 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;
30
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.StringTokenizer JavaDoc;
34
35 import org.apache.tools.ant.Project;
36 import org.apache.tools.ant.taskdefs.condition.Condition;
37
38 import com.idaremedia.antx.AntXFixture;
39 import com.idaremedia.antx.AssertableProjectComponent;
40 import com.idaremedia.antx.ExportedProperties;
41 import com.idaremedia.antx.FlexString;
42 import com.idaremedia.antx.helpers.InnerString;
43 import com.idaremedia.antx.helpers.Strings;
44 import com.idaremedia.antx.helpers.Tk;
45 import com.idaremedia.antx.ownhelpers.InnerProperties;
46 import com.idaremedia.antx.parameters.Handling;
47 import com.idaremedia.antx.parameters.IgnoreWhitespaceEnabled;
48 import com.idaremedia.antx.parameters.IsA;
49 import com.idaremedia.antx.parameters.MalformedCheckEnabled;
50 import com.idaremedia.antx.parameters.PropertySource;
51 import com.idaremedia.antx.parameters.TrimEnabled;
52
53 /**
54  * Starting implementation for any condition that evaluates a set of nested
55  * items (properties, variables, references, etc.) against some simple existence
56  * condition.
57  *
58  * @since JWare/AntX 0.2 (pulled up from AllSet AntX 0.5)
59  * @author ssmc, &copy;2002-2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
60  * @version 0.5
61  * @.safety single
62  * @.group api,infra
63  **/

64
65 public abstract class CheckSetCondition extends AssertableProjectComponent
66     implements Condition, TrimEnabled, IgnoreWhitespaceEnabled, MalformedCheckEnabled
67 {
68     /**
69      * Initializes new set check condition instance.
70      * @param iam CV-label (non-null)
71      **/

72     protected CheckSetCondition(String JavaDoc iam)
73     {
74         super(iam);
75     }
76
77
78     /**
79      * Initializes a set check condition for a set of existing properties.
80      * @param properties comma-delimited list of properties
81      * @param P condition's project
82      **/

83     protected CheckSetCondition(String JavaDoc iam, String JavaDoc properties, final Project P)
84     {
85         super(iam);
86         setProject(P);
87         setProperties(properties);
88     }
89
90
91
92     /**
93      * Marks this condition as a negation. Evaluates <i>true</i>
94      * if <em>none</em> of the included items are set.
95      **/

96     protected void setNegate(boolean negate)
97     {
98         m_isNegate = negate;
99     }
100
101
102     /**
103      * Returns <i>true</i> if this is a negative test.
104      **/

105     public final boolean isNegate()
106     {
107         return m_isNegate;
108     }
109     
110
111
112     /**
113      * Tells this conditon to look for items containing positive
114      * boolean string values.
115      * @since JWare/AntX 0.5
116      **/

117     protected final void setTruesOnly()
118     {
119         m_truesOnly = true;
120     }
121     
122     
123     
124     /**
125      * Returns whether this condition should check all candidate
126      * items for a positive boolean string. Is false by default.
127      * @since JWare/AntX 0.5
128      **/

129     protected final boolean isTruesOnly()
130     {
131         return m_truesOnly;
132     }
133
134
135 // ---------------------------------------------------------------------------------------
136
// Parameters:
137
// ---------------------------------------------------------------------------------------
138

139     /**
140      * Sets property name updated by evaluation method.
141      * @param property the property's name (non-null)
142      **/

143     public void setTrueProperty(String JavaDoc property)
144     {
145         m_updateProperty = property;
146         m_isVar = false;
147     }
148
149
150     /**
151      * Returns property name updated by evaluation method. Returns
152      * <i>null</i> if never set or value is an exported property.
153      **/

154     public final String JavaDoc getTrueProperty()
155     {
156         return m_isVar ? null : m_updateProperty;
157     }
158
159
160     /**
161      * Sets exported property name updated by evaluation method.
162      * @param variable the exported property's name (non-null)
163      **/

164     public void setTrueVariable(String JavaDoc variable)
165     {
166         m_updateProperty = variable;
167         m_isVar = true;
168     }
169
170
171     /**
172      * Returns the exported property name update by evaluation method.
173      * Returns <i>null</i> if never set or value is a regular property.
174      * @see #isUpdateVariable
175      **/

176     public final String JavaDoc getTrueVariable()
177     {
178         return m_isVar ? m_updateProperty : null;
179     }
180
181
182     /**
183      * Returns <i>true</i> if this condition is setup to update an
184      * exported property on evaluation.
185      **/

186     public final boolean isUpdateVariable()
187     {
188         return m_isVar;
189     }
190
191
192     /**
193      * Sets whether a property containing only whitespace is
194      * considered "not-set."
195      **/

196     public void setWhitespace(Handling response)
197     {
198         m_ignoreWS= Handling.IGNORE.equals
199             (Handling.simplifyIgnoreOrNot(response,Handling.ACCEPT));
200     }
201
202
203     /**
204      * Returns <i>true</i> if property values of all whitespace
205      * should be ignored (as if not set). Defaults <i>false</i>.
206      **/

207     public final boolean ignoreWhitespace()
208     {
209         return m_ignoreWS;
210     }
211
212
213     /**
214      * Sets whether included item values will be trimmed before
215      * considered "not-set."
216      * @param trim <i>true</i> if trim item values
217      **/

218     public void setTrim(boolean trim)
219     {
220         m_willTrim = trim;
221     }
222
223
224     /**
225      * Returns <i>true</i> if include values will be trimmed
226      * before evaluated. Defaults to <i>false</i>.
227      **/

228     public final boolean willTrim()
229     {
230         return m_willTrim;
231     }
232
233
234     /**
235      * Tells this condition whether properties must be completely
236      * resolved to be considered "set". If the choice is
237      * either "ignore" or "accept" then unresolved property
238      * values are considered okidoki (set).
239      * @since JWare/AntX 0.4
240      **/

241     public void setMalformed(Handling response)
242     {
243         // malformed="accept|ignore"
244
if (Handling.isYes(response,Handling.REJECT)) {
245             m_checkUnresolved= false;
246         }
247         // malformed="reject|balk|inherit"
248
else {
249             m_checkUnresolved= true;
250         }
251     }
252
253
254     /**
255      * Returns how this condition will handle unresolved
256      * properties. Returnes either "accept" or "reject".
257      * @since JWare/AntX 0.4
258      **/

259     public final Handling getMalformedHandling()
260     {
261         return m_checkUnresolved ? Handling.REJECT : Handling.ACCEPT;
262     }
263
264
265     /**
266      * Short-hand for adding a list of properties to this condition.
267      * @param properties comma-delimited list of properties
268      **/

269     public final void setProperties(String JavaDoc properties)
270     {
271         StringTokenizer JavaDoc st= new StringTokenizer JavaDoc(properties,",");
272         InnerString holder= new InnerString();
273         while (st.hasMoreTokens()) {
274             holder.setValue(st.nextToken());
275             includeItem(new FlexString(holder.toString(getProject()),true));
276         }
277     }
278
279
280     /**
281      * Short-hand for adding a nested pattern-based property
282      * set to this condition. The property set's domain is limited
283      * to the {@linkplain PropertySource#SCRIPT script}.
284      * @param pattern the pattern against which all script
285      * properties checked (non-null)
286      * @.sideeffect Turns malformed='reject' on automatically
287      * @since JWare/AntX 0.4
288      **/

289     public final void setPropertiesLike(String JavaDoc pattern)
290     {
291         InnerProperties holder = new InnerProperties();
292         holder.setProject(getProject());
293         holder.setLike(pattern);
294         holder.setDomain(PropertySource.SCRIPT.getValue());
295         addConfiguredProperties(holder);
296     }
297
298
299
300     /**
301      * Short-hand for adding a list of variables to this condition.
302      * @param variables comma-delimited list of variables
303      * @since JWare/AntX 0.5
304      **/

305     public final void setVariables(String JavaDoc variables)
306     {
307         StringTokenizer JavaDoc st= new StringTokenizer JavaDoc(variables,",");
308         InnerString holder= new InnerString();
309         while (st.hasMoreTokens()) {
310             holder.setValue(st.nextToken());
311             FlexString item = new FlexString(holder.toString(getProject()),false);
312             item.setIsExported(true);
313             includeItem(item);
314         }
315     }
316
317
318
319     /**
320      * Short-hand for adding a list of references to this condition.
321      * @param references comma-delimited list of variables
322      * @since JWare/AntX 0.5
323      **/

324     public final void setReferences(String JavaDoc references)
325     {
326         StringTokenizer JavaDoc st= new StringTokenizer JavaDoc(references,",");
327         InnerString holder= new InnerString();
328         while (st.hasMoreTokens()) {
329             holder.setValue(st.nextToken());
330             FlexString item = new FlexString(holder.toString(getProject()),false);
331             item.setIsReference(true);
332             includeItem(item);
333         }
334     }
335
336
337 // ---------------------------------------------------------------------------------------
338
// Nested Elements:
339
// ---------------------------------------------------------------------------------------
340

341     /**
342      * Returns name updated by evaluation method (either as property
343      * or as variable). Returns <i>null</i> if neither an update property
344      * nor an update variable have been set.
345      **/

346     protected final String JavaDoc getUpdateProperty()
347     {
348         return m_updateProperty;
349     }
350
351
352     /**
353      * Returns this condition's list of nested items. Never returns
354      * <i>null</i>.
355      **/

356     List JavaDoc getIncludes()
357     {
358         return m_includedItems;
359     }
360
361
362     /**
363      * Returns a readonly iterator for this condition's list of
364      * nested property definitions. Never returns <i>null</i>. The
365      * returned iterator also returns contents of nested property
366      * sets (in order) as flex values.
367      * @since JWare/AntX 0.4
368      **/

369     protected final Iterator JavaDoc getIncludesIterator()
370     {
371         return new FlexIncludesIterator(new FlexOuterCondition() {
372                 public Project getProject() {
373                     return CheckSetCondition.this.getProject();
374                 }
375                 public List JavaDoc getIncludes() {
376                     return CheckSetCondition.this.getIncludes();
377                 }
378                 public boolean willTrim() {
379                     return CheckSetCondition.this.willTrim();
380                 }
381                 public boolean ignoreWhitespace() {
382                     return false;
383                 }
384             });
385     }
386
387
388     /**
389      * Adds the given item to this condition's set of items.
390      * This object assumes ownership of flex string object.
391      **/

392     protected final void includeItem(FlexString s)
393     {
394         s.setProject(getProject());
395         s.setTrim(willTrim());
396         getIncludes().add(s);
397     }
398
399
400     /**
401      * Adds the given properties declaration to this set.
402      * Properties are determined at evaluation time. This object
403      * assumes ownership of declaration.
404      * @param decl properties declaration (non-null)
405      * @since JWare/AntX 0.4
406      **/

407     public final void includeItem(InnerProperties decl)
408     {
409         getIncludes().add(decl);
410     }
411
412
413     /**
414      * Adds a new property to this set. Property's value
415      * will be determined at evaluation time.
416      * @param holder property element
417      **/

418     public void addConfiguredProperty(InnerString holder)
419     {
420         require_(holder!=null,"addP- nonzro prop");
421         includeItem(new FlexString(holder.toString(getProject()),true));
422     }
423
424
425     /**
426      * Adds a new exported property to this set. Variable's
427      * value will be determined at evaluation time.
428      * @param holder property element
429      **/

430     public void addConfiguredVariable(InnerString holder)
431     {
432         require_(holder!=null,"addV- nonzro xprop");
433         FlexString s = new FlexString(holder.toString(getProject()));
434         s.setIsExported(true);
435         includeItem(s);
436     }
437
438
439     /**
440      * Synonym for {@linkplain #addConfiguredVariable
441      * addConfiguredVariable}.
442      **/

443     public final void addConfiguredVar(InnerString holder)
444     {
445         addConfiguredVariable(holder);
446     }
447
448
449     /**
450      * Adds a new reference to this set. Reference's value determined
451      * at evaluation time.
452      * @param holder reference element
453      **/

454     public void addConfiguredReference(InnerString holder)
455     {
456         require_(holder!=null,"addRef- nonzro refid");
457         FlexString s = new FlexString(holder.toString(getProject()));
458         s.setIsReference(true);
459         includeItem(s);
460     }
461
462
463     /**
464      * Adds a collection of properties to this set. Properties
465      * are determined at evaluation time.
466      * @param decl properties declaration (non-null)
467      * @since JWare/AntX 0.4
468      * @.sideeffect Turns malformed='reject' on automatically
469      **/

470     public void addConfiguredProperties(InnerProperties decl)
471     {
472         require_(decl!=null,"addProps- nonzro decl");
473         includeItem(decl);
474         setMalformed(Handling.REJECT);
475     }
476
477
478 // ---------------------------------------------------------------------------------------
479
// Evaluation
480
// ---------------------------------------------------------------------------------------
481

482     /**
483      * Returns <i>true</i> if all of this condition's nested items have
484      * been explicitly set in its project's environment. If this condition's
485      * {@linkplain #setTruesOnly trues only} option has been set. Items must
486      * exist and be equal to a positive boolean string.
487      **/

488     public boolean eval()
489     {
490         verifyInProject_("eval");
491
492         final Project P = getProject();
493         boolean notsets = isNegate();
494         boolean istrue = notsets ? true : false;
495         int ALL = getIncludes().size();
496
497         if (ALL!=0) {
498             Boolean JavaDoc answer = null;
499             boolean ignoreWS = ignoreWhitespace();
500             boolean iffTrue = isTruesOnly();
501             Iterator JavaDoc itr = getIncludesIterator();
502             int N=0;
503
504             while (itr.hasNext()) {
505                 FlexString xv = (FlexString)itr.next();
506                 String JavaDoc v = null;
507
508                 if (xv.isProperty() && m_checkUnresolved) {
509                     if (LocalPropertyExaminer.verifyProperty(P,xv)) {
510                         v = xv.getValue(P);
511                     } else {
512                         log("CheckSet dropped '"+xv.get()+"' property because"+
513                             " contains unresolved references", Project.MSG_DEBUG);
514                     }
515                 } else {
516                     v = xv.getValue(P);
517                 }
518                 N++;
519                 if (v!=null && ignoreWS && Tk.isWhitespace(v)) {
520                     v = null;
521                 }
522                 if (v==null) {
523                     if (notsets) { continue; }
524                     answer= Boolean.FALSE;
525                     break;
526                 } else if (notsets) {
527                     if (!iffTrue || (Tk.string2PosBool(v)==Boolean.TRUE)) {
528                         answer= Boolean.FALSE;
529                         break;
530                     }
531                 } else if (iffTrue) {
532                     if (Tk.string2PosBool(v)!=Boolean.TRUE) {
533                         answer= Boolean.FALSE;
534                         break;
535                     }
536                 }
537             }
538             if (answer!=null) {
539                 istrue = answer.booleanValue();
540             } else {
541                 istrue = true/*N==ALL*/;
542             }
543         }
544
545         if (istrue && m_updateProperty!=null) {
546             String JavaDoc what = Strings.TRUE;
547             if (isUpdateVariable()) {
548                 log("CheckSet was true; setting true-variable '"+m_updateProperty+"'",
549                     Project.MSG_DEBUG);
550                 ExportedProperties.set(m_updateProperty,what);
551             } else {
552                 log("CheckSet was true; setting true-property '"+m_updateProperty+"'",
553                     Project.MSG_DEBUG);
554                 getProject().setNewProperty(m_updateProperty,what);
555             }
556         }
557
558         return istrue;
559     }
560
561
562
563     /**
564      * Common utility method for setting a checkset condition from a
565      * short hand value URI. To differentiate between a properties,
566      * variables, etc&#46;, set the URI's query string to the correct
567      * "isa" setting; for example:
568      * <span class="src">$alltrue:@(list)?variable</span>.
569      * @param uriFragment the value URI (resolved) (non-null)
570      **/

571     protected void xsetFromURIFragment(String JavaDoc uriFragment)
572     {
573         String JavaDoc list = uriFragment;
574         IsA isa= IsA.PROPERTY;
575
576         int i = uriFragment.lastIndexOf("?");
577         if (i>0) {
578             list = uriFragment.substring(0,i++);
579             if (i<uriFragment.length()) {
580                 String JavaDoc isaName = uriFragment.substring(i);
581                 isa = IsA.from(isaName,isa);
582             }
583         }
584         switch (isa.getIndex()) {
585             case IsA.VARIABLE_INDEX: {
586                 setVariables(list);
587                 break;
588             }
589             case IsA.REFERENCE_INDEX:{
590                 setReferences(list);
591                 break;
592             }
593             default: {
594                 setProperties(list);
595             }
596         }
597     }
598
599
600     private String JavaDoc m_updateProperty;
601     private boolean m_isVar;
602     private boolean m_isNegate;
603     private boolean m_ignoreWS, m_willTrim;
604     private List JavaDoc m_includedItems= AntXFixture.newList(5);
605     private boolean m_checkUnresolved;
606     private boolean m_truesOnly;
607 }
608
609 /* end-of-CheckSetCondition.java */
610
Popular Tags