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.lang3.StringUtils;
25  import org.apache.turbine.services.TurbineServices;
26  import org.apache.turbine.services.template.TemplateService;
27  
28  /**
29   * This is a mapper like the BaseMapper but it returns its
30   * results with the extension of the template names passed or (if no
31   * extension is passed), the default extension.
32   *
33   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
34   * @version $Id$
35   */
36  
37  public abstract class BaseTemplateMapper
38      extends BaseMapper
39  {
40      /** A prefix which is used to separate the various template types (screen, layouts, navigation) */
41      protected String prefix = "";
42  
43      /**
44       * Default C'tor. If you use this C'tor, you must use
45       * the bean setter to set the various properties needed for
46       * this mapper before first usage.
47       */
48      public BaseTemplateMapper()
49      {
50          super();
51      }
52  
53      /**
54       * Get the Prefix value.
55       * @return the Prefix value.
56       */
57      public String getPrefix()
58      {
59          return prefix;
60      }
61  
62      /**
63       * Set the Prefix value.
64       * @param prefix The new Prefix value.
65       */
66      public void setPrefix(String prefix)
67      {
68          this.prefix = prefix;
69      }
70  
71      /**
72       * Returns the default name for the passed Template.
73       * If the template has no extension, the default extension
74       * is added.
75       * If the template is empty, the default template is
76       * returned.
77       *
78       * @param template The template name.
79       *
80       * @return the mapped default name for the template.
81       */
82      @Override
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                 TemplateService templateService = (TemplateService)TurbineServices.getInstance().getService(TemplateService.SERVICE_NAME);
102 
103                 if (StringUtils.isNotEmpty(templateService.getDefaultExtension()))
104                 {
105                     resBuf.append(TemplateService.EXTENSION_SEPARATOR);
106                     resBuf.append(templateService.getDefaultExtension());
107                 }
108             }
109             else
110             {
111                 resBuf.append(templateComponents[templateComponents.length -1].substring(dotIndex));
112             }
113             res = resBuf.toString();
114         }
115         return res;
116     }
117 }