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  
29  /**
30   * The base implementation of Turbine {@link
31   * org.apache.turbine.services.template.TemplateEngineService}.
32   *
33   * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
34   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
35   * @version $Id: BaseTemplateEngineService.java 1078552 2011-03-06 19:58:46Z tv $
36   */
37  public abstract class BaseTemplateEngineService
38      extends TurbineBaseService
39      implements TemplateEngineService
40  {
41      /**
42       * A Map containing the configuration for the template
43       * engine service. The configuration contains:
44       *
45       * 1) template extensions
46       * 2) default page
47       * 3) default screen
48       * 4) default layout
49       * 5) default navigation
50       * 6) default error screen
51       */
52      private final Hashtable<String, Object> configuration = new Hashtable<String, Object>();
53  
54      /**
55       * @see org.apache.turbine.services.template.TemplateEngineService#registerConfiguration
56       */
57      public void registerConfiguration(String defaultExt)
58      {
59          initConfiguration(defaultExt);
60          TurbineTemplate.registerTemplateEngineService(this);
61      }
62  
63      /**
64       * @see org.apache.turbine.services.template.TemplateEngineService#getTemplateEngineServiceConfiguration
65       */
66      public Hashtable<String, Object> getTemplateEngineServiceConfiguration()
67      {
68          return configuration;
69      }
70  
71      /**
72       * @see org.apache.turbine.services.template.TemplateEngineService#getAssociatedFileExtensions
73       */
74      public String[] getAssociatedFileExtensions()
75      {
76          return (String[]) configuration.get(TEMPLATE_EXTENSIONS);
77      }
78  
79      /**
80       * Initialize the Template Engine Service.
81       *
82       * Note engine file extension associations.  First attempts to
83       * pull a list of custom extensions from the property file value
84       * keyed by <code>template.extension</code>.  If none are defined,
85       * uses the value keyed by
86       * <code>template.default.extension</code>, defaulting to the
87       * emergency value supplied by <code>defaultExt</code>.
88       *
89       * @param defaultExt The default used when the default defined in the
90       *                   properties file is missing or misconfigured.
91       */
92      protected void initConfiguration(String defaultExt)
93      {
94          Configuration config = getConfiguration();
95  
96          //
97          // Should modify the configuration class to take defaults
98          // here, should have to do this.
99          //
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 }