| 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.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 | * 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 1071091 2011-02-15 22:06:55Z tv $ | |
| 44 | */ | |
| 45 | ||
| 46 | public class ScreenDefaultTemplateMapper | |
| 47 | extends BaseTemplateMapper | |
| 48 | implements Mapper | |
| 49 | { | |
| 50 | /** Logging */ | |
| 51 | 0 | private static Log log = LogFactory.getLog(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 | public String doMapping(String template) | |
| 71 | { | |
| 72 | 0 | log.debug("doMapping(" + template + ")"); |
| 73 | // Copy our elements into an array | |
| 74 | 0 | List<String> components |
| 75 | = new ArrayList<String>(Arrays.asList(StringUtils.split( | |
| 76 | template, | |
| 77 | String.valueOf(TemplateService.TEMPLATE_PARTS_SEPARATOR)))); | |
| 78 | 0 | int componentSize = components.size() - 1 ; |
| 79 | ||
| 80 | // This method never gets an empty string passed. | |
| 81 | // So this is never < 0 | |
| 82 | 0 | String templateName = components.get(componentSize); |
| 83 | 0 | components.remove(componentSize--); |
| 84 | ||
| 85 | 0 | log.debug("templateName is " + templateName); |
| 86 | ||
| 87 | // Last element decides, which template Service to use... | |
| 88 | 0 | TemplateEngineService tes = TurbineTemplate.getTemplateEngineService(templateName); |
| 89 | ||
| 90 | 0 | if (tes == null) |
| 91 | { | |
| 92 | 0 | return null; |
| 93 | } | |
| 94 | ||
| 95 | 0 | String defaultName = "Default.vm"; |
| 96 | ||
| 97 | // This is an optimization. If the name we're looking for is | |
| 98 | // already the default name for the template, don't do a "first run" | |
| 99 | // which looks for an exact match. | |
| 100 | 0 | boolean firstRun = !templateName.equals(defaultName); |
| 101 | ||
| 102 | for(;;) | |
| 103 | { | |
| 104 | 0 | String templatePackage = StringUtils.join(components.iterator(), String.valueOf(separator)); |
| 105 | ||
| 106 | 0 | log.debug("templatePackage is now: " + templatePackage); |
| 107 | ||
| 108 | 0 | StringBuffer testName = new StringBuffer(); |
| 109 | ||
| 110 | 0 | if (!components.isEmpty()) |
| 111 | { | |
| 112 | 0 | testName.append(templatePackage); |
| 113 | 0 | testName.append(separator); |
| 114 | } | |
| 115 | ||
| 116 | 0 | testName.append((firstRun) |
| 117 | ? templateName | |
| 118 | : defaultName); | |
| 119 | ||
| 120 | // But the Templating service must look for the name with prefix | |
| 121 | 0 | StringBuffer templatePath = new StringBuffer(); |
| 122 | 0 | if (StringUtils.isNotEmpty(prefix)) |
| 123 | { | |
| 124 | 0 | templatePath.append(prefix); |
| 125 | 0 | templatePath.append(separator); |
| 126 | } | |
| 127 | 0 | templatePath.append(testName); |
| 128 | ||
| 129 | 0 | log.debug("Looking for " + templatePath); |
| 130 | ||
| 131 | 0 | if (tes.templateExists(templatePath.toString())) |
| 132 | { | |
| 133 | 0 | log.debug("Found it, returning " + testName); |
| 134 | 0 | return testName.toString(); |
| 135 | } | |
| 136 | ||
| 137 | 0 | if (firstRun) |
| 138 | { | |
| 139 | 0 | firstRun = false; |
| 140 | } | |
| 141 | else | |
| 142 | { | |
| 143 | // We run this loop only two times. The | |
| 144 | // first time with the 'real' name and the | |
| 145 | // second time with "Default". The second time | |
| 146 | // we will end up here and break the for(;;) loop. | |
| 147 | break; | |
| 148 | } | |
| 149 | 0 | } |
| 150 | ||
| 151 | 0 | log.debug("Returning default"); |
| 152 | 0 | return getDefaultName(template); |
| 153 | } | |
| 154 | } |