tes 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 myStringFormat = new XMLFormat(null) {...} XMLFormat myCollectionFormat = new XMLFormat(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[]
- all primitive types wrappers (e.g.
Boolean, Integer ...
)
@author
Jean-Marie Dautelle
@version 5.4, December 1, 2009