Overview | Package | Class | Tree | Deprecated | Index | Help
| J avolution v5.3 (J2SE 1.5+)
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## javolution.xml
Class XMLBinding

    java.lang.Object
      javolution.xml.XMLBinding

All Implemented Interfaces:
  java.io.Serializable, Reusable, XMLSerializable

    public class XMLBinding

extends java.lang.Object
implements Reusable, 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:

- Impervious to obfuscation.
- Unaffected by any class refactoring.
- Can be mapped to multiple implementations. For example:`

           // 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 <T> XMLFormat<T> getFormat(Class<T> cls) {
                 Field[] fields = cls.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) {...}
             protected <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 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 ...`)

Version:
  5.3, April 9, 2007
Author:
  Jean-Marie Dautelle
See Also:
  Serialized Form

Constructor Summary
`XMLBinding()` Default constructor.

Method Summary
`protected
`<T> XMLFormat<T>`
` | `getFormat(java.lang.Class<T> cls)` Returns the XML format for the specified class/interface.
`protected java.lang.Class` | `readClass(XMLStreamReader reader, boolean useAttributes)` Reads the class corresponding to the current XML element.
`void` | `reset()` Resets the internal state of this object to its default values.
`void` | `setAlias(java.lang.Class cls, QName qName)` Sets the qualified alias for the specified class.
`void` | `setAlias(java.lang.Class cls, java.lang.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(java.lang.String name)` Convenience method equivalent to `setClassAttribute(QName.valueOf(name))`.
`protected void` | `writeClass(java.lang.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.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Constructor Detail

### XMLBinding

    public XMLBinding()

  Default constructor.

Method Detail

### setAlias

    public void setAlias(java.lang.Class cls,
                         QName qName)

  Sets the qualified alias for the specified class.

  Parameters:
    `cls` - the class being aliased.
    `qName` - the qualified name.

### setAlias

    public final void setAlias(java.lang.Class cls,
                               java.lang.String alias)

  Convenient method equivalent to `setAlias(cls, QName.valueOf(alias))`.

  Parameters:
    `cls` - the class being aliased.
    `alias` - the alias for the specified class.

### setClassAttribute

    public void 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).

  Parameters:
    `classAttribute` - the qualified name of the class attribute or `null`.``

### setClassAttribute

    public final void setClassAttribute(java.lang.String name)

  Convenience method equivalent to `setClassAttribute(QName.valueOf(name))`.

  Parameters:
    `name` - the name of the class attribute or `null`.``

### getFormat

    protected <T> XMLFormat<T> getFormat(java.lang.Class<T> 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.
  Returns:
    the XML format for the specified class.

### readClass

    protected java.lang.Class readClass(XMLStreamReader reader,
                                        boolean useAttributes)
                                 throws XMLStreamException

  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.

  Parameters:
    `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).
  Returns:
    the corresponding class.
  Throws:
    `XMLStreamException`

### writeClass

    protected void writeClass(java.lang.Class cls,
                              XMLStreamWriter writer,
                              boolean useAttributes)
                       throws XMLStreamException

  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.

  Parameters:
    `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).
  Throws:
    `XMLStreamException`

### reset

    public void reset()

  Description copied from interface: `Reusable`
  Resets the internal state of this object to its default values.

  Specified by:
    `reset` in interface `Reusable`

Overview | Package | Class | Tree | Deprecated | Index | Help
| J avolution v5.3 (J2SE 1.5+)
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Copyright © 2005 - 2009 Javolution.