KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > valueuri > TransformValueURIHandler


1 /**
2  * $Id: TransformValueURIHandler.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.valueuri;
30
31 import java.util.Map JavaDoc;
32
33 import org.apache.tools.ant.Project;
34
35 import com.idaremedia.antx.AntX;
36 import com.idaremedia.antx.AntXFixture;
37 import com.idaremedia.antx.Iteration;
38 import com.idaremedia.antx.apis.Requester;
39 import com.idaremedia.antx.helpers.Tk;
40 import com.idaremedia.antx.parameters.TransformHelper;
41 import com.idaremedia.antx.parameters.ValueTransform;
42 import com.idaremedia.antx.starters.ValueURIHandlerSkeleton;
43
44 /**
45  * Value URI handler that applies the builtin AntX {@linkplain ValueTransform} to
46  * a given string. The allowed inputs are determined by what's legitimate for
47  * the indicated scheme. Note that date/time value uris are usually handled by the
48  * general date valueURI handler not this class.
49  * <p/>
50  * <b>Example Usage:</b><pre>
51  * &lt;property name="build.id" value="${<b>$uppercase:</b>@(build.name)}"/&gt;
52  * &lt;property name="userscratch.url" value="${<b>$ospathurl:</b>@(TMPDIR)}"/&gt;
53  *
54  * -- To Install --
55  * &lt;manageuris action="install"&gt;
56  * &lt;parameter name="transform"
57  * value="com.idaremedia.antx.valueuri.TransformValueURIHandler"/&gt;
58  * &lt;parameter name="ospath"
59  * value="com.idaremedia.antx.valueuri.TransformValueURIHandler"/&gt;
60  * &lt;parameter name="lowercase"
61  * value="com.idaremedia.antx.valueuri.TransformValueURIHandler"/&gt;
62  * &#8230;<i>[All other transform shortcuts like uppercase, etc.]</i>
63  * &lt;/manageuris&gt;
64  * </pre>
65  *
66  * @since JWare/AntX 0.5
67  * @author ssmc, &copy;2004-2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
68  * @version 0.5
69  * @.safety multiple
70  * @.group api,helper
71  **/

72
73 public class TransformValueURIHandler extends ValueURIHandlerSkeleton
74 {
75     private static final Map JavaDoc LINKS = AntXFixture.newMap();
76     static {
77         //NB: long-winded and uglee but obvious. Also not
78
// likely to change anytime soon...
79
LINKS.put("$transform:",null);
80         LINKS.put("$ospath:",ValueTransform.OSPATH);
81         LINKS.put("$path:",ValueTransform.OSPATH);
82         LINKS.put("$ospathurl:",ValueTransform.OSPATHURL);
83         LINKS.put("$pathurl:",ValueTransform.OSPATHURL);
84         LINKS.put("$uppercase:",ValueTransform.UPPERCASE);
85         LINKS.put("$lowercase:",ValueTransform.LOWERCASE);
86         LINKS.put("$trim:",ValueTransform.TRIM);
87         LINKS.put("$stripws:",ValueTransform.STRIPWS);
88         LINKS.put("$decimal:",ValueTransform.DECIMAL);
89     }
90
91
92
93     /**
94      * Initializes a new transform value uri handler.
95      **/

96     public TransformValueURIHandler()
97     {
98         super();
99     }
100
101
102
103     /**
104      * Returns the best fit value tranform for the format directive
105      * embedded in a given value uri. You can use the general "$transform:"
106      * scheme can name a transform that does not have a shorthand scheme.
107      * For example: <span class="src">$transform:@(crdate)?longdatetime</span>.
108      * @param fullUri the full uri (including the '$scheme:' prefix)
109      * @param clnt problem handler (non-null)
110      * @return the formatter or <i>null</i> if no match found.
111      **/

112     public static ValueTransform defaultTransform(String JavaDoc fullUri, Requester clnt)
113     {
114         ValueTransform valfmt= null;
115         int i= fullUri.indexOf(':');
116         if (i>0) {
117             String JavaDoc which= fullUri.substring(0,++i);
118             valfmt = (ValueTransform)LINKS.get(which);
119             if (valfmt==null && (i=fullUri.indexOf("?",i))>0) {
120                 i++;
121                 if (i<fullUri.length()-1) {
122                     String JavaDoc byname = Tk.resolveString(clnt.getProject(),
123                         fullUri.substring(i),true);
124                     valfmt = ValueTransform.from(byname);
125                 }
126             }
127         }
128         return valfmt;
129     }
130
131
132
133     /**
134      * Programmatic extension point that allows subclasses to add
135      * own protocol names and value transform enum. Can also use to override
136      * the default formats associated with our standard names.
137      * @param name protocol marker string (non-null)
138      * @param valfmt ValueTransform enum value (non-null)
139      **/

140     public static void addMapping(String JavaDoc name, ValueTransform valfmt)
141     {
142         AntX.require_(name!=null&&valfmt!=null, "TransformValueURIHandler:",
143                      "addMaping- nonzro args");
144         synchronized(LINKS) {
145             LINKS.put(name,valfmt);
146         }
147     }
148
149
150
151     /**
152      * Tries to transform a script-supplied string as instructed. The
153      * result is returned as a string. If an exception was thrown by
154      * the transformation and the current iteration is not set to halt
155      * valueuris problems, this method returns <i>null</i>.
156      **/

157     public String JavaDoc valueFrom(String JavaDoc uriFragment, String JavaDoc fullUri, Requester clnt)
158     {
159         ValueTransform vt = defaultTransform(fullUri,clnt);
160         if (vt!=null) {
161             int i = uriFragment.lastIndexOf('?');
162             if (i>0) {
163                 uriFragment = Tk.resolveString(clnt.getProject(),
164                     uriFragment.substring(0,i), true);
165             } else {
166                 uriFragment = Tk.resolveString(clnt.getProject(),
167                     uriFragment, true);
168             }
169             try {
170                 return TransformHelper.apply(vt,uriFragment,clnt.getProject());
171             } catch(RuntimeException JavaDoc anyX) {
172                 clnt.problem(anyX.getMessage(),Project.MSG_WARN);
173                 if (Iteration.defaultdefaults().isHaltIfError("valueuris")) {
174                     throw anyX;
175                 }
176             }
177         }
178         return null;
179     }
180 }
181
182 /* end-of-TransformValueURIHandler.java */
Popular Tags