KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.List JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Arrays JavaDoc;
22
23 import org.apache.commons.lang.StringUtils;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 import org.apache.turbine.services.template.TemplateEngineService;
29 import org.apache.turbine.services.template.TemplateService;
30 import org.apache.turbine.services.template.TurbineTemplate;
31
32 /**
33  * This is a pretty simple mapper which returns template pathes for
34  * a supplied template name. If the path does not exist, it looks for
35  * a templated called "Default" in the same package.
36  * This path can be used by the TemplateEngine to access
37  * a certain resource to actually render the template.
38  *
39  * @author <a HREF="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
40  * @version $Id: ScreenDefaultTemplateMapper.java,v 1.1.2.2 2004/05/20 03:06:48 seade Exp $
41  */

42
43 public class ScreenDefaultTemplateMapper
44     extends BaseTemplateMapper
45     implements Mapper
46 {
47     /** Logging */
48     private static Log log = LogFactory.getLog(ScreenDefaultTemplateMapper.class);
49
50     /**
51      * Default C'tor. If you use this C'tor, you must use
52      * the bean setter to set the various properties needed for
53      * this mapper before first usage.
54      */

55     public ScreenDefaultTemplateMapper()
56     {
57     }
58
59     /**
60      * Look for a given Template, then try the
61      * default.
62      *
63      * @param template The template name.
64      * @return The parsed module name.
65      */

66     public String JavaDoc doMapping(String JavaDoc template)
67     {
68         log.debug("doMapping(" + template + ")");
69         // Copy our elements into an array
70
List JavaDoc components
71             = new ArrayList JavaDoc(Arrays.asList(StringUtils.split(
72                                               template,
73                                               String.valueOf(TemplateService.TEMPLATE_PARTS_SEPARATOR))));
74         int componentSize = components.size() - 1 ;
75
76         // This method never gets an empty string passed.
77
// So this is never < 0
78
String JavaDoc templateName = (String JavaDoc) components.get(componentSize);
79         components.remove(componentSize--);
80
81         log.debug("templateName is " + templateName);
82
83         // Last element decides, which template Service to use...
84
TemplateEngineService tes = TurbineTemplate.getTemplateEngineService(templateName);
85
86         if (tes == null)
87         {
88             return null;
89         }
90
91         String JavaDoc defaultName = "Default.vm";
92
93         // This is an optimization. If the name we're looking for is
94
// already the default name for the template, don't do a "first run"
95
// which looks for an exact match.
96
boolean firstRun = !templateName.equals(defaultName);
97
98         for(;;)
99         {
100             String JavaDoc templatePackage = StringUtils.join(components.iterator(), String.valueOf(separator));
101
102             log.debug("templatePackage is now: " + templatePackage);
103
104             StringBuffer JavaDoc testName = new StringBuffer JavaDoc();
105
106             if (!components.isEmpty())
107             {
108                 testName.append(templatePackage);
109                 testName.append(separator);
110             }
111
112             testName.append((firstRun)
113                 ? templateName
114                 : defaultName);
115
116             // But the Templating service must look for the name with prefix
117
StringBuffer JavaDoc templatePath = new StringBuffer JavaDoc();
118             if (StringUtils.isNotEmpty(prefix))
119             {
120                 templatePath.append(prefix);
121                 templatePath.append(separator);
122             }
123             templatePath.append(testName);
124
125             log.debug("Looking for " + templatePath);
126
127             if (tes.templateExists(templatePath.toString()))
128             {
129                 log.debug("Found it, returning " + testName);
130                 return testName.toString();
131             }
132
133             if (firstRun)
134             {
135                 firstRun = false;
136             }
137             else
138             {
139                 // We run this loop only two times. The
140
// first time with the 'real' name and the
141
// second time with "Default". The second time
142
// we will end up here and break the for(;;) loop.
143
break;
144             }
145         }
146
147         log.debug("Returning default");
148         return getDefaultName(template);
149     }
150 }
151
Popular Tags