001: /*
002: * Copyright (c) 2003 The Visigoth Software Society. All rights
003: * reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * 2. Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in
014: * the documentation and/or other materials provided with the
015: * distribution.
016: *
017: * 3. The end-user documentation included with the redistribution, if
018: * any, must include the following acknowledgement:
019: * "This product includes software developed by the
020: * Visigoth Software Society (http://www.visigoths.org/)."
021: * Alternately, this acknowledgement may appear in the software itself,
022: * if and wherever such third-party acknowledgements normally appear.
023: *
024: * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
025: * project contributors may be used to endorse or promote products derived
026: * from this software without prior written permission. For written
027: * permission, please contact visigoths@visigoths.org.
028: *
029: * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
030: * nor may "FreeMarker" or "Visigoth" appear in their names
031: * without prior written permission of the Visigoth Software Society.
032: *
033: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
034: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
035: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
036: * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
037: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
038: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
039: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
040: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
041: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
042: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
043: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
044: * SUCH DAMAGE.
045: * ====================================================================
046: *
047: * This software consists of voluntary contributions made by many
048: * individuals on behalf of the Visigoth Software Society. For more
049: * information on the Visigoth Software Society, please see
050: * http://www.visigoths.org/
051: */
052:
053: package freemarker.template;
054:
055: import java.io.Writer;
056: import java.io.PrintWriter;
057: import freemarker.core.Environment;
058:
059: /**
060: * An API for objects that handle exceptions that are thrown during
061: * template rendering.
062: * @author <a href="mailto:jon@revusky.com">Jonathan Revusky</a>
063: */
064:
065: public interface TemplateExceptionHandler {
066:
067: /**
068: * handle the exception.
069: * @param te the exception that occurred.
070: * @param env The environment object that represents the rendering context
071: * @param out the character output stream to output to.
072: */
073: void handleTemplateException(TemplateException te, Environment env,
074: Writer out) throws TemplateException;
075:
076: /**
077: * This is a TemplateExceptionHandler which simply skips errors. It does nothing
078: * to handle the event. Note that the exception is still logged in any case, before
079: * being passed to the handler.
080: */
081: TemplateExceptionHandler IGNORE_HANDLER = new TemplateExceptionHandler() {
082: public void handleTemplateException(TemplateException te,
083: Environment env, Writer out) {
084: }
085: };
086:
087: /**
088: * This is a TemplateExceptionHandler that simply rethrows the exception.
089: * Note that the exception is logged before being rethrown.
090: */
091: TemplateExceptionHandler RETHROW_HANDLER = new TemplateExceptionHandler() {
092: public void handleTemplateException(TemplateException te,
093: Environment env, Writer out) throws TemplateException {
094: throw te;
095: }
096: };
097:
098: /**
099: * This is a TemplateExceptionHandler used when you develop the templates. This handler
100: * outputs the stack trace information to the client and then rethrows the exception.
101: */
102: TemplateExceptionHandler DEBUG_HANDLER = new TemplateExceptionHandler() {
103: public void handleTemplateException(TemplateException te,
104: Environment env, Writer out) throws TemplateException {
105: PrintWriter pw = (out instanceof PrintWriter) ? (PrintWriter) out
106: : new PrintWriter(out);
107: te.printStackTrace(pw);
108: pw.flush();
109: throw te;
110: }
111: };
112:
113: /**
114: * This is a TemplateExceptionHandler used when you develop HTML templates. This handler
115: * outputs the stack trace information to the client and then rethrows the exception, and
116: * surrounds it with tags to make the error message readable with the browser.
117: */
118: TemplateExceptionHandler HTML_DEBUG_HANDLER = new TemplateExceptionHandler() {
119: public void handleTemplateException(TemplateException te,
120: Environment env, Writer out) throws TemplateException {
121: PrintWriter pw = (out instanceof PrintWriter) ? (PrintWriter) out
122: : new PrintWriter(out);
123: pw
124: .println("<!-- FREEMARKER ERROR MESSAGE STARTS HERE -->"
125: + "<script language=javascript>//\"></script>"
126: + "<script language=javascript>//\'></script>"
127: + "<script language=javascript>//\"></script>"
128: + "<script language=javascript>//\'></script>"
129: + "</title></xmp></script></noscript></style></object>"
130: + "</head></pre></table>"
131: + "</form></table></table></table></a></u></i></b>"
132: + "<div align=left "
133: + "style='background-color:#FFFF00; color:#FF0000; "
134: + "display:block; border-top:double; padding:2pt; "
135: + "font-size:medium; font-family:Arial,sans-serif; "
136: + "font-style: normal; font-variant: normal; "
137: + "font-weight: normal; text-decoration: none; "
138: + "text-transform: none'>"
139: + "<b style='font-size:medium'>FreeMarker template error!</b>"
140: + "<pre><xmp>");
141: te.printStackTrace(pw);
142: pw.println("</xmp></pre></div></html>");
143: pw.flush();
144: throw te;
145: }
146: };
147: }
|