KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > tool > TemplateLink


1 package org.apache.turbine.tool;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache Turbine" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache Turbine", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import org.apache.turbine.DynamicURI;
58 import org.apache.turbine.RunData;
59 import org.apache.turbine.services.pull.ApplicationTool;
60
61 /**
62  * A customized version of the DynamicURI to be used in Templates.
63  * This is automatically inserted into the template context by the
64  * appropriate templating service so page authors can create links
65  * in templates. Here's an example of its Velocity/WebMacro use:
66  *
67  * <p><code>
68  * $link.setPage("index.wm").addPathInfo("hello","world")
69  * This would return: http://foo.com/Turbine/template/index.wm/hello/world
70  * </code>
71  *
72  * @author <a HREF="mbryson@mont.mindspring.com">Dave Bryson</a>
73  * @author <a HREF="mailto:jon@latchkey.com">Jon S. Stevens</a>
74  * @author <a HREF="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
75  * @version $Id: TemplateLink.java,v 1.1 2002/06/01 22:05:28 jvanzyl Exp $
76  */

77 public class TemplateLink
78     extends DynamicURI
79     implements ApplicationTool
80 {
81     /** the pathinfo key stored in the DynamicURI */
82     public static final String JavaDoc TEMPLATE_KEY = "template";
83
84     /** cache of the template name for getPage() */
85     private String JavaDoc template = null;
86
87     /**
88      * Default constructor
89      * <p>
90      * The init method must be called before use.
91      */

92     public TemplateLink()
93     {
94     }
95
96     /**
97      * Constructor.
98      *
99      * @param data a Turbine RunData object.
100      */

101     public TemplateLink(RunData data)
102     {
103         super(data);
104     }
105
106     /**
107      * This will initialise a TemplateLink object that was
108      * constructed with the default constructor (ApplicationTool
109      * method).
110      *
111      * @param data assumed to be a RunData object
112      */

113     public void init(Object JavaDoc data)
114     {
115         // we just blithely cast to RunData as if another object
116
// or null is passed in we'll throw an appropriate runtime
117
// exception.
118
super.init((RunData)data);
119     }
120
121     /**
122      * Refresh method - does nothing
123      */

124     public void refresh()
125     {
126         // empty
127
}
128
129     /**
130      * This will turn off the execution of res.encodeURL(). This is useful
131      * for cases where you don't want to see the session information
132      */

133     public TemplateLink setEncodeURLOff()
134     {
135         setEncodeUrl(false);
136         return this;
137     }
138
139     /**
140      * Sets the template variable used by the Template Service.
141      *
142      * @param t A String with the template name.
143      * @return A TemplateLink.
144      */

145     public TemplateLink setPage(String JavaDoc t)
146     {
147         template = t;
148         addPathInfo(TEMPLATE_KEY,t);
149         return this;
150     }
151
152     /**
153      * Gets the template variable used by the Template Service.
154      * It is only available after setPage() has been called.
155      *
156      * @return The template name.
157      */

158     public String JavaDoc getPage()
159     {
160         return template;
161     }
162
163     /**
164      * Set to false to skip the scheme, host, and port sections of the url.
165      * The default is to return absolute url's from the toString method.
166      *
167      * @param b a <code>boolean</code> value
168      * @return a <code>TemplateLink</code> value
169      */

170     public TemplateLink setAbsolute(boolean b)
171     {
172         setRelative(!b);
173         return this;
174     }
175
176     /**
177      * Returns the URI. After rendering the URI, it clears the
178      * pathInfo and QueryString portions of the DynamicURI.
179      *
180      * @return A String with the URI in the form
181      * http://foo.com/Turbine/template/index.wm/hello/world
182      */

183     public String JavaDoc toString()
184     {
185         String JavaDoc output = super.toString();
186
187         // This was added to allow multilple $link variables in one
188
// template.
189
removePathInfo();
190         removeQueryData();
191         setEncodeUrl(true);
192         setAbsolute(true);
193
194         return output;
195     }
196
197     /**
198      * Returns the URI leaving the source intact. Wraps directly to the
199      * <code>DynamicURI.toString</code> method of the superclass
200      * (avoiding the local toString implementation).
201      *
202      * @return A String with the URI in the form
203      * http://foo.com/Turbine/template/index.wm/hello/world
204      */

205     public String JavaDoc getURI()
206     {
207         return super.toString();
208     }
209 }
210
Popular Tags