|
J avolution v5.2 (J2SE 1.5+) | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjavolution.xml.XMLBinding
public class XMLBinding
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:
// 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 {
public <T> XMLFormat<T> getFormat(Class<T> cls) {
Field[] fields = clt.getDeclaredFields();
return new XMLReflectionFormat<T>(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<String> _myStringFormat = new XMLFormat<String>(null) {...}
XMLFormat<Collection> _myCollectionFormat = new XMLFormat<Collection>(null) {...}
public <T> XMLFormat<T> getFormat(Class<T> cls) {
if (String.class.equals(cls))
return _myStringFormat;
if (Collection.class.isAssignableFrom(cls))
return _myCollectionFormat;
return super.getFormat(cls);
}
}
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[]
Boolean, Integer ...
)
Constructor Summary | |
---|---|
XMLBinding()
Default constructor. |
Method Summary | ||
---|---|---|
protected java.lang.Class |
getClass(CharArray name)
Returns the class identified by the specified name (value of class attribute during unmarshalling). |
|
protected java.lang.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). |
|
|
getFormat(java.lang.Class<T> cls)
Returns the XML format for the specified class/interface. |
|
protected java.lang.String |
getLocalName(java.lang.Class cls)
Returns the local name identifying the specified class (the element local name during marshalling). |
|
protected java.lang.String |
getName(java.lang.Class cls)
Returns the name identifying the specified class (value of class attribute during marshalling). |
|
protected java.lang.String |
getURI(java.lang.Class cls)
Returns the URI identifying the specified class (the element namespace URI during marshalling). |
|
void |
reset()
Resets the internal state of this object to its default values. |
|
void |
setAlias(java.lang.Class cls,
java.lang.String alias)
Sets the alias of the specified class. |
|
void |
setClassAttribute(java.lang.String name)
Sets the name of the attribute holding the classname/alias (by default "class" ). |
|
void |
setClassAttribute(java.lang.String localName,
java.lang.String uri)
Sets the local name and namespace URI of the attribute holding the classname/alias (by default "class" and no namespace URI). |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public XMLBinding()
Method Detail |
---|
public void setAlias(java.lang.Class cls, java.lang.String alias)
cls
- the class being aliased.alias
- the alias for the specified class.public void setClassAttribute(java.lang.String name)
"class"
).
If the local name is null
then the class attribute
is never read/written (which may prevent unmarshalling).
name
- the local name of the attribute or null
.public void setClassAttribute(java.lang.String localName, java.lang.String uri)
"class"
and no namespace URI).
If the local name is null
then the class attribute
is never read/written (which may prevent unmarshalling).
localName
- the local name of the attribute or null
.uri
- the URI of the attribute or null
if the
class attribute has no namespace URI.public <T> XMLFormat<T> getFormat(java.lang.Class<T> cls)
cls
- the class for which the XML format is returned.
protected java.lang.String getName(java.lang.Class cls)
class attribute
during marshalling).
The default implementation returns the class alias (if any) or
cls.getName()
.
cls
- the class for which a name identifier is returned.
protected java.lang.Class getClass(CharArray name) throws java.lang.ClassNotFoundException
class attribute
during unmarshalling).
The default implementation returns an aliased class or
Class.forName(name.toString())
.
name
- the class name identifier.
java.lang.ClassNotFoundException
protected java.lang.String getLocalName(java.lang.Class cls)
this.getName(cls)
.
cls
- the class for which the local name is returned.
protected java.lang.String getURI(java.lang.Class cls)
null
(no namespace URI).
cls
- the class for which the namespace URI is returned.
null
if none.protected java.lang.Class getClass(CharArray localName, CharArray uri) throws java.lang.ClassNotFoundException
getClass(localName)
.
localName
- the class local name identifier.uri
- the class URI identifier (can be null
).
java.lang.ClassNotFoundException
public void reset()
Reusable
reset
in interface Reusable
|
J avolution v5.2 (J2SE 1.5+) | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |