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 org.apache.commons.lang.StringUtils;
25  
26  import org.apache.turbine.services.template.TemplateService;
27  import org.apache.turbine.services.template.TurbineTemplate;
28  
29  /**
30   * This is a mapper like the BaseMapper but it returns its
31   * results with the extension of the template names passed or (if no
32   * extension is passed), the default extension.
33   *
34   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
35   * @version $Id: BaseTemplateMapper.java 1709648 2015-10-20 17:08:10Z tv $
36   */
37  
38  public abstract class BaseTemplateMapper
39      extends BaseMapper
40  {
41      /** A prefix which is used to separate the various template types (screen, layouts, navigation) */
42      protected String prefix = "";
43  
44      /**
45       * Default C'tor. If you use this C'tor, you must use
46       * the bean setter to set the various properties needed for
47       * this mapper before first usage.
48       */
49      public BaseTemplateMapper()
50      {
51          super();
52      }
53  
54      /**
55       * Get the Prefix value.
56       * @return the Prefix value.
57       */
58      public String getPrefix()
59      {
60          return prefix;
61      }
62  
63      /**
64       * Set the Prefix value.
65       * @param prefix The new Prefix value.
66       */
67      public void setPrefix(String prefix)
68      {
69          this.prefix = prefix;
70      }
71  
72      /**
73       * Returns the default name for the passed Template.
74       * If the template has no extension, the default extension
75       * is added.
76       * If the template is empty, the default template is
77       * returned.
78       *
79       * @param template The template name.
80       *
81       * @return the mapped default name for the template.
82       */
83      public String getDefaultName(String template)
84      {
85          String res = super.getDefaultName(template);
86  
87          // Does the Template Name component have an extension?
88          String [] components
89              = StringUtils.split(res, String.valueOf(separator));
90  
91          if (components[components.length -1 ].indexOf(TemplateService.EXTENSION_SEPARATOR) < 0)
92          {
93              StringBuilder resBuf = new StringBuilder();
94              resBuf.append(res);
95              String [] templateComponents = StringUtils.split(template, String.valueOf(TemplateService.TEMPLATE_PARTS_SEPARATOR));
96  
97              // Only the extension of the Template name component is interesting...
98              int dotIndex = templateComponents[templateComponents.length -1].lastIndexOf(TemplateService.EXTENSION_SEPARATOR);
99              if (dotIndex < 0)
100             {
101                 if (StringUtils.isNotEmpty(TurbineTemplate.getDefaultExtension()))
102                 {
103                     resBuf.append(TemplateService.EXTENSION_SEPARATOR);
104                     resBuf.append(TurbineTemplate.getDefaultExtension());
105                 }
106             }
107             else
108             {
109                 resBuf.append(templateComponents[templateComponents.length -1].substring(dotIndex));
110             }
111             res = resBuf.toString();
112         }
113         return res;
114     }
115 }