001: package org.apache.turbine.services.template;
002:
003: /*
004: * Copyright 2001-2005 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License")
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.util.Hashtable;
020:
021: import org.apache.commons.configuration.Configuration;
022:
023: import org.apache.turbine.services.TurbineBaseService;
024:
025: /**
026: * The base implementation of Turbine {@link
027: * org.apache.turbine.services.template.TemplateEngineService}.
028: *
029: * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
030: * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
031: * @version $Id: BaseTemplateEngineService.java 264148 2005-08-29 14:21:04Z henning $
032: */
033: public abstract class BaseTemplateEngineService extends
034: TurbineBaseService implements TemplateEngineService {
035: /**
036: * A Map containing the configuration for the template
037: * engine service. The configuration contains:
038: *
039: * 1) template extensions
040: * 2) default page
041: * 3) default screen
042: * 4) default layout
043: * 5) default navigation
044: * 6) default error screen
045: */
046: private Hashtable configuration = new Hashtable();
047:
048: /**
049: * @see org.apache.turbine.services.template.TemplateEngineService#registerConfiguration
050: */
051: public void registerConfiguration(String defaultExt) {
052: initConfiguration(defaultExt);
053: TurbineTemplate.registerTemplateEngineService(this );
054: }
055:
056: /**
057: * @see org.apache.turbine.services.template.TemplateEngineService#getTemplateEngineServiceConfiguration
058: */
059: public Hashtable getTemplateEngineServiceConfiguration() {
060: return configuration;
061: }
062:
063: /**
064: * @see org.apache.turbine.services.template.TemplateEngineService#getAssociatedFileExtensions
065: */
066: public String[] getAssociatedFileExtensions() {
067: return (String[]) configuration.get(TEMPLATE_EXTENSIONS);
068: }
069:
070: /**
071: * Initialize the Template Engine Service.
072: *
073: * Note engine file extension associations. First attempts to
074: * pull a list of custom extensions from the property file value
075: * keyed by <code>template.extension</code>. If none are defined,
076: * uses the value keyed by
077: * <code>template.default.extension</code>, defaulting to the
078: * emergency value supplied by <code>defaultExt</code>.
079: *
080: * @param defaultExt The default used when the default defined in the
081: * properties file is missing or misconfigured.
082: */
083: protected void initConfiguration(String defaultExt) {
084: Configuration config = getConfiguration();
085:
086: //
087: // Should modify the configuration class to take defaults
088: // here, should have to do this.
089: //
090: String[] fileExtensionAssociations = config
091: .getStringArray(TEMPLATE_EXTENSIONS);
092:
093: if (fileExtensionAssociations == null
094: || fileExtensionAssociations.length == 0) {
095: fileExtensionAssociations = new String[1];
096: fileExtensionAssociations[0] = config.getString(
097: DEFAULT_TEMPLATE_EXTENSION, defaultExt);
098: }
099:
100: configuration.put(TEMPLATE_EXTENSIONS,
101: fileExtensionAssociations);
102:
103: /*
104: * We need some better error checking here and should probably
105: * throw an exception here if these things aren't set
106: * up correctly.
107: */
108:
109: String[] copyParams = { DEFAULT_PAGE, DEFAULT_SCREEN,
110: DEFAULT_LAYOUT, DEFAULT_NAVIGATION,
111: DEFAULT_ERROR_SCREEN, DEFAULT_LAYOUT_TEMPLATE,
112: DEFAULT_SCREEN_TEMPLATE };
113:
114: for (int i = 0; i < copyParams.length; i++) {
115: configuration.put(copyParams[i], config.getString(
116: copyParams[i], ""));
117: }
118: }
119:
120: /**
121: * @see org.apache.turbine.services.template.TemplateEngineService#templateExists
122: */
123: public abstract boolean templateExists(String template);
124: }
|