You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jaxme-dev@ws.apache.org by Alexey Loubyansky <al...@jboss.org> on 2004/01/07 13:19:47 UTC

IoC document provider (marshalling)

Before we start to integrate IoC document factory into JaxMe, I would
like you to consider the same approach for marshalling.

Now, having a weird Java representation of XML content, I would like to
marshal it back into XML. In my current implementation the requiremenets
are:
1. XML schema or DTD;
2. Java representation of the XML content.

How it works.
The XML schema (or DTD) is parsed (I use XSSchema). Then I get the root
element and navigate the schema tree asking the DocumentProvider for
children and element values.
DocumentProvider has a set of:
* public Object provideChildren() methods that take two parameters: the
first one is the Java object that is asked for childrent, the second
parameter is the name (local name) that identifies the children. The
method can return null if children name is unrecognized.

* public Object provideValue() methods that also take two parameters:
the first one is the Java object that is asked for a value of its child
of simple type and the name (local name) of this simple child.
(Attributes are not yet supported.)

Below is an example of BookDocumentProvider for
http://www.xml.com/pub/a/2000/11/29/schemas/part1.html.
Most methods of DocumentProvider interface except for getDocument() are
weird.
This same BookDocumentProvider works with three different XML schemas
for book from the article and a DTD that I wrote myself. What do you
think about this approach?

Thanks for your time.

public class BookDocumentProvider
   implements DocumentProvider
{
   private Book book;

   public BookDocumentProvider(Book book)
   {
      this.book = book;
   }

   /** DocumentProvider implementation */
   public String getDoctype()
   {
      return "book";
   }

   /** DocumentProvider implementation */
   public String getPublicId()
   {
      return "-//DTD Books//EN";
   }

   /** DocumentProvider implementation */
   public String getSystemId()
   {
      return "books.dtd";
   }

   /** DocumentProvider implementation */
   public String getRootLocalName()
   {
      return "book";
   }

   /** DocumentProvider implementation */
   public String getRootPrefix()
   {
      return null;
   }

   /** DocumentProvider implementation */
   public String getRootNamespaceUri()
   {
      return null;
   }

   /** DocumentProvider implementation */
   public Object getDocument()
   {
      return book;
   }

   public Object provideChildren(Book book, String name)
   {
      Object children = null;
      if(name.equals("book"))
      {
         children = book;
      }
      else if(name.equals("character"))
      {
         children = book.getCharacters();
      }
      return children;
   }

   public Object provideValue(Book book, String name)
   {
      Object value = null;
      if("title".equals(name))
      {
         value = book.getTitle();
      }
      else if("author".equals(name))
      {
         value = book.getAuthor();
      }
      return value;
   }

   public Object provideValue(BookCharacter character, String name)
   {
      Object value = null;
      if("name".equals(name))
      {
         value = character.getName();
      }
      else if("friend-of".equals(name))
      {
         value = character.getFriendOf();
      }
      else if("since".equals(name))
      {
         value = character.getSince();
      }
      else if("qualification".equals(name))
      {
         value = character.getQualification();
      }
      return value;
   }
}


---------------------------------------------------------------------
To unsubscribe, e-mail: jaxme-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: jaxme-dev-help@ws.apache.org