Version 3

    ObjectModelProvider

     

    Here is the definition of the ObjectModelProvider interface.

     

    package org.jboss.xml.binding;
    
    public interface ObjectModelProvider
    {
       /**
        * Called by the framework when a root XML element is marshalled.
        *
        * @param o            the root of the object graph
        * @param namespaceURI namespace URI of the root XML element being marshalled
        * @param localName    local name of the root XML element being marshalled
        * @return an object that represents the root XML element corresponding to the namespace URI and local name
        */
       java.lang.Object getRoot(java.lang.Object o, java.lang.String namespaceURI, java.lang.String localName);
    }
    

     

    All object model providers should implement this interface. Besides the

    getRoot

    method defined in the interface, object model providers should also implement a set of the following methods descovered by the framework at runtime with introspection.

     

    a set of
    getChildren()
    methods

    This method is called on the object model provider by the framework when marshalling of a new XML element started.

    Each

    getChildren

    method must have three arguments:

    1. parent object of a concrete Java type (not

      java.lang.Object

      ) that is "asked" for its children

    2. namespace URI of the child XML element as java.lang.String

    3. local name of the child element as java.lang.String

     

    A

    getChildren

    method returns children that represent the namespace URI and local name in the XML content.

    The method can return null if there are no children in this object graph corresponding to the namespace and local name.

    The method can return a single object if there is only one child object corresponding to the namespace and local name.

    If there are many children that match the namespace URI and local name, the method can return them as an array, java.util.List, java.util.Collection or java.util.Iterator.

     

    a set of
    getElementValue
    methods

    This method is called on the object model provider by the framework for objects that represent XML elements with simple content, i.e. elements that don't contain nested XML elements.

    The method must have three arguments:

    1. an object of a concrete Java type (not java.lang.Object) that is "asked" to provide a value of the XML element being marshalled

    2. namespace URI as java.lang.String of the XML element being marshalled

    3. local name as java.lang.String of the XML element being marshalled

     

    The method returns either null if the object model does not have any value corresponding to the namespace URI and local name (in this case the XML content will not contain this XML element) or the actual value of the XML element.

     

    a set of
    getAttributeValue
    methods

    This method is called on the object model provider by the framework for objects that represent XML elements with attributes.

    The method must have three arguments:

    1. an object of a concrete Java type (not java.lang.Object) that is "asked" to provide a value for the XML attribute being marshalled

    2. namespace URI of the XML attribute being marshalled

    3. local name of the XML attribute being marshalled

     

    The method returns either null if the object graph does not have any value corresponding to the namespace URI and local name (in this case the XML content will not contain this attribute) or the actual value of the XML attribute.

     

    Code example

     

    Let's now put it all together and write an Object Model Provider for our book.

     

    public class BookObjectProvider
       implements ObjectModelProvider
    {
       public Object getRoot(Object o, String namespaceURI, String localName)
       {
          return o;
       }
    
       public Object getChildren(Book book, String namespaceUri, String localName)
       {
          Object children = null;
          if(localName.equals("book"))
          {
             children = book;
          }
          else if(localName.equals("character"))
          {
             children = book.getCharacters();
          }
          return children;
       }
    
       public Object getAttributeValue(Book book, String namespaceUri, String localName)
       {
          Object value;
          if("isbn".equals(localName))
          {
             value = book.getIsbn();
          }
          else
          {
             value = null;
          }
          return value;
       }
    
       public Object getElementValue(Book book, String namespaceUri, String localName)
       {
          Object value;
          if("title".equals(localName))
          {
             value = book.getTitle();
          }
          else if("author".equals(localName))
          {
             value = book.getAuthor();
          }
          else
          {
             value = null;
          }
          return value;
       }
    
       public Object getElementValue(BookCharacter character, String namespaceUri, String localName)
       {
          Object value = null;
          if("name".equals(localName))
          {
             value = character.getName();
          }
          else if("friend-of".equals(localName))
          {
             value = character.getFriendOf();
          }
          else if("since".equals(localName))
          {
             value = character.getSince();
          }
          else if("qualification".equals(localName))
          {
             value = character.getQualification();
          }
          return value;
       }
    }
    

     

    DTD Based Marshalling Client

     

          // obtain an instance of Book to marshal
          Book book = createBook();
    
          // get the output writter to write the XML content
          StringWriter xmlOutput = new StringWriter();
    
          // get the DTD source
          InputStream is = getResource("xml/book/books.dtd");
          Reader dtdReader = new InputStreamReader(is);
    
          // create an instance of DTD marshaller
          Marshaller marshaller = new DtdMarshaller();
    
          // map publicId to systemId as it should appear in the resulting XML file
          marshaller.mapPublicIdToSystemId("-//DTD Books//EN", "resources/xml/book/books.dtd");
    
          // create an instance of ObjectModelProvider with the book instance to be marshalled
          ObjectModelProvider provider = new BookObjectProvider();
    
          // marshal the book
          marshaller.marshal(dtdReader, provider, book, xmlOutput);
    
          // close DTD reader
          dtdReader.close();
    

     

    XML Schema Based Marshalling Client

     

          // obtain an instance of Book to marshal
          Book book = createBook();
    
          // get the output writter to write the XML content
          StringWriter xmlOutput = new StringWriter();
    
          // get the XML Schema source
          InputStream is = getResource("xml/book/books.xsd");
          Reader xsReader = new InputStreamReader(is);
    
          // create an instance of XML Schema marshaller
          XsMarshaller marshaller = new XsMarshaller();
    
          // we need to specify what elements are top most (roots) providing namespace URI, prefix and local name
          marshaller.addRootElement("http://example.org/ns/books/", "", "book");
    
          // declare default namespace
          marshaller.declareNamespace(null, "http://example.org/ns/books/");
    
          // add schema location by declaring xsi namespace and adding xsi:schemaReader attribute
          marshaller.declareNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
          marshaller.addAttribute("xsi", "schemaReader", "string", "http://example.org/ns/books/ resources/book/books.xsd");
    
          // create an instance of Object Model Provider with no book
          ObjectModelProvider provider = new BookObjectProvider();
    
          // marshall Book instance passing it as an argument instead of using the one that is returned by the BookObjectProvider
          marshaller.marshal(xsReader, provider, book, xmlOutput);
    
          // close XML Schema reader
          xsReader.close();