KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: IsSetTrue.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;
30
31 import com.idaremedia.antx.helpers.Strings;
32 import com.idaremedia.antx.helpers.Tk;
33 import com.idaremedia.antx.parameters.IgnoreCaseEnabled;
34 import com.idaremedia.antx.parameters.IsA;
35 import com.idaremedia.antx.parameters.SynonymsEnabled;
36
37 /**
38  * Flexible Is-Set-True condition check. This condition helps get around the problem
39  * of chained conditions where a later condition's evaluation depends on the
40  * side-effects of a previous conditon's evaluation (like say its property flag) but
41  * that previous condition hasn't been evaluated when Ant parses (and replaces)
42  * attribute values. For instance, without this condition the following would never
43  * evaluate <i>true</i> like you might think it would:<pre>
44  * &lt;tally trueproperty="tally.true"&gt;
45  * &lt;tally trueproperty="subtally.true"&gt;
46  * &lt;istrue value="true"/&gt;
47  * &lt;/tally&gt;
48  * &lt;istrue value="${subtally.true}"/&gt; <b>&lt;-- this is always false</b>
49  * &lt;/tally&gt;
50  * &lt;assert istrue="${tally.true}"/&gt; <b>&lt;-- this will always fail</b>
51  * </pre>
52  * Instead you have to write:<pre>
53  * &lt;tally trueproperty="tally.true"&gt;
54  * &lt;tally trueproperty="subtally.true"&gt;
55  * &lt;istrue value="true"/&gt;
56  * &lt;/tally&gt;
57  * &lt;issettrue property="subtally.true"/&gt; <b>&lt;-- this will be true</b>
58  * &lt;/tally&gt;
59  * &lt;assert issettrue="tally.true"/&gt; <b>&lt;-- this will pass</b>
60  * </pre>
61  * More examples:
62  * <pre>
63  * &lt;issettrue property="jdk14.present" synonyms="off"/&gt;
64  * &lt;issettrue property="cvs.disable" ignorecase="true"/&gt;
65  * &lt;issettrue reference="tally.debug"/&gt;
66  *
67  * -OR, to just work like &lt;istrue&gt; does-
68  * &lt;issettrue value="${jdk14.present}"/&gt;
69  *</pre>
70  *
71  * @since JWare/AntX 0.2
72  * @author ssmc, &copy;2002-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
73  * @version 0.5
74  * @.safety single
75  * @.group api,infra
76  **/

77
78 public class IsSetTrue extends SimpleFlexCondition
79     implements IgnoreCaseEnabled, SynonymsEnabled
80 {
81     /**
82      * Initializes new IsSetTrue condition. Will return <i>false</i>
83      * if item-under-test is not set.
84      **/

85     public IsSetTrue()
86     {
87     }
88
89
90     /**
91      * Initializes pre-defined IsSetTrue condition.
92      * @param property the property against which condition checks
93      **/

94     public IsSetTrue(String JavaDoc property)
95     {
96         setProperty(property);
97     }
98
99
100     /**
101      * Initializes pre-defined IsSetTrue condition with variable or
102      * property
103      * @param value the property against which condition checks
104      * @param isP <i>true</i> if this property otherwise is a variable
105      **/

106     public IsSetTrue(String JavaDoc value, boolean isP)
107     {
108         if (isP) {
109             setProperty(value);
110         } else {
111             setVariable(value);
112         }
113     }
114
115
116     /**
117      * Sets whether property value should be lowercased before
118      * comparision.
119      **/

120     public void setIgnoreCase(boolean ignore)
121     {
122         getValueHelper().setIgnoreCase(ignore);
123     }
124
125
126     /**
127      * Returns <i>true</i> if this condition will ignore case
128      * of property values when comparing to "<i>true</i>."
129      **/

130     public final boolean isIgnoreCase()
131     {
132         return getValueHelper().isIgnoreCase();
133     }
134
135
136
137     /**
138      * Sets whether <i>true</i> synonyms like <i>yes</i> are
139      * acceptable. Defaults <i>true</i>.
140      **/

141     public void setSynonyms(boolean allow)
142     {
143         m_allowAll = allow;
144     }
145
146
147     /**
148      * Returns <i>true</i> if this condition will allow
149      * "<i>true</i>" synonyms like "<i>on</i>".
150      **/

151     public final boolean allowSynonyms()
152     {
153         return m_allowAll;
154     }
155
156
157
158     /**
159      * Sets this condition to evaluate a literal value as-is.
160      * This parameter allows this condition work exactly like
161      * standard &lt;istrue&gt; condition.
162      * @param value the literal value to compare
163      **/

164     public void setValue(String JavaDoc value)
165     {
166         require_(value!=null,"setValu- nonzro");
167         setLiteral(value);
168     }
169
170
171
172     /**
173      * Marks this condition's property as the default field
174      * set by a value URI.
175      * @since JWare/AntX 0.5
176      * @return IsA.PROPERTY always.
177      */

178     protected IsA getDefaultIsAForURI()
179     {
180         return IsA.PROPERTY;
181     }
182
183
184
185     /**
186      * Returns <i>true</i> if the condition's property is
187      * defined as the string "<i>true<i>".
188      **/

189     public boolean eval()
190     {
191         verifyCanEvaluate_("eval");
192
193         String JavaDoc value = getValueHelper().getValue();
194
195         return allowSynonyms() ? Tk.booleanFrom(value) :
196             Strings.TRUE.equals(value);
197     }
198
199
200     private boolean m_allowAll=true;//NB:defaults yes
201
}
202
203 /* end-of-IsSetTrue.java */
204
Popular Tags