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.lang.StringUtils;
    28  import org.apache.turbine.services.template.TemplateEngineService;
    29  import org.apache.turbine.services.template.TurbineTemplate;
    30
    31  /**
    32   * A base class for the various mappers which contains common
    33   * code.
    34   *
    35   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
    36   * @version $Id: BaseMapper.java 1073174 2011-02-21 22:18:45Z tv $
    37   */
    38
    39  public abstract class BaseMapper
    40  {
    41      /** True if this mapper should cache template -> name mappings */
    42      private boolean useCache = false;
    43
    44      /** Default cache size. Just a number out of thin air. Will be set at init time */
    45      private int cacheSize = 5;
    46
    47      /** The internal template -> name mapping cache */
    48      private Map<String, String> templateCache = null;
    49
    50      /** The name of the default property to pull from the Template Engine Service if the default is requested */
    51      protected String defaultProperty;
    52
    53      /** The separator used to concatenate the result parts for this mapper. */
    54      protected char separator;
    55
    56      // Note: You might _not_ use TurbineTemplate.<xxx> in the C'tor and the init method.
    57      // The service isn't configured yet and if you do, the Broker will try to reinit the
    58      // Service which leads to an endless loop and a deadlock.
    59
    60      /**
    61       * Default C'tor. If you use this C'tor, you must use
    62       * the bean setter to set the various properties needed for
    63       * this mapper before first usage.
    64       */
    65      public BaseMapper()
    66      {
    67          // empty
    68      }
    69
    70      /**
    71       * Get the CacheSize value.
    72       * @return the CacheSize value.
    73       */
    74      public int getCacheSize()
    75      {
    76          return cacheSize;
    77      }
    78
    79      /**
    80       * Set the CacheSize value.
    81       * @param cacheSize The new CacheSize value.
    82       */
    83      public void setCacheSize(int cacheSize)
    84      {
    85          this.cacheSize = cacheSize;
    86      }
    87
    88      /**
    89       * Get the UseCache value.
    90       * @return the UseCache value.
    91       */
    92      public boolean isUseCache()
    93      {
    94          return useCache;
    95      }
    96
    97      /**
    98       * Set the UseCache value.
    99       * @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 }