KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > components > template > TemplateEngineManager


1 package com.opensymphony.webwork.components.template;
2
3 import com.opensymphony.webwork.config.Configuration;
4
5 import java.util.HashMap JavaDoc;
6 import java.util.Map JavaDoc;
7
8 /**
9  * The TemplateEngineManager will return a template engine for the template
10  * Date: Sep 28, 2004 11:58:29 AM
11  *
12  * @author jcarreira
13  */

14 public class TemplateEngineManager {
15     public static final String JavaDoc DEFAULT_TEMPLATE_TYPE_CONFIG_KEY = "webwork.ui.templateSuffix";
16
17     private static final TemplateEngineManager MANAGER = new TemplateEngineManager();
18     public static final String JavaDoc DEFAULT_TEMPLATE_TYPE = "ftl";
19
20     Map JavaDoc templateEngines = new HashMap JavaDoc();
21
22     private TemplateEngineManager() {
23         templateEngines.put("ftl", new FreemarkerTemplateEngine());
24         templateEngines.put("vm", new VelocityTemplateEngine());
25         templateEngines.put("jsp", new JspTemplateEngine());
26     }
27
28     public static void registerTemplateEngine(String JavaDoc templateExtension, TemplateEngine templateEngine) {
29         MANAGER.templateEngines.put(templateExtension, templateEngine);
30     }
31
32     /**
33      * Gets the TemplateEngine for the template name. If the template name has an extension (for instance foo.jsp), then
34      * this extension will be used to look up the appropriate TemplateEngine. If it does not have an extension, it will
35      * look for a Configuration setting "webwork.ui.templateSuffix" for the extension, and if that is not set, it
36      * will fall back to "vm" as the default.
37      */

38     public static TemplateEngine getTemplateEngine(Template template) {
39         String JavaDoc templateType = DEFAULT_TEMPLATE_TYPE;
40         String JavaDoc templateName = template.toString();
41         if (templateName.indexOf(".") > 0) {
42             templateType = templateName.substring(templateName.indexOf(".") + 1);
43         } else {
44             if (Configuration.isSet(DEFAULT_TEMPLATE_TYPE_CONFIG_KEY)) {
45                 templateType = (String JavaDoc) Configuration.get(DEFAULT_TEMPLATE_TYPE_CONFIG_KEY);
46             }
47         }
48         return (TemplateEngine) MANAGER.templateEngines.get(templateType);
49     }
50
51
52 }
53
Popular Tags