| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| LayoutTemplateMapper |
|
| 6.0;6 |
| 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.TurbineServices; | |
| 32 | import org.apache.turbine.services.template.TemplateEngineService; | |
| 33 | import org.apache.turbine.services.template.TemplateService; | |
| 34 | ||
| 35 | /** | |
| 36 | * This mapper is responsible for the lookup of templates for the Layout | |
| 37 | * It tries to look in various packages for a match: | |
| 38 | * | |
| 39 | * 1. about,directions,Driving.vm <- exact match | |
| 40 | * 2. about,directions,Default.vm <- package match, Default name | |
| 41 | * 3. about,Default.vm <- stepping up in the hierarchy | |
| 42 | * 4. Default.vm <- The name configured as default.layout.template | |
| 43 | * in the corresponding Templating Engine | |
| 44 | ||
| 45 | * | |
| 46 | * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> | |
| 47 | * @version $Id: LayoutTemplateMapper.java 1723699 2016-01-08 11:29:21Z tv $ | |
| 48 | */ | |
| 49 | ||
| 50 | public class LayoutTemplateMapper | |
| 51 | extends BaseTemplateMapper | |
| 52 | implements Mapper | |
| 53 | { | |
| 54 | /** Logging */ | |
| 55 | 32 | private static Log log = LogFactory.getLog(LayoutTemplateMapper.class); |
| 56 | ||
| 57 | /** | |
| 58 | * Default C'tor. If you use this C'tor, you must use | |
| 59 | * the bean setter to set the various properties needed for | |
| 60 | * this mapper before first usage. | |
| 61 | */ | |
| 62 | public LayoutTemplateMapper() | |
| 63 | 39 | { |
| 64 | // empty | |
| 65 | 39 | } |
| 66 | ||
| 67 | /** | |
| 68 | * Look for a given Template, then try the | |
| 69 | * defaults until we hit the root. | |
| 70 | * | |
| 71 | * @param template The template name. | |
| 72 | * @return The parsed module name. | |
| 73 | */ | |
| 74 | @Override | |
| 75 | public String doMapping(String template) | |
| 76 | { | |
| 77 | 16 | log.debug("doMapping(" + template + ")"); |
| 78 | // Copy our elements into an array | |
| 79 | 16 | List<String> components |
| 80 | = new ArrayList<String>(Arrays.asList(StringUtils.split( | |
| 81 | template, | |
| 82 | String.valueOf(TemplateService.TEMPLATE_PARTS_SEPARATOR)))); | |
| 83 | 16 | int componentSize = components.size() - 1 ; |
| 84 | ||
| 85 | // This method never gets an empty string passed. | |
| 86 | // So this is never < 0 | |
| 87 | 16 | String templateName = components.get(componentSize); |
| 88 | 16 | components.remove(componentSize--); |
| 89 | ||
| 90 | 16 | log.debug("templateName is " + templateName); |
| 91 | ||
| 92 | // Last element decides, which template Service to use... | |
| 93 | 16 | TemplateService templateService = (TemplateService)TurbineServices.getInstance().getService(TemplateService.SERVICE_NAME); |
| 94 | 16 | TemplateEngineService tes = templateService.getTemplateEngineService(templateName); |
| 95 | ||
| 96 | 16 | if (tes == null) |
| 97 | { | |
| 98 | 0 | return null; |
| 99 | } | |
| 100 | ||
| 101 | // We're, after all, a Layout Template Mapper... | |
| 102 | 16 | String defaultName = templateService.getDefaultLayoutTemplateName(templateName); |
| 103 | ||
| 104 | // This is an optimization. If the name we're looking for is | |
| 105 | // already the default name for the template, don't do a "first run" | |
| 106 | // which looks for an exact match. | |
| 107 | 16 | boolean firstRun = !templateName.equals(defaultName); |
| 108 | ||
| 109 | for(;;) | |
| 110 | { | |
| 111 | 28 | String templatePackage = StringUtils.join(components.iterator(), String.valueOf(separator)); |
| 112 | ||
| 113 | 28 | log.debug("templatePackage is now: " + templatePackage); |
| 114 | ||
| 115 | 28 | StringBuilder testName = new StringBuilder(); |
| 116 | ||
| 117 | 28 | if (!components.isEmpty()) |
| 118 | { | |
| 119 | 4 | testName.append(templatePackage); |
| 120 | 4 | testName.append(separator); |
| 121 | } | |
| 122 | ||
| 123 | 28 | testName.append((firstRun) |
| 124 | ? templateName | |
| 125 | : defaultName); | |
| 126 | ||
| 127 | // But the Templating service must look for the name with prefix | |
| 128 | 28 | StringBuilder templatePath = new StringBuilder(); |
| 129 | 28 | if (StringUtils.isNotEmpty(prefix)) |
| 130 | { | |
| 131 | 28 | templatePath.append(prefix); |
| 132 | 28 | templatePath.append(separator); |
| 133 | } | |
| 134 | 28 | templatePath.append(testName); |
| 135 | ||
| 136 | 28 | log.debug("Looking for " + templatePath); |
| 137 | ||
| 138 | 28 | if (tes.templateExists(templatePath.toString())) |
| 139 | { | |
| 140 | 6 | log.debug("Found it, returning " + testName); |
| 141 | 6 | return testName.toString(); |
| 142 | } | |
| 143 | ||
| 144 | 22 | if (firstRun) |
| 145 | { | |
| 146 | 10 | firstRun = false; |
| 147 | } | |
| 148 | else | |
| 149 | { | |
| 150 | // We're no longer on the first Run (so we | |
| 151 | // already tested the "Default" template) | |
| 152 | // and the package is empty (we've hit the | |
| 153 | // root. So we now break the endless loop. | |
| 154 | 12 | if (components.isEmpty()) |
| 155 | { | |
| 156 | 10 | break; // for(;;) |
| 157 | } | |
| 158 | // We still have components. Remove the | |
| 159 | // last one and go through the loop again. | |
| 160 | 2 | components.remove(componentSize--); |
| 161 | } | |
| 162 | 12 | } |
| 163 | ||
| 164 | 10 | log.debug("Returning default"); |
| 165 | 10 | return getDefaultName(template); |
| 166 | } | |
| 167 | } |