Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ScreenDefaultTemplateMapper |
|
| 5.5;5,5 |
1 | package org.apache.turbine.services.template.mapper; | |
2 | ||
3 | ||
4 | /* | |
5 | * Licensed to the Apache Software Foundation (ASF) under one | |
6 | * or more contributor license agreements. See the NOTICE file | |
7 | * distributed with this work for additional information | |
8 | * regarding copyright ownership. The ASF licenses this file | |
9 | * to you under the Apache License, Version 2.0 (the | |
10 | * "License"); you may not use this file except in compliance | |
11 | * with the License. You may obtain a copy of the License at | |
12 | * | |
13 | * http://www.apache.org/licenses/LICENSE-2.0 | |
14 | * | |
15 | * Unless required by applicable law or agreed to in writing, | |
16 | * software distributed under the License is distributed on an | |
17 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
18 | * KIND, either express or implied. See the License for the | |
19 | * specific language governing permissions and limitations | |
20 | * under the License. | |
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.services.TurbineServices; | |
32 | import org.apache.turbine.services.template.TemplateEngineService; | |
33 | import org.apache.turbine.services.template.TemplateService; | |
34 | ||
35 | /** | |
36 | * This is a pretty simple mapper which returns template pathes for | |
37 | * a supplied template name. If the path does not exist, it looks for | |
38 | * a templated called "Default" in the same package. | |
39 | * This path can be used by the TemplateEngine to access | |
40 | * a certain resource to actually render the template. | |
41 | * | |
42 | * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> | |
43 | * @version $Id: ScreenDefaultTemplateMapper.java 1854688 2019-03-03 10:36:42Z tv $ | |
44 | */ | |
45 | ||
46 | public class ScreenDefaultTemplateMapper | |
47 | extends BaseTemplateMapper | |
48 | implements Mapper | |
49 | { | |
50 | /** Logging */ | |
51 | 0 | private static final Logger log = LogManager.getLogger(ScreenDefaultTemplateMapper.class); |
52 | ||
53 | /** | |
54 | * Default C'tor. If you use this C'tor, you must use | |
55 | * the bean setter to set the various properties needed for | |
56 | * this mapper before first usage. | |
57 | */ | |
58 | public ScreenDefaultTemplateMapper() | |
59 | 0 | { |
60 | // empty | |
61 | 0 | } |
62 | ||
63 | /** | |
64 | * Look for a given Template, then try the | |
65 | * default. | |
66 | * | |
67 | * @param template The template name. | |
68 | * @return The parsed module name. | |
69 | */ | |
70 | @Override | |
71 | public String doMapping(String template) | |
72 | { | |
73 | 0 | log.debug("doMapping({})", template); |
74 | // Copy our elements into an array | |
75 | 0 | List<String> components |
76 | 0 | = new ArrayList<String>(Arrays.asList(StringUtils.split( |
77 | template, | |
78 | 0 | String.valueOf(TemplateService.TEMPLATE_PARTS_SEPARATOR)))); |
79 | 0 | int componentSize = components.size() - 1 ; |
80 | ||
81 | // This method never gets an empty string passed. | |
82 | // So this is never < 0 | |
83 | 0 | String templateName = components.get(componentSize); |
84 | 0 | components.remove(componentSize--); |
85 | ||
86 | 0 | log.debug("templateName is {}", templateName); |
87 | ||
88 | // Last element decides, which template Service to use... | |
89 | 0 | TemplateService templateService = (TemplateService)TurbineServices.getInstance().getService(TemplateService.SERVICE_NAME); |
90 | 0 | TemplateEngineService tes = templateService.getTemplateEngineService(templateName); |
91 | ||
92 | 0 | if (tes == null) |
93 | { | |
94 | 0 | return null; |
95 | } | |
96 | ||
97 | 0 | String defaultName = "Default.vm"; |
98 | ||
99 | // This is an optimization. If the name we're looking for is | |
100 | // already the default name for the template, don't do a "first run" | |
101 | // which looks for an exact match. | |
102 | 0 | boolean firstRun = !templateName.equals(defaultName); |
103 | ||
104 | for(;;) | |
105 | { | |
106 | 0 | String templatePackage = StringUtils.join(components.iterator(), String.valueOf(separator)); |
107 | ||
108 | 0 | log.debug("templatePackage is now: {}", templatePackage); |
109 | ||
110 | 0 | StringBuilder testName = new StringBuilder(); |
111 | ||
112 | 0 | if (!components.isEmpty()) |
113 | { | |
114 | 0 | testName.append(templatePackage); |
115 | 0 | testName.append(separator); |
116 | } | |
117 | ||
118 | 0 | testName.append((firstRun) |
119 | ? templateName | |
120 | : defaultName); | |
121 | ||
122 | // But the Templating service must look for the name with prefix | |
123 | 0 | StringBuilder templatePath = new StringBuilder(); |
124 | 0 | if (StringUtils.isNotEmpty(prefix)) |
125 | { | |
126 | 0 | templatePath.append(prefix); |
127 | 0 | templatePath.append(separator); |
128 | } | |
129 | 0 | templatePath.append(testName); |
130 | ||
131 | 0 | log.debug("Looking for {}", templatePath); |
132 | ||
133 | 0 | if (tes.templateExists(templatePath.toString())) |
134 | { | |
135 | 0 | log.debug("Found it, returning {}", testName); |
136 | 0 | return testName.toString(); |
137 | } | |
138 | ||
139 | 0 | if (firstRun) |
140 | { | |
141 | 0 | firstRun = false; |
142 | } | |
143 | else | |
144 | { | |
145 | // We run this loop only two times. The | |
146 | // first time with the 'real' name and the | |
147 | // second time with "Default". The second time | |
148 | // we will end up here and break the for(;;) loop. | |
149 | break; | |
150 | } | |
151 | 0 | } |
152 | ||
153 | 0 | log.debug("Returning default"); |
154 | 0 | return getDefaultName(template); |
155 | } | |
156 | } |