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 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.List;
27
28 import org.apache.commons.lang.StringUtils;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
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
37
38
39
40
41
42
43
44
45
46 public class ScreenDefaultTemplateMapper
47 extends BaseTemplateMapper
48 implements Mapper
49 {
50
51 private static Log log = LogFactory.getLog(ScreenDefaultTemplateMapper.class);
52
53
54
55
56
57
58 public ScreenDefaultTemplateMapper()
59 {
60
61 }
62
63
64
65
66
67
68
69
70 public String doMapping(String template)
71 {
72 log.debug("doMapping(" + template + ")");
73
74 List<String> components
75 = new ArrayList<String>(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 = 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 = "Default.vm";
96
97
98
99
100 boolean firstRun = !templateName.equals(defaultName);
101
102 for(;;)
103 {
104 String templatePackage = StringUtils.join(components.iterator(), String.valueOf(separator));
105
106 log.debug("templatePackage is now: " + templatePackage);
107
108 StringBuilder testName = new StringBuilder();
109
110 if (!components.isEmpty())
111 {
112 testName.append(templatePackage);
113 testName.append(separator);
114 }
115
116 testName.append((firstRun)
117 ? templateName
118 : defaultName);
119
120
121 StringBuilder templatePath = new StringBuilder();
122 if (StringUtils.isNotEmpty(prefix))
123 {
124 templatePath.append(prefix);
125 templatePath.append(separator);
126 }
127 templatePath.append(testName);
128
129 log.debug("Looking for " + templatePath);
130
131 if (tes.templateExists(templatePath.toString()))
132 {
133 log.debug("Found it, returning " + testName);
134 return testName.toString();
135 }
136
137 if (firstRun)
138 {
139 firstRun = false;
140 }
141 else
142 {
143
144
145
146
147 break;
148 }
149 }
150
151 log.debug("Returning default");
152 return getDefaultName(template);
153 }
154 }