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.lang3.StringUtils;
29 import org.apache.logging.log4j.LogManager;
30 import org.apache.logging.log4j.Logger;
31 import org.apache.turbine.modules.Assembler;
32 import org.apache.turbine.modules.Loader;
33 import org.apache.turbine.services.template.TemplateService;
34 import org.apache.turbine.util.TurbineException;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public class ClassMapper
56 extends BaseMapper
57 implements Mapper
58 {
59
60 private Loader<? extends Assembler> loader = null;
61
62
63 private static final Logger log = LogManager.getLogger(ClassMapper.class);
64
65
66
67
68
69
70 public ClassMapper()
71 {
72
73 }
74
75
76
77
78
79 public Loader<? extends Assembler> getLoader()
80 {
81 return loader;
82 }
83
84
85
86
87
88 public void setLoader(Loader<? extends Assembler> loader)
89 {
90 this.loader = loader;
91 log.debug("Loader is {}", this.loader);
92 }
93
94
95
96
97
98
99
100
101 @Override
102 public String doMapping(String template)
103 {
104 log.debug("doMapping({})", template);
105
106
107 List<String> components
108 = new ArrayList<>(Arrays.asList(StringUtils.split(
109 template,
110 String.valueOf(TemplateService.TEMPLATE_PARTS_SEPARATOR))));
111 int componentSize = components.size() - 1 ;
112
113
114
115 String className = components.get(componentSize);
116 components.remove(componentSize--);
117
118 log.debug("className is {}", className);
119
120
121 int dotIndex = className.lastIndexOf(TemplateService.EXTENSION_SEPARATOR);
122 className = (dotIndex < 0) ? className : className.substring(0, dotIndex);
123
124
125
126
127 boolean firstRun = !className.equals(TemplateService.DEFAULT_NAME);
128
129 for(;;)
130 {
131 String pkg = StringUtils.join(components.iterator(), String.valueOf(separator));
132 StringBuilder testName = new StringBuilder();
133
134 log.debug("classPackage is now: {}", pkg);
135
136 if (!components.isEmpty())
137 {
138 testName.append(pkg);
139 testName.append(separator);
140 }
141
142 testName.append((firstRun)
143 ? className
144 : TemplateService.DEFAULT_NAME);
145
146 log.debug("Looking for {}", testName);
147 try
148 {
149 loader.getAssembler(testName.toString());
150 log.debug("Found it, returning {}", testName);
151 return testName.toString();
152 }
153 catch (TurbineException e)
154 {
155 log.error("Turbine Exception Class mapping", e);
156 }
157 catch (Exception e)
158 {
159
160 }
161
162 if (firstRun)
163 {
164 firstRun = false;
165 }
166 else
167 {
168 if (components.isEmpty())
169 {
170 break;
171 }
172 components.remove(componentSize--);
173 }
174 }
175
176 log.debug("Returning default");
177 return getDefaultName(template);
178 }
179 }
180
181
182
183