1 package org.apache.fulcrum.template; 2 3 56 57 import java.io.File ; 58 import java.io.OutputStream ; 59 import java.io.Writer ; 60 import java.util.HashMap ; 61 62 import org.apache.fulcrum.BaseService; 63 import org.apache.fulcrum.InitializationException; 64 import org.apache.fulcrum.ServiceException; 65 66 106 public class TurbineTemplateService 107 extends BaseService 108 implements TemplateService 109 { 110 114 protected static final String NO_FILE_EXT = ""; 115 116 119 private String defaultExtension; 120 121 129 private HashMap templateEngineRegistry; 130 131 public TurbineTemplateService() 132 { 133 } 134 135 140 public void init() 141 throws InitializationException 142 { 143 setInit(true); 144 } 145 146 153 public String [] translateTemplatePaths(String [] templatePaths) 154 { 155 for (int i = 0; i < templatePaths.length; i++) 156 { 157 templatePaths[i] = getRealPath(templatePaths[i]); 158 } 159 return templatePaths; 160 } 161 162 170 public boolean templateExists(String template, 171 String [] templatePaths) 172 { 173 for (int i = 0; i < templatePaths.length; i++) 174 { 175 if (new File (templatePaths[i],template).exists()) 176 { 177 return true; 178 } 179 } 180 return false; 181 } 182 183 195 public boolean templateExists(String template) 196 { 197 TemplateEngineService tes = getTemplateEngineService(template); 198 199 if (tes != null) 200 { 201 return tes.templateExists(template); 202 } 203 else 204 { 205 return false; 206 } 207 } 208 209 215 public synchronized void registerTemplateEngineService(TemplateEngineService service) 216 { 217 HashMap registry = templateEngineRegistry != null ? 220 (HashMap ) templateEngineRegistry.clone() : new HashMap (); 221 222 String [] exts = service.getAssociatedFileExtensions(); 223 224 for (int i = 0; i < exts.length; i++) 225 { 226 registry.put(exts[i], service); 227 } 228 templateEngineRegistry = registry; 229 } 230 231 238 protected TemplateEngineService getTemplateEngineService(String template) 239 { 240 HashMap registry = templateEngineRegistry; 241 if (registry != null && template != null) 242 { 243 int dotIndex = template.lastIndexOf('.'); 244 String ext = dotIndex == -1 ? 245 defaultExtension : template.substring(dotIndex + 1); 246 return (TemplateEngineService) registry.get(ext); 247 } 248 else 249 { 250 return null; 251 } 252 } 253 254 public String handleRequest(TemplateContext context, String template) 255 throws ServiceException 256 { 257 TemplateEngineService tes = getTemplateEngineService(template); 258 return tes.handleRequest(context, template); 259 } 260 261 public void handleRequest(TemplateContext context, String template, 262 OutputStream outputStream) 263 throws ServiceException 264 { 265 TemplateEngineService tes = getTemplateEngineService(template); 266 tes.handleRequest(context, template, outputStream); 267 } 268 269 public void handleRequest(TemplateContext context, String template, 270 Writer writer) 271 throws ServiceException 272 { 273 TemplateEngineService tes = getTemplateEngineService(template); 274 tes.handleRequest(context, template, writer); 275 } 276 277 public TemplateContext getTemplateContext() 278 { 279 return new DefaultTemplateContext(); 280 } 281 } 282 | Popular Tags |