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