KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > condition > solo > ConditionValueURIHandler


1 /**
2  * $Id: ConditionValueURIHandler.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2004-2005 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.condition.solo;
30
31 import org.apache.tools.ant.Location;
32 import org.apache.tools.ant.Project;
33 import org.apache.tools.ant.TaskAdapter;
34 import org.apache.tools.ant.taskdefs.condition.Condition;
35
36 import com.idaremedia.antx.Iteration;
37 import com.idaremedia.antx.NoiseLevel;
38 import com.idaremedia.antx.apis.Requester;
39 import com.idaremedia.antx.helpers.Strings;
40 import com.idaremedia.antx.starters.ValueURIHandlerSkeleton;
41
42 /**
43  * Value URI handler that records the result of evaluating a referenced condition or
44  * rule type. Because most Ant conditions are not designed for reuse and are not
45  * thread safe, the effect of executing a general Ant condition from a value URI
46  * is undefined. However, AntX conditions and rule types work fine. Executing an
47  * AntX rule <em>task</em> (like a &lt;tally&gt;) is also possible; however, you
48  * must carefully define the task's contents to avoid explicitly causing any
49  * premature property evaluation.
50  * <p/>
51  * This handler is not installed automatically by the AntX runtime, you must install
52  * it explicitly like the example below. Condition value URIs are often linked to
53  * either the <span class="src">$test:</span> or the <span class="src">$criteria:</span>
54  * scheme.
55  * <p/>
56  * If a called condition signals an error, this handler will return the string
57  * "<span class="src">error</span>" <em>unless</em> the current iteration's haltiferror
58  * flag has been turned on for value URIs. If so, this handler lets the error
59  * propagate.
60  * <p/>
61  * <b>Example Usage:</b><pre>
62  * &lt;tallyset id="failquick"&gt;
63  * &lt;matches pattern="youbetcha|yup|yessirree" var="haltiferror"/&gt;
64  * &lt;/tallyset&gt;
65  * ...
66  * &lt;assign var="haltiferror" value="${$default:haltiferror}"/&gt;
67  * &lt;do true="${<b>$test:</b>failquick}"&gt;
68  * ...
69  * &lt;/do&gt;
70  *
71  * -- To Install --
72  * &lt;manageuris action="install"&gt;
73  * &lt;parameter name="test"
74  * value="com.idaremedia.antx.condition.solo.ConditionValueURIHandler"/&gt;
75  * &lt;/manageuris&gt;
76  * </pre>
77  *
78  * @since JWare/AntX 0.5
79  * @author ssmc, &copy;2004-2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
80  * @version 0.5
81  * @.safety special (as safe as called condition which is usually single-threaded)
82  * @.group api,helper
83  * @see com.idaremedia.antx.condition.ShortHandValueURIHandler
84  **/

85
86 public final class ConditionValueURIHandler extends ValueURIHandlerSkeleton
87 {
88     private static final String JavaDoc T= Strings.TRUE;
89     private static final String JavaDoc F= Strings.FALSE;
90     private static final String JavaDoc E= "error";
91
92
93     /**
94      * Initializes a new condition value handler.
95      **/

96     public ConditionValueURIHandler()
97     {
98     }
99
100
101     /**
102      * Looks for the named condition reference and evaluates it. The
103      * result is returned as a string. If an exception was thrown by
104      * the condition and the current iteration is not set to halt for
105      * valueuris problems, this method returns the string
106      * "<span class="src">error</span>".
107      **/

108     public String JavaDoc valueFrom(String JavaDoc uriFragment, String JavaDoc fullUri, Requester clnt)
109     {
110         final Project project = clnt.getProject();
111         Object JavaDoc value = project.getReference(uriFragment);
112
113         if (value instanceof TaskAdapter) {
114             value = ((TaskAdapter)value).getProxy();
115         }
116
117         // a. safest (mt-safe also)
118
if (value instanceof ShareableCondition) {
119             ShareableCondition c = (ShareableCondition)value;
120             NoiseLevel effect = c.getFailureEffect();
121             if (effect!=null && effect.isAsBadAs(NoiseLevel.ERROR)) {
122                 String JavaDoc e = Iteration.uistrs().get
123                     ("brul.referal.valueuri.mismatch",fullUri);
124                 clnt.problem(e, Project.MSG_WARN);
125             }
126             try {
127                 return c.eval(new Adapter(clnt)) ? T : F;
128             } catch(RuntimeException JavaDoc anyX) {
129                 if (Iteration.defaultdefaults().isHaltIfError("valueuris")) {
130                     throw anyX;
131                 }
132                 return E;
133             }
134         }
135         // b. standard (unsafe for most Ant stuff)
136
else if (value instanceof Condition) {
137             Condition c = (Condition)value;
138             try {
139                 return c.eval() ? T : F;
140             } catch(RuntimeException JavaDoc anyX) {
141                 if (Iteration.defaultdefaults().isHaltIfError("valueuris")) {
142                     throw anyX;
143                 }
144                 return E;
145             }
146         }
147         return null;
148     }
149
150
151     /**
152      * Adapter to let a shared rule access call-specifics for value URI handler.
153      *
154      * @since JWare/AntX 0.5
155      * @author ssmc, &copy;2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
156      * @version 0.5
157      * @.safety multiple
158      * @.group impl,infra
159      **/

160     static class Adapter implements ShareableConditionUser
161     {
162         Adapter(Requester clnt)
163         {
164             this.clnt = clnt;
165         }
166         public String JavaDoc getMsgId()
167         {
168             return null;
169         }
170         public Location getLocation()
171         {
172             return clnt.getLocation();
173         }
174         public String JavaDoc getUpdateProperty()
175         {
176             return null;
177         }
178         public String JavaDoc getUpdateVariable()
179         {
180             return null;
181         }
182         public String JavaDoc getUpdateValue()
183         {
184             return null;
185         }
186         private final Requester clnt;
187     }
188 }
189
190 /* end-of-ConditionValueURIHandler.java */
Popular Tags