KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > flowcontrol > match > MatchCondition


1 /**
2  * $Id: MatchCondition.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 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 (LGPL) as published
8  * by the Free Software Foundation; either version 2.1 of the License, or (at your option)
9  * any 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 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 GNU 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.flowcontrol.match;
30
31 import org.apache.tools.ant.BuildException;
32 import org.apache.tools.ant.Project;
33 import org.apache.tools.ant.types.Reference;
34
35 import com.idaremedia.antx.AntX;
36 import com.idaremedia.antx.FlexString;
37 import com.idaremedia.antx.apis.Requester;
38 import com.idaremedia.antx.condition.solo.RulesTk;
39 import com.idaremedia.antx.helpers.GenericParameters;
40 import com.idaremedia.antx.ownhelpers.ScopedProperties;
41
42 /**
43  * Switch choice that matches value-under-test and a context against a named
44  * condition definition. If the condition evaluates true, the choice is matched.
45  * If the enclosing switch task has a non-null switch value, the value is overlayed
46  * under the property name "<span class="src">switch.value</span>" before the condition
47  * is called. This value takes precedence over any property named in the evaluation
48  * context.
49  *
50  * @since JWare/AntX 0.5
51  * @author ssmc, &copy;2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
52  * @version 0.5
53  * @.safety single
54  * @.group impl,infra
55  **/

56
57 public class MatchCondition extends ChoiceTask
58 {
59     /**
60      * Creates new nested MatchCondition instance.
61      **/

62     public MatchCondition()
63     {
64         super(AntX.flow+"switch");
65     }
66
67
68
69     /**
70      * Initializes new condition choice task with
71      * custom delay configuration.
72      * @param iam CV-label
73      * @param delayConfiguration <i>true</i> if delay
74      * nested task configuration
75      **/

76     protected MatchCondition(String JavaDoc iam, boolean delayConfiguration)
77     {
78         super(iam,delayConfiguration);
79     }
80
81
82
83     /**
84      * Initializes the enclosing project of this component.
85      * Updates any internal project-components too.
86      **/

87     public void setProject(Project P)
88     {
89         super.setProject(P);
90         m_conditionId.setProject(P);
91     }
92
93
94
95     /**
96      * Returns the raw condition specifier. This value can be
97      * <i>null</i>, a property's name, a variable's name, or a
98      * reference.
99      **/

100     public String JavaDoc getFlexValue()
101     {
102         return m_conditionId.get();
103     }
104
105
106
107     /**
108      * Returns <i>true</i> if no condition id has been set.
109      **/

110     public boolean isUndefined()
111     {
112         return m_conditionId.isUndefined();
113     }
114
115
116
117     /**
118      * Returns the condition's id used by this choice's
119      * comparision. Returns <i>null</i> if value never defined
120      * or determined value doesn't exist; for example, if the
121      * named reference doesn't exist in project.
122      **/

123     public String JavaDoc getVUT()
124     {
125         return m_conditionId.getValue(getProject());
126     }
127
128
129
130     /**
131      * Initializes the condition id holder as a direct literal.
132      * @param id the condition id as flex value (non-null)
133      **/

134     private void setFlexValue(String JavaDoc id)
135     {
136         m_conditionId.set(id);
137         m_conditionId.setIsLiteral();
138     }
139
140
141
142     /**
143      * Sets the name of the condition directly. The item must
144      * exist when this choice is evaluated.
145      * @param condition the condition's name (non-null)
146      **/

147     public void setCriteria(Reference condition)
148     {
149         require_(condition!=null,"setTest- nonzro reference");
150         setFlexValue(condition.getRefId());
151     }
152
153
154
155     /**
156      * Synonym for {@linkplain #setCriteria setCriteria()} that
157      * matches the standard AntX condition value URI protocol.
158      * @param condition the condition's name (non-null)
159      **/

160     public final void setTest(Reference condition)
161     {
162         setCriteria(condition);
163     }
164
165
166
167     /**
168      * Returns this choice's condition reference id if it is
169      * known. Will return <i>null</i> if value is not defined as
170      * a literal (known) value.
171      **/

172     public final String JavaDoc getCriteria()
173     {
174         return m_conditionId.isLiteral() ? m_conditionId.get() : null;
175     }
176
177
178
179     /**
180      * Sets the property from which condition's reference is read.
181      * @param property the property's name (non-null)
182      **/

183     public void setProperty(String JavaDoc property)
184     {
185         require_(property!=null,"setProperty- nonzro name");
186         setFlexValue(property);
187         m_conditionId.setIsProperty(true);
188     }
189
190
191
192     /**
193      * Sets the variable from which condition's reference is read.
194      * @param variable the variable's name (non-null)
195      **/

196     public void setVariable(String JavaDoc variable)
197     {
198         require_(variable!=null,"setVariable- nonzro name");
199         setFlexValue(variable);
200         m_conditionId.setIsExported(true);
201     }
202
203
204
205     /**
206      * Sets the reference from which condition's reference is read.
207      * @param reference the reference's name (non-null)
208      **/

209     public void setReference(String JavaDoc reference)
210     {
211         require_(reference!=null,"setReference- nonzro name");
212         setFlexValue(reference);
213         m_conditionId.setIsReference(true);
214     }
215
216
217
218     /**
219      * Installs all of our context properties and switch value
220      * as an overlay before calling condition.
221      **/

222     private void installOverlay(String JavaDoc vut, Reference context)
223     {
224         Project P= getProject();
225         
226         //NB: Ordering is important; let the switch.value prevail!
227
if (context!=null) {
228             Object JavaDoc o = P.getReference(context.getRefId());
229             if (o instanceof GenericParameters) {
230                 m_overlay = new ScopedProperties(P,true);
231                 m_overlay.put((GenericParameters)o);
232             }
233         }
234         if (vut!=null) {
235             if (m_overlay==null) {
236                 m_overlay = new ScopedProperties(P,true);
237             }
238             m_overlay.put("switch.value",vut);
239         }
240         if (m_overlay!=null) {
241             m_overlay.install();
242         }
243     }
244
245
246
247
248     /**
249      * Undoes the effect of {@linkplain #installOverlay}.
250      **/

251     private void uninstallOverlay()
252     {
253         if (m_overlay!=null) {
254             m_overlay.uninstall(null);
255             m_overlay=null;
256         }
257     }
258
259
260
261     /**
262      * Ensures we are defined and the referred-to object is a
263      * condition-compatible object.
264      * @throws BuildException if undefined.
265      * @throws BuildException if reference is not conditon compatible.
266      */

267     protected void verifyCanExecute_(String JavaDoc calr)
268     {
269         super.verifyCanExecute_(calr);
270         
271         String JavaDoc testId = m_conditionId.getValue();
272         RulesTk.verifyTest(testId,m_rqlink);
273     }
274
275
276
277     /**
278      * Evaluates this choice's condition for a positive result. Returns
279      * <i>true</i> if this choice's criteria are met by the incoming
280      * value and evaluation context.
281      * @param vut value under test (vut-vut)
282      * @param context [optional] evaluation context
283      * @throws BuildException if condition reference not named.
284      **/

285     public boolean eval(String JavaDoc vut, Reference context)
286         throws BuildException
287     {
288         verifyCanExecute_("eval");
289         installOverlay(vut, context);
290         try {
291             String JavaDoc testId = m_conditionId.getValue();
292             return RulesTk.evalTest(testId, m_rqlink);
293         } finally {
294             uninstallOverlay();
295         }
296     }
297
298
299     private FlexString m_conditionId = new FlexString();
300     private ScopedProperties m_overlay;//only-for eval
301
private Requester m_rqlink = new Requester.ForComponent(this);
302 }
303
304 /* end-of-MatchCondition.java */
Popular Tags