001    package org.apache.turbine.services.template.mapper;
002    
003    
004    /*
005     * Licensed to the Apache Software Foundation (ASF) under one
006     * or more contributor license agreements.  See the NOTICE file
007     * distributed with this work for additional information
008     * regarding copyright ownership.  The ASF licenses this file
009     * to you under the Apache License, Version 2.0 (the
010     * "License"); you may not use this file except in compliance
011     * with the License.  You may obtain a copy of the License at
012     *
013     *   http://www.apache.org/licenses/LICENSE-2.0
014     *
015     * Unless required by applicable law or agreed to in writing,
016     * software distributed under the License is distributed on an
017     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
018     * KIND, either express or implied.  See the License for the
019     * specific language governing permissions and limitations
020     * under the License.
021     */
022    
023    
024    import java.util.HashMap;
025    import java.util.Map;
026    
027    import org.apache.commons.lang.StringUtils;
028    import org.apache.turbine.services.template.TemplateEngineService;
029    import org.apache.turbine.services.template.TurbineTemplate;
030    
031    /**
032     * A base class for the various mappers which contains common
033     * code.
034     *
035     * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
036     * @version $Id: BaseMapper.java 1073174 2011-02-21 22:18:45Z tv $
037     */
038    
039    public abstract class BaseMapper
040    {
041        /** True if this mapper should cache template -> name mappings */
042        private boolean useCache = false;
043    
044        /** Default cache size. Just a number out of thin air. Will be set at init time */
045        private int cacheSize = 5;
046    
047        /** The internal template -> name mapping cache */
048        private Map<String, String> templateCache = null;
049    
050        /** The name of the default property to pull from the Template Engine Service if the default is requested */
051        protected String defaultProperty;
052    
053        /** The separator used to concatenate the result parts for this mapper. */
054        protected char separator;
055    
056        // Note: You might _not_ use TurbineTemplate.<xxx> in the C'tor and the init method.
057        // The service isn't configured yet and if you do, the Broker will try to reinit the
058        // Service which leads to an endless loop and a deadlock.
059    
060        /**
061         * Default C'tor. If you use this C'tor, you must use
062         * the bean setter to set the various properties needed for
063         * this mapper before first usage.
064         */
065        public BaseMapper()
066        {
067            // empty
068        }
069    
070        /**
071         * Get the CacheSize value.
072         * @return the CacheSize value.
073         */
074        public int getCacheSize()
075        {
076            return cacheSize;
077        }
078    
079        /**
080         * Set the CacheSize value.
081         * @param cacheSize The new CacheSize value.
082         */
083        public void setCacheSize(int cacheSize)
084        {
085            this.cacheSize = cacheSize;
086        }
087    
088        /**
089         * Get the UseCache value.
090         * @return the UseCache value.
091         */
092        public boolean isUseCache()
093        {
094            return useCache;
095        }
096    
097        /**
098         * Set the UseCache value.
099         * @param newUseCache The new UseCache value.
100         */
101        public void setUseCache(boolean useCache)
102        {
103            this.useCache = useCache;
104        }
105    
106        /**
107         * Get the DefaultProperty value.
108         * @return the DefaultProperty value.
109         */
110        public String getDefaultProperty()
111        {
112            return defaultProperty;
113        }
114    
115        /**
116         * Set the DefaultProperty value.
117         * @param defaultProperty The new DefaultProperty value.
118         */
119        public void setDefaultProperty(String defaultProperty)
120        {
121            this.defaultProperty = defaultProperty;
122        }
123    
124        /**
125         * Get the Separator value.
126         * @return the Separator value.
127         */
128        public char getSeparator()
129        {
130            return separator;
131        }
132    
133        /**
134         * Set the Separator value.
135         * @param separator The new Separator value.
136         */
137        public void setSeparator(char separator)
138        {
139            this.separator = separator;
140        }
141    
142        /**
143         * Initializes the Mapper. Must be called before the mapper might be used.
144         */
145        public void init()
146        {
147            if (useCache)
148            {
149                templateCache = new HashMap<String, String>(cacheSize);
150            }
151        }
152    
153        /**
154         * Returns the default name for the passed Template.
155         * If the passed template has no extension,
156         * the default extension is assumed.
157         * If the template is empty, the default template is
158         * returned.
159         *
160         * @param template The template name.
161         *
162         * @return the mapped default name for the template.
163         */
164    
165        public String getDefaultName(String template)
166        {
167            // We might get a Name without an extension passed. If yes, then we use
168            // the Default extension
169    
170            TemplateEngineService tes
171                = TurbineTemplate.getTemplateEngineService(template);
172    
173            if (StringUtils.isEmpty(template) || (tes == null))
174            {
175                return TurbineTemplate.getDefaultTemplate();
176            }
177    
178            String defaultName = (String) tes.getTemplateEngineServiceConfiguration()
179                .get(defaultProperty);
180    
181            return StringUtils.isEmpty(defaultName)
182                ? TurbineTemplate.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    }