GeoPubby  Version 0.1.0.0
VelocityHelper Class Reference

A facade class that simplifies using a custom Velocity engine from a servlet. More...

Collaboration diagram for VelocityHelper:

Public Member Functions

 VelocityHelper (ServletContext servletContext, HttpServletResponse response)
 
Context getVelocityContext ()
 
void renderXHTML (String templateName)
 Renders a template using the template variables put into the velocity context. More...
 

Private Member Functions

VelocityEngine getVelocityEngine ()
 
VelocityEngine createVelocityEngine ()
 

Private Attributes

final ServletContext servletContext
 
final HttpServletResponse response
 
final Context velocityContext
 

Static Private Attributes

static final String VELOCITY_ENGINE
 

Detailed Description

A facade class that simplifies using a custom Velocity engine from a servlet.

It encapsulates creation of the VelocityEngine instance, its storage in the servlet context, and the rendering of templates into the servlet response output stream.

Constructor & Destructor Documentation

◆ VelocityHelper()

VelocityHelper ( ServletContext  servletContext,
HttpServletResponse  response 
)
28  {
30  this.response = response;
31  this.velocityContext = new VelocityContext();
32  }
final HttpServletResponse response
Definition: VelocityHelper.java:25
final ServletContext servletContext
Definition: VelocityHelper.java:24
final Context velocityContext
Definition: VelocityHelper.java:26

References VelocityHelper.response, and VelocityHelper.servletContext.

Member Function Documentation

◆ createVelocityEngine()

VelocityEngine createVelocityEngine ( )
private
67  {
68  try {
69  VelocityEngine result = new VelocityEngine();
70  result.setProperty("output.encoding", "utf-8");
71  result.setProperty("file.resource.loader.path",
72  servletContext.getRealPath("/") + "/WEB-INF/templates/");
73  result.setProperty("velocimacro.context.localscope", true);
74 
75  // Turn off Velocity logging
76  result.setProperty("runtime.log.logsystem.class",
77  "org.apache.velocity.runtime.log.NullLogSystem");
78 
79  // XML-escape *all* references inserted into templates
80  result.setProperty("eventhandler.referenceinsertion.class", EscapeXmlReference.class.getName());
81 
82  // Enable caching
83  result.setProperty("file.resource.loader.cache", true);
84 
85  result.init();
86  return result;
87  } catch (Exception ex) {
88  throw new RuntimeException(ex);
89  }
90  }

References VelocityHelper.servletContext.

Referenced by VelocityHelper.getVelocityEngine().

◆ getVelocityContext()

Context getVelocityContext ( )
Returns
A receptacle for template variables
37  {
38  return velocityContext;
39  }

References VelocityHelper.velocityContext.

◆ getVelocityEngine()

VelocityEngine getVelocityEngine ( )
private
58  {
59  synchronized (servletContext) {
60  if (servletContext.getAttribute(VELOCITY_ENGINE) == null) {
62  }
63  return (VelocityEngine) servletContext.getAttribute(VELOCITY_ENGINE);
64  }
65  }
VelocityEngine createVelocityEngine()
Definition: VelocityHelper.java:67
static final String VELOCITY_ENGINE
Definition: VelocityHelper.java:21

References VelocityHelper.createVelocityEngine(), VelocityHelper.servletContext, and VelocityHelper.VELOCITY_ENGINE.

Referenced by VelocityHelper.renderXHTML().

◆ renderXHTML()

void renderXHTML ( String  templateName)

Renders a template using the template variables put into the velocity context.

44  {
45  response.addHeader("Content-Type", "text/html; charset=utf-8");
46  response.addHeader("Cache-Control", "no-cache");
47  response.addHeader("Pragma", "no-cache");
48  try {
49  OutputStreamWriter writer = new OutputStreamWriter(response.getOutputStream(), "utf-8");
50  getVelocityEngine().mergeTemplate(templateName,"UTF-8", velocityContext,
51  writer);
52  writer.close();
53  } catch (Exception ex) {
54  throw new RuntimeException(ex);
55  }
56  }
VelocityEngine getVelocityEngine()
Definition: VelocityHelper.java:58

References VelocityHelper.getVelocityEngine(), VelocityHelper.response, and VelocityHelper.velocityContext.

Member Data Documentation

◆ response

final HttpServletResponse response
private

◆ servletContext

final ServletContext servletContext
private

◆ VELOCITY_ENGINE

final String VELOCITY_ENGINE
staticprivate
Initial value:
=
VelocityHelper.class.getName() + ".VELOCITY_ENGINE"
VelocityHelper(ServletContext servletContext, HttpServletResponse response)
Definition: VelocityHelper.java:28

Referenced by VelocityHelper.getVelocityEngine().

◆ velocityContext

final Context velocityContext
private