Javolution 6.0.0 java
|
Public Member Functions | |
XMLBinding () | |
void | setAlias (Class<?> cls, QName qName) |
final void | setAlias (Class<?> cls, String alias) |
void | setClassAttribute (QName classAttribute) |
final void | setClassAttribute (String name) |
void | reset () |
Protected Member Functions | |
XMLFormat<?> | getFormat (Class<?> forClass) throws XMLStreamException |
Class<?> | readClass (XMLStreamReader reader, boolean useAttributes) throws XMLStreamException |
void | writeClass (Class<?> cls, XMLStreamWriter writer, boolean useAttributes) throws XMLStreamException |
Static Package Attributes | |
static final XMLBinding | DEFAULT = new XMLBinding() |
Private Attributes | |
QName | _classAttribute = QName.valueOf("class") |
final FastMap< Class<?>, QName > | _classToAlias = new FastMap<Class<?>, QName>() |
final FastMap< QName, Class<?> > | _aliasToClass = new FastMap<QName, Class<?>>() |
Static Private Attributes | |
static final long | serialVersionUID = 6611041662550083919L |
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:
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 { 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); } } [/code]
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 ...
) Definition at line 100 of file XMLBinding.java.
javolution.xml.XMLBinding.XMLBinding | ( | ) |
|
protected |
Returns the XML format for the specified class/interface. The default implementation returns the XMLContext#getFormat for the specified class.
forClass | the class for which the XML format is returned. |
null
). Definition at line 179 of file XMLBinding.java.
References javolution.xml.XMLContext.getFormat().
Referenced by javolution.xml.XMLFormat< T >.OutputElement.add(), and javolution.xml.XMLFormat< T >.InputElement.readInstanceOf().
|
protected |
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.
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 |
Definition at line 203 of file XMLBinding.java.
References javolution.xml.XMLBinding._aliasToClass, javolution.xml.XMLBinding._classAttribute, javolution.xml.QName.getLocalName(), javolution.xml.QName.getNamespaceURI(), and javolution.xml.QName.valueOf().
Referenced by javolution.xml.XMLFormat< T >.InputElement.get(), and javolution.xml.XMLFormat< T >.InputElement.getNext().
void javolution.xml.XMLBinding.reset | ( | ) |
Definition at line 291 of file XMLBinding.java.
References javolution.xml.XMLBinding._aliasToClass, javolution.xml.XMLBinding._classAttribute, javolution.xml.XMLBinding._classToAlias, and javolution.xml.QName.valueOf().
void javolution.xml.XMLBinding.setAlias | ( | Class<?> | cls, |
QName | qName | ||
) |
Sets the qualified alias for the specified class.
cls | the class being aliased. |
qName | the qualified name. |
Definition at line 133 of file XMLBinding.java.
References javolution.xml.XMLBinding._aliasToClass, and javolution.xml.XMLBinding._classToAlias.
Referenced by javolution.xml.XMLBinding.setAlias().
final void javolution.xml.XMLBinding.setAlias | ( | Class<?> | cls, |
String | alias | ||
) |
Convenient method equivalent to setAlias(cls, QName.valueOf(alias)).
cls | the class being aliased. |
alias | the alias for the specified class. |
Definition at line 145 of file XMLBinding.java.
References javolution.xml.XMLBinding.setAlias(), and javolution.xml.QName.valueOf().
void javolution.xml.XMLBinding.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).
classAttribute | the qualified name of the class attribute or null |
Definition at line 157 of file XMLBinding.java.
References javolution.xml.XMLBinding._classAttribute.
Referenced by javolution.xml.XMLBinding.setClassAttribute().
final void javolution.xml.XMLBinding.setClassAttribute | ( | String | name | ) |
Convenience method equivalent to setClassAttribute(QName.valueOf(name)).
name | the name of the class attribute or null |
Definition at line 167 of file XMLBinding.java.
References javolution.xml.XMLBinding.setClassAttribute(), and javolution.xml.QName.valueOf().
|
protected |
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.
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 |
Definition at line 264 of file XMLBinding.java.
References javolution.xml.XMLBinding._classAttribute, javolution.xml.XMLBinding._classToAlias, javolution.xml.QName.getLocalName(), javolution.xml.QName.getNamespaceURI(), and javolution.xml.QName.toString().
Referenced by javolution.xml.XMLFormat< T >.OutputElement.add().
|
private |
Holds the alias (QName) to class mapping.
Definition at line 120 of file XMLBinding.java.
Referenced by javolution.xml.XMLBinding.readClass(), javolution.xml.XMLBinding.reset(), and javolution.xml.XMLBinding.setAlias().
|
private |
Holds the class attribute.
Definition at line 110 of file XMLBinding.java.
Referenced by javolution.xml.XMLBinding.readClass(), javolution.xml.XMLBinding.reset(), javolution.xml.XMLBinding.setClassAttribute(), and javolution.xml.XMLBinding.writeClass().
|
private |
Holds the class to alias (QName) mapping.
Definition at line 115 of file XMLBinding.java.
Referenced by javolution.xml.XMLBinding.reset(), javolution.xml.XMLBinding.setAlias(), and javolution.xml.XMLBinding.writeClass().
|
staticpackage |
Holds the default instance used by readers/writers (thread-safe).
Definition at line 105 of file XMLBinding.java.
Referenced by javolution.xml.XMLFormat< T >.InputElement.reset(), and javolution.xml.XMLFormat< T >.OutputElement.reset().
|
staticprivate |
Definition at line 297 of file XMLBinding.java.