1 package org.apache.turbine.util.template;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import org.apache.turbine.services.template.TurbineTemplate;
23 import org.apache.turbine.util.RunData;
24 import org.apache.turbine.util.uri.URIConstants;
25
26
27 /***
28 * This is a wrapper for Template specific information. It's part of
29 * the RunData object and can extract the information it needs to do
30 * the job directly from the data.getParameters().
31 *
32 * @author <a href="mailto:mbryson@mindspring.com">Dave Bryson</a>
33 * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
34 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
35 * @version $Id: TemplateInfo.java 278824 2005-09-05 20:01:15Z henning $
36 */
37 public class TemplateInfo
38 {
39
40
41 public static final String NAVIGATION_TEMPLATE = "00navigation_template00";
42 public static final String LAYOUT_TEMPLATE = "00layout_template00";
43 public static final String SERVICE_NAME = "template_service";
44
45
46 private RunData data = null;
47
48
49 private Map tempStorage = null;
50
51 /***
52 * Constructor
53 *
54 * @param RunData A Turbine Rundata object.
55 */
56 public TemplateInfo(RunData data)
57 {
58 this.data = data;
59 tempStorage = new HashMap(10);
60 }
61
62 /***
63 * Get the value of navigationTemplate.
64 *
65 * @return A String with the value of navigationTemplate.
66 */
67 public String getNavigationTemplate()
68 {
69 return getString(TemplateInfo.NAVIGATION_TEMPLATE);
70 }
71
72 /***
73 * Set the value of navigationTemplate.
74 *
75 * @param v Value to assign to navigationTemplate.
76 */
77 public void setNavigationTemplate(String v)
78 {
79 setTemp(TemplateInfo.NAVIGATION_TEMPLATE, v);
80 }
81
82 /***
83 * Get the value of screen for the RunData parameters. This
84 * information comes from PathInfo or a QueryString.
85 *
86 * @return A String with the value of screen.
87 */
88 public String getScreenTemplate()
89 {
90 return data.getParameters().getString(URIConstants.CGI_TEMPLATE_PARAM, null);
91 }
92
93 /***
94 * Set the value of screen. This is really just a method to hide
95 * using the RunData Parameter.
96 *
97 * @param v Value to assign to screen.
98 */
99 public void setScreenTemplate(String v)
100 {
101 data.getParameters().setString(URIConstants.CGI_TEMPLATE_PARAM, v);
102
103
104
105
106
107 try
108 {
109 setLayoutTemplate(TurbineTemplate.getLayoutTemplateName(v));
110 }
111 catch (Exception e)
112 {
113
114
115
116 }
117 }
118
119 /***
120 * Get the value of layout.
121 *
122 * @return A String with the value of layout.
123 */
124 public String getLayoutTemplate()
125 {
126 String value = getString(TemplateInfo.LAYOUT_TEMPLATE);
127 return value;
128 }
129
130 /***
131 * Set the value of layout.
132 *
133 * @param v Value to assign to layout.
134 */
135 public void setLayoutTemplate(String v)
136 {
137 setTemp(TemplateInfo.LAYOUT_TEMPLATE, v);
138 }
139
140 /***
141 * Get the value of Template context. This will be cast to the
142 * proper Context by its Service.
143 *
144 * @param name The name of the template context.
145 * @return An Object with the Value of context.
146 */
147 public Object getTemplateContext(String name)
148 {
149 return getTemp(name);
150 }
151
152 /***
153 * Set the value of context.
154 *
155 * @param name The name of the template context.
156 * @param v Value to assign to context.
157 */
158 public void setTemplateContext(String name, Object v)
159 {
160 setTemp(name, v);
161 }
162
163 /***
164 * Get the value of service.
165 *
166 * @return A String with the value of service.
167 */
168 public String getService()
169 {
170 return getString(TemplateInfo.SERVICE_NAME);
171 }
172
173 /***
174 * Set the value of service.
175 *
176 * @param v Value to assign to service.
177 */
178 public void setService(String v)
179 {
180 setTemp(TemplateInfo.SERVICE_NAME, v);
181 }
182
183 /***
184 * Get an object from temporary storage.
185 *
186 * @param name A String with the name of the object.
187 * @return An Object.
188 */
189 public Object getTemp(String name)
190 {
191 return tempStorage.get(name);
192 }
193
194 /***
195 * Get an object from temporary storage, or a default value.
196 *
197 * @param name A String with the name of the object.
198 * @param def An Object, the default value.
199 * @return An Object.
200 */
201 public Object getTemp(String name, Object def)
202 {
203 try
204 {
205 Object val = tempStorage.get(name);
206 return (val != null) ? val : def;
207 }
208 catch (Exception e)
209 {
210 return def;
211 }
212 }
213
214 /***
215 * Put an object into temporary storage.
216 *
217 * @param name A String with the name of the object.
218 * @param value An Object, the value.
219 */
220 public void setTemp(String name, Object value)
221 {
222 tempStorage.put(name, value);
223 }
224
225 /***
226 * Return a String[] from the temp hash map.
227 *
228 * @param name A String with the name of the object.
229 * @return A String[].
230 */
231 public String[] getStringArray(String name)
232 {
233 String[] value = null;
234 Object object = getTemp(name, null);
235 if (object != null)
236 {
237 value = (String[]) object;
238 }
239 return value;
240 }
241
242 /***
243 * Return a String from the temp hash map.
244 *
245 * @param name A String with the name of the object.
246 * @return A String.
247 */
248 public String getString(String name)
249 {
250 String value = null;
251 Object object = getTemp(name, null);
252 if (object != null)
253 {
254 value = (String) object;
255 }
256 return value;
257 }
258
259 /***
260 * Remove an object from the temporary storage.
261 *
262 * @param name A String with the name of the object.
263 * @return The object that was removed or <code>null</code>
264 * if the name was not a key.
265 */
266 public Object removeTemp(String name)
267 {
268 return tempStorage.remove(name);
269 }
270
271
272
273
274
275
276 public Object[] getTempKeys()
277 {
278 return tempStorage.keySet().toArray();
279 }
280 }