1 package org.apache.turbine.util.template;
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 java.util.Hashtable;
58 import org.apache.turbine.services.template.TurbineTemplate;
59 import org.apache.turbine.util.RunData;
60
61 /***
62 * This is a wrapper for Template specific information. It's part of
63 * the RunData object and can extract the information it needs to do
64 * the job directly from the data.getParameters().
65 *
66 * @author <a href="mailto:mbryson@mindspring.com">Dave Bryson</a>
67 * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
68 * @version $Id: TemplateInfo.java,v 1.2 2002/07/11 16:53:20 mpoeschl Exp $
69 */
70 public class TemplateInfo
71 {
72 /* Constants for tempStorage hashtable. */
73 public static final String NAVIGATION_TEMPLATE = "00navigation_template00";
74 public static final String LAYOUT_TEMPLATE = "00layout_template00";
75 public static final String SERVICE_NAME = "template_service";
76
77 /* Handle to the RunData object. */
78 private RunData data = null;
79
80 /* Place to store information about templates. */
81 private Hashtable tempStorage = null;
82
83 /***
84 * Constructor
85 *
86 * @param RunData A Turbine Rundata object.
87 */
88 public TemplateInfo(RunData data)
89 {
90 this.data = data;
91 tempStorage = new Hashtable(10);
92 }
93
94 /***
95 * Get the value of navigationTemplate.
96 *
97 * @return A String with the value of navigationTemplate.
98 */
99 public String getNavigationTemplate()
100 {
101 String temp = getString(TemplateInfo.NAVIGATION_TEMPLATE);
102 if (temp != null)
103 {
104 temp = temp.replace(',', '/');
105 }
106 return temp;
107 }
108
109 /***
110 * Set the value of navigationTemplate.
111 *
112 * @param v Value to assign to navigationTemplate.
113 */
114 public void setNavigationTemplate(String v)
115 {
116 setTemp(TemplateInfo.NAVIGATION_TEMPLATE, v);
117 }
118
119 /***
120 * Get the value of screen for the RunData parameters. This
121 * information comes from PathInfo or a QueryString. Note: The
122 * template name is always "cleaned" with this method, which
123 * replaces ',' with '/'.
124 *
125 * @return A String with the value of screen.
126 */
127 public String getScreenTemplate()
128 {
129 String temp = data.getParameters().getString("template", null);
130 if (temp != null)
131 {
132 temp = temp.replace(',', '/');
133 }
134 return temp;
135 }
136
137 /***
138 * Set the value of screen. This is really just a method to hide
139 * using the RunData Parameter.
140 *
141 * @param v Value to assign to screen.
142 */
143 public void setScreenTemplate(String v)
144 {
145 data.getParameters().setString("template", v);
146
147 /*
148 * We have changed the screen template so
149 * we should now update the layout template
150 * as well. We will use the template service
151 * to help us out.
152 */
153 try
154 {
155 setLayoutTemplate(TurbineTemplate.getLayoutTemplateName(v));
156 }
157 catch (Exception e)
158 {
159 /*
160 * do nothing.
161 */
162 }
163 }
164
165 /***
166 * Get the value of layout.
167 *
168 * @return A String with the value of layout.
169 */
170 public String getLayoutTemplate()
171 {
172 String value = getString(TemplateInfo.LAYOUT_TEMPLATE);
173 return value;
174 }
175
176 /***
177 * Set the value of layout.
178 *
179 * @param v Value to assign to layout.
180 */
181 public void setLayoutTemplate(String v)
182 {
183 setTemp(TemplateInfo.LAYOUT_TEMPLATE,v);
184 }
185
186 /***
187 * Get the value of Template context. This will be cast to the
188 * proper Context by its Service.
189 *
190 * @param name The name of the template context.
191 * @return An Object with the Value of context.
192 */
193 public Object getTemplateContext(String name)
194 {
195 return getTemp(name);
196 }
197
198 /***
199 * Set the value of context.
200 *
201 * @param name The name of the template context.
202 * @param v Value to assign to context.
203 */
204 public void setTemplateContext(String name, Object v)
205 {
206 setTemp(name, v);
207 }
208
209 /***
210 * Get the value of service.
211 *
212 * @return A String with the value of service.
213 */
214 public String getService()
215 {
216 return getString(TemplateInfo.SERVICE_NAME);
217 }
218
219 /***
220 * Set the value of service.
221 *
222 * @param v Value to assign to service.
223 */
224 public void setService(String v)
225 {
226 setTemp(TemplateInfo.SERVICE_NAME, v);
227 }
228
229 /***
230 * Get an object from temporary storage.
231 *
232 * @param name A String with the name of the object.
233 * @return An Object.
234 */
235 public Object getTemp(String name)
236 {
237 return tempStorage.get(name);
238 }
239
240 /***
241 * Get an object from temporary storage, or a default value.
242 *
243 * @param name A String with the name of the object.
244 * @param def An Object, the default value.
245 * @return An Object.
246 */
247 public Object getTemp(String name, Object def)
248 {
249 try
250 {
251 Object val = tempStorage.get(name);
252 if (val == null)
253 {
254 return def;
255 }
256 return val;
257 }
258 catch (Exception e)
259 {
260 return def;
261 }
262 }
263
264 /***
265 * Put an object into temporary storage.
266 *
267 * @param name A String with the name of the object.
268 * @param value An Object, the value.
269 */
270 public void setTemp(String name, Object value)
271 {
272 tempStorage.put(name, value);
273 }
274
275 /***
276 * Return a String[] from the temp hashtable.
277 *
278 * @param name A String with the name of the object.
279 * @return A String[].
280 */
281 public String[] getStringArray(String name)
282 {
283 String[] value = null;
284 Object object = getTemp(name, null);
285 if (object != null)
286 {
287 value = (String[]) object;
288 }
289 return value;
290 }
291
292 /***
293 * Return a String from the temp hashtable.
294 *
295 * @param name A String with the name of the object.
296 * @return A String.
297 */
298 public String getString(String name)
299 {
300 String value = null;
301 Object object = getTemp(name,null);
302 if (object != null)
303 {
304 value = (String) object;
305 }
306 return value;
307 }
308
309 /***
310 * Remove an object from the temporary storage.
311 *
312 * @param name A String with the name of the object.
313 * @return The object that was removed or <code>null</code>
314 * if the name was not a key.
315 */
316 public Object removeTemp(String name)
317 {
318 return tempStorage.remove(name);
319 }
320
321 /*
322 * Returns all the available names in the temporary storage.
323 *
324 * @return A object array with the keys.
325 */
326 public Object[] getTempKeys()
327 {
328 return tempStorage.keySet().toArray();
329 }
330 }
This page was automatically generated by Maven