freemarker.template
Class TemplateEventAdapter

java.lang.Object
  extended by freemarker.template.TemplateEventAdapter
All Implemented Interfaces:
TemplateRuntimeHandler

public class TemplateEventAdapter
extends java.lang.Object
implements TemplateRuntimeHandler

Adapter class for firing events that could happen at Template runtime. Different listeners can be registered for different events that can occur. So far only one listener class is defined: TemplateExceptionEvent.

Usage example:

 // Configure Log4j
 ...

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

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

 // Set up the listener
 TemplateEventAdapter adapter = new TemplateEventAdapter();
 adapter.addTemplateExceptionListener( new Log4jExceptionListener() );

 // Add more listeners as required
 adapter.addTemplateExceptionListener( ... );

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

Version:
$Id: TemplateEventAdapter.java 1153 2013-04-15 10:59:37Z run2000 $
See Also:
SimpleEventAdapter

Nested Class Summary
 
Nested classes/interfaces inherited from interface freemarker.template.TemplateRuntimeHandler
TemplateRuntimeHandler.Severity
 
Field Summary
static TemplateRuntimeHandler DefaultEventAdapter
          The default TemplateRuntimeHandler instance when no other one is specified.
protected  ListenerAdapter<TemplateExceptionEvent,TemplateExceptionListener> exceptionThrownAdapter
          A ListenerAdapter for exceptionThrown events.
static TemplateRuntimeHandler NullEventAdapter
          The trivial TemplateRuntimeHandler instance where no events are fired.
protected  GenericEventMulticaster<TemplateExceptionListener> templateExceptionListeners
          The multicaster that notifies all event listeners when an exception occurs.
 
Constructor Summary
TemplateEventAdapter()
          Creates new TemplateEventAdapter.
 
Method Summary
 void addTemplateExceptionListener(TemplateExceptionListener el)
          Adds a listener for TemplateExceptionEvent events.
 void fireExceptionThrown(java.lang.Object source, java.lang.Exception exception, java.io.Writer output, java.lang.String sourceName, TemplateRuntimeHandler.Severity severity)
          Fires a TemplateExceptionEvent to all the current listeners.
 TemplateExceptionListener[] getTemplateExceptionListeners()
          Retrieves all the current TemplateExceptionListeners that are listening for events.
 void removeTemplateExceptionListener(TemplateExceptionListener el)
          Removes the specified listener.
 java.lang.String toString()
          Returns a string representation of the object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DefaultEventAdapter

public static final TemplateRuntimeHandler DefaultEventAdapter
The default TemplateRuntimeHandler instance when no other one is specified.


NullEventAdapter

public static final TemplateRuntimeHandler NullEventAdapter
The trivial TemplateRuntimeHandler instance where no events are fired.


templateExceptionListeners

protected GenericEventMulticaster<TemplateExceptionListener> templateExceptionListeners
The multicaster that notifies all event listeners when an exception occurs.


exceptionThrownAdapter

protected ListenerAdapter<TemplateExceptionEvent,TemplateExceptionListener> exceptionThrownAdapter
A ListenerAdapter for exceptionThrown events.

Constructor Detail

TemplateEventAdapter

public TemplateEventAdapter()
Creates new TemplateEventAdapter.

Method Detail

fireExceptionThrown

public void fireExceptionThrown(java.lang.Object source,
                                java.lang.Exception exception,
                                java.io.Writer output,
                                java.lang.String sourceName,
                                TemplateRuntimeHandler.Severity severity)
Fires a TemplateExceptionEvent to all the current listeners.

Specified by:
fireExceptionThrown in interface TemplateRuntimeHandler
Parameters:
source - the source object of the event
exception - the exception that caused the event to be fired
output - the current Template output stream
sourceName - the name of the source class and method that fired the event
severity - the severity of the exception

addTemplateExceptionListener

public void addTemplateExceptionListener(TemplateExceptionListener el)
Adds a listener for TemplateExceptionEvent events.

Parameters:
el - the event listener to be added

removeTemplateExceptionListener

public void removeTemplateExceptionListener(TemplateExceptionListener el)
Removes the specified listener.

Parameters:
el - the event listener to be removed

getTemplateExceptionListeners

public TemplateExceptionListener[] getTemplateExceptionListeners()
Retrieves all the current TemplateExceptionListeners that are listening for events.

Returns:
an array of TemplateExceptionListeners

toString

public java.lang.String toString()
Returns a string representation of the object.

Overrides:
toString in class java.lang.Object
Returns:
a String representation of the object