View Javadoc

    1   package org.apache.turbine.services.template;
    2
    3
    4   /*
    5    * Licensed to the Apache Software Foundation (ASF) under one
    6    * or more contributor license agreements.  See the NOTICE file
    7    * distributed with this work for additional information
    8    * regarding copyright ownership.  The ASF licenses this file
    9    * to you under the Apache License, Version 2.0 (the
    10   * "License"); you may not use this file except in compliance
    11   * with the License.  You may obtain a copy of the License at
    12   *
    13   *   http://www.apache.org/licenses/LICENSE-2.0
    14   *
    15   * Unless required by applicable law or agreed to in writing,
    16   * software distributed under the License is distributed on an
    17   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    18   * KIND, either express or implied.  See the License for the
    19   * specific language governing permissions and limitations
    20   * under the License.
    21   */
    22
    23
    24  import java.util.Hashtable;
    25
    26  import org.apache.commons.configuration.Configuration;
    27  import org.apache.turbine.services.TurbineBaseService;
    28  import org.apache.turbine.services.TurbineServices;
    29
    30  /**
    31   * The base implementation of Turbine {@link
    32   * org.apache.turbine.services.template.TemplateEngineService}.
    33   *
    34   * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
    35   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
    36   * @version $Id: BaseTemplateEngineService.java 1723699 2016-01-08 11:29:21Z tv $
    37   */
    38  public abstract class BaseTemplateEngineService
    39      extends TurbineBaseService
    40      implements TemplateEngineService
    41  {
    42      /**
    43       * A Map containing the configuration for the template
    44       * engine service. The configuration contains:
    45       *
    46       * 1) template extensions
    47       * 2) default page
    48       * 3) default screen
    49       * 4) default layout
    50       * 5) default navigation
    51       * 6) default error screen
    52       */
    53      private final Hashtable<String, Object> configuration = new Hashtable<String, Object>();
    54
    55      /**
    56       * @see org.apache.turbine.services.template.TemplateEngineService#registerConfiguration
    57       */
    58      @Override
    59      public void registerConfiguration(String defaultExt)
    60      {
    61          initConfiguration(defaultExt);
    62          TemplateService templateService = (TemplateService)TurbineServices.getInstance().getService(TemplateService.SERVICE_NAME);
    63          templateService.registerTemplateEngineService(this);
    64      }
    65
    66      /**
    67       * @see org.apache.turbine.services.template.TemplateEngineService#getTemplateEngineServiceConfiguration
    68       */
    69      @Override
    70      public Hashtable<String, Object> getTemplateEngineServiceConfiguration()
    71      {
    72          return configuration;
    73      }
    74
    75      /**
    76       * @see org.apache.turbine.services.template.TemplateEngineService#getAssociatedFileExtensions
    77       */
    78      @Override
    79      public String[] getAssociatedFileExtensions()
    80      {
    81          return (String[]) configuration.get(TEMPLATE_EXTENSIONS);
    82      }
    83
    84      /**
    85       * Initialize the Template Engine Service.
    86       *
    87       * Note engine file extension associations.  First attempts to
    88       * pull a list of custom extensions from the property file value
    89       * keyed by <code>template.extension</code>.  If none are defined,
    90       * uses the value keyed by
    91       * <code>template.default.extension</code>, defaulting to the
    92       * emergency value supplied by <code>defaultExt</code>.
    93       *
    94       * @param defaultExt The default used when the default defined in the
    95       *                   properties file is missing or misconfigured.
    96       */
    97      protected void initConfiguration(String defaultExt)
    98      {
    99          Configuration config = getConfiguration();
    100
    101         //
    102         // Should modify the configuration class to take defaults
    103         // here, should have to do this.
    104         //
    105         String[] fileExtensionAssociations =
    106                 config.getStringArray(TEMPLATE_EXTENSIONS);
    107
    108         if (fileExtensionAssociations == null ||
    109             fileExtensionAssociations.length == 0)
    110         {
    111             fileExtensionAssociations = new String[1];
    112             fileExtensionAssociations[0] = config.getString(
    113                     DEFAULT_TEMPLATE_EXTENSION, defaultExt);
    114         }
    115
    116         configuration.put(TEMPLATE_EXTENSIONS, fileExtensionAssociations);
    117
    118         /*
    119          * We need some better error checking here and should probably
    120          * throw an exception here if these things aren't set
    121          * up correctly.
    122          */
    123
    124         String[] copyParams = {
    125             DEFAULT_PAGE,
    126             DEFAULT_SCREEN,
    127             DEFAULT_LAYOUT,
    128             DEFAULT_NAVIGATION,
    129             DEFAULT_ERROR_SCREEN,
    130             DEFAULT_LAYOUT_TEMPLATE,
    131             DEFAULT_SCREEN_TEMPLATE
    132         };
    133
    134         for (int i = 0; i < copyParams.length; i++)
    135         {
    136             configuration.put(copyParams[i], config.getString(copyParams[i], ""));
    137         }
    138     }
    139
    140     /**
    141      * @see org.apache.turbine.services.template.TemplateEngineService#templateExists
    142      */
    143     @Override
    144     public abstract boolean templateExists(String template);
    145 }