001package org.apache.turbine.services.template.mapper; 002 003 004/* 005 * Licensed to the Apache Software Foundation (ASF) under one 006 * or more contributor license agreements. See the NOTICE file 007 * distributed with this work for additional information 008 * regarding copyright ownership. The ASF licenses this file 009 * to you under the Apache License, Version 2.0 (the 010 * "License"); you may not use this file except in compliance 011 * with the License. You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, 016 * software distributed under the License is distributed on an 017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 018 * KIND, either express or implied. See the License for the 019 * specific language governing permissions and limitations 020 * under the License. 021 */ 022 023 024import java.util.ArrayList; 025import java.util.Arrays; 026import java.util.List; 027 028import org.apache.commons.lang3.StringUtils; 029import org.apache.logging.log4j.LogManager; 030import org.apache.logging.log4j.Logger; 031import org.apache.turbine.services.TurbineServices; 032import org.apache.turbine.services.template.TemplateEngineService; 033import org.apache.turbine.services.template.TemplateService; 034 035/** 036 * This mapper is responsible for the lookup of templates for the Layout 037 * It tries to look in various packages for a match: 038 * 039 * 1. about,directions,Driving.vm <- exact match 040 * 2. about,directions,Default.vm <- package match, Default name 041 * 3. about,Default.vm <- stepping up in the hierarchy 042 * 4. Default.vm <- The name configured as default.layout.template 043 * in the corresponding Templating Engine 044 045 * 046 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> 047 * @version $Id$ 048 */ 049 050public class LayoutTemplateMapper 051 extends BaseTemplateMapper 052 implements Mapper 053{ 054 /** Logging */ 055 private static final Logger log = LogManager.getLogger(LayoutTemplateMapper.class); 056 057 /** 058 * Default C'tor. If you use this C'tor, you must use 059 * the bean setter to set the various properties needed for 060 * this mapper before first usage. 061 */ 062 public LayoutTemplateMapper() 063 { 064 // empty 065 } 066 067 /** 068 * Look for a given Template, then try the 069 * defaults until we hit the root. 070 * 071 * @param template The template name. 072 * @return The parsed module name. 073 */ 074 @Override 075 public String doMapping(String template) 076 { 077 log.debug("doMapping({})", template); 078 // Copy our elements into an array 079 List<String> components 080 = new ArrayList<>(Arrays.asList(StringUtils.split( 081 template, 082 String.valueOf(TemplateService.TEMPLATE_PARTS_SEPARATOR)))); 083 int componentSize = components.size() - 1 ; 084 085 // This method never gets an empty string passed. 086 // So this is never < 0 087 String templateName = components.get(componentSize); 088 components.remove(componentSize--); 089 090 log.debug("templateName is {}", templateName); 091 092 // Last element decides, which template Service to use... 093 TemplateService templateService = (TemplateService)TurbineServices.getInstance().getService(TemplateService.SERVICE_NAME); 094 TemplateEngineService tes = templateService.getTemplateEngineService(templateName); 095 096 if (tes == null) 097 { 098 return null; 099 } 100 101 // We're, after all, a Layout Template Mapper... 102 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 boolean firstRun = !templateName.equals(defaultName); 108 109 for(;;) 110 { 111 String templatePackage = StringUtils.join(components.iterator(), String.valueOf(separator)); 112 113 log.debug("templatePackage is now: {}", templatePackage); 114 115 StringBuilder testName = new StringBuilder(); 116 117 if (!components.isEmpty()) 118 { 119 testName.append(templatePackage); 120 testName.append(separator); 121 } 122 123 testName.append((firstRun) 124 ? templateName 125 : defaultName); 126 127 // But the Templating service must look for the name with prefix 128 StringBuilder templatePath = new StringBuilder(); 129 if (StringUtils.isNotEmpty(prefix)) 130 { 131 templatePath.append(prefix); 132 templatePath.append(separator); 133 } 134 templatePath.append(testName); 135 136 log.debug("Looking for {}", templatePath); 137 138 if (tes.templateExists(templatePath.toString())) 139 { 140 log.debug("Found it, returning {}", testName); 141 return testName.toString(); 142 } 143 144 if (firstRun) 145 { 146 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 if (components.isEmpty()) 155 { 156 break; // for(;;) 157 } 158 // We still have components. Remove the 159 // last one and go through the loop again. 160 components.remove(componentSize--); 161 } 162 } 163 164 log.debug("Returning default"); 165 return getDefaultName(template); 166 } 167}