1 package org.apache.turbine.services.template.mapper;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.util.List;
20 import java.util.ArrayList;
21 import java.util.Arrays;
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 mapper is responsible for the lookup of templates for the Layout
34 * It tries to look in various packages for a match:
35 *
36 * 1. about,directions,Driving.vm <- exact match
37 * 2. about,directions,Default.vm <- package match, Default name
38 * 3. about,Default.vm <- stepping up in the hierarchy
39 * 4. Default.vm <- The name configured as default.layout.template
40 * in the corresponding Templating Engine
41
42 *
43 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
44 * @version $Id: LayoutTemplateMapper.java 264148 2005-08-29 14:21:04Z henning $
45 */
46
47 public class LayoutTemplateMapper
48 extends BaseTemplateMapper
49 implements Mapper
50 {
51 /*** Logging */
52 private static Log log = LogFactory.getLog(LayoutTemplateMapper.class);
53
54 /***
55 * Default C'tor. If you use this C'tor, you must use
56 * the bean setter to set the various properties needed for
57 * this mapper before first usage.
58 */
59 public LayoutTemplateMapper()
60 {
61 }
62
63 /***
64 * Look for a given Template, then try the
65 * defaults until we hit the root.
66 *
67 * @param template The template name.
68 * @return The parsed module name.
69 */
70 public String doMapping(String template)
71 {
72 log.debug("doMapping(" + template + ")");
73
74 List components
75 = new ArrayList(Arrays.asList(StringUtils.split(
76 template,
77 String.valueOf(TemplateService.TEMPLATE_PARTS_SEPARATOR))));
78 int componentSize = components.size() - 1 ;
79
80
81
82 String templateName = (String) components.get(componentSize);
83 components.remove(componentSize--);
84
85 log.debug("templateName is " + templateName);
86
87
88 TemplateEngineService tes = TurbineTemplate.getTemplateEngineService(templateName);
89
90 if (tes == null)
91 {
92 return null;
93 }
94
95 String defaultName =
96 TurbineTemplate.getDefaultLayoutTemplateName(templateName);
97
98
99
100
101 boolean firstRun = !templateName.equals(defaultName);
102
103 for(;;)
104 {
105 String templatePackage = StringUtils.join(components.iterator(), String.valueOf(separator));
106
107 log.debug("templatePackage is now: " + templatePackage);
108
109 StringBuffer testName = new StringBuffer();
110
111 if (!components.isEmpty())
112 {
113 testName.append(templatePackage);
114 testName.append(separator);
115 }
116
117 testName.append((firstRun)
118 ? templateName
119 : defaultName);
120
121
122 StringBuffer templatePath = new StringBuffer();
123 if (StringUtils.isNotEmpty(prefix))
124 {
125 templatePath.append(prefix);
126 templatePath.append(separator);
127 }
128 templatePath.append(testName);
129
130 log.debug("Looking for " + templatePath);
131
132 if (tes.templateExists(templatePath.toString()))
133 {
134 log.debug("Found it, returning " + testName);
135 return testName.toString();
136 }
137
138 if (firstRun)
139 {
140 firstRun = false;
141 }
142 else
143 {
144
145
146
147
148 if (components.isEmpty())
149 {
150 break;
151 }
152
153
154 components.remove(componentSize--);
155 }
156 }
157
158 log.debug("Returning default");
159 return getDefaultName(template);
160 }
161 }