J avolution v5.4 (J2SE 1.6+)

javolution.xml
Class XMLBinding

java.lang.Object
  extended by javolution.xml.XMLBinding
All Implemented Interfaces:
java.io.Serializable, Reusable, XMLSerializable

public class XMLBinding
extends java.lang.Object
implements Reusable, XMLSerializable

This class represents the binding between Java classes and their XML representation (XMLFormat).

Custom XML bindings can also be used to alias class names and ensure that the XML representation is:

More advanced bindings can also be created through sub-classing.

 
     // XML binding using reflection.
     public ReflectionBinding extends XMLBinding {
         protected XMLFormat getFormat(Class forClass) {
             Field[] fields = forClass.getDeclaredFields();
             return new XMLReflectionFormat(fields);
         }
     }
     
     // XML binding read from DTD input source.
     public DTDBinding extends XMLBinding {
         public DTDBinding(InputStream dtd) {
             ...
         }
     }
     
     // XML binding overriding default formats.
     public MyBinding extends XMLBinding {
         // Non-static formats use unmapped XMLFormat instances.
         XMLFormat<String> myStringFormat = new XMLFormat<String>(null) {...}
         XMLFormat<Collection> myCollectionFormat = new XMLFormat<Collection>(null) {...}
         protected XMLFormat getFormat(Class forClass) throws XMLStreamException {
             if (String.class.equals(forClass))
                  return myStringFormat;
             if (Collection.class.isAssignableFrom(forClass))
                  return myCollectionFormat;
             return super.getFormat(cls);
         }
     }
     

The default XML binding implementation supports all static XML formats (static members of the classes being mapped) as well as the following types:

Version:
5.4, December 1, 2009
Author:
Jean-Marie Dautelle
See Also:
Serialized Form

Constructor Summary
XMLBinding()
          Default constructor.
 
Method Summary
protected  XMLFormat getFormat(java.lang.Class forClass)
          Returns the XML format for the specified class/interface.
protected  java.lang.Class readClass(XMLStreamReader reader, boolean useAttributes)
          Reads the class corresponding to the current XML element.
 void reset()
          Resets the internal state of this object to its default values.
 void setAlias(java.lang.Class cls, QName qName)
          Sets the qualified alias for the specified class.
 void setAlias(java.lang.Class cls, java.lang.String alias)
          Convenient method equivalent to setAlias(cls, QName.valueOf(alias)).
 void setClassAttribute(QName classAttribute)
          Sets the qualified name of the attribute holding the class identifier.
 void setClassAttribute(java.lang.String name)
          Convenience method equivalent to setClassAttribute(QName.valueOf(name)).
protected  void writeClass(java.lang.Class cls, XMLStreamWriter writer, boolean useAttributes)
          Writes the specified class to the current XML element attributes or to a new element if the element attributes cannot be used.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLBinding

public XMLBinding()
Default constructor.

Method Detail

setAlias

public void setAlias(java.lang.Class cls,
                     QName qName)
Sets the qualified alias for the specified class.

Parameters:
cls - the class being aliased.
qName - the qualified name.

setAlias

public final void setAlias(java.lang.Class cls,
                           java.lang.String alias)
Convenient method equivalent to setAlias(cls, QName.valueOf(alias)).

Parameters:
cls - the class being aliased.
alias - the alias for the specified class.

setClassAttribute

public void setClassAttribute(QName classAttribute)
Sets the qualified name of the attribute holding the class identifier. If the local name is null the class attribute is never read/written (which may prevent unmarshalling).

Parameters:
classAttribute - the qualified name of the class attribute or null.

setClassAttribute

public final void setClassAttribute(java.lang.String name)
Convenience method equivalent to setClassAttribute(QName.valueOf(name)).

Parameters:
name - the name of the class attribute or null.

getFormat

protected XMLFormat getFormat(java.lang.Class forClass)
                       throws XMLStreamException
Returns the XML format for the specified class/interface. The default implementation returns the default format for the specified class.

Parameters:
forClass - the class for which the XML format is returned.
Returns:
the XML format for the specified class.
Throws:
XMLStreamException - if there is no format for the specified class.

readClass

protected java.lang.Class readClass(XMLStreamReader reader,
                                    boolean useAttributes)
                             throws XMLStreamException
Reads the class corresponding to the current XML element. This method is called by XMLFormat.InputElement.getNext() XMLFormat.InputElement.get(String) and XMLFormat.InputElement.get(String, String) to retrieve the Java class corresponding to the current XML element. If useAttributes is set, the default implementation reads the class name from the class attribute; otherwise the class name (or alias) is read from the current element qualified name.

Parameters:
reader - the XML stream reader.
useAttributes - indicates if the element's attributes should be used to identify the class (e.g. when the element name is specified by the user then attributes have to be used).
Returns:
the corresponding class.
Throws:
XMLStreamException

writeClass

protected void writeClass(java.lang.Class cls,
                          XMLStreamWriter writer,
                          boolean useAttributes)
                   throws XMLStreamException
Writes the specified class to the current XML element attributes or to a new element if the element attributes cannot be used. This method is called by XMLFormat.OutputElement.add(Object) and XMLFormat.OutputElement.add(Object, String) and XMLFormat.OutputElement.add(Object, String, String) to identify the Java class corresponding to the XML element.

Parameters:
cls - the class to be written.
writer - the XML stream writer.
useAttributes - indicates if the element's attributes should be used to identify the class (e.g. when the element name is specified by the user then attributes have to be used).
Throws:
XMLStreamException

reset

public void reset()
Description copied from interface: Reusable
Resets the internal state of this object to its default values.

Specified by:
reset in interface Reusable

J avolution v5.4 (J2SE 1.6+)

Copyright © 2005 - 2009 Javolution.