com.waveset.util
Class XmlObjectFactory

java.lang.Object
  extended bycom.waveset.util.XmlObjectFactory
All Implemented Interfaces:
javax.naming.spi.DirObjectFactory, javax.naming.spi.ObjectFactory

public class XmlObjectFactory
extends java.lang.Object
implements javax.naming.spi.ObjectFactory, javax.naming.spi.DirObjectFactory

A class that provides factory methods to create Object instances from XML and serialize objects to XML. This includes the maintenance of a global registry element and class handlers that can be dynamically extended.

The class is declared abstract as it is not intended to be instantiated, all methods are static.


Field Summary
static java.lang.String CLASS_ATTRIBUTE
          The name of the attribute we recognize as the instantiation class override if it isn't the element name.
static java.lang.String code_id
           
static java.lang.String DEFAULT_PACKAGE
          The default package name we'll use to locate implementation classes for element tag names.
static java.lang.String OLD_CLASS_ATTRIBUTE
          Attribute.CLASS ("class") was the original CLASS_ATTRIBUTE.
static java.lang.String REF_ADDR_TYPE
           
static java.lang.String WRAPPER_ELEMENT
          The standard wrapper element we put around objects so we don't have to know the element name to build the surrounding XML.
 
Constructor Summary
XmlObjectFactory()
           
 
Method Summary
static java.lang.Object clone(java.lang.Object src)
          Clone an XmlObject.
static java.lang.Object create(org.w3c.dom.Element element)
          Build a Object from a DOM Element.
static java.lang.Object create(java.lang.String xml)
          Build an Object from an XML string.
static java.lang.Object create(java.lang.String xml, boolean validate)
          Build an Object from an XML string.
static java.util.List createList(org.w3c.dom.Element element)
          Build a list of objects.
static java.lang.Object createWrappered(org.w3c.dom.Element element)
          Build a Object from a DOM Element.
static void dump(java.lang.Object obj)
           
static void dumpFile(java.lang.Object obj, java.lang.String file)
          Serialize any XmlObject to a file.
 java.lang.Object getObjectInstance(java.lang.Object obj, javax.naming.Name name, javax.naming.Context nameCtx, java.util.Hashtable environment)
           
 java.lang.Object getObjectInstance(java.lang.Object obj, javax.naming.Name name, javax.naming.Context nameCtx, java.util.Hashtable environment, javax.naming.directory.Attributes attrs)
           
static javax.naming.Reference getReference(java.lang.Object o)
           
static boolean isXmlObject(java.lang.String name)
          Test to see if a name would be a valid XmlObject element name.
protected static void println(java.lang.Object o)
          Ubiquitous macro.
static void register(java.lang.String elementName, java.lang.String className)
          Register an element-to-class mapping.
static void register(XmlObjectHandler handler)
          Register a handler object.
static java.lang.String toXml(java.lang.Object obj)
          Serialize an arbitrary object into an XML string.
static void toXml(java.lang.StringBuffer b, int indent, java.lang.Object obj)
          Serialize an arbitrary object into an XML buffer.
static void toXml(java.lang.StringBuffer b, int indent, java.lang.String wrapper, java.lang.Object obj)
          Serialize an arbitrary object into an XML buffer, wrapped by another element.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

code_id

public static final java.lang.String code_id
See Also:
Constant Field Values

DEFAULT_PACKAGE

public static final java.lang.String DEFAULT_PACKAGE
The default package name we'll use to locate implementation classes for element tag names.

This is a bit kludgey, since its a package we're not in, but I want to keep the class down in util so we can use it here. To make this more general we could get the deafult package from a properties file or the environment.

See Also:
Constant Field Values

WRAPPER_ELEMENT

public static final java.lang.String WRAPPER_ELEMENT
The standard wrapper element we put around objects so we don't have to know the element name to build the surrounding XML.

See Also:
Constant Field Values

CLASS_ATTRIBUTE

public static final java.lang.String CLASS_ATTRIBUTE
The name of the attribute we recognize as the instantiation class override if it isn't the element name. Sigh, "class" would be nice, but that's already being used in a couple of places.

See Also:
Constant Field Values

OLD_CLASS_ATTRIBUTE

public static final java.lang.String OLD_CLASS_ATTRIBUTE
Attribute.CLASS ("class") was the original CLASS_ATTRIBUTE.

See Also:
Constant Field Values

REF_ADDR_TYPE

public static final java.lang.String REF_ADDR_TYPE
Constructor Detail

XmlObjectFactory

public XmlObjectFactory()
Method Detail

create

public static java.lang.Object create(java.lang.String xml)
                               throws WavesetException
Build an Object from an XML string. DTD Validation is not performed.

Throws:
WavesetException

create

public static java.lang.Object create(java.lang.String xml,
                                      boolean validate)
                               throws WavesetException
Build an Object from an XML string.

Validation against the DTD can be controlled with the validate argument. Normally this is off for performance, but in some contexts you may want it on.

Throws:
WavesetException

create

public static java.lang.Object create(org.w3c.dom.Element element)
                               throws WavesetException
Build a Object from a DOM Element. We don't handle the wrapper element at this level, should we?

Throws:
WavesetException

createWrappered

public static java.lang.Object createWrappered(org.w3c.dom.Element element)
                                        throws WavesetException
Build a Object from a DOM Element. The element supplied is assumed to be a wrapper element, the object returned is deifined by the first child of the wrapper.

Throws:
WavesetException

createList

public static java.util.List createList(org.w3c.dom.Element element)
                                 throws WavesetException
Build a list of objects. The element is a wrappern element, presumably not since those are registered objects.

Throws:
WavesetException

clone

public static java.lang.Object clone(java.lang.Object src)
                              throws WavesetException
Clone an XmlObject.

Throws:
WavesetException

isXmlObject

public static boolean isXmlObject(java.lang.String name)
Test to see if a name would be a valid XmlObject element name. Note that this won't handle the case where the class is specified as an attribute.


toXml

public static void toXml(java.lang.StringBuffer b,
                         int indent,
                         java.lang.Object obj)
Serialize an arbitrary object into an XML buffer.

The object is usually an XmlObject, but we also handle arbitrary objects, provided there is an XmlObjectHandler registered for this class.


toXml

public static void toXml(java.lang.StringBuffer b,
                         int indent,
                         java.lang.String wrapper,
                         java.lang.Object obj)
Serialize an arbitrary object into an XML buffer, wrapped by another element.


toXml

public static java.lang.String toXml(java.lang.Object obj)
Serialize an arbitrary object into an XML string.


dumpFile

public static void dumpFile(java.lang.Object obj,
                            java.lang.String file)
Serialize any XmlObject to a file. AbstractXmlObject already defines this, but you can use this for Maps and other native types.


dump

public static void dump(java.lang.Object obj)

register

public static void register(java.lang.String elementName,
                            java.lang.String className)
Register an element-to-class mapping. Use this if you have an XmlObject that doesn't want to use the default rules for mapping element names to element names.

It would be nice if we could pass the Class here, but I'm not sure "this" is accessible from static initializers, experiment...

Have to avoid throwing here, since we're going to be called from static initializers which I don't think can throw?

Registrations of this form go only in the _constructors map, we don't need a _serializers entry since the object is normally an XmlObject, and we can get the serializer through that interface.


register

public static void register(XmlObjectHandler handler)
Register a handler object.

Use this if you want to support construction and serialization of things that aren't XmlObjects, or need more control.


getObjectInstance

public java.lang.Object getObjectInstance(java.lang.Object obj,
                                          javax.naming.Name name,
                                          javax.naming.Context nameCtx,
                                          java.util.Hashtable environment)
                                   throws java.lang.Exception
Specified by:
getObjectInstance in interface javax.naming.spi.ObjectFactory
Returns:
a Java Object from a reference.

Required in order to implement JNDI ObjectFactory.

Throws:
java.lang.Exception

getObjectInstance

public java.lang.Object getObjectInstance(java.lang.Object obj,
                                          javax.naming.Name name,
                                          javax.naming.Context nameCtx,
                                          java.util.Hashtable environment,
                                          javax.naming.directory.Attributes attrs)
                                   throws java.lang.Exception
Specified by:
getObjectInstance in interface javax.naming.spi.DirObjectFactory
Returns:
a Java Object from a reference (and possibly attributes).

Required in order to implement JNDI DirObjectFactory.

Throws:
java.lang.Exception

getReference

public static javax.naming.Reference getReference(java.lang.Object o)
                                           throws javax.naming.NamingException
Returns:
a Reference to an object if XmlObjectFactory can serialize and deserialize it; otherwise null.

A Reference can be bound into any JNDI Service Provider.

Throws:
javax.naming.NamingException

println

protected static void println(java.lang.Object o)
Ubiquitous macro.