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
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 import java.util.ArrayList;
58 import java.util.Arrays;
59 import java.util.List;
60
61 import org.apache.commons.lang.StringUtils;
62
63 import org.apache.turbine.modules.Loader;
64
65 import org.apache.commons.logging.Log;
66 import org.apache.commons.logging.LogFactory;
67
68 import org.apache.turbine.services.template.TemplateService;
69
70 /***
71 * This mapper tries to map Template names to class names. If no direct match
72 * is found, it tries matches "upwards" in the package hierarchy until either
73 * a match is found or the root is hit. Then it returns the name of the
74 * default class from the TemplateEngineService.
75 *
76 * 1. about.directions.Driving <- direct matching the template to the class name
77 * 2. about.directions.Default <- matching the package, class name is Default
78 * 3. about.Default <- stepping up in the package hierarchy, looking for Default
79 * 4. Default <- Class called "Default" without package
80 * 5. VelocityScreen <- The class configured by the Service (VelocityService) to
81 *
82 * Please note, that no actual packages are searched. This is the scope of the
83 * TemplateEngine Loader which is passed at construction time.
84 *
85 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
86 * @version $Id: ClassMapper.java,v 1.1 2003/07/22 10:58:49 henning Exp $
87 */
88
89 public class ClassMapper
90 extends BaseMapper
91 implements Mapper
92 {
93 /**</package-summary/html">The loader for actually trying out the package names *//package-summary.html">em>* The loader for actually trying out the package names */
94 private Loader loader = null;
95
96 /*** Logging */
97 private static Log log = LogFactory.getLog(ClassMapper.class);
98
99 /***
100 * Default C'tor. If you use this C'tor, you must use
101 * the bean setter to set the various properties needed for
102 * this mapper before first usage.
103 */
104 public ClassMapper()
105 {
106 }
107
108 /***
109 * Get the Loader value.
110 * @return the Loader value.
111 */
112 public Loader getLoader()
113 {
114 return loader;
115 }
116
117 /***
118 * Set the Loader value.
119 * @param loader The new Loader value.
120 */
121 public void setLoader(Loader loader)
122 {
123 this.loader = loader;
124 log.debug("Loader is " + this.loader);
125 }
126
127 /***
128 * Strip off a possible extension, replace all "," with "."
129 * Look through the given package path until a match is found.
130 *
131 * @param template The template name.
132 * @return A class name for the given template.
133 */
134 public String doMapping(String template)
135 {
136 log.debug("doMapping(" + template + ")");
137
138
139 List components
140 = new ArrayList(Arrays.asList(StringUtils.split(
141 template,
142 String.valueOf(TemplateService.TEMPLATE_PARTS_SEPARATOR))));
143 int componentSize = components.size() - 1 ;
144
145
146
147 String className = (String) components.get(componentSize);
148 components.remove(componentSize--);
149
150 log.debug("className is " + className);
151
152
153 int dotIndex = className.lastIndexOf(TemplateService.EXTENSION_SEPARATOR);
154 className = (dotIndex < 0) ? className : className.substring(0, dotIndex);
155
156
157
158
159 boolean firstRun = !className.equals(TemplateService.DEFAULT_NAME);
160
161 for(;;)
162 {
163 String pkg = StringUtils.join(components.iterator(), String.valueOf(separator));
164 StringBuffer testName = new StringBuffer();
165
166 log.debug("classPackage is now: " + pkg);
167
168 if (!components.isEmpty())
169 {
170 testName.append(pkg);
171 testName.append(separator);
172 }
173
174 testName.append((firstRun)
175 ? className
176 : TemplateService.DEFAULT_NAME);
177
178 log.debug("Looking for " + testName);
179 try
180 {
181 loader.getAssembler(testName.toString());
182 log.debug("Found it, returning " + testName);
183 return testName.toString();
184 }
185 catch (Exception e)
186 {
187
188 }
189
190 if (firstRun)
191 {
192 firstRun = false;
193 }
194 else
195 {
196 if (components.isEmpty())
197 {
198 break;
199 }
200 components.remove(componentSize--);
201 }
202 }
203
204 log.debug("Returning default");
205 return getDefaultName(template);
206 }
207 }
208
209
210
211