Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
WebmacroTemplateEngine |
|
| 2.0;2 |
1 | /* | |
2 | * $Source: /usr/cvsroot/melati/melati/src/site/resources/withWebmacro/org.melati.template.webmacro.WebmacroTemplateEngine.html,v $ | |
3 | * $Revision: 1.1 $ | |
4 | * | |
5 | * Copyright (C) 2005 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 | * http://paneris.org/~timp | |
43 | */ | |
44 | ||
45 | package org.melati.template.webmacro; | |
46 | ||
47 | ||
48 | import org.melati.Melati; | |
49 | import org.melati.MelatiConfig; | |
50 | import org.melati.poem.AccessPoemException; | |
51 | import org.melati.template.AbstractTemplateEngine; | |
52 | import org.melati.template.NotFoundException; | |
53 | import org.melati.template.TemplateContext; | |
54 | import org.melati.template.TemplateEngine; | |
55 | import org.melati.template.TemplateEngineException; | |
56 | import org.melati.util.MelatiStringWriter; | |
57 | import org.melati.util.MelatiWriter; | |
58 | import org.webmacro.InitException; | |
59 | import org.webmacro.PropertyException; | |
60 | import org.webmacro.WM; | |
61 | import org.webmacro.Context; | |
62 | ||
63 | ||
64 | /** | |
65 | * Wrapper for the WebMacro Template Engine for use with Melati. | |
66 | */ | |
67 | public class WebmacroTemplateEngine extends AbstractTemplateEngine implements TemplateEngine { | |
68 | ||
69 | /** The name of the engine. */ | |
70 | public static final String NAME = "webmacro"; | |
71 | ||
72 | /** The WebMacro. */ | |
73 | public WM wm; | |
74 | //private WebContext _webContext; | |
75 | ||
76 | /** | |
77 | * Constructor. | |
78 | */ | |
79 | public WebmacroTemplateEngine() { | |
80 | 256 | super(); |
81 | 256 | } |
82 | ||
83 | /** | |
84 | * Construct a new Engine. | |
85 | * | |
86 | * @param melatiConfig a {@link MelatiConfig} | |
87 | * @throws TemplateEngineException if any problem occurs with the engine | |
88 | */ | |
89 | public void init(MelatiConfig melatiConfig) | |
90 | throws TemplateEngineException { | |
91 | try { | |
92 | 220 | wm = new WM (); |
93 | 0 | } catch (InitException e) { |
94 | 0 | throw new TemplateEngineException(e); |
95 | 220 | } |
96 | 220 | } |
97 | ||
98 | /** | |
99 | * Create a new, empty, Context for WebMacro. | |
100 | * | |
101 | * @param melati the {@link Melati} | |
102 | * @return a {@link TemplateContext} | |
103 | */ | |
104 | public TemplateContext getTemplateContext(Melati melati) { | |
105 | 840 | Context context = new Context(wm.getBroker()); |
106 | 840 | return new WebmacroTemplateContext(context); |
107 | } | |
108 | ||
109 | /** | |
110 | * The name of the template engine (used to find the templets). | |
111 | * @return the name of the current configured template engine | |
112 | */ | |
113 | public String getName () { | |
114 | 1016 | return NAME; |
115 | } | |
116 | ||
117 | /** | |
118 | * @return the extension of the templates used by | |
119 | * WebMacro, including the dot. | |
120 | * | |
121 | */ | |
122 | public String templateExtension() { | |
123 | 2970 | return ".wm"; |
124 | } | |
125 | ||
126 | /** | |
127 | * Get a template given it's name. | |
128 | * | |
129 | * @param templateName the name of the template to find | |
130 | * @return a template | |
131 | * @throws NotFoundException if template not found | |
132 | */ | |
133 | public org.melati.template.Template template(String templateName) | |
134 | throws NotFoundException { | |
135 | try { | |
136 | 1524 | org.webmacro.Template template = wm.getTemplate(templateName); |
137 | 1206 | return new WebmacroTemplate(template); |
138 | 314 | } catch (org.webmacro.NotFoundException e) { |
139 | 314 | throw new NotFoundException("Could not find template " + templateName); |
140 | 4 | } catch (Exception e) { |
141 | 4 | throw new TemplateEngineException(e); |
142 | } | |
143 | } | |
144 | ||
145 | /** | |
146 | * Expand the Template against the context. | |
147 | * | |
148 | * @param out a {@link MelatiWriter} to output on | |
149 | * @param templateName the name of the template to expand | |
150 | * @param templateContext the {@link TemplateContext} to expand | |
151 | * the template against | |
152 | * @throws NotFoundException if template not found | |
153 | */ | |
154 | public void expandTemplate(MelatiWriter out, | |
155 | String templateName, | |
156 | TemplateContext templateContext) | |
157 | throws NotFoundException { | |
158 | 1050 | expandTemplate (out, template(templateName), templateContext); |
159 | 1040 | } |
160 | ||
161 | /** | |
162 | * Expand the Template against the context. | |
163 | * | |
164 | * @param out a {@link MelatiWriter} to output on | |
165 | * @param template the {@link org.melati.template.Template} to expand | |
166 | * @param templateContext the {@link TemplateContext} to expand | |
167 | * the template against | |
168 | */ | |
169 | public void expandTemplate(MelatiWriter out, | |
170 | org.melati.template.Template template, | |
171 | TemplateContext templateContext) { | |
172 | try { | |
173 | 1686 | template.write(out, templateContext, this); |
174 | 22 | } catch (TemplateEngineException problem) { |
175 | // underlying will always be a PropertyException | |
176 | 22 | PropertyException underlying = (PropertyException)problem.subException; |
177 | 22 | Throwable caught = underlying.getCause(); |
178 | 22 | if (caught instanceof AccessPoemException) { |
179 | 8 | throw (AccessPoemException)caught; |
180 | } | |
181 | 14 | throw problem; |
182 | 1664 | } |
183 | 1664 | } |
184 | ||
185 | /** | |
186 | * {@inheritDoc} | |
187 | * @see org.melati.template.AbstractTemplateEngine#expandedTemplate | |
188 | */ | |
189 | public String expandedTemplate(org.melati.template.Template template, | |
190 | TemplateContext templateContext) { | |
191 | 4 | MelatiStringWriter s = new MelatiWebmacroStringWriter(); |
192 | 4 | expandTemplate(s, template, templateContext); |
193 | 4 | return s.toString(); |
194 | } | |
195 | ||
196 | /** | |
197 | * Return a {@link MelatiWebmacroStringWriter}. | |
198 | * | |
199 | * {@inheritDoc} | |
200 | * @see org.melati.Melati#getStringWriter() | |
201 | * @see org.melati.template.AbstractTemplateEngine#getStringWriter() | |
202 | */ | |
203 | public MelatiStringWriter getStringWriter() { | |
204 | 14456 | return new MelatiWebmacroStringWriter(); |
205 | } | |
206 | ||
207 | /** | |
208 | * Get the underlying engine. | |
209 | * | |
210 | * @return the configured template engine | |
211 | */ | |
212 | public Object getEngine() { | |
213 | 0 | return wm; |
214 | } | |
215 | ||
216 | } |