01: /*
02: * Copyright (c) 2002-2006 by OpenSymphony
03: * All rights reserved.
04: */
05: package com.opensymphony.webwork.components.template;
06:
07: import com.opensymphony.webwork.config.Configuration;
08:
09: import java.util.HashMap;
10: import java.util.Map;
11:
12: /**
13: * The TemplateEngineManager will return a template engine for the template
14: *
15: * @author jcarreira
16: */
17: public class TemplateEngineManager {
18: public static final String DEFAULT_TEMPLATE_TYPE_CONFIG_KEY = "webwork.ui.templateSuffix";
19:
20: private static final TemplateEngineManager MANAGER = new TemplateEngineManager();
21:
22: /** The default template extenstion is <code>ftl</code>. */
23: public static final String DEFAULT_TEMPLATE_TYPE = "ftl";
24:
25: Map templateEngines = new HashMap();
26:
27: private TemplateEngineManager() {
28: templateEngines.put("ftl", new FreemarkerTemplateEngine());
29: templateEngines.put("vm", new VelocityTemplateEngine());
30: templateEngines.put("jsp", new JspTemplateEngine());
31: }
32:
33: /**
34: * Registers the given template engine.
35: * <p/>
36: * Will add the engine to the existing list of known engines.
37: * @param templateExtension filename extension (eg. .jsp, .ftl, .vm).
38: * @param templateEngine the engine.
39: */
40: public static void registerTemplateEngine(String templateExtension,
41: TemplateEngine templateEngine) {
42: MANAGER.templateEngines.put(templateExtension, templateEngine);
43: }
44:
45: /**
46: * Gets the TemplateEngine for the template name. If the template name has an extension (for instance foo.jsp), then
47: * this extension will be used to look up the appropriate TemplateEngine. If it does not have an extension, it will
48: * look for a Configuration setting "webwork.ui.templateSuffix" for the extension, and if that is not set, it
49: * will fall back to "ftl" as the default.
50: *
51: * @param template Template used to determine which TemplateEngine to return
52: * @param templateTypeOverride Overrides the default template type
53: * @return the engine.
54: */
55: public static TemplateEngine getTemplateEngine(Template template,
56: String templateTypeOverride) {
57: String templateType = DEFAULT_TEMPLATE_TYPE;
58: String templateName = template.toString();
59: if (templateName.indexOf(".") > 0) {
60: templateType = templateName.substring(templateName
61: .indexOf(".") + 1);
62: } else if (templateTypeOverride != null
63: && templateTypeOverride.length() > 0) {
64: templateType = templateTypeOverride;
65: } else if (Configuration
66: .isSet(DEFAULT_TEMPLATE_TYPE_CONFIG_KEY)) {
67: templateType = (String) Configuration
68: .get(DEFAULT_TEMPLATE_TYPE_CONFIG_KEY);
69: }
70: return (TemplateEngine) MANAGER.templateEngines
71: .get(templateType);
72: }
73:
74: }
|