KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: StringEquals.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 org.apache.tools.ant.BuildException;
32 import org.apache.tools.ant.Project;
33
34 import com.idaremedia.antx.FlexString;
35 import com.idaremedia.antx.StringEquality;
36 import com.idaremedia.antx.parameters.IgnoreCaseEnabled;
37 import com.idaremedia.antx.parameters.TrimEnabled;
38 import com.idaremedia.antx.parameters.ValueMatchEnabled;
39
40 /**
41  * Flexible String-Equals condition that evaluates <i>true</i> if an unknown value
42  * equals a predefined value. This condition allows script writers to include simple
43  * &lt;equals&gt; checks in rules where properties or variables may or may not
44  * exist before the rule is parsed; for instance:
45  * <pre>
46  * &lt;rule id="is.public.dist"&gt;
47  * &lt;require&gt;
48  * &lt;equals match="public" property="dist.type"/&gt;
49  * &lt;noneset&gt;
50  * &lt;property name ="build.number"&gt;
51  * &lt;property name ="disable.cvs"&gt;
52  * &lt;property name ="disable.clean"&gt;
53  * &lt;/noneset&gt;
54  * &lt;/require&gt;
55  * &lt;/rule&gt;
56  * </pre>
57  *
58  * @since JWare/AntX 0.3
59  * @author ssmc, &copy;2003-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
60  * @version 0.5
61  * @.safety multiple (after fully configured)
62  * @.group api,infra
63  * @see StringEquality
64  **/

65
66 public final class StringEquals extends FlexCondition
67     implements IgnoreCaseEnabled, TrimEnabled, ValueMatchEnabled
68 {
69     /**
70      * Initializes a new StringEquals condition. The new
71      * condition's match value and flex value must be defined
72      * before it is evaluated.
73      **/

74     public StringEquals()
75     {
76         super();
77         m_impl.setOperator(StringEquality.OP_EQUALS);
78         getValueHelper().setLenient(false);
79     }
80
81
82     /**
83      * Initializes the enclosing project of this condition.
84      * Also updates this condition's helper bits.
85      **/

86     public void setProject(Project P)
87     {
88         super.setProject(P);
89         m_impl.setProject(P);
90     }
91
92
93     /**
94      * Returns this equality condition's underlying unknown
95      * argument's flex string. Never returns <i>null</i>.
96      **/

97     protected final FlexString getValueHelper()
98     {
99         return m_impl.getUnknownValueGetter();
100     }
101
102
103 // ---------------------------------------------------------------------------------------
104
// Parameters:
105
// ---------------------------------------------------------------------------------------
106

107     /**
108      * Sets this condition's known to-be-equaled value.
109      * @param value value against which determined value is checked (non-null)
110      **/

111     public void setMatch(String JavaDoc value)
112     {
113         require_(value!=null,"setMatch- nonzro mtch valu");
114         m_impl.setKnownArg(value);
115     }
116
117
118     /**
119      * Returns value against which the determined value is checked.
120      * Returns <i>null</i> if never set.
121      **/

122     public final String JavaDoc getMatch()
123     {
124         return m_impl.getKnownArg();
125     }
126
127
128
129     /**
130      * Sets a to-be-checked literal value.
131      * @param value value to be matched (non-null)
132      **/

133     public final void setValue(String JavaDoc value)
134     {
135         require_(value!=null,"setValue- nonzro");
136         setLiteral(value);
137     }
138
139
140
141     /**
142      * Set whether the values should be trimmed of whitespace
143      * before they are compared.
144      **/

145     public void setTrim(boolean trim)
146     {
147         m_impl.setTrim(trim);
148     }
149
150
151     /**
152      * Returns <i>true</i> if the values will be trimmed before
153      * they are compared.
154      **/

155     public final boolean willTrim()
156     {
157         return m_impl.willTrim();
158     }
159
160
161
162     /**
163      * Set whether the values should be lower-cased before
164      * they are compared.
165      **/

166     public void setIgnoreCase(boolean ignore)
167     {
168         m_impl.setIgnoreCase(ignore);
169     }
170
171
172     /**
173      * Returns <i>true</i> if the values will be lower-cased
174      * before they are compared.
175      **/

176     public final boolean isIgnoreCase()
177     {
178         return m_impl.isIgnoreCase();
179     }
180
181
182 // ---------------------------------------------------------------------------------------
183
// Evaluation:
184
// ---------------------------------------------------------------------------------------
185

186     /**
187      * Returns <i>true</i> if the calculated value equals the expected
188      * value.
189      **/

190     public boolean eval()
191     {
192         verifyCanEvaluate_("eval");
193
194         return m_impl.eval();
195     }
196
197
198     /**
199      * Verifies that this condition is in valid project and has both its
200      * known and unknown values defined.
201      * @param calr caller's identifier
202      * @throws BuildException if not in project or all bits not defined
203      **/

204     protected void verifyCanEvaluate_(String JavaDoc calr)
205     {
206         super.verifyCanEvaluate_(calr);
207
208         if (getMatch()==null) {
209             String JavaDoc ermsg = uistrs().get("task.needs.this.attr",
210                                         getTypicalName(),"match");
211             log(ermsg,Project.MSG_ERR);
212             throw new BuildException(ermsg);
213         }
214     }
215
216
217     private final StringEquality m_impl = new StringEquality();
218 }
219
220 /* end-of-StringEquals.java */
221
Popular Tags