01: /*
02: * Copyright (c) 2002-2006 by OpenSymphony
03: * All rights reserved.
04: */
05: package com.opensymphony.webwork.components.template;
06:
07: import com.opensymphony.webwork.ServletActionContext;
08: import com.opensymphony.webwork.views.velocity.VelocityManager;
09: import org.apache.commons.logging.Log;
10: import org.apache.commons.logging.LogFactory;
11: import org.apache.velocity.app.VelocityEngine;
12: import org.apache.velocity.context.Context;
13:
14: import javax.servlet.ServletContext;
15: import javax.servlet.http.HttpServletRequest;
16: import javax.servlet.http.HttpServletResponse;
17: import java.io.IOException;
18: import java.io.Writer;
19: import java.util.Iterator;
20: import java.util.List;
21: import java.util.Map;
22:
23: /**
24: * Velocity based template engine.
25: *
26: * @author jcarreira
27: */
28: public class VelocityTemplateEngine extends BaseTemplateEngine {
29: private static final Log LOG = LogFactory
30: .getLog(VelocityTemplateEngine.class);
31:
32: public void renderTemplate(TemplateRenderingContext templateContext)
33: throws Exception {
34: // get the various items required from the stack
35: Map actionContext = templateContext.getStack().getContext();
36: ServletContext servletContext = (ServletContext) actionContext
37: .get(ServletActionContext.SERVLET_CONTEXT);
38: HttpServletRequest req = (HttpServletRequest) actionContext
39: .get(ServletActionContext.HTTP_REQUEST);
40: HttpServletResponse res = (HttpServletResponse) actionContext
41: .get(ServletActionContext.HTTP_RESPONSE);
42:
43: // prepare velocity
44: VelocityManager velocityManager = VelocityManager.getInstance();
45: velocityManager.init(servletContext);
46: VelocityEngine velocityEngine = velocityManager
47: .getVelocityEngine();
48:
49: // get the list of templates we can use
50: List templates = templateContext.getTemplate()
51: .getPossibleTemplates(this );
52:
53: // find the right template
54: org.apache.velocity.Template template = null;
55: String templateName = null;
56: Exception exception = null;
57: for (Iterator iterator = templates.iterator(); iterator
58: .hasNext();) {
59: Template t = (Template) iterator.next();
60: templateName = getFinalTemplateName(t);
61: try {
62: // try to load, and if it works, stop at the first one
63: template = velocityEngine.getTemplate(templateName);
64: break;
65: } catch (IOException e) {
66: if (exception == null) {
67: exception = e;
68: }
69: }
70: }
71:
72: if (template == null) {
73: LOG.error("Could not load template "
74: + templateContext.getTemplate());
75: if (exception != null) {
76: throw exception;
77: } else {
78: return;
79: }
80: }
81:
82: if (LOG.isDebugEnabled()) {
83: LOG.debug("Rendering template " + templateName);
84: }
85:
86: Context context = velocityManager.createContext(templateContext
87: .getStack(), req, res);
88:
89: Writer outputWriter = templateContext.getWriter();
90: context.put("tag", templateContext.getTag());
91: context.put("parameters", templateContext.getParameters());
92:
93: template.merge(context, outputWriter);
94: }
95:
96: protected String getSuffix() {
97: return "vm";
98: }
99: }
|