View Javadoc
1   package org.apache.turbine.services.template.mapper;
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.HashMap;
25  import java.util.Map;
26  
27  import org.apache.commons.lang3.StringUtils;
28  import org.apache.turbine.services.TurbineServices;
29  import org.apache.turbine.services.template.TemplateEngineService;
30  import org.apache.turbine.services.template.TemplateService;
31  
32  /**
33   * A base class for the various mappers which contains common
34   * code.
35   *
36   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
37   * @version $Id$
38   */
39  
40  public abstract class BaseMapper
41  {
42      /** True if this mapper should cache template -> name mappings */
43      private boolean useCache = false;
44  
45      /** Default cache size. Just a number out of thin air. Will be set at init time */
46      private int cacheSize = 5;
47  
48      /** The internal template -> name mapping cache */
49      private Map<String, String> templateCache = null;
50  
51      /** The name of the default property to pull from the Template Engine Service if the default is requested */
52      protected String defaultProperty;
53  
54      /** The separator used to concatenate the result parts for this mapper. */
55      protected char separator;
56  
57      // Note: You might _not_ use TurbineTemplate.<xxx> in the C'tor and the init method.
58      // The service isn't configured yet and if you do, the Broker will try to reinit the
59      // Service which leads to an endless loop and a deadlock.
60  
61      /**
62       * Default C'tor. If you use this C'tor, you must use
63       * the bean setter to set the various properties needed for
64       * this mapper before first usage.
65       */
66      public BaseMapper()
67      {
68          // empty
69      }
70  
71      /**
72       * Get the CacheSize value.
73       * @return the CacheSize value.
74       */
75      public int getCacheSize()
76      {
77          return cacheSize;
78      }
79  
80      /**
81       * Set the CacheSize value.
82       * @param cacheSize The new CacheSize value.
83       */
84      public void setCacheSize(int cacheSize)
85      {
86          this.cacheSize = cacheSize;
87      }
88  
89      /**
90       * Get the UseCache value.
91       * @return the UseCache value.
92       */
93      public boolean isUseCache()
94      {
95          return useCache;
96      }
97  
98      /**
99       * Set the UseCache value.
100      * @param useCache The new UseCache value.
101      */
102     public void setUseCache(boolean useCache)
103     {
104         this.useCache = useCache;
105     }
106 
107     /**
108      * Get the DefaultProperty value.
109      * @return the DefaultProperty value.
110      */
111     public String getDefaultProperty()
112     {
113         return defaultProperty;
114     }
115 
116     /**
117      * Set the DefaultProperty value.
118      * @param defaultProperty The new DefaultProperty value.
119      */
120     public void setDefaultProperty(String defaultProperty)
121     {
122         this.defaultProperty = defaultProperty;
123     }
124 
125     /**
126      * Get the Separator value.
127      * @return the Separator value.
128      */
129     public char getSeparator()
130     {
131         return separator;
132     }
133 
134     /**
135      * Set the Separator value.
136      * @param separator The new Separator value.
137      */
138     public void setSeparator(char separator)
139     {
140         this.separator = separator;
141     }
142 
143     /**
144      * Initializes the Mapper. Must be called before the mapper might be used.
145      */
146     public void init()
147     {
148         if (useCache)
149         {
150             templateCache = new HashMap<>(cacheSize);
151         }
152     }
153 
154     /**
155      * Returns the default name for the passed Template.
156      * If the passed template has no extension,
157      * the default extension is assumed.
158      * If the template is empty, the default template is
159      * returned.
160      *
161      * @param template The template name.
162      *
163      * @return the mapped default name for the template.
164      */
165 
166     public String getDefaultName(String template)
167     {
168         // We might get a Name without an extension passed. If yes, then we use
169         // the Default extension
170         TemplateServiceorg/apache/turbine/services/template/TemplateService.html#TemplateService">TemplateService templateService = (TemplateService)TurbineServices.getInstance().getService(TemplateService.SERVICE_NAME);
171         TemplateEngineService tes = templateService.getTemplateEngineService(template);
172 
173         if (StringUtils.isEmpty(template) || (tes == null))
174         {
175             return templateService.getDefaultTemplate();
176         }
177 
178         String defaultName = (String) tes.getTemplateEngineServiceConfiguration()
179             .get(defaultProperty);
180 
181         return StringUtils.isEmpty(defaultName)
182             ? templateService.getDefaultTemplate()
183             : defaultName;
184     }
185 
186     /**
187      * Return the first match name for the given template name.
188      *
189      * @param template The template name.
190      *
191      * @return The first matching class or template name.
192      */
193     public String getMappedName(String template)
194     {
195         if (StringUtils.isEmpty(template))
196         {
197             return null;
198         }
199 
200         if (useCache && templateCache.containsKey(template))
201         {
202             return templateCache.get(template);
203         }
204 
205         String res = doMapping(template);
206 
207         // Never cache "null" return values and empty Strings.
208         if (useCache && StringUtils.isNotEmpty(res))
209         {
210             templateCache.put(template, res);
211         }
212 
213         return res;
214     }
215 
216     /**
217      * The actual mapping implementation class. It
218      * is guaranteed that never an empty or null
219      * template name is passed to it. This might
220      * return null.
221      *
222      * @param template The template name.
223      * @return The mapped class or template name.
224      */
225     public abstract String doMapping(String template);
226 }