KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > components > template > VelocityTemplateEngine


1 package com.opensymphony.webwork.components.template;
2
3 import com.opensymphony.webwork.ServletActionContext;
4 import com.opensymphony.webwork.views.velocity.AbstractTagDirective;
5 import com.opensymphony.webwork.views.velocity.VelocityManager;
6 import com.opensymphony.xwork.ActionContext;
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9 import org.apache.velocity.app.VelocityEngine;
10 import org.apache.velocity.context.Context;
11
12 import javax.servlet.ServletContext JavaDoc;
13 import javax.servlet.http.HttpServletRequest JavaDoc;
14 import javax.servlet.http.HttpServletResponse JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.Writer JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20
21 /**
22  * VelocityTemplateEngine renders Velocity templates
23  * Date: Sep 28, 2004 12:01:09 PM
24  *
25  * @author jcarreira
26  */

27 public class VelocityTemplateEngine extends BaseTemplateEngine {
28     private static final Log LOG = LogFactory.getLog(VelocityTemplateEngine.class);
29
30     public void renderTemplate(TemplateRenderingContext templateContext) throws Exception JavaDoc {
31         // get the various items required from the stack
32
Map JavaDoc actionContext = templateContext.getStack().getContext();
33         ServletContext JavaDoc servletContext = (ServletContext JavaDoc) actionContext.get(ServletActionContext.SERVLET_CONTEXT);
34         HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc) actionContext.get(ServletActionContext.HTTP_REQUEST);
35         HttpServletResponse JavaDoc res = (HttpServletResponse JavaDoc) actionContext.get(ServletActionContext.HTTP_RESPONSE);
36
37         // prepare velocity
38
VelocityManager velocityManager = VelocityManager.getInstance();
39         velocityManager.init(servletContext);
40         VelocityEngine velocityEngine = velocityManager.getVelocityEngine();
41
42         // get the list of templates we can use
43
List JavaDoc templates = templateContext.getTemplate().getPossibleTemplates(this);
44
45         // find the right template
46
org.apache.velocity.Template template = null;
47         String JavaDoc templateName = null;
48         Exception JavaDoc exception = null;
49         for (Iterator JavaDoc iterator = templates.iterator(); iterator.hasNext();) {
50             Template t = (Template) iterator.next();
51             templateName = getFinalTemplateName(t);
52             try {
53                 // try to load, and if it works, stop at the first one
54
template = velocityEngine.getTemplate(templateName);
55                 break;
56             } catch (IOException JavaDoc e) {
57                 if (exception == null) {
58                     exception = e;
59                 }
60             }
61         }
62
63         if (template == null) {
64             LOG.error("Could not load template " + templateContext.getTemplate());
65             if (exception != null) {
66                 throw exception;
67             } else {
68                 return;
69             }
70         }
71
72         if (LOG.isDebugEnabled()) {
73             LOG.debug("Rendering template " + templateName);
74         }
75
76         Context context = velocityManager.createContext(templateContext.getStack(), req, res);
77
78         // get the writer (ghetto JSP hack here, should be removed someday)
79
Writer JavaDoc outputWriter = (Writer JavaDoc) ActionContext.getContext().get(AbstractTagDirective.VELOCITY_WRITER);
80         if (outputWriter == null) {
81             outputWriter = templateContext.getWriter();
82         }
83
84         context.put("tag", templateContext.getTag());
85         context.put("parameters", templateContext.getParameters());
86
87         template.merge(context, outputWriter);
88     }
89
90     protected String JavaDoc getSuffix() {
91         return "vm";
92     }
93 }
94
Popular Tags