%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.turbine.services.template.TurbineTemplateService |
|
|
1 | package org.apache.turbine.services.template; |
|
2 | ||
3 | /* |
|
4 | * Copyright 2001-2005 The Apache Software Foundation. |
|
5 | * |
|
6 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
7 | * you may not use this file except in compliance with the License. |
|
8 | * You may obtain a copy of the License at |
|
9 | * |
|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
11 | * |
|
12 | * Unless required by applicable law or agreed to in writing, software |
|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 | * See the License for the specific language governing permissions and |
|
16 | * limitations under the License. |
|
17 | */ |
|
18 | ||
19 | import java.io.File; |
|
20 | ||
21 | import java.util.Collections; |
|
22 | import java.util.HashMap; |
|
23 | import java.util.Map; |
|
24 | ||
25 | import org.apache.commons.configuration.Configuration; |
|
26 | ||
27 | import org.apache.commons.lang.StringUtils; |
|
28 | ||
29 | import org.apache.commons.logging.Log; |
|
30 | import org.apache.commons.logging.LogFactory; |
|
31 | ||
32 | import org.apache.turbine.Turbine; |
|
33 | import org.apache.turbine.TurbineConstants; |
|
34 | import org.apache.turbine.modules.LayoutLoader; |
|
35 | import org.apache.turbine.modules.Loader; |
|
36 | import org.apache.turbine.modules.NavigationLoader; |
|
37 | import org.apache.turbine.modules.PageLoader; |
|
38 | import org.apache.turbine.modules.ScreenLoader; |
|
39 | import org.apache.turbine.services.InitializationException; |
|
40 | import org.apache.turbine.services.TurbineBaseService; |
|
41 | import org.apache.turbine.services.factory.TurbineFactory; |
|
42 | import org.apache.turbine.services.servlet.TurbineServlet; |
|
43 | import org.apache.turbine.services.template.mapper.BaseTemplateMapper; |
|
44 | import org.apache.turbine.services.template.mapper.ClassMapper; |
|
45 | import org.apache.turbine.services.template.mapper.DirectMapper; |
|
46 | import org.apache.turbine.services.template.mapper.DirectTemplateMapper; |
|
47 | import org.apache.turbine.services.template.mapper.LayoutTemplateMapper; |
|
48 | import org.apache.turbine.services.template.mapper.Mapper; |
|
49 | import org.apache.turbine.services.template.mapper.ScreenTemplateMapper; |
|
50 | import org.apache.turbine.util.RunData; |
|
51 | import org.apache.turbine.util.TurbineException; |
|
52 | import org.apache.turbine.util.uri.URIConstants; |
|
53 | ||
54 | /** |
|
55 | * This service provides a method for mapping templates to their |
|
56 | * appropriate Screens or Navigations. It also allows templates to |
|
57 | * define a layout/navigations/screen modularization within the |
|
58 | * template structure. It also performs caching if turned on in the |
|
59 | * properties file. |
|
60 | * |
|
61 | * This service is not bound to a specific templating engine but we |
|
62 | * will use the Velocity templating engine for the examples. It is |
|
63 | * available by using the VelocityService. |
|
64 | * |
|
65 | * This assumes the following properties in the Turbine configuration: |
|
66 | * |
|
67 | * <pre> |
|
68 | * # Register the VelocityService for the "vm" extension. |
|
69 | * services.VelocityService.template.extension=vm |
|
70 | * |
|
71 | * # Default Java class for rendering a Page in this service |
|
72 | * # (must be found on the class path (org.apache.turbine.modules.page.VelocityPage)) |
|
73 | * services.VelocityService.default.page = VelocityPage |
|
74 | * |
|
75 | * # Default Java class for rendering a Screen in this service |
|
76 | * # (must be found on the class path (org.apache.turbine.modules.screen.VelocityScreen)) |
|
77 | * services.VelocityService.default.screen=VelocityScreen |
|
78 | * |
|
79 | * # Default Java class for rendering a Layout in this service |
|
80 | * # (must be found on the class path (org.apache.turbine.modules.layout.VelocityOnlyLayout)) |
|
81 | * services.VelocityService.default.layout = VelocityOnlyLayout |
|
82 | * |
|
83 | * # Default Java class for rendering a Navigation in this service |
|
84 | * # (must be found on the class path (org.apache.turbine.modules.navigation.VelocityNavigation)) |
|
85 | * services.VelocityService.default.navigation=VelocityNavigation |
|
86 | * |
|
87 | * # Default Template Name to be used as Layout. If nothing else is |
|
88 | * # found, return this as the default name for a layout |
|
89 | * services.VelocityService.default.layout.template = Default.vm |
|
90 | * </pre> |
|
91 | * If you want to render a template, a search path is used to find |
|
92 | * a Java class which might provide information for the context of |
|
93 | * this template. |
|
94 | * |
|
95 | * If you request e.g. the template screen |
|
96 | * |
|
97 | * about,directions,Driving.vm |
|
98 | * |
|
99 | * then the following class names are searched (on the module search |
|
100 | * path): |
|
101 | * |
|
102 | * 1. about.directions.Driving <- direct matching the template to the class name |
|
103 | * 2. about.directions.Default <- matching the package, class name is Default |
|
104 | * 3. about.Default <- stepping up in the package hierarchy, looking for Default |
|
105 | * 4. Default <- Class called "Default" without package |
|
106 | * 5. VelocityScreen <- The class configured by the Service (VelocityService) to |
|
107 | * |
|
108 | * And if you have the following module packages configured: |
|
109 | * |
|
110 | * module.packages = org.apache.turbine.modules, com.mycorp.modules |
|
111 | * |
|
112 | * then the class loader will look for |
|
113 | * |
|
114 | * org.apache.turbine.modules.screens.about.directions.Driving |
|
115 | * com.mycorp.modules.screens.about.directions.Driving |
|
116 | * org.apache.turbine.modules.screens.about.directions.Default |
|
117 | * com.mycorp.modules.screens.about.directions.Default |
|
118 | * org.apache.turbine.modules.screens.about.Default |
|
119 | * com.mycorp.modules.screens.about.Default |
|
120 | * org.apache.turbine.modules.screens.Default |
|
121 | * com.mycorp.modules.screens.Default |
|
122 | * org.apache.turbine.modules.screens.VelocityScreen |
|
123 | * com.mycorp.modules.screens.VelocityScreen |
|
124 | * |
|
125 | * Most of the times, you don't have any backing Java class for a |
|
126 | * template screen, so the first match will be |
|
127 | * org.apache.turbine.modules.screens.VelocityScreen |
|
128 | * which then renders your screen. |
|
129 | * |
|
130 | * Please note, that your Screen Template (Driving.vm) must exist! |
|
131 | * If it does not exist, the Template Service will report an error. |
|
132 | * |
|
133 | * Once the screen is found, the template service will look for |
|
134 | * the Layout and Navigation templates of your Screen. Here, the |
|
135 | * template service looks for matching template names! |
|
136 | * |
|
137 | * Consider our example: about,directions,Driving.vm (Screen Name) |
|
138 | * |
|
139 | * Now the template service will look for the following Navigation |
|
140 | * and Layout templates: |
|
141 | * |
|
142 | * 1. about,directions,Driving.vm <- exact match |
|
143 | * 2. about,directions,Default.vm <- package match, Default name |
|
144 | * 3. about,Default.vm <- stepping up in the hierarchy |
|
145 | * 4. Default.vm <- The name configured as default.layout.template |
|
146 | * in the Velocity service. |
|
147 | * |
|
148 | * And now Hennings' two golden rules for using templates: |
|
149 | * |
|
150 | * Many examples and docs from older Turbine code show template pathes |
|
151 | * with a slashes. Repeat after me: "TEMPLATE NAMES NEVER CONTAIN SLASHES!" |
|
152 | * |
|
153 | * Many examples and docs from older Turbine code show templates that start |
|
154 | * with "/". This is not only a violation of the rule above but actively breaks |
|
155 | * things like loading templates from a jar with the velocity jar loader. Repeat |
|
156 | * after me: "TEMPLATE NAMES ARE NOT PATHES. THEY'RE NOT ABSOLUTE AND HAVE NO |
|
157 | * LEADING /". |
|
158 | * |
|
159 | * If you now wonder how a template name is mapped to a file name: This is |
|
160 | * scope of the templating engine. Velocity e.g. has this wonderful option to |
|
161 | * load templates from jar archives. There is no single file but you tell |
|
162 | * velocity "get about,directions,Driving.vm" and it returns the rendered |
|
163 | * template. This is not the job of the Templating Service but of the Template |
|
164 | * rendering services like VelocityService. |
|
165 | * |
|
166 | * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a> |
|
167 | * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a> |
|
168 | * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a> |
|
169 | * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a> |
|
170 | * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a> |
|
171 | * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> |
|
172 | * @version $Id: TurbineTemplateService.java 264148 2005-08-29 14:21:04Z henning $ |
|
173 | */ |
|
174 | 126 | public class TurbineTemplateService |
175 | extends TurbineBaseService |
|
176 | implements TemplateService |
|
177 | { |
|
178 | /** Logging */ |
|
179 | 168 | private static Log log = LogFactory.getLog(TurbineTemplateService.class); |
180 | ||
181 | /** Represents Page Objects */ |
|
182 | public static final int PAGE_KEY = 0; |
|
183 | ||
184 | /** Represents Page Objects */ |
|
185 | public static final String PAGE_NAME = "page"; |
|
186 | ||
187 | /** Represents Screen Objects */ |
|
188 | public static final int SCREEN_KEY = 1; |
|
189 | ||
190 | /** Represents Screen Objects */ |
|
191 | public static final String SCREEN_NAME = "screen"; |
|
192 | ||
193 | /** Represents Layout Objects */ |
|
194 | public static final int LAYOUT_KEY = 2; |
|
195 | ||
196 | /** Represents Layout Objects */ |
|
197 | public static final String LAYOUT_NAME = "layout"; |
|
198 | ||
199 | /** Represents Navigation Objects */ |
|
200 | public static final int NAVIGATION_KEY = 3; |
|
201 | ||
202 | /** Represents Navigation Objects */ |
|
203 | public static final String NAVIGATION_NAME = "navigation"; |
|
204 | ||
205 | /** Represents Layout Template Objects */ |
|
206 | public static final int LAYOUT_TEMPLATE_KEY = 4; |
|
207 | ||
208 | /** Represents Layout Template Objects */ |
|
209 | public static final String LAYOUT_TEMPLATE_NAME = "layout.template"; |
|
210 | ||
211 | /** Represents Screen Template Objects */ |
|
212 | public static final int SCREEN_TEMPLATE_KEY = 5; |
|
213 | ||
214 | /** Represents Screen Template Objects */ |
|
215 | public static final String SCREEN_TEMPLATE_NAME = "screen.template"; |
|
216 | ||
217 | /** Represents Navigation Template Objects */ |
|
218 | public static final int NAVIGATION_TEMPLATE_KEY = 6; |
|
219 | ||
220 | /** Represents Navigation Template Objects */ |
|
221 | public static final String NAVIGATION_TEMPLATE_NAME = "navigation.template"; |
|
222 | ||
223 | /** Number of different Template Types that we know of */ |
|
224 | public static final int TEMPLATE_TYPES = 7; |
|
225 | ||
226 | /** Here we register the mapper objects for our various object types */ |
|
227 | 42 | private Mapper [] mapperRegistry = null; |
228 | ||
229 | /** |
|
230 | * The default file extension used as a registry key when a |
|
231 | * template's file extension cannot be determined. |
|
232 | * |
|
233 | * @deprecated. Use TemplateService.DEFAULT_EXTENSION_VALUE. |
|
234 | */ |
|
235 | protected static final String NO_FILE_EXT = TemplateService.DEFAULT_EXTENSION_VALUE; |
|
236 | ||
237 | ||
238 | /** Flag set if cache is to be used. */ |
|
239 | 42 | private boolean useCache = false; |
240 | ||
241 | /** Default extension for templates. */ |
|
242 | private String defaultExtension; |
|
243 | ||
244 | /** Default template without the default extension. */ |
|
245 | private String defaultTemplate; |
|
246 | ||
247 | /** |
|
248 | * The mappings of template file extensions to {@link |
|
249 | * org.apache.turbine.services.template.TemplateEngineService} |
|
250 | * implementations. Implementing template engines can locate |
|
251 | * templates within the capability of any resource loaders they |
|
252 | * may possess, and other template engines are stuck with file |
|
253 | * based template hierarchy only. |
|
254 | */ |
|
255 | 42 | private Map templateEngineRegistry = null; |
256 | ||
257 | /** |
|
258 | * C'tor |
|
259 | */ |
|
260 | public TurbineTemplateService() |
|
261 | 42 | { |
262 | 42 | } |
263 | ||
264 | /** |
|
265 | * Called the first time the Service is used. |
|
266 | * |
|
267 | * @exception InitializationException Something went wrong when |
|
268 | * setting up the Template Service. |
|
269 | */ |
|
270 | public void init() |
|
271 | throws InitializationException |
|
272 | { |
|
273 | // Get the configuration for the template service. |
|
274 | 44 | Configuration config = getConfiguration(); |
275 | ||
276 | // Get the default extension to use if nothing else is applicable. |
|
277 | 44 | defaultExtension = config.getString(TemplateService.DEFAULT_EXTENSION_KEY, |
278 | TemplateService.DEFAULT_EXTENSION_VALUE); |
|
279 | ||
280 | 44 | defaultTemplate = config.getString(TemplateService.DEFAULT_TEMPLATE_KEY, |
281 | TemplateService.DEFAULT_TEMPLATE_VALUE); |
|
282 | ||
283 | // Check to see if we are going to be caching modules. |
|
284 | // Aaargh, who moved this _out_ of the TemplateService package? |
|
285 | 44 | useCache = Turbine.getConfiguration().getBoolean(TurbineConstants.MODULE_CACHE_KEY, |
286 | TurbineConstants.MODULE_CACHE_DEFAULT); |
|
287 | ||
288 | 44 | log.debug("Default Extension: " + defaultExtension); |
289 | 44 | log.debug("Default Template: " + defaultTemplate); |
290 | 44 | log.debug("Use Caching: " + useCache); |
291 | ||
292 | 44 | templateEngineRegistry = Collections.synchronizedMap(new HashMap()); |
293 | ||
294 | 44 | initMapper(config); |
295 | 44 | setInit(true); |
296 | 44 | } |
297 | ||
298 | /** |
|
299 | * Returns true if the Template Service has caching activated |
|
300 | * |
|
301 | * @return true if Caching is active. |
|
302 | */ |
|
303 | public boolean isCaching() |
|
304 | { |
|
305 | 2 | return useCache; |
306 | } |
|
307 | ||
308 | /** |
|
309 | * Get the default template name extension specified |
|
310 | * in the template service properties. If no extension |
|
311 | * is defined, return the empty string. |
|
312 | * |
|
313 | * @return The default extension. |
|
314 | */ |
|
315 | public String getDefaultExtension() |
|
316 | { |
|
317 | 26 | return StringUtils.isNotEmpty(defaultExtension) ? defaultExtension : ""; |
318 | } |
|
319 | ||
320 | /** |
|
321 | * Return Extension for a supplied template |
|
322 | * |
|
323 | * @param template The template name |
|
324 | * |
|
325 | * @return extension The extension for the supplied template |
|
326 | */ |
|
327 | public String getExtension(String template) |
|
328 | { |
|
329 | 124 | if (StringUtils.isEmpty(template)) |
330 | { |
|
331 | 0 | return getDefaultExtension(); |
332 | } |
|
333 | ||
334 | 124 | int dotIndex = template.indexOf(EXTENSION_SEPARATOR); |
335 | ||
336 | 124 | return (dotIndex < 0) ? getDefaultExtension() : template.substring(dotIndex + 1); |
337 | } |
|
338 | ||
339 | ||
340 | /** |
|
341 | * Returns the Default Template Name with the Default Extension. |
|
342 | * If the extension is unset, return only the template name |
|
343 | * |
|
344 | * @return The default template Name |
|
345 | */ |
|
346 | public String getDefaultTemplate() |
|
347 | { |
|
348 | 42 | StringBuffer sb = new StringBuffer(); |
349 | 42 | sb.append(defaultTemplate); |
350 | 42 | if (StringUtils.isNotEmpty(defaultExtension)) |
351 | { |
|
352 | 0 | sb.append(EXTENSION_SEPARATOR); |
353 | 0 | sb.append(getDefaultExtension()); |
354 | } |
|
355 | 42 | return sb.toString(); |
356 | } |
|
357 | ||
358 | /** |
|
359 | * Get the default page module name of the template engine |
|
360 | * service corresponding to the default template name extension. |
|
361 | * |
|
362 | * @return The default page module name. |
|
363 | */ |
|
364 | public String getDefaultPage() |
|
365 | { |
|
366 | 4 | return getDefaultPageName(getDefaultTemplate()); |
367 | } |
|
368 | ||
369 | /** |
|
370 | * Get the default screen module name of the template engine |
|
371 | * service corresponding to the default template name extension. |
|
372 | * |
|
373 | * @return The default screen module name. |
|
374 | */ |
|
375 | public String getDefaultScreen() |
|
376 | { |
|
377 | 4 | return getDefaultScreenName(getDefaultTemplate()); |
378 | } |
|
379 | ||
380 | /** |
|
381 | * Get the default layout module name of the template engine |
|
382 | * service corresponding to the default template name extension. |
|
383 | * |
|
384 | * @return The default layout module name. |
|
385 | */ |
|
386 | public String getDefaultLayout() |
|
387 | { |
|
388 | 4 | return getDefaultLayoutName(getDefaultTemplate()); |
389 | } |
|
390 | ||
391 | /** |
|
392 | * Get the default navigation module name of the template engine |
|
393 | * service corresponding to the default template name extension. |
|
394 | * |
|
395 | * @return The default navigation module name. |
|
396 | */ |
|
397 | public String getDefaultNavigation() |
|
398 | { |
|
399 | 4 | return getDefaultNavigationName(getDefaultTemplate()); |
400 | } |
|
401 | ||
402 | /** |
|
403 | * Get the default layout template name of the template engine |
|
404 | * service corresponding to the default template name extension. |
|
405 | * |
|
406 | * @return The default layout template name. |
|
407 | */ |
|
408 | public String getDefaultLayoutTemplate() |
|
409 | { |
|
410 | 4 | return getDefaultLayoutTemplateName(getDefaultTemplate()); |
411 | } |
|
412 | ||
413 | /** |
|
414 | * Get the default page module name of the template engine |
|
415 | * service corresponding to the template name extension of |
|
416 | * the named template. |
|
417 | * |
|
418 | * @param template The template name. |
|
419 | * @return The default page module name. |
|
420 | */ |
|
421 | public String getDefaultPageName(String template) |
|
422 | { |
|
423 | 8 | return ((Mapper) mapperRegistry[PAGE_KEY]).getDefaultName(template); |
424 | } |
|
425 | ||
426 | /** |
|
427 | * Get the default screen module name of the template engine |
|
428 | * service corresponding to the template name extension of |
|
429 | * the named template. |
|
430 | * |
|
431 | * @param template The template name. |
|
432 | * @return The default screen module name. |
|
433 | */ |
|
434 | public String getDefaultScreenName(String template) |
|
435 | { |
|
436 | 8 | return ((Mapper) mapperRegistry[SCREEN_KEY]).getDefaultName(template); |
437 | } |
|
438 | ||
439 | /** |
|
440 | * Get the default layout module name of the template engine |
|
441 | * service corresponding to the template name extension of |
|
442 | * the named template. |
|
443 | * |
|
444 | * @param template The template name. |
|
445 | * @return The default layout module name. |
|
446 | */ |
|
447 | public String getDefaultLayoutName(String template) |
|
448 | { |
|
449 | 8 | return ((Mapper) mapperRegistry[LAYOUT_KEY]).getDefaultName(template); |
450 | } |
|
451 | ||
452 | /** |
|
453 | * Get the default navigation module name of the template engine |
|
454 | * service corresponding to the template name extension of |
|
455 | * the named template. |
|
456 | * |
|
457 | * @param template The template name. |
|
458 | * @return The default navigation module name. |
|
459 | */ |
|
460 | public String getDefaultNavigationName(String template) |
|
461 | { |
|
462 | 8 | return ((Mapper) mapperRegistry[NAVIGATION_KEY]).getDefaultName(template); |
463 | } |
|
464 | ||
465 | /** |
|
466 | * Get the default layout template name of the template engine |
|
467 | * service corresponding to the template name extension of |
|
468 | * the named template. |
|
469 | * |
|
470 | * @param template The template name. |
|
471 | * @return The default layout template name. |
|
472 | */ |
|
473 | public String getDefaultLayoutTemplateName(String template) |
|
474 | { |
|
475 | 22 | return ((Mapper) mapperRegistry[LAYOUT_TEMPLATE_KEY]).getDefaultName(template); |
476 | } |
|
477 | ||
478 | /** |
|
479 | * Find the default page module name for the given request. |
|
480 | * |
|
481 | * @param data The encapsulation of the request to retrieve the |
|
482 | * default page for. |
|
483 | * @return The default page module name. |
|
484 | */ |
|
485 | public String getDefaultPageName(RunData data) |
|
486 | { |
|
487 | 0 | String template = data.getParameters().get(URIConstants.CGI_TEMPLATE_PARAM); |
488 | 0 | return (template != null) ? |
489 | getDefaultPageName(template) : getDefaultPage(); |
|
490 | } |
|
491 | ||
492 | /** |
|
493 | * Find the default layout module name for the given request. |
|
494 | * |
|
495 | * @param data The encapsulation of the request to retrieve the |
|
496 | * default layout for. |
|
497 | * @return The default layout module name. |
|
498 | */ |
|
499 | public String getDefaultLayoutName(RunData data) |
|
500 | { |
|
501 | 0 | String template = data.getParameters().get(URIConstants.CGI_TEMPLATE_PARAM); |
502 | 0 | return (template != null) ? |
503 | getDefaultLayoutName(template) : getDefaultLayout(); |
|
504 | } |
|
505 | ||
506 | /** |
|
507 | * Locate and return the name of the screen module to be used |
|
508 | * with the named screen template. |
|
509 | * |
|
510 | * @param template The screen template name. |
|
511 | * @return The found screen module name. |
|
512 | * @exception Exception, a generic exception. |
|
513 | */ |
|
514 | public String getScreenName(String template) |
|
515 | throws Exception |
|
516 | { |
|
517 | 18 | return ((Mapper) mapperRegistry[SCREEN_KEY]).getMappedName(template); |
518 | } |
|
519 | ||
520 | /** |
|
521 | * Locate and return the name of the layout module to be used |
|
522 | * with the named layout template. |
|
523 | * |
|
524 | * @param template The layout template name. |
|
525 | * @return The found layout module name. |
|
526 | * @exception Exception, a generic exception. |
|
527 | */ |
|
528 | public String getLayoutName(String template) |
|
529 | throws Exception |
|
530 | { |
|
531 | 18 | return ((Mapper) mapperRegistry[LAYOUT_KEY]).getMappedName(template); |
532 | } |
|
533 | ||
534 | /** |
|
535 | * Locate and return the name of the navigation module to be used |
|
536 | * with the named navigation template. |
|
537 | * |
|
538 | * @param template The navigation template name. |
|
539 | * @return The found navigation module name. |
|
540 | * @exception Exception, a generic exception. |
|
541 | */ |
|
542 | public String getNavigationName(String template) |
|
543 | throws Exception |
|
544 | { |
|
545 | 18 | return ((Mapper) mapperRegistry[NAVIGATION_KEY]).getMappedName(template); |
546 | } |
|
547 | ||
548 | /** |
|
549 | * Locate and return the name of the screen template corresponding |
|
550 | * to the given template name parameter. This might return null if |
|
551 | * the screen is not found! |
|
552 | * |
|
553 | * @param template The template name parameter. |
|
554 | * @return The found screen template name. |
|
555 | * @exception Exception, a generic exception. |
|
556 | */ |
|
557 | public String getScreenTemplateName(String template) |
|
558 | throws Exception |
|
559 | { |
|
560 | 14 | return ((Mapper) mapperRegistry[SCREEN_TEMPLATE_KEY]).getMappedName(template); |
561 | } |
|
562 | ||
563 | /** |
|
564 | * Locate and return the name of the layout template corresponding |
|
565 | * to the given screen template name parameter. |
|
566 | * |
|
567 | * @param template The template name parameter. |
|
568 | * @return The found screen template name. |
|
569 | * @exception Exception, a generic exception. |
|
570 | */ |
|
571 | public String getLayoutTemplateName(String template) |
|
572 | throws Exception |
|
573 | { |
|
574 | 14 | return ((Mapper) mapperRegistry[LAYOUT_TEMPLATE_KEY]).getMappedName(template); |
575 | } |
|
576 | ||
577 | /** |
|
578 | * Locate and return the name of the navigation template corresponding |
|
579 | * to the given template name parameter. This might return null if |
|
580 | * the navigation is not found! |
|
581 | * |
|
582 | * @param template The template name parameter. |
|
583 | * @return The found navigation template name. |
|
584 | * @exception Exception, a generic exception. |
|
585 | */ |
|
586 | public String getNavigationTemplateName(String template) |
|
587 | throws Exception |
|
588 | { |
|
589 | 0 | return ((Mapper) mapperRegistry[NAVIGATION_TEMPLATE_KEY]).getMappedName(template); |
590 | } |
|
591 | ||
592 | /** |
|
593 | * Translates the supplied template paths into their Turbine-canonical |
|
594 | * equivalent (probably absolute paths). This is used if the templating |
|
595 | * engine (e.g. JSP) does not provide any means to load a page but |
|
596 | * the page path is passed to the servlet container. |
|
597 | * |
|
598 | * @param templatePaths An array of template paths. |
|
599 | * @return An array of translated template paths. |
|
600 | * @deprecated Each template engine service should know how to translate |
|
601 | * a request onto a file. |
|
602 | */ |
|
603 | public String[] translateTemplatePaths(String[] templatePaths) |
|
604 | { |
|
605 | 0 | for (int i = 0; i < templatePaths.length; i++) |
606 | { |
|
607 | 0 | templatePaths[i] = TurbineServlet.getRealPath(templatePaths[i]); |
608 | } |
|
609 | 0 | return templatePaths; |
610 | } |
|
611 | ||
612 | /** |
|
613 | * Delegates to the appropriate {@link |
|
614 | * org.apache.turbine.services.template.TemplateEngineService} to |
|
615 | * check the existance of the specified template. |
|
616 | * |
|
617 | * @param template The template to check for the existance of. |
|
618 | * @param templatePaths The paths to check for the template. |
|
619 | * @deprecated Use templateExists from the various Templating Engines |
|
620 | */ |
|
621 | public boolean templateExists(String template, |
|
622 | String[] templatePaths) |
|
623 | { |
|
624 | 0 | for (int i = 0; i < templatePaths.length; i++) |
625 | { |
|
626 | 0 | if (new File(templatePaths[i], template).exists()) |
627 | { |
|
628 | 0 | return true; |
629 | } |
|
630 | } |
|
631 | 0 | return false; |
632 | } |
|
633 | ||
634 | /** |
|
635 | * Registers the provided template engine for use by the |
|
636 | * <code>TemplateService</code>. |
|
637 | * |
|
638 | * @param service The <code>TemplateEngineService</code> to register. |
|
639 | */ |
|
640 | public synchronized void registerTemplateEngineService(TemplateEngineService service) |
|
641 | { |
|
642 | 42 | String[] exts = service.getAssociatedFileExtensions(); |
643 | ||
644 | 84 | for (int i = 0; i < exts.length; i++) |
645 | { |
|
646 | 42 | templateEngineRegistry.put(exts[i], service); |
647 | } |
|
648 | 42 | } |
649 | ||
650 | /** |
|
651 | * The {@link org.apache.turbine.services.template.TemplateEngineService} |
|
652 | * associated with the specified template's file extension. |
|
653 | * |
|
654 | * @param template The template name. |
|
655 | * @return The template engine service. |
|
656 | */ |
|
657 | public TemplateEngineService getTemplateEngineService(String template) |
|
658 | { |
|
659 | 124 | return (TemplateEngineService) templateEngineRegistry.get(getExtension(template)); |
660 | } |
|
661 | ||
662 | /** |
|
663 | * Register a template Mapper to the service. This Mapper |
|
664 | * performs the template mapping and searching for a specific |
|
665 | * object type which is managed by the TemplateService. |
|
666 | * |
|
667 | * @param templateKey One of the _KEY constants for the Template object types. |
|
668 | * @param mapper An object which implements the Mapper interface. |
|
669 | */ |
|
670 | private void registerMapper(int templateKey, Mapper mapper) |
|
671 | { |
|
672 | 308 | mapper.init(); |
673 | 308 | mapperRegistry[templateKey] = mapper; |
674 | 308 | } |
675 | ||
676 | /** |
|
677 | * Load and configure the Template mappers for |
|
678 | * the Template Service. |
|
679 | * |
|
680 | * @param conf The current configuration object. |
|
681 | * @throws InitializationException A problem occured trying to set up the mappers. |
|
682 | */ |
|
683 | private void initMapper(Configuration conf) |
|
684 | throws InitializationException |
|
685 | { |
|
686 | // Create a registry with the number of Template Types managed by this service. |
|
687 | // We could use a List object here and extend the number of managed objects |
|
688 | // dynamically. However, by using an Object Array, we get much more performance |
|
689 | // out of the Template Service. |
|
690 | 44 | mapperRegistry = new Mapper [TEMPLATE_TYPES]; |
691 | ||
692 | 44 | String [] mapperNames = new String [] { |
693 | PAGE_NAME,SCREEN_NAME, LAYOUT_NAME, |
|
694 | NAVIGATION_NAME, LAYOUT_TEMPLATE_NAME, SCREEN_TEMPLATE_NAME, NAVIGATION_TEMPLATE_NAME |
|
695 | }; |
|
696 | ||
697 | 44 | String [] mapperClasses = new String [] { |
698 | DirectMapper.class.getName(), |
|
699 | ClassMapper.class.getName(), |
|
700 | ClassMapper.class.getName(), |
|
701 | ClassMapper.class.getName(), |
|
702 | LayoutTemplateMapper.class.getName(), |
|
703 | ScreenTemplateMapper.class.getName(), |
|
704 | DirectTemplateMapper.class.getName() |
|
705 | }; |
|
706 | ||
707 | 44 | int [] mapperCacheSize = new class="keyword">int [] { |
708 | 0, |
|
709 | conf.getInt( |
|
710 | TurbineConstants.SCREEN_CACHE_SIZE_KEY, |
|
711 | TurbineConstants.SCREEN_CACHE_SIZE_DEFAULT), |
|
712 | conf.getInt( |
|
713 | TurbineConstants.LAYOUT_CACHE_SIZE_KEY, |
|
714 | TurbineConstants.LAYOUT_CACHE_SIZE_DEFAULT), |
|
715 | conf.getInt( |
|
716 | TurbineConstants.NAVIGATION_CACHE_SIZE_KEY, |
|
717 | TurbineConstants.NAVIGATION_CACHE_SIZE_DEFAULT), |
|
718 | conf.getInt( |
|
719 | TurbineConstants.LAYOUT_CACHE_SIZE_KEY, |
|
720 | TurbineConstants.LAYOUT_CACHE_SIZE_DEFAULT), |
|
721 | conf.getInt( |
|
722 | TurbineConstants.SCREEN_CACHE_SIZE_KEY, |
|
723 | TurbineConstants.SCREEN_CACHE_SIZE_DEFAULT), |
|
724 | conf.getInt( |
|
725 | TurbineConstants.NAVIGATION_CACHE_SIZE_KEY, |
|
726 | TurbineConstants.NAVIGATION_CACHE_SIZE_DEFAULT) |
|
727 | }; |
|
728 | ||
729 | 44 | String [] mapperDefaultProperty = new String [] { |
730 | TemplateEngineService.DEFAULT_PAGE, |
|
731 | TemplateEngineService.DEFAULT_SCREEN, |
|
732 | TemplateEngineService.DEFAULT_LAYOUT, |
|
733 | TemplateEngineService.DEFAULT_NAVIGATION, |
|
734 | TemplateEngineService.DEFAULT_LAYOUT_TEMPLATE, |
|
735 | TemplateEngineService.DEFAULT_SCREEN_TEMPLATE, |
|
736 | TemplateEngineService.DEFAULT_NAVIGATION_TEMPLATE |
|
737 | }; |
|
738 | ||
739 | 44 | char [] mapperSeparator = new class="keyword">char [] { '.', '.', '.', '.', '/', '/', '/' }; |
740 | ||
741 | 44 | Loader [] mapperLoader = new Loader [] { |
742 | PageLoader.getInstance(), |
|
743 | ScreenLoader.getInstance(), |
|
744 | LayoutLoader.getInstance(), |
|
745 | NavigationLoader.getInstance(), |
|
746 | null, class="keyword">null, class="keyword">null}; |
|
747 | ||
748 | 44 | String [] mapperPrefix = new String [] { |
749 | null, class="keyword">null, class="keyword">null, class="keyword">null, |
|
750 | TurbineConstants.LAYOUT_PREFIX, |
|
751 | TurbineConstants.SCREEN_PREFIX, |
|
752 | TurbineConstants.NAVIGATION_PREFIX }; |
|
753 | ||
754 | 352 | for (int i = 0; i < TEMPLATE_TYPES; i++) |
755 | { |
|
756 | 308 | StringBuffer mapperProperty = new StringBuffer(); |
757 | 308 | mapperProperty.append("mapper."); |
758 | 308 | mapperProperty.append(mapperNames[i]); |
759 | 308 | mapperProperty.append(".class"); |
760 | ||
761 | 308 | String mapperClass = |
762 | conf.getString(mapperProperty.toString(), mapperClasses[i]); |
|
763 | ||
764 | 308 | log.info("Using " + mapperClass + " to map " + mapperNames[i] + " elements"); |
765 | ||
766 | 308 | Mapper tm = null; |
767 | ||
768 | try |
|
769 | { |
|
770 | 308 | tm = (Mapper) TurbineFactory.getInstance(mapperClass); |
771 | 154 | } |
772 | 0 | catch (TurbineException te) |
773 | { |
|
774 | 0 | throw new InitializationException("", te); |
775 | 154 | } |
776 | ||
777 | 308 | tm.setUseCache(useCache); |
778 | 308 | tm.setCacheSize(mapperCacheSize[i]); |
779 | 308 | tm.setDefaultProperty(mapperDefaultProperty[i]); |
780 | 308 | tm.setSeparator(mapperSeparator[i]); |
781 | ||
782 | 308 | if ((mapperLoader[i] != null) && (tm instanceof ClassMapper)) |
783 | { |
|
784 | 132 | ((ClassMapper) tm).setLoader(mapperLoader[i]); |
785 | } |
|
786 | ||
787 | 308 | if ((mapperPrefix[i] != null) && (tm instanceof BaseTemplateMapper)) |
788 | { |
|
789 | 132 | ((BaseTemplateMapper) tm).setPrefix(mapperPrefix[i]); |
790 | } |
|
791 | ||
792 | 308 | registerMapper(i, tm); |
793 | } |
|
794 | 44 | } |
795 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |