1 package org.apache.turbine.pipeline; 2 3 56 57 import java.util.ArrayList ; 58 import java.util.List ; 59 import java.util.Iterator ; 60 import org.apache.turbine.Turbine; 61 import org.apache.turbine.TurbineConstants; 62 import org.apache.turbine.Resolver; 63 import org.apache.turbine.modules.Module; 64 import org.apache.commons.logging.Log; 65 import org.apache.commons.logging.LogFactory; 66 67 71 73 78 81 92 115 116 130 131 138 public class DefaultResolver 139 implements Resolver, TurbineConstants 140 { 141 private static final Log log = LogFactory.getLog(DefaultResolver.class); 142 143 protected String defaultTemplate; 144 145 public void init() 146 throws Exception 147 { 148 defaultTemplate = 149 Turbine.getConfiguration().getString("template.default") + 150 "." + 151 Turbine.getConfiguration().getString("template.default.extension"); 152 153 int idx = defaultTemplate.indexOf('/'); 155 if (idx == -1 || idx > 0) 156 { 157 defaultTemplate = "/" + defaultTemplate; 158 } 159 160 log.debug("DefaultResolver: default template " + defaultTemplate); 161 } 162 163 169 public String getTemplate(String moduleType, String targetTemplate) 170 throws Exception 171 { 172 return findTemplate(moduleType, targetTemplate); 173 } 174 175 183 public Module getModule(String type, String name) 184 throws Exception 185 { 186 return findModule(type, name); 187 } 188 189 protected String findTemplate(String moduleType, String targetTemplate) 190 throws Exception 191 { 192 StringBuffer sb = new StringBuffer (); 193 PipelineUtil.parseTemplatePath(targetTemplate, sb); 194 Iterator j = getPossibleTemplates(sb.toString()); 195 196 while (j.hasNext()) 197 { 198 String template = moduleType + "/" + (String ) j.next(); 199 if (Module.templateExists(template)) 200 { 201 return template; 202 } 203 } 204 205 return moduleType + defaultTemplate; 208 } 209 210 217 protected Iterator getPossibleTemplates(String template) 218 throws Exception 219 { 220 List packages = new ArrayList (); 221 222 StringBuffer pckage = new StringBuffer (); 224 int i = PipelineUtil.parseTemplatePath(template,pckage); 225 226 if (pckage.charAt(0) == '/') 227 { 228 pckage.deleteCharAt(0); 229 i--; 230 } 231 232 String extension = Turbine.getConfiguration() 233 .getString("template.default.extension", "vm"); 234 235 int j = 9999; 239 String module; 240 while (j-- > 0) 241 { 242 module = pckage.toString(); 243 244 packages.add(module); 245 246 pckage.setLength(i + 1); 247 if (i > 0) 248 { 249 for (i = pckage.length() - 2; i >= 0; i--) 251 { 252 if (pckage.charAt(i) == '/') 253 { 254 break; 255 } 256 } 257 } 258 else if (j > 0) 259 { 260 j = 1; 262 } 263 pckage.append("Default").append(".").append(extension); 264 } 265 266 return packages.iterator(); 268 } 269 270 protected Module findModule(String type, String name) 271 throws Exception 272 { 273 return Turbine.getModuleLoader().getModule(type,name); 276 } 277 } 278 | Popular Tags |