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