View Javadoc

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