View Javadoc

1   package org.apache.turbine.services.template.mapper;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.commons.lang.StringUtils;
23  
24  import org.apache.turbine.services.template.TemplateService;
25  import org.apache.turbine.services.template.TurbineTemplate;
26  
27  /***
28   * This is a mapper like the BaseMapper but it returns its
29   * results with the extension of the template names passed or (if no
30   * extension is passed), the default extension.
31   *
32   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
33   * @version $Id: BaseTemplateMapper.java 534527 2007-05-02 16:10:59Z tv $
34   */
35  
36  public abstract class BaseTemplateMapper
37      extends BaseMapper
38  {
39      /*** A prefix which is used to separate the various template types (screen, layouts, navigation) */
40      protected String prefix = "";
41  
42      /***
43       * Default C'tor. If you use this C'tor, you must use
44       * the bean setter to set the various properties needed for
45       * this mapper before first usage.
46       */
47      public BaseTemplateMapper()
48      {
49          super();
50      }
51  
52      /***
53       * Get the Prefix value.
54       * @return the Prefix value.
55       */
56      public String getPrefix()
57      {
58          return prefix;
59      }
60  
61      /***
62       * Set the Prefix value.
63       * @param prefix The new Prefix value.
64       */
65      public void setPrefix(String prefix)
66      {
67          this.prefix = prefix;
68      }
69  
70      /***
71       * Returns the default name for the passed Template.
72       * If the template has no extension, the default extension
73       * is added.
74       * If the template is empty, the default template is
75       * returned.
76       *
77       * @param template The template name.
78       *
79       * @return the mapped default name for the template.
80       */
81      public String getDefaultName(String template)
82      {
83          String res = super.getDefaultName(template);
84  
85          // Does the Template Name component have an extension?
86          String [] components
87              = StringUtils.split(res, String.valueOf(separator));
88  
89          if (components[components.length -1 ].indexOf(TemplateService.EXTENSION_SEPARATOR) < 0)
90          {
91              StringBuffer resBuf = new StringBuffer();
92              resBuf.append(res);
93              String [] templateComponents = StringUtils.split(template, String.valueOf(TemplateService.TEMPLATE_PARTS_SEPARATOR));
94  
95              // Only the extension of the Template name component is interesting...
96              int dotIndex = templateComponents[templateComponents.length -1].lastIndexOf(TemplateService.EXTENSION_SEPARATOR);
97              if (dotIndex < 0)
98              {
99                  if (StringUtils.isNotEmpty(TurbineTemplate.getDefaultExtension()))
100                 {
101                     resBuf.append(TemplateService.EXTENSION_SEPARATOR);
102                     resBuf.append(TurbineTemplate.getDefaultExtension());
103                 }
104             }
105             else
106             {
107                 resBuf.append(templateComponents[templateComponents.length -1].substring(dotIndex));
108             }
109             res = resBuf.toString();
110         }
111         return res;
112     }
113 }