001: /*
002: * $Id: TemplateEngineManager.java 491656 2007-01-01 22:10:12Z mrdon $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.components.template;
022:
023: import java.util.Collections;
024: import java.util.HashMap;
025: import java.util.Map;
026: import java.util.Set;
027:
028: import com.opensymphony.xwork2.config.ConfigurationException;
029: import com.opensymphony.xwork2.inject.Container;
030: import com.opensymphony.xwork2.inject.Inject;
031:
032: /**
033: * The TemplateEngineManager will return a template engine for the template
034: */
035: public class TemplateEngineManager {
036: public static final String DEFAULT_TEMPLATE_TYPE_CONFIG_KEY = "struts.ui.templateSuffix";
037:
038: /** The default template extenstion is <code>ftl</code>. */
039: public static final String DEFAULT_TEMPLATE_TYPE = "ftl";
040:
041: Map<String, EngineFactory> templateEngines = new HashMap<String, EngineFactory>();
042: Container container;
043: String defaultTemplateType;
044:
045: @Inject(DEFAULT_TEMPLATE_TYPE_CONFIG_KEY)
046: public void setDefaultTemplateType(String type) {
047: this .defaultTemplateType = type;
048: }
049:
050: @Inject
051: public void setContainer(Container container) {
052: this .container = container;
053: Map<String, EngineFactory> map = new HashMap<String, EngineFactory>();
054: Set<String> prefixes = container
055: .getInstanceNames(TemplateEngine.class);
056: for (String prefix : prefixes) {
057: map.put(prefix, new LazyEngineFactory(prefix));
058: }
059: this .templateEngines = Collections.unmodifiableMap(map);
060:
061: }
062:
063: /**
064: * Registers the given template engine.
065: * <p/>
066: * Will add the engine to the existing list of known engines.
067: * @param templateExtension filename extension (eg. .jsp, .ftl, .vm).
068: * @param templateEngine the engine.
069: */
070: public void registerTemplateEngine(String templateExtension,
071: final TemplateEngine templateEngine) {
072: templateEngines.put(templateExtension, new EngineFactory() {
073: public TemplateEngine create() {
074: return templateEngine;
075: }
076: });
077: }
078:
079: /**
080: * Gets the TemplateEngine for the template name. If the template name has an extension (for instance foo.jsp), then
081: * this extension will be used to look up the appropriate TemplateEngine. If it does not have an extension, it will
082: * look for a Configuration setting "struts.ui.templateSuffix" for the extension, and if that is not set, it
083: * will fall back to "ftl" as the default.
084: *
085: * @param template Template used to determine which TemplateEngine to return
086: * @param templateTypeOverride Overrides the default template type
087: * @return the engine.
088: */
089: public TemplateEngine getTemplateEngine(Template template,
090: String templateTypeOverride) {
091: String templateType = DEFAULT_TEMPLATE_TYPE;
092: String templateName = template.toString();
093: if (templateName.indexOf(".") > 0) {
094: templateType = templateName.substring(templateName
095: .indexOf(".") + 1);
096: } else if (templateTypeOverride != null
097: && templateTypeOverride.length() > 0) {
098: templateType = templateTypeOverride;
099: } else {
100: String type = defaultTemplateType;
101: if (type != null) {
102: templateType = type;
103: }
104: }
105: return templateEngines.get(templateType).create();
106: }
107:
108: /** Abstracts loading of the template engine */
109: interface EngineFactory {
110: public TemplateEngine create();
111: }
112:
113: /**
114: * Allows the template engine to be loaded at request time, so that engines that are missing
115: * dependencies aren't accessed if never used.
116: */
117: class LazyEngineFactory implements EngineFactory {
118: private String name;
119:
120: public LazyEngineFactory(String name) {
121: this .name = name;
122: }
123:
124: public TemplateEngine create() {
125: TemplateEngine engine = container.getInstance(
126: TemplateEngine.class, name);
127: if (engine == null) {
128: throw new ConfigurationException(
129: "Unable to locate template engine: " + name);
130: }
131: return engine;
132: }
133: }
134: }
|