1 /* 2 * $Source$ 3 * $Revision$ 4 * 5 * Copyright (C) 2000 Tim Joyce 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 Joyce <timj At paneris.org> 42 * http://paneris.org/ 43 * 68 Sandbanks Rd, Poole, Dorset. BH14 8BY. UK 44 */ 45 46 package org.melati.template; 47 48 import java.io.IOException; 49 import java.io.PrintWriter; 50 51 import javax.servlet.http.HttpServletResponse; 52 import javax.servlet.http.HttpServlet; 53 54 import org.melati.Melati; 55 import org.melati.MelatiConfig; 56 import org.melati.util.MelatiIOException; 57 import org.melati.util.MelatiWriter; 58 import org.melati.util.MelatiSimpleWriter; 59 import org.melati.util.MelatiStringWriter; 60 import org.melati.util.MelatiBufferedWriter; 61 62 63 /** 64 * The <code>null</code> {@link ServletTemplateEngine}. 65 * 66 * @author Tim Joyce 67 */ 68 public class NoTemplateEngine extends AbstractTemplateEngine implements ServletTemplateEngine { 69 70 private String message = 71 "No Template engine is Configured, please specify an engine in " + 72 "org.melati.MelatiConfig.properties"; 73 74 /** 75 * Constructor. 76 */ 77 public NoTemplateEngine() { 78 super(); 79 } 80 81 /** 82 * Construct a null Engine. 83 * 84 * @param config a {@link MelatiConfig} 85 */ 86 public void init(MelatiConfig config) { 87 // we don't throw an exception here as it gets hidden away, rather 88 // it is better to wait until expandTemplate or getParameters, as 89 // that produces a nice 90 // exception to the browser 91 } 92 93 /** 94 * Construct a new Engine for use in a servlet environment. 95 * 96 * @see org.melati.servlet.TemplateServlet 97 * @param melatiConfig a {@link MelatiConfig} 98 * @param servlet the servlet we are within 99 * @throws TemplateEngineException if any problem occurs with the engine 100 */ 101 public void init(MelatiConfig melatiConfig, HttpServlet servlet) 102 throws TemplateEngineException { 103 init(melatiConfig); 104 } 105 106 /** 107 * Throw exception. 108 * 109 * @throws TemplateEngineException if any problem occurs with the engine 110 * @return a {@link TemplateContext} 111 */ 112 public TemplateContext getTemplateContext() 113 throws TemplateEngineException { 114 throw new TemplateEngineException(message); 115 } 116 117 /** 118 * Throw exception. 119 * 120 * @param melati the {@link Melati} 121 * @throws TemplateEngineException always 122 * @return an exception 123 */ 124 public ServletTemplateContext getServletTemplateContext(Melati melati) 125 throws TemplateEngineException { 126 throw new TemplateEngineException(message); 127 } 128 129 /** 130 * The name of the template engine (used to find the templets). 131 * @return the name of the current configured template engine 132 */ 133 public String getName() { 134 return "none"; 135 } 136 137 /** 138 * @return the extension of the templates used by this template engine 139 */ 140 public String templateExtension() { 141 return ".none"; 142 } 143 144 /** 145 * Get a template given its name. 146 * 147 * @param templateName the name of the template to find 148 * @return a template 149 */ 150 public Template template(String templateName) { 151 throw new TemplateEngineException( 152 "The template " + 153 templateName + " could not be found because you have not configured a template engine."); 154 } 155 156 /** 157 * Expand the Template against the context. 158 * 159 * @param out a {@link MelatiWriter} to output on 160 * @param templateName the name of the template to expand 161 * @param templateContext the {@link ServletTemplateContext} to expand 162 * the template against 163 * @throws TemplateEngineException if invoked 164 */ 165 public void expandTemplate(MelatiWriter out, String templateName, 166 TemplateContext templateContext) 167 throws TemplateEngineException { 168 throw new TemplateEngineException(message); 169 } 170 171 172 /** 173 * Expand the Template against the context. 174 * 175 * @param out a {@link MelatiWriter} to output on 176 * @param melatiTemplate the {@link Template} to expand 177 * @param templateContext the {@link ServletTemplateContext} to expand 178 * the template against 179 * @throws TemplateEngineException if invoked 180 */ 181 public void expandTemplate(MelatiWriter out, Template melatiTemplate, 182 TemplateContext templateContext) 183 throws TemplateEngineException { 184 throw new TemplateEngineException(message); 185 } 186 187 /** 188 * Expand the Template against the context. 189 * 190 * @param melatiTemplate the {@link Template} to expand 191 * @param templateContext the {@link ServletTemplateContext} to expand 192 * the template against 193 * @throws TemplateEngineException if invoked 194 * {@inheritDoc} 195 * @see org.melati.template.TemplateEngine#expandedTemplate 196 * (org.melati.template.Template, org.melati.template.TemplateContext) 197 */ 198 public String expandedTemplate(Template melatiTemplate, 199 TemplateContext templateContext) 200 throws TemplateEngineException { 201 throw new TemplateEngineException(message); 202 } 203 204 /** 205 * @param response the <code>HttpServletResponse</code> that this 206 * writer will be part of 207 * @param buffered whether the writer should be buffered 208 * @return a {@link MelatiWriter} 209 * appropriate for this engine. 210 */ 211 public MelatiWriter getServletWriter(HttpServletResponse response, 212 boolean buffered) { 213 PrintWriter printWriter = null; 214 try { 215 printWriter = response.getWriter(); 216 } catch (IOException e) { 217 throw new MelatiIOException(e); 218 } 219 if (buffered) 220 return new MelatiBufferedWriter(printWriter); 221 else 222 return new MelatiSimpleWriter(printWriter); 223 } 224 225 /** 226 * Return a {@link MelatiStringWriter}. 227 * {@inheritDoc} 228 * @see org.melati.template.TemplateEngine#getStringWriter() 229 * @see Melati#getStringWriter() 230 */ 231 public MelatiStringWriter getStringWriter() { 232 return new MelatiStringWriter(); 233 } 234 235 /** 236 * Get the underlying engine. 237 * 238 * @return 'none' 239 */ 240 public Object getEngine() { 241 return "none"; 242 } 243 244 }