buoy.xml

Class WidgetEncoder


public class WidgetEncoder
extends java.lang.Object

This class is used for serializing Widgets as XML. This allows user interfaces to be saved in a persistent form, then reconstructed using the WidgetDecoder class.

To use this class, simply call WidgetEncoder.writeObject(), passing it the root Widget to save and an OutputStream to write the XML to. It will save that Widget, along with all other objects referenced by it: child Widgets, event listeners, models, etc.

The serialization is done through the java.beans.XMLEncoder class. It therefore can encode any object which is a bean. An object which is not a bean can only be saved if a PersistenceDelegate has been defined for it. The buoy.xml.delegate package includes PersistenceDelegates for all of the Widgets in the buoy.widget package. If you wish to define delegates for other classes, you can do so by calling WidgetEncoder.setPersistenceDelegate(). If the class in question is an EventSource, the delegate should generally subclass EventSourceDelegate and invoke initializeEventLinks() from its initialize() method so that its event listeners will be properly saved.

Author:
Peter Eastman

Method Summary

static void
setPersistenceDelegate(Class cls, PersistenceDelegate delegate)
Register a persistence delegate for a class.
static void
writeObject(Object obj, OutputStream out)
Serialize an object hierarchy as XML.
static void
writeObject(Object obj, OutputStream out, ExceptionListener listener)
Serialize an object hierarchy as XML.

Method Details

setPersistenceDelegate

public static void setPersistenceDelegate(Class cls,
                                          PersistenceDelegate delegate)
Register a persistence delegate for a class.
Parameters:
cls - the class for which to define a PersistenceDelegate
delegate - the delegate to use for saving that class

writeObject

public static void writeObject(Object obj,
                               OutputStream out)
Serialize an object hierarchy as XML. The OutputStream will be closed once the XML is written.
Parameters:
obj - the object which forms the root of the hierarchy to save
out - the OutputStream to write the XML to

writeObject

public static void writeObject(Object obj,
                               OutputStream out,
                               ExceptionListener listener)
Serialize an object hierarchy as XML. If recoverable errors occur during serialization, the listener will be notified of them. The OutputStream will be closed once the XML is written.
Parameters:
obj - the object which forms the root of the hierarchy to save
out - the OutputStream to write the XML to
listener - the listener to notify of recoverable errors

Written by Peter Eastman.