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