1 /*
2 * $Source$
3 * $Revision$
4 *
5 * Copyright (C) 2004 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@paneris.org>
42 */
43
44 package org.melati.template.webmacro;
45
46 import java.io.IOException;
47
48 import javax.servlet.http.HttpServlet;
49 import javax.servlet.http.HttpServletResponse;
50
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.template.TemplateIOException;
57 import org.melati.util.MelatiWriter;
58 import org.webmacro.InitException;
59 import org.webmacro.WM;
60 import org.webmacro.servlet.WebContext;
61
62
63 /**
64 * Wrapper for the WebMacro Template Engine for use in a Servlet context.
65 */
66 public class WebmacroServletTemplateEngine
67 extends WebmacroTemplateEngine implements ServletTemplateEngine {
68
69 /**
70 * Constructor.
71 */
72 public WebmacroServletTemplateEngine() {
73 super();
74 }
75
76 /**
77 * Construct a new Engine for use in a servlet environment.
78 *
79 * @see org.melati.servlet.TemplateServlet
80 * @param melatiConfig a {@link MelatiConfig}
81 * @param servlet the servlet we are within
82 * @throws TemplateEngineException if any problem occurs with the engine
83 */
84 public void init(MelatiConfig melatiConfig, HttpServlet servlet)
85 throws TemplateEngineException {
86 try {
87 wm = new WM(servlet);
88 } catch (InitException e) {
89 throw new TemplateEngineException(e);
90 }
91 }
92
93 /**
94 * Get the generic parameters for WebMacro,
95 * with servlet objects in the Context.
96 *
97 * @param melati the {@link Melati}
98 * @return a {@link ServletTemplateContext}
99 */
100 public ServletTemplateContext getServletTemplateContext(Melati melati) {
101 WebContext webContext = new WebContext(wm.getBroker(),
102 melati.getRequest(),
103 melati.getResponse());
104 return new WebmacroServletTemplateContext(webContext);
105 }
106
107 /**
108 * @param response the <code>HttpServletResponse</code> that this
109 * writer will be part of
110 * @param buffered whether the writer should be buffered
111 * @return a {@link MelatiWriter}
112 * appropriate for this engine.
113 */
114 public MelatiWriter getServletWriter(HttpServletResponse response,
115 boolean buffered) {
116 if (buffered) {
117 try {
118 return new MelatiBufferedFastWriter(wm.getBroker(),response);
119 } catch (IOException e) {
120 throw new TemplateIOException(e);
121 }
122 } else {
123 try {
124 return new MelatiFastWriter(wm.getBroker(),response);
125 } catch (IOException e) {
126 throw new TemplateIOException(e);
127 }
128 }
129 }
130
131
132
133 }