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