KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: IsBoolean.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2003-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.SynonymsEnabled;
35
36 /**
37  * Simple Is-A-Boolean condition check. Example usage:
38  * <pre>
39  * &lt;isboolean value="${junit.present}"/&gt;
40  * &lt;isboolean property="is.dist.build"/&gt;
41  * &lt;isboolean variable="defaults.disabled" synonyms="off"/&gt;
42  *</pre>
43  *
44  * @since JWare/AntX 0.2
45  * @author ssmc, &copy;2003-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
46  * @version 0.5
47  * @.safety single
48  * @.group api,infra
49  **/

50
51 public final class IsBoolean extends SimpleFlexCondition
52     implements IgnoreCaseEnabled, SynonymsEnabled
53 {
54     /**
55      * Initializes new IsBoolean condition. A value type must be
56      * specified before this condition is evaluated.
57      **/

58     public IsBoolean()
59     {
60         super();
61     }
62
63
64     /**
65      * Initializes a fully-defined IsBoolean condition.
66      * @param property the property against which condition checks
67      **/

68     public IsBoolean(String JavaDoc property)
69     {
70         setProperty(property);
71     }
72
73
74     /**
75      * Initializes an IsBoolean condition to either a property or
76      * a variable value check.
77      * @param value the property or varaible against which condition checks
78      * @param isP <i>true</i> if <code>value</code> is property's name
79      **/

80     public IsBoolean(String JavaDoc value, boolean isP)
81     {
82         if (isP) {
83             setProperty(value);
84         } else {
85             setVariable(value);
86         }
87     }
88
89
90     /**
91      * Sets whether value should be lowercased before checked.
92      **/

93     public void setIgnoreCase(boolean ignore)
94     {
95         getValueHelper().setIgnoreCase(ignore);
96     }
97
98
99     /**
100      * Returns <i>true</i> if this condition will ignore value's
101      * case when it's checked. Defaults <i>false</i>.
102      **/

103     public final boolean isIgnoreCase()
104     {
105         return getValueHelper().isIgnoreCase();
106     }
107
108
109     /**
110      * Sets whether value will be checked for boolean synonyms
111      * like "<i>off</i>" or "<i>yes</i>". Defaults <i>on</i>.
112      * @param allowAll <i>true</i> if synonyms are enabled (default)
113      **/

114     public void setSynonyms(boolean allowAll)
115     {
116         m_allowAll = allowAll;
117     }
118
119
120     /**
121      * Returns <i>true</i> if value allowed to be a boolean synonyms.
122      * Defaults <i>true</i>.
123      **/

124     public final boolean allowSynonyms()
125     {
126         return m_allowAll;
127     }
128
129
130
131     /**
132      * Sets this condition to evaluate a literal value as-is.
133      * @param value the literal value to check
134      **/

135     public void setValue(String JavaDoc value)
136     {
137         require_(value!=null,"setValu- nonzro");
138         setLiteral(value);
139     }
140
141
142
143     /**
144      * Returns <i>true</i> if this condition's property, reference,
145      * or variable value matches a boolean value name (explicitly).
146      **/

147     public boolean eval()
148     {
149         verifyCanEvaluate_("eval");
150
151         String JavaDoc value = getValueHelper().getValue();
152         if (value==null) {
153             return false;
154         }
155
156         boolean isbool;
157         if (allowSynonyms()) {
158             isbool = Tk.string2PosBool(value).booleanValue() ||
159                 !Tk.string2NegBool(value).booleanValue();
160         } else {
161             isbool = Strings.TRUE.equals(value) ||
162                 Strings.FALSE.equals(value);
163         }
164         return isbool;
165     }
166
167
168     private boolean m_allowAll=true;
169 }
170
171 /* end-of-IsBoolean.java */
172
Popular Tags