| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| VelocityServletTemplateEngine |
|
| 1.5;1.5 |
| 1 | /* | |
| 2 | * $Source: /usr/cvsroot/melati/melati/src/main/java/org/melati/template/velocity/VelocityServletTemplateEngine.java,v $ | |
| 3 | * $Revision: 1.4 $ | |
| 4 | * | |
| 5 | * Copyright (C) 2007 Tim Pizey | |
| 6 | * | |
| 7 | * Part of Melati (http://melati.org), a framework for the rapid | |
| 8 | * development of clean, maintainable web applications. | |
| 9 | * | |
| 10 | * Melati is free software; Permission is granted to copy, distribute | |
| 11 | * and/or modify this software under the terms either: | |
| 12 | * | |
| 13 | * a) the GNU General Public License as published by the Free Software | |
| 14 | * Foundation; either version 2 of the License, or (at your option) | |
| 15 | * any later version, | |
| 16 | * | |
| 17 | * or | |
| 18 | * | |
| 19 | * b) any version of the Melati Software License, as published | |
| 20 | * at http://melati.org | |
| 21 | * | |
| 22 | * You should have received a copy of the GNU General Public License and | |
| 23 | * the Melati Software License along with this program; | |
| 24 | * if not, write to the Free Software Foundation, Inc., | |
| 25 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the | |
| 26 | * GNU General Public License and visit http://melati.org to obtain the | |
| 27 | * Melati Software License. | |
| 28 | * | |
| 29 | * Feel free to contact the Developers of Melati (http://melati.org), | |
| 30 | * if you would like to work out a different arrangement than the options | |
| 31 | * outlined here. It is our intention to allow Melati to be used by as | |
| 32 | * wide an audience as possible. | |
| 33 | * | |
| 34 | * This program is distributed in the hope that it will be useful, | |
| 35 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 36 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 37 | * GNU General Public License for more details. | |
| 38 | * | |
| 39 | * Contact details for copyright holder: | |
| 40 | * | |
| 41 | * Tim Pizey <timp At paneris.org> | |
| 42 | */ | |
| 43 | package org.melati.template.velocity; | |
| 44 | ||
| 45 | import java.io.IOException; | |
| 46 | ||
| 47 | import javax.servlet.http.HttpServlet; | |
| 48 | import javax.servlet.http.HttpServletResponse; | |
| 49 | ||
| 50 | import org.apache.velocity.VelocityContext; | |
| 51 | import org.melati.Melati; | |
| 52 | import org.melati.MelatiConfig; | |
| 53 | import org.melati.template.ServletTemplateContext; | |
| 54 | import org.melati.template.ServletTemplateEngine; | |
| 55 | import org.melati.template.TemplateEngineException; | |
| 56 | import org.melati.util.MelatiWriter; | |
| 57 | ||
| 58 | /** | |
| 59 | * @author timp | |
| 60 | * @since 22 Aug 2007 | |
| 61 | * | |
| 62 | */ | |
| 63 | public class VelocityServletTemplateEngine extends VelocityTemplateEngine | |
| 64 | implements ServletTemplateEngine { | |
| 65 | ||
| 66 | /** | |
| 67 | * Constructor. | |
| 68 | */ | |
| 69 | public VelocityServletTemplateEngine() { | |
| 70 | 51 | super(); |
| 71 | 51 | } |
| 72 | ||
| 73 | /** | |
| 74 | * Construct a new Engine for use in a servlet environment. For Velocity this | |
| 75 | * is no different to initialisation outside of a servlets environment. | |
| 76 | * | |
| 77 | * @see org.melati.servlet.TemplateServlet | |
| 78 | * @param melatiConfig | |
| 79 | * a {@link MelatiConfig} | |
| 80 | * @param servlet | |
| 81 | * the servlet we are within | |
| 82 | * @throws TemplateEngineException | |
| 83 | * if any problem occurs with the engine | |
| 84 | */ | |
| 85 | public void init(MelatiConfig melatiConfig, HttpServlet servlet) | |
| 86 | throws TemplateEngineException { | |
| 87 | 0 | init(melatiConfig); |
| 88 | 0 | } |
| 89 | ||
| 90 | /** | |
| 91 | * @param response | |
| 92 | * the <code>HttpServletResponse</code> that this writer will be | |
| 93 | * part of | |
| 94 | * @param buffered | |
| 95 | * whether the writer should be buffered | |
| 96 | * @throws IOException | |
| 97 | * if there is a problem with the filesystem. | |
| 98 | * @return a {@link MelatiWriter} appropriate for this engine. | |
| 99 | */ | |
| 100 | public MelatiWriter getServletWriter(HttpServletResponse response, | |
| 101 | boolean buffered) throws IOException { | |
| 102 | 0 | if (buffered) { |
| 103 | 0 | return new MelatiBufferedVelocityWriter(response); |
| 104 | } else { | |
| 105 | 0 | return new MelatiVelocityWriter(response); |
| 106 | } | |
| 107 | } | |
| 108 | ||
| 109 | /** | |
| 110 | * Get the template context for Velocity, with servlet specific objects added. | |
| 111 | * | |
| 112 | * @param melati | |
| 113 | * the {@link Melati} | |
| 114 | * @return a {@link TemplateContext} | |
| 115 | */ | |
| 116 | public ServletTemplateContext getServletTemplateContext(Melati melati) { | |
| 117 | 0 | VelocityContext context = new VelocityContext(); |
| 118 | 0 | org.melati.template.velocity.HttpServletRequestWrap req = |
| 119 | new org.melati.template.velocity.HttpServletRequestWrap( | |
| 120 | melati.getRequest()); | |
| 121 | 0 | context.put(VelocityServletTemplateContext.REQUEST, req); |
| 122 | 0 | context.put(VelocityServletTemplateContext.RESPONSE, melati.getResponse()); |
| 123 | 0 | return new VelocityServletTemplateContext(context); |
| 124 | } | |
| 125 | ||
| 126 | ||
| 127 | } |