| |
|
| java.lang.Object javolution.xml.XMLBinding
XMLBinding | public class XMLBinding implements Reusable,XMLSerializable(Code) | | This class represents the binding between Java classes and
their XML representation (
XMLFormat ); the binding may be shared
among multiple
XMLObjectReader /
XMLObjectWriter
instances (thread-safe).
Custom XML bindings can also be used to alias class names and
ensure that the XML representation is:
- Impervious to obfuscation.
- Unnaffected by any class refactoring.
- Can be mapped to multiple implementations. For example:[code]
// Creates a binding to serialize Swing components into high-level XML
// and deserialize the same XML into SWT components.
XMLBinding swingBinding = new XMLBinding();
swingBinding.setAlias(javax.swing.JButton.class, "Button");
swingBinding.setAlias(javax.swing.JTable.class, "Table");
...
XMLBinding swtBinding = new XMLBinding();
swtBinding.setAlias(org.eclipse.swt.widgets.Button.class, "Button");
swtBinding.setAlias(org.eclipse.swt.widgets.Table.class, "Table");
...
// Writes Swing Desktop to XML.
XMLObjectWriter writer = new XMLObjectWriter().setBinding(swingBinding);
writer.setOutput(new FileOutputStream("C:/desktop.xml"));
writer.write(swingDesktop, "Desktop", SwingDesktop.class);
writer.close();
// Reads back high-level XML to a SWT implementation!
XMLObjectReader reader = new XMLObjectReader().setXMLBinding(swtBinding);
reader.setInput(new FileInputStream("C:/desktop.xml"));
SWTDesktop swtDesktop = reader.read("Desktop", SWTDesktop.class);
reader.close();
[/code]
More advanced bindings can also be created through sub-classing.[code]
// XML binding using reflection.
public ReflectionBinding extends XMLBinding {
public XMLFormat getFormat(Class cls) {
Field[] fields = clt.getDeclaredFields();
return new XMLReflectionFormat(fields);
}
}
// XML binding read from DTD input source.
public DTDBinding extends XMLBinding {
public DTDBinding(InputStream dtd) {
...
}
}
// XML binding overriding statically bounded formats.
public MyBinding extends XMLBinding {
// Non-static formats use unmapped XMLFormat instances.
XMLFormat _myStringFormat = new XMLFormat(null) {...}
XMLFormat _myCollectionFormat = new XMLFormat(null) {...}
public XMLFormat getFormat(Class cls) {
if (String.class.equals(cls))
return _myStringFormat;
if (Collection.class.isAssignableFrom(cls))
return _myCollectionFormat;
return super.getFormat(cls);
}
}
[/code]
The default XML binding supports all static XML formats
(static members of the classes being mapped) as well as the
following types:
java.lang.Object (empty element)
java.lang.Class
java.lang.String
java.lang.Appendable
java.util.Collection
java.util.Map
java.lang.Object[]
- all primitive types wrappers (e.g.
Boolean, Integer ... )
author: Jean-Marie Dautelle version: 4.0, April 9, 2007 |
Field Summary | |
final static XMLFormat | APPENDABLE_XML Holds the default XML representation for java.lang.Appendable
instances. | final static XMLFormat | BOOLEAN_XML Holds the default XML representation for java.lang.Boolean . | final static XMLFormat | BYTE_XML Holds the default XML representation for java.lang.Byte . | final static XMLFormat | CHARACTER_XML Holds the default XML representation for java.lang.Character . | final static XMLFormat | CLASS_XML Holds the default XML representation for java.lang.Class
instances. | final static XMLFormat | COLLECTION_XML Holds the default XML representation for java.util.Collection
instances. | final static XMLBinding | DEFAULT Holds the default instance used by readers/writers. | final static XMLFormat | FASTCOLLECTION_XML Holds the default XML representation for FastCollection instances. | final static XMLFormat | FASTCOMPARATOR_XML Holds the default XML representation for FastComparator instances
(format ensures unicity of predefined comparator). | final static XMLFormat | FASTMAP_XML Holds the default XML representation for FastMap instances. | final static XMLFormat | INDEX_XML Holds the default XML representation for indexes. | final static XMLFormat | INTEGER_XML Holds the default XML representation for java.lang.Integer . | final static XMLFormat | LONG_XML Holds the default XML representation for java.lang.Long . | final static XMLFormat | MAP_XML Holds the default XML representation for java.util.Map
instances. | final static XMLFormat | OBJECT_XML Holds the static XML format for Object.class instances
(default format when a more specialized format does not exist). | final static XMLFormat | PERSISTENT_CONTEXT_XML Holds the XML representation for persistent contexts
(holds persistent reference mapping). | final static XMLFormat | SHORT_XML Holds the default XML representation for java.lang.Short . | final static XMLFormat | STRING_XML Holds the default XML representation for java.lang.String
instances. | final static XMLFormat | TEXT_XML Holds the default XML representation for Text instances. | final static XMLFormat | XML Holds the XML representation of this binding (class/alias mapping
and class attribute values). |
Constructor Summary | |
public | XMLBinding() Default constructor. |
Method Summary | |
protected Class | getClass(CharArray name) Returns the class identified by the specified name (value of
XMLBinding.setClassAttribute class attribute during unmarshalling).
The default implementation returns an aliased class or
Class.forName(name.toString()) .
Parameters: name - the class name identifier. | protected Class | getClass(CharArray localName, CharArray uri) Returns the class identified by the specified local name and URI
(the element local name and URI during unmarshalling). | public XMLFormat | getFormat(Class cls) Returns the XML format for the specified class/interface.
The default implementation returns the most
specialized static format compatible with the specified class.
Parameters: cls - the class for which the XML format is returned. | protected String | getLocalName(Class cls) Returns the local name identifying the specified class (the element local
name during marshalling). | protected String | getName(Class cls) Returns the name identifying the specified class (value of
XMLBinding.setClassAttribute class attribute during marshalling). | protected String | getURI(Class cls) Returns the URI identifying the specified class (the element namespace
URI during marshalling). | final Class | readClassAttribute(XMLStreamReaderImpl reader) Reads the class from the class attribute of current XML element.
Parameters: reader - the reader to be used. | public void | reset() | public void | setAlias(Class cls, String alias) Sets the alias of the specified class. | public void | setClassAttribute(String name) Sets the name of the attribute holding the classname/alias
(by default"class" ). | public void | setClassAttribute(String localName, String uri) Sets the local name and namespace URI of the attribute holding the
classname/alias (by default"class" and no namespace URI). | final void | writeClassAttribute(XMLStreamWriterImpl writer, Class cls) Writes the class to the class attribute of the current XML element. |
APPENDABLE_XML | final static XMLFormat APPENDABLE_XML(Code) | | Holds the default XML representation for java.lang.Appendable
instances. This representation consists of a "value" attribute
holding the characters.
|
BOOLEAN_XML | final static XMLFormat BOOLEAN_XML(Code) | | Holds the default XML representation for java.lang.Boolean .
|
BYTE_XML | final static XMLFormat BYTE_XML(Code) | | Holds the default XML representation for java.lang.Byte .
|
CHARACTER_XML | final static XMLFormat CHARACTER_XML(Code) | | Holds the default XML representation for java.lang.Character .
|
CLASS_XML | final static XMLFormat CLASS_XML(Code) | | Holds the default XML representation for java.lang.Class
instances. This representation consists of a "name"
attribute holding the class name.
|
COLLECTION_XML | final static XMLFormat COLLECTION_XML(Code) | | Holds the default XML representation for java.util.Collection
instances. This representation consists of nested XML elements one for
each element of the collection. The elements' order is defined by
the collection iterator order. Collections are deserialized using their
default constructor.
|
DEFAULT | final static XMLBinding DEFAULT(Code) | | Holds the default instance used by readers/writers.
|
FASTCOMPARATOR_XML | final static XMLFormat FASTCOMPARATOR_XML(Code) | | Holds the default XML representation for FastComparator instances
(format ensures unicity of predefined comparator).
|
INDEX_XML | final static XMLFormat INDEX_XML(Code) | | Holds the default XML representation for indexes.
This presentation consists of a "value" attribute
holding the index int value.
|
INTEGER_XML | final static XMLFormat INTEGER_XML(Code) | | Holds the default XML representation for java.lang.Integer .
|
LONG_XML | final static XMLFormat LONG_XML(Code) | | Holds the default XML representation for java.lang.Long .
|
MAP_XML | final static XMLFormat MAP_XML(Code) | | Holds the default XML representation for java.util.Map
instances. This representation consists of key/value pair as nested
XML elements. For example:[code]
[/code]
The elements' order is defined by the map's entries iterator order.
Maps are deserialized using their default constructor.
|
OBJECT_XML | final static XMLFormat OBJECT_XML(Code) | | Holds the static XML format for Object.class instances
(default format when a more specialized format does not exist).
The XML representation consists of an empty element with no attribute.
|
PERSISTENT_CONTEXT_XML | final static XMLFormat PERSISTENT_CONTEXT_XML(Code) | | Holds the XML representation for persistent contexts
(holds persistent reference mapping).
|
SHORT_XML | final static XMLFormat SHORT_XML(Code) | | Holds the default XML representation for java.lang.Short .
|
STRING_XML | final static XMLFormat STRING_XML(Code) | | Holds the default XML representation for java.lang.String
instances. This representation consists of a "value"
attribute holding the string.
|
TEXT_XML | final static XMLFormat TEXT_XML(Code) | | Holds the default XML representation for Text instances.
This representation consists of a "value" attribute
holding the characters.
|
XML | final static XMLFormat XML(Code) | | Holds the XML representation of this binding (class/alias mapping
and class attribute values).
|
XMLBinding | public XMLBinding()(Code) | | Default constructor.
|
getClass | protected Class getClass(CharArray localName, CharArray uri) throws ClassNotFoundException(Code) | | Returns the class identified by the specified local name and URI
(the element local name and URI during unmarshalling). The default
implementation returns getClass(localName) .
Parameters: localName - the class local name identifier. Parameters: uri - the class URI identifier (can be null ). the corresponding class. throws: ClassNotFoundException - |
getFormat | public XMLFormat getFormat(Class cls)(Code) | | Returns the XML format for the specified class/interface.
The default implementation returns the most
specialized static format compatible with the specified class.
Parameters: cls - the class for which the XML format is returned. the XML format for the specified class. |
getLocalName | protected String getLocalName(Class cls)(Code) | | Returns the local name identifying the specified class (the element local
name during marshalling). The default implementation returns
this.getName(cls) .
Parameters: cls - the class for which the local name is returned. the local name of the specified class. |
getName | protected String getName(Class cls)(Code) | | Returns the name identifying the specified class (value of
XMLBinding.setClassAttribute class attribute during marshalling).
The default implementation returns the class alias (if any) or
cls.getName() .
Parameters: cls - the class for which a name identifier is returned. the alias or name for the class. |
getURI | protected String getURI(Class cls)(Code) | | Returns the URI identifying the specified class (the element namespace
URI during marshalling). The default implementation returns
null (no namespace URI).
Parameters: cls - the class for which the namespace URI is returned. the URI for the specified class or null if none. |
reset | public void reset()(Code) | | |
setAlias | public void setAlias(Class cls, String alias)(Code) | | Sets the alias of the specified class. Classes may have multiple
aliases but any given alias maps to a single class.
Parameters: cls - the class being aliased. Parameters: alias - the alias for the specified class. |
setClassAttribute | public void setClassAttribute(String name)(Code) | | Sets the name of the attribute holding the classname/alias
(by default"class" ).
If the local name is null then the class attribute
is never read/written (which may prevent unmarshalling).
Parameters: name - the local name of the attribute or null . |
setClassAttribute | public void setClassAttribute(String localName, String uri)(Code) | | Sets the local name and namespace URI of the attribute holding the
classname/alias (by default"class" and no namespace URI).
If the local name is null then the class attribute
is never read/written (which may prevent unmarshalling).
Parameters: localName - the local name of the attribute or null . Parameters: uri - the URI of the attribute or null if the class attribute has no namespace URI. |
|
|
|