public class XMLBinding extends Object implements 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:
// 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();
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:
java.lang.Object
(empty element)java.lang.Class
java.lang.String
java.lang.Appendable
java.util.Collection
java.util.Map
java.lang.Object[]
Boolean, Integer ...
)Constructor and Description |
---|
XMLBinding()
Default constructor.
|
Modifier and Type | Method and Description |
---|---|
protected XMLFormat<?> |
getFormat(Class<?> forClass)
Returns the XML format for the specified class/interface.
|
protected Class<?> |
readClass(XMLStreamReader reader,
boolean useAttributes)
Reads the class corresponding to the current XML element.
|
void |
reset() |
void |
setAlias(Class<?> cls,
QName qName)
Sets the qualified alias for the specified class.
|
void |
setAlias(Class<?> cls,
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(String name)
Convenience method equivalent to
setClassAttribute(QName.valueOf(name)) . |
protected void |
writeClass(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.
|
public void setAlias(Class<?> cls, QName qName)
cls
- the class being aliased.qName
- the qualified name.public final void setAlias(Class<?> cls, String alias)
setAlias(cls, QName.valueOf(alias))
.cls
- the class being aliased.alias
- the alias for the specified class.public void setClassAttribute(QName classAttribute)
null
the class
attribute is never read/written (which may prevent unmarshalling).classAttribute
- the qualified name of the class attribute or
null.
public final void setClassAttribute(String name)
setClassAttribute(QName.valueOf(name))
.name
- the name of the class attribute or null.
protected XMLFormat<?> getFormat(Class<?> forClass) throws XMLStreamException
XMLContext.getFormat(java.lang.Class<? extends T>)
for the specified class.forClass
- the class for which the XML format is returned.null
).XMLStreamException
protected Class<?> readClass(XMLStreamReader reader, boolean useAttributes) throws XMLStreamException
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.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).XMLStreamException
protected void writeClass(Class<?> cls, XMLStreamWriter writer, boolean useAttributes) throws XMLStreamException
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.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).XMLStreamException
public void reset()
Copyright © 2005-2013 Javolution. All Rights Reserved.