KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: ShortHandValueURIHandler.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.condition;
30
31 import org.apache.tools.ant.Task;
32
33 import com.idaremedia.antx.AntX;
34 import com.idaremedia.antx.apis.Requester;
35 import com.idaremedia.antx.helpers.InstanceFactory;
36 import com.idaremedia.antx.helpers.Tk;
37 import com.idaremedia.antx.starters.ValueURIHandlerSkeleton;
38
39 /**
40  * Value URI handler that returns the string "<span class="src">true</span>" or
41  * "<span class="src">false</span>" based on the inlined short hand condition's
42  * result. Will return <i>null</i> if the named condition is not recognized. This
43  * value URI handler is not installed by default; you must explicitly install
44  * it as shown in the examples section.
45  * <p/>
46  * However you install this handler, you can only use the AntX-defined URI scheme
47  * (condition) names. See the full declaration file
48  * <span class="src">com/idaremedia/antx/valueuri/antlib.xml</span> for a listing
49  * of all the recognized short hand condition names (of course, you could just
50  * read this source file also).
51  * <p/>
52  * To allow you to specify the properties in the evaluated text, the short hand value
53  * handler understands the alternate "<span class="src">@(propertyname)</span>"
54  * format in the URI fragment; see example below.
55  * <p/>
56  * <b>Example Usage:</b><pre>
57  * &lt;do true="${<b>$isantversion:</b>^.*version 1\.6.*"&gt;...
58  * &lt;domatch value="${<b>$allsettrue:</b>metrics.enabled,maven.enabled}"&gt;...
59  * &lt;property name="vars.enabled" value="${<b>$filenotempty:</b>@(varsfile)}"/&gt;
60  *
61  * -- To Install --
62  * &lt;manageuris action="install"&gt;
63  * &lt;parameter name="isset"
64  * value="com.idaremedia.antx.condition.ShortHandValueURIHandler"/&gt;
65  * &lt;parameter name="isnotset"
66  * value="com.idaremedia.antx.condition.ShortHandValueURIHandler"/&gt;
67  * ...<i>[All other standard names and your own custom ones if desired]</i>
68  * &lt;/manageuris&gt;
69  * </pre>
70  *
71  * @since JWare/AntX 0.5
72  * @author ssmc, &copy;2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
73  * @version 0.5
74  * @.safety single
75  * @.group api,helper
76  * @see URIable
77  * @see com.idaremedia.antx.condition.solo.ConditionValueURIHandler
78  **/

79
80 public class ShortHandValueURIHandler extends ValueURIHandlerSkeleton
81 {
82     /**
83      * Set up the map of name to condition evaluators.
84      */

85     private static InstanceFactory m_IF= new InstanceFactory();
86     static {
87         m_IF.put("$isset:",IsSet.class);
88         m_IF.put("$isnotset:",IsNotSet.class);
89         m_IF.put("$notset:",IsNotSet.class);
90         m_IF.put("$issettrue:",IsSetTrue.class);
91         m_IF.put("$allset:",AllSet.class);
92         m_IF.put("$allsettrue:",AllSetTrue.class);
93         m_IF.put("$alltrue:",AllSetTrue.class);
94         m_IF.put("$anyset:",AnySet.class);
95         m_IF.put("$anysettrue:",AnySetTrue.class);
96         m_IF.put("$anytrue:",AnySetTrue.class);
97         m_IF.put("$noneset:",NoneSet.class);
98         m_IF.put("$isbool:",IsBoolean.class);
99         m_IF.put("$isboolean:",IsBoolean.class);
100         m_IF.put("$isnumeric:",IsNumeric.class);
101         m_IF.put("$isnumber:",IsNumeric.class);
102         m_IF.put("$notwhitespace:",IsNotWhitespace.class);
103         m_IF.put("$isnotwhitespace:",IsNotWhitespace.class);
104         m_IF.put("$isref:",IsReference.class);
105         m_IF.put("$isreference:",IsReference.class);
106         m_IF.put("$isantversion:",IsAntVersion.class);
107         m_IF.put("$filenotempty:",FileNotEmpty.class);
108         m_IF.put("$isdirectory:",IsDirectory.class);
109         m_IF.put("$isdir:",IsDirectory.class);
110         m_IF.put("$isresource:",IsResource.class);
111         m_IF.put("$isres:",IsResource.class);
112     }
113
114
115
116     /**
117      * Initializes a new short hand value uri handler.
118      * Only the standard condition names are supported by this
119      * handler.
120      * @see #addMapping addMapping(&#8230;)
121      **/

122     public ShortHandValueURIHandler()
123     {
124     }
125
126
127
128     /**
129      * Programmatic extension point that allows subclasses to add
130      * own condition names and condition classes.
131      * @param name protocol marker string (non-null)
132      * @param claz URIable class (non-null)
133      * @throws ClassCastException if <span class="src">claz</span> is not
134      * assign-compatible with {@linkplain URIable}.
135      **/

136     public static void addMapping(String JavaDoc name, Class JavaDoc claz)
137     {
138         AntX.require_(name!=null && claz!=null,
139             AntX.conditions+"ValueURIHandler:", "addMapin- nonzro args");
140
141         if (!URIable.class.isAssignableFrom(claz)) {
142             throw new ClassCastException JavaDoc(claz.getName());
143         }
144
145         synchronized(m_IF) {
146             m_IF.put(name,claz);
147         }
148     }
149
150
151
152     /**
153      * Common work that determines and evaluates the condition
154      * represented by the value URI.
155      * @param uriFragment resource information from uri (non-null)
156      * @param fullUri the full value uri (non-null)
157      * @param clnt caller information (non-null)
158      * @return result or <i>null</i> if did not find match.
159      **/

160     protected Boolean JavaDoc eval(String JavaDoc uriFragment, String JavaDoc fullUri, Requester clnt)
161     {
162         int i= fullUri.indexOf(':');
163         if (i>0) {
164             String JavaDoc which= fullUri.substring(0,++i);
165             if (i<fullUri.length()) {
166                 Object JavaDoc c= m_IF.newInstance(which);
167                 if (c instanceof URIable) {
168                     clnt.getProject().setProjectReference(c);
169                     if (c instanceof Task) {
170                         Task t = (Task)c;
171                         t.setLocation(clnt.getLocation());
172                         t.init();
173                     }
174                     String JavaDoc fragment = fullUri.substring(i);
175                     fragment = Tk.resolveString(clnt.getProject(),fragment,true);
176                     ((URIable)c).xsetFromURI(fragment);
177                     return ((URIable)c).eval() ? Boolean.TRUE : Boolean.FALSE;
178                 }
179             }
180         }
181         return null;
182     }
183
184
185
186     /**
187      * Returns either the string "<span class="src">true</span>" or
188      * "<span class="src">false</span>" for a known condition. Otherwise
189      * returns whatever the default value for an unrecognized URI would
190      * be (usually <i>null</i>).
191      * @return "true" or "false" if uri described a known condition,
192      * otherwise <i>null</i>.
193      **/

194     public String JavaDoc valueFrom(String JavaDoc uriFragment, String JavaDoc fullUri, Requester clnt)
195     {
196         Boolean JavaDoc B = eval(uriFragment, fullUri, clnt);
197         if (B!=null) {
198             return String.valueOf(B.booleanValue());
199         }
200         return getDefaultValue(fullUri,clnt);
201     }
202 }
203
204
205 /* end-of-ShortHandValueURIHandler.java */
Popular Tags