freemarker.ext.misc
Class WriterExceptionListener

java.lang.Object
  extended by freemarker.ext.misc.WriterExceptionListener
All Implemented Interfaces:
TemplateExceptionListener, java.util.EventListener

public final class WriterExceptionListener
extends java.lang.Object
implements TemplateExceptionListener

Adapts FM-Classic's exception events to an IO Writer class. This would typically be either a File writer, or adapted to another output such as System.err. By default the writer is flushed after each message that is written.

Usage example:

 // Create a writer for error messages
 Writer errorWriter = new OutputStreamWriter( System.err );

 // Set up the template
 Template template = new Template(filename);

 // Set up the model
 TemplateModelRoot modelRoot = (...)

 // Set up the listener
 SimpleEventAdapter adapter = new SimpleEventAdapter( new WriterExceptionListener( errorWriter ));

 // Call the template
 template.process( modelRoot, writer, adapter );
 

Since:
1.8.2
Version:
$Id: WriterExceptionListener.java 1101 2013-04-01 04:17:32Z run2000 $
Author:
Nicholas Cull

Constructor Summary
WriterExceptionListener(java.io.Writer writer)
          Constructor that takes a Writer as the target for error messages.
WriterExceptionListener(java.io.Writer writer, boolean autoFlush)
          Constructor that takes a Writer as the target for error messages, and a flag to indicate whether the flush() method should be called after every event.
 
Method Summary
 void exceptionThrown(TemplateExceptionEvent e)
          This method is called whenever a TemplateExceptionEvent is generated by a FM-Classic template.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WriterExceptionListener

public WriterExceptionListener(java.io.Writer writer)
Constructor that takes a Writer as the target for error messages. Autoflush is set to true by default.

Parameters:
writer - the Writer to which error messages will be sent

WriterExceptionListener

public WriterExceptionListener(java.io.Writer writer,
                               boolean autoFlush)
Constructor that takes a Writer as the target for error messages, and a flag to indicate whether the flush() method should be called after every event.

Parameters:
writer - the Writer to which error messages will be sent
autoFlush - should the Writer be flushed after every message
Method Detail

exceptionThrown

public void exceptionThrown(TemplateExceptionEvent e)
                     throws TemplateException
This method is called whenever a TemplateExceptionEvent is generated by a FM-Classic template. Implement this method to decide how to respond to such events.

Specified by:
exceptionThrown in interface TemplateExceptionListener
Throws:
TemplateException