KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > template > mapper > BaseTemplateMapper


1 package org.apache.turbine.services.template.mapper;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import org.apache.commons.lang.StringUtils;
20
21 import org.apache.turbine.services.template.TemplateService;
22 import org.apache.turbine.services.template.TurbineTemplate;
23
24 /**
25  * This is a mapper like the BaseMapper but it returns its
26  * results with the extension of the template names passed or (if no
27  * extension is passed), the default extension.
28  *
29  * @author <a HREF="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
30  * @version $Id: BaseTemplateMapper.java,v 1.1.2.2 2004/05/20 03:06:48 seade Exp $
31  */

32
33 public abstract class BaseTemplateMapper
34     extends BaseMapper
35 {
36     /** A prefix which is used to separate the various template types (screen, layouts, navigation) */
37     protected String JavaDoc prefix = "";
38
39     /**
40      * Default C'tor. If you use this C'tor, you must use
41      * the bean setter to set the various properties needed for
42      * this mapper before first usage.
43      */

44     public BaseTemplateMapper()
45     {
46         super();
47     }
48
49     /**
50      * Get the Prefix value.
51      * @return the Prefix value.
52      */

53     public String JavaDoc getPrefix()
54     {
55         return prefix;
56     }
57
58     /**
59      * Set the Prefix value.
60      * @param prefix The new Prefix value.
61      */

62     public void setPrefix(String JavaDoc prefix)
63     {
64         this.prefix = prefix;
65     }
66
67     /**
68      * Returns the default name for the passed Template.
69      * If the template has no extension, the default extension
70      * is added.
71      * If the template is empty, the default template is
72      * returned.
73      *
74      * @param template The template name.
75      *
76      * @return the mapped default name for the template.
77      */

78     public String JavaDoc getDefaultName(String JavaDoc template)
79     {
80         String JavaDoc res = super.getDefaultName(template);
81
82         // Does the Template Name component have an extension?
83
String JavaDoc [] components
84             = StringUtils.split(res, String.valueOf(separator));
85
86         if (components[components.length -1 ].indexOf(TemplateService.EXTENSION_SEPARATOR) < 0)
87         {
88             StringBuffer JavaDoc resBuf = new StringBuffer JavaDoc();
89             resBuf.append(res);
90             String JavaDoc [] templateComponents = StringUtils.split(template, String.valueOf(TemplateService.TEMPLATE_PARTS_SEPARATOR));
91
92             // Only the extension of the Template name component is interesting...
93
int dotIndex = templateComponents[templateComponents.length -1].lastIndexOf(TemplateService.EXTENSION_SEPARATOR);
94             if (dotIndex < 0)
95             {
96                 if (StringUtils.isNotEmpty(TurbineTemplate.getDefaultExtension()))
97                 {
98                     resBuf.append(TemplateService.EXTENSION_SEPARATOR);
99                     resBuf.append(TurbineTemplate.getDefaultExtension());
100                 }
101             }
102             else
103             {
104                 resBuf.append(templateComponents[templateComponents.length -1].substring(dotIndex));
105             }
106             res = resBuf.toString();
107         }
108         return res;
109     }
110 }
111
Popular Tags