View Javadoc

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