JavaScript is disabled on your browser.
javolution.xml

Class XMLBinding

  • All Implemented Interfaces:
    Serializable, XMLSerializable


    public class XMLBinding
    extends Object
    implements 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 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);
     }
     }
     

    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.4, December 1, 2009
    Author:
    Jean-Marie Dautelle
    See Also:
    Serialized Form
    • Constructor Detail

      • XMLBinding

        public XMLBinding()
        Default constructor.
    • Method Detail

      • setAlias

        public void setAlias(Class<?> cls,
         QName qName)
        Sets the qualified alias for the specified class.
        Parameters:
        cls - the class being aliased.
        qName - the qualified name.
      • 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.
      • readClass

        protected 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
      • reset

        public void reset()

Copyright © 2005-2013 Javolution. All Rights Reserved.

AltStyle によって変換されたページ (->オリジナル) /