001package 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
024import java.util.HashMap;
025import java.util.Map;
026
027import org.apache.commons.lang3.StringUtils;
028import org.apache.turbine.services.TurbineServices;
029import org.apache.turbine.services.template.TemplateEngineService;
030import org.apache.turbine.services.template.TemplateService;
031
032/**
033 * A base class for the various mappers which contains common
034 * code.
035 *
036 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
037 * @version $Id$
038 */
039
040public abstract class BaseMapper
041{
042    /** True if this mapper should cache template -> name mappings */
043    private boolean useCache = false;
044
045    /** Default cache size. Just a number out of thin air. Will be set at init time */
046    private int cacheSize = 5;
047
048    /** The internal template -> name mapping cache */
049    private Map<String, String> templateCache = null;
050
051    /** The name of the default property to pull from the Template Engine Service if the default is requested */
052    protected String defaultProperty;
053
054    /** The separator used to concatenate the result parts for this mapper. */
055    protected char separator;
056
057    // Note: You might _not_ use TurbineTemplate.<xxx> in the C'tor and the init method.
058    // The service isn't configured yet and if you do, the Broker will try to reinit the
059    // Service which leads to an endless loop and a deadlock.
060
061    /**
062     * Default C'tor. If you use this C'tor, you must use
063     * the bean setter to set the various properties needed for
064     * this mapper before first usage.
065     */
066    public BaseMapper()
067    {
068        // empty
069    }
070
071    /**
072     * Get the CacheSize value.
073     * @return the CacheSize value.
074     */
075    public int getCacheSize()
076    {
077        return cacheSize;
078    }
079
080    /**
081     * Set the CacheSize value.
082     * @param cacheSize The new CacheSize value.
083     */
084    public void setCacheSize(int cacheSize)
085    {
086        this.cacheSize = cacheSize;
087    }
088
089    /**
090     * Get the UseCache value.
091     * @return the UseCache value.
092     */
093    public boolean isUseCache()
094    {
095        return useCache;
096    }
097
098    /**
099     * 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        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}