Overview | Package | Class | Tree | Deprecated | Index | Help
| J avolution v5.2 (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`); 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:

- Impervious to obfuscation.
- Unnaffected 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 {
             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[]`
- all primitive types wrappers (e.g. `Boolean, Integer ...`)

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

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).
`
`<T> XMLFormat<T>`
` | `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

### XMLBinding

    public XMLBinding()

  Default constructor.

Method Detail

### setAlias

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

  Sets the alias of the specified class. Classes may have multiple aliases but any given alias maps to a single class.

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

### setClassAttribute

    public void setClassAttribute(java.lang.String name)

  Sets the name of the attribute holding the classname/alias (by default`"class"`). If the local name is `null` then the class attribute is never read/written (which may prevent unmarshalling).

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

### setClassAttribute

    public 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). If the local name is `null` then the class attribute is never read/written (which may prevent unmarshalling).

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

### getFormat

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

### getName

    protected java.lang.String getName(java.lang.Class cls)

  Returns the name identifying the specified class (value of `class attribute` during marshalling). The default implementation returns the class alias (if any) or `cls.getName()`.

  Parameters:
    `cls` - the class for which a name identifier is returned.
  Returns:
    the alias or name for the class.

### getClass

    protected java.lang.Class getClass(CharArray name)
                                throws java.lang.ClassNotFoundException

  Returns the class identified by the specified name (value of `class attribute` during unmarshalling). The default implementation returns an aliased class or `Class.forName(name.toString())`.

  Parameters:
    `name` - the class name identifier.
  Returns:
    the class for the specified name.
  Throws:
    `java.lang.ClassNotFoundException`

### getLocalName

    protected java.lang.String getLocalName(java.lang.Class cls)

  Returns the local name identifying the specified class (the element local name during marshalling). The default implementation returns `this.getName(cls)`.

  Parameters:
    `cls` - the class for which the local name is returned.
  Returns:
    the local name of the specified class.

### getURI

    protected java.lang.String getURI(java.lang.Class cls)

  Returns the URI identifying the specified class (the element namespace URI during marshalling). The default implementation returns `null` (no namespace URI).

  Parameters:
    `cls` - the class for which the namespace URI is returned.
  Returns:
    the URI for the specified class or `null` if none.

### getClass

    protected java.lang.Class getClass(CharArray localName,
                                       CharArray uri)
                                throws java.lang.ClassNotFoundException

  Returns the class identified by the specified local name and URI (the element local name and URI during unmarshalling). The default implementation returns `getClass(localName)`.

  Parameters:
    `localName` - the class local name identifier.
    `uri` - the class URI identifier (can be `null`).
  Returns:
    the corresponding class.
  Throws:
    `java.lang.ClassNotFoundException`

### 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.2 (J2SE 1.5+)
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Copyright © 2005 - 2007 Javolution.