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