1 package org.apache.turbine.services.template.mapper; 2 3 18 19 import java.util.List ; 20 import java.util.ArrayList ; 21 import java.util.Arrays ; 22 23 import org.apache.commons.lang.StringUtils; 24 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 28 import org.apache.turbine.services.template.TemplateEngineService; 29 import org.apache.turbine.services.template.TemplateService; 30 import org.apache.turbine.services.template.TurbineTemplate; 31 32 46 47 public class LayoutTemplateMapper 48 extends BaseTemplateMapper 49 implements Mapper 50 { 51 52 private static Log log = LogFactory.getLog(LayoutTemplateMapper.class); 53 54 59 public LayoutTemplateMapper() 60 { 61 } 62 63 70 public String doMapping(String template) 71 { 72 log.debug("doMapping(" + template + ")"); 73 List components 75 = new ArrayList (Arrays.asList(StringUtils.split( 76 template, 77 String.valueOf(TemplateService.TEMPLATE_PARTS_SEPARATOR)))); 78 int componentSize = components.size() - 1 ; 79 80 String templateName = (String ) components.get(componentSize); 83 components.remove(componentSize--); 84 85 log.debug("templateName is " + templateName); 86 87 TemplateEngineService tes = TurbineTemplate.getTemplateEngineService(templateName); 89 90 if (tes == null) 91 { 92 return null; 93 } 94 95 String defaultName = 96 TurbineTemplate.getDefaultLayoutTemplateName(templateName); 98 boolean firstRun = !templateName.equals(defaultName); 102 103 for(;;) 104 { 105 String templatePackage = StringUtils.join(components.iterator(), String.valueOf(separator)); 106 107 log.debug("templatePackage is now: " + templatePackage); 108 109 StringBuffer testName = new StringBuffer (); 110 111 if (!components.isEmpty()) 112 { 113 testName.append(templatePackage); 114 testName.append(separator); 115 } 116 117 testName.append((firstRun) 118 ? templateName 119 : defaultName); 120 121 StringBuffer templatePath = new StringBuffer (); 123 if (StringUtils.isNotEmpty(prefix)) 124 { 125 templatePath.append(prefix); 126 templatePath.append(separator); 127 } 128 templatePath.append(testName); 129 130 log.debug("Looking for " + templatePath); 131 132 if (tes.templateExists(templatePath.toString())) 133 { 134 log.debug("Found it, returning " + testName); 135 return testName.toString(); 136 } 137 138 if (firstRun) 139 { 140 firstRun = false; 141 } 142 else 143 { 144 if (components.isEmpty()) 149 { 150 break; } 152 components.remove(componentSize--); 155 } 156 } 157 158 log.debug("Returning default"); 159 return getDefaultName(template); 160 } 161 } 162 | Popular Tags |