View Javadoc

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