KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: FlexCondition.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 org.apache.tools.ant.BuildException;
32 import org.apache.tools.ant.Project;
33 import org.apache.tools.ant.taskdefs.condition.Condition;
34
35 import com.idaremedia.antx.AntX;
36 import com.idaremedia.antx.AssertableProjectComponent;
37 import com.idaremedia.antx.FlexString;
38 import com.idaremedia.antx.helpers.Tk;
39 import com.idaremedia.antx.parameters.FlexValueSupport;
40
41 /**
42  * A condition whose unknown value can be either an inlined literal value, a property's
43  * value, a variable's value, or a reference's value. Property, variable, and reference
44  * values are determined at evaluation time. FlexConditions are useful when embedded
45  * inside shareable build rules because their checked-values are determined when the
46  * condition is evaluated not as the build file is parsed.
47  *
48  * @since JWare/AntX 0.2
49  * @author ssmc, &copy;2002-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
50  * @version 0.5
51  * @.safety single
52  * @.group impl,infra
53  **/

54
55 public abstract class FlexCondition extends AssertableProjectComponent
56     implements Condition, FlexValueSupport
57 {
58     /**
59      * Initializes a new flex condition.
60      **/

61     protected FlexCondition()
62     {
63         super(AntX.conditions);
64     }
65
66
67     /**
68      * Initializes a new CV-labeled flex condition.
69      * @param iam CV-label (non-null)
70      **/

71     protected FlexCondition(String JavaDoc iam)
72     {
73         super(iam);
74     }
75
76
77
78     /**
79      * Returns underlying flex value as-is. The returned value is
80      * one of: a literal value (used as-is), a property name,
81      * a variable name, or a reference identifier.
82      **/

83     public final String JavaDoc getFlexValue()
84     {
85         return getValueHelper().get();
86     }
87
88
89
90     /**
91      * Returns flex value resolved of any property references. The
92      * returned value is one of: a literal value (used as-is), a
93      * property name, a variable name, or a reference identifier.
94      * @since JWare/AntX 0.4
95      **/

96     public final String JavaDoc getResolvedFlexValue()
97     {
98         return getValueHelper().sourceString(getProject());
99     }
100
101
102
103     /**
104      * Sets a property whose value is to be evaluated
105      * by this condition. Property isn't read until this condition
106      * is evaluated.
107      * @param property the property's name (non-null)
108      **/

109     public void setProperty(String JavaDoc property)
110     {
111         setLiteral(property);
112         getValueHelper().setIsProperty(true);
113     }
114
115
116     /**
117      * Returns <i>true</i> if this condition is setup for a
118      * property value check.
119      **/

120     public final boolean isProperty()
121     {
122         return getValueHelper().isProperty();
123     }
124
125
126     /**
127      * Returns property name used by evaluation method. Returns
128      * <i>null</i> if never set or value is an exported property.
129      **/

130     public final String JavaDoc getProperty()
131     {
132         return isProperty() ? getValueHelper().get() : null;
133     }
134
135
136
137     /**
138      * Sets exported property name used by evaluation method.
139      * Property's value determined at evaluation time.
140      * @param exportedproperty the exported property's name (non-null)
141      **/

142     public void setVariable(String JavaDoc exportedproperty)
143     {
144         setLiteral(exportedproperty);
145         getValueHelper().setIsExported(true);
146     }
147
148
149     /**
150      * Synonym for {@linkplain #setVariable setVariable}.
151      **/

152     public final void setVar(String JavaDoc exportedproperty)
153     {
154         setVariable(exportedproperty);
155     }
156
157
158     /**
159      * Returns <i>true</i> if this condition is setup for an
160      * exported property check.
161      **/

162     public final boolean isVariable()
163     {
164         return getValueHelper().isExported();
165     }
166
167
168     /**
169      * Returns the exported property name used by evaluation
170      * method. Returns <i>null</i> if never set or value is a regular
171      * property.
172      * @see #isVariable
173      **/

174     public final String JavaDoc getVariable()
175     {
176         return isVariable() ? getValueHelper().get() : null;
177     }
178
179
180
181     /**
182      * Sets a reference whose value is to be evaluated
183      * by this condition. Reference isn't read until this condition
184      * is evaluated.
185      * @param refid the reference's name (non-null)
186      **/

187     public void setReference(String JavaDoc refid)
188     {
189         setLiteral(refid);
190         getValueHelper().setIsReference(true);
191     }
192
193
194     /**
195      * Returns <i>true</i> if this condition is setup for a
196      * reference value check.
197      **/

198     public final boolean isReference()
199     {
200         return getValueHelper().isReference();
201     }
202
203
204     /**
205      * Returns reference name used by evaluation method. Returns
206      * <i>null</i> if never set or check isn't for a reference.
207      **/

208     public final String JavaDoc getReference()
209     {
210         return isReference() ? getValueHelper().get() : null;
211     }
212
213
214
215     /**
216      * Sets this condition's to-be-evaluated literal string.
217      * Subclass should determine under what parameter a literal
218      * is exposed (for example, 'value' or 'name' or 'spam').
219      * @param rawvalue value to be matched (non-null)
220      **/

221     public void setLiteral(String JavaDoc rawvalue)
222     {
223         getValueHelper().set(rawvalue);
224         getValueHelper().setIsLiteral();//NB:clr all is-a-flags!
225
}
226
227
228     /**
229      * Returns <i>true</i> if this condition is setup for an
230      * literal (as-is) value check.
231      **/

232     public final boolean isLiteral()
233     {
234         return getValueHelper().isLiteral();
235     }
236
237
238     /**
239      * Returns literal value used by this evaluation method or
240      * <i>null</i> if never set or value is not a literal (to be used
241      * as-is).
242      **/

243     public final String JavaDoc getLiteral()
244     {
245         return isLiteral() ? getValueHelper().get() : null;
246     }
247
248
249
250     /**
251      * Verifies that this condition's value has been explicitly
252      * defined.
253      * @param required [optional] list of possible value parameters; use
254      * "<i>null</i>" for default list
255      * @throws BuildException if value not defined
256      **/

257     protected void verifyIsDefined_(String JavaDoc required)
258     {
259         if (getValueHelper().isUndefined()) {
260             if (required==null) {
261                 required="property|variable|reference";
262             }
263             String JavaDoc ermsg = uistrs().get("task.needs.this.attr",
264                                         getTypicalName(),required);
265             log(ermsg,Project.MSG_ERR);
266             throw new BuildException(ermsg);
267         }
268     }
269
270
271
272     /**
273      * Call to verify that this condition can proceed with evaluation.
274      * By default ensures this condition is in a valid project and
275      * has a defined value.
276      * @param calr caller's identifier
277      * @see #verifyIsDefined_
278      * @throws BuildException if not in project or value not defined
279      **/

280     protected void verifyCanEvaluate_(String JavaDoc calr)
281     {
282         verifyInProject_(calr);
283         verifyIsDefined_(null);
284     }
285
286
287
288     /**
289      * Returns the underlying value's flex string for this
290      * condition. Must never return <i>null</i>.
291      **/

292     protected abstract FlexString getValueHelper();
293
294
295     /**
296      * Returns this condition's typical name. Used in error messages.
297      * By default returns lowercased version of this condition's class's
298      * leaf name. For example, condition
299      * "<code>com.idaremedia.antx.condition.IsNotSet</code>"is named
300      * "<i>isnotset</i>".
301      **/

302     protected String JavaDoc getTypicalName()
303     {
304         return Tk.lowercaseFrom(Tk.leafNameFrom(getClass()));
305     }
306 }
307
308 /* end-of-FlexCondition.java */
309
Popular Tags