org.hydrateframework
Class XMLDocWriterParser

java.lang.Object
  extended by org.hydrateframework.XMLDocWriterParser
All Implemented Interfaces:
org.xml.sax.ContentHandler

public class XMLDocWriterParser
extends java.lang.Object
implements org.xml.sax.ContentHandler

This class is responsible for managing both the reading and writing of an XML document based on a particular class model. The code generator will derive an object from this class for each XML schema defined in the class definition file. The class manages the start and end of the document and the start and end of the namespace.

The following is a typical usage pattern to write out XML:

 import org.apache.xml.serializer.Serializer;
 import org.apache.xml.serializer.SerializerFactory;
 import org.apache.xml.serializer.OutputPropertiesFactory;
 import org.apache.xml.serializer.Method;
 import org.xml.sax.ContentHandler;
 .
 .
 .
  
      // The object context contains knowledge about the object model
      // you created.  Configure it with YourGeneratedModelClass.configureContext(..)  
         final ObjectContext ctx = new ObjectContext(true);
        MyModel.configureContext(ctx);
        
      // Get ready to write out XML
      Properties props = OutputPropertiesFactory.getDefaultMethodProperties(Method.XML);
      props.setProperty("{http://xml.apache.org/xalan}indent-amount", "4");
      props.setProperty(OutputKeys.INDENT, "yes");
      props.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
      Serializer ser = SerializerFactory.getSerializer(props);

      // Just write the data to System.out
      ser.setOutputStream(System.out);
      ContentHandler hnd = ser.asContentHandler();
 
      // Create an instance of your generated XML Writer/Parser derived
      // from this class       
      MySchemaXML xml = new MySchemaXML(ctx, hnd, "");

      // Simple Usage
      xml.startDoc();
      xml.writeXML(myObject);
      xml.finishDoc();
 
      // Advanced Usage...
  
      // Start the document
      xml.startDoc();
     
      // TO DO You can write your own sax events direct to the handler here
      // if you want to wrap the generated xml in your own message.
 
      // Set the output filter to control the writing process
      xml.setOutputFilter(new XMLDocWriterParser.TestOutputHandler() {
          public Attributes beforeObject(ContentHandler hnd, Object o,
                            String xPath, Attributes attrs) {

              // .. for example you can prevent an object with a particular
              // attribute value from being written.
              if (xPath.equals("/Root/MyObject") &&
                                  o instanceof MyChildObject) {
                           
                  if (((MyChildObject)o).getWriteToXML() == true) {
                      return attrs;
                  } else {
                        // suppress output of this object
                      return null;
                  }
              } 
              return attrs;
          }
      });
      xml.writeXML(scenario);
 
      // TO DO You can write your own sax events direct to the handler here
      // to complete the wrapping of the generated XML.
 
      // Finish the document
      xml.finishDoc();
 

The following is a typical usage pattern to read in XML:

 import javax.xml.parsers.SAXParser;
 import javax.xml.parsers.SAXParserFactory;
 import org.xml.sax.InputSource;
 import org.xml.sax.XMLReader;
 .
 .
 .
 
      // The object context contains knowledge about the object model
      // you created.  Configure it with YourGeneratedModelClass.configureContext(..)  
         final ObjectContext ctx = new ObjectContext(true);
        MyModel.configureContext(ctx);
    
      // Get ready to write out XML
      final SAXParserFactory spf = SAXParserFactory.newInstance();
      spf.setNamespaceAware(true);
      SAXParser sp = spf.newSAXParser();
      XMLReader rdr = sp.getXMLReader();

      // Create the reader, connect it to the parser and parse
      MySchemaXML xml = new MySchemaXML(ctx, null, "");
      rdr.setContentHandler(xml);
      rdr.parse(new InputSource(new ByteArrayInputStream(data)));
 
      // Retrieve parsed data
      MyObject object = xml.getNewMyObject();
 

Version:
@(#)$Revision: 1.5 $
Author:
David Chamberlin

Nested Class Summary
static interface XMLDocWriterParser.TestOutput
          This interface is called at various points when the framework is writing out the xml document.
static class XMLDocWriterParser.TestOutputHandler
          This implementation of the TestOutput interface provides a default implementation for each of its methods.
 
Field Summary
protected static char[] listSeparator
           
protected static org.apache.commons.logging.Log m_log
           
static java.lang.String xsiUri
           
 
Constructor Summary
XMLDocWriterParser(ObjectContext ctx, org.xml.sax.ContentHandler hnd, XMLDocWriterParser.TestOutput testOut)
          Constructor
 
Method Summary
 void addPrefixMapping(java.lang.String prefix, java.lang.String namespace)
           
 void afterAttributes(java.lang.Object o)
          Called by the XMLElementWriterParser after object attributes are written to the SAX output stream.
 void afterObject(java.lang.Object o)
          Called by the XMLElementWriterParser after an object has been written to the SAX output stream.
 java.lang.String[] beforeAttributes(java.lang.Object o, java.lang.String[] exclude)
          Called by the XMLElementWriterParser before object attributes are written to the SAX output stream.
 org.xml.sax.Attributes beforeObject(java.lang.Object o, org.xml.sax.Attributes attrs)
          Called by the XMLElementWriterParser before an object is to be written to the SAX output stream.
 void characters(char[] chars, int offset, int len)
           
 void doEndElement(java.lang.String uri, java.lang.String localName, java.lang.String qName)
           
 void doStartElement(java.lang.String uri, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes attrs)
           
 void endDocument()
           
 void endElement(java.lang.String uri, java.lang.String localName, java.lang.String qName)
           
 void endPrefixMapping(java.lang.String prefix)
           
 java.lang.String findPrefixFromNamespace(java.lang.String namespace)
          Return the prefix that has been designated for the given namespace.
 void finishDoc()
           
 int getBuildType()
          Get the build type that will be used to construct objects - this will determine for example whether the XML parser looks in the database for existing objects as it reads the XML.
 java.util.Comparator getComparator(java.lang.String elemName)
          Called by the XMLElementWriterParser in order to determine the ordering of elements in the SAX output stream.
 ObjectContext getContext()
           
 org.xml.sax.ContentHandler getHandler()
           
 org.xml.sax.Attributes getNamespaceAttributes()
           
 java.lang.String getReadText()
           
 XMLElementWriterParser getRootElement()
           
 java.lang.String getXPath()
           
 void ignorableWhitespace(char[] values, int param, int param2)
           
 void processingInstruction(java.lang.String str, java.lang.String str1)
           
 void resetReadText()
           
 void setBuildType(int bldType)
          Set the build type that will be used to construct objects - this will determine for example whether the XML parser looks in the database for existing objects as it reads the XML.
 void setDocumentLocator(org.xml.sax.Locator locator)
           
 void setOutputFilter(XMLDocWriterParser.TestOutput testOut)
          Establish the object that will be used to filter the XML output.
 void setRootElement(XMLElementWriterParser root)
           
 void skippedEntity(java.lang.String str)
           
 void startDoc()
           
 void startDocument()
           
 void startElement(java.lang.String uri, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes attrs)
           
 void startPrefixMapping(java.lang.String prefix, java.lang.String namespace)
           
 void writeXML(java.lang.Object[] o)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

xsiUri

public static final java.lang.String xsiUri
See Also:
Constant Field Values

listSeparator

protected static final char[] listSeparator

m_log

protected static final org.apache.commons.logging.Log m_log
Constructor Detail

XMLDocWriterParser

public XMLDocWriterParser(ObjectContext ctx,
                          org.xml.sax.ContentHandler hnd,
                          XMLDocWriterParser.TestOutput testOut)
                   throws org.xml.sax.SAXException
Constructor

Parameters:
ctx - the object context that this writer/parser will be reading/writing objects to.
hnd - the content handler that this writer/parser will write its SAX stream to.
testOut - the implementation of this interface will be called back at key points during the writing of the XML document. This interface gives an opportunity to veto certain parts of the document or insert additional XML as required
Throws:
org.xml.sax.SAXException
See Also:
XMLDocWriterParser.TestOutput
Method Detail

writeXML

public void writeXML(java.lang.Object[] o)
              throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException

setOutputFilter

public void setOutputFilter(XMLDocWriterParser.TestOutput testOut)
Establish the object that will be used to filter the XML output. An implementation of the XMLDocWriterParser.TestOutput interface allows control over which elements are output, which attributes are output and permits additional document sections or attributes to be inserted into the output.

Parameters:
testOut - The object that will filter the xml output.

getComparator

public java.util.Comparator getComparator(java.lang.String elemName)
Called by the XMLElementWriterParser in order to determine the ordering of elements in the SAX output stream.

Parameters:
elemName - the name of the element that is about to be written
Returns:
the comparator to be used in sorting the elements
See Also:
XMLDocWriterParser.TestOutput.getComparator(java.lang.String)

findPrefixFromNamespace

public java.lang.String findPrefixFromNamespace(java.lang.String namespace)
Return the prefix that has been designated for the given namespace.

Parameters:
namespace - the namespace uri to search for.
Returns:
the prefix

getBuildType

public int getBuildType()
Get the build type that will be used to construct objects - this will determine for example whether the XML parser looks in the database for existing objects as it reads the XML.

Returns:
the build type - one of the Assembler.BUILD_.. values.

setBuildType

public void setBuildType(int bldType)
Set the build type that will be used to construct objects - this will determine for example whether the XML parser looks in the database for existing objects as it reads the XML.

Parameters:
bldType - the build type - one of the Assembler.BUILD_.. values.

beforeObject

public org.xml.sax.Attributes beforeObject(java.lang.Object o,
                                           org.xml.sax.Attributes attrs)
Called by the XMLElementWriterParser before an object is to be written to the SAX output stream.

Parameters:
o - the object that is about to be written
attrs - the attributes that will be attached to the object element
Returns:
the attributes that should be attached or null
See Also:
XMLDocWriterParser.TestOutput.beforeObject(org.xml.sax.ContentHandler, java.lang.Object, java.lang.String, org.xml.sax.Attributes)

beforeAttributes

public java.lang.String[] beforeAttributes(java.lang.Object o,
                                           java.lang.String[] exclude)
Called by the XMLElementWriterParser before object attributes are written to the SAX output stream.

Parameters:
o - the object whose attributes are about to be written
exclude - a list of attributes to be excluded
Returns:
the list of attributes to be excluded or null
See Also:
XMLDocWriterParser.TestOutput.beforeAttributes(org.xml.sax.ContentHandler, java.lang.Object, java.lang.String, java.lang.String[])

afterAttributes

public void afterAttributes(java.lang.Object o)
Called by the XMLElementWriterParser after object attributes are written to the SAX output stream.

Parameters:
o - the object whose attributes have been written
See Also:
XMLDocWriterParser.TestOutput.afterAttributes(org.xml.sax.ContentHandler, java.lang.Object, java.lang.String)

afterObject

public void afterObject(java.lang.Object o)
Called by the XMLElementWriterParser after an object has been written to the SAX output stream.

Parameters:
o - the object which has been written
See Also:
XMLDocWriterParser.TestOutput.afterObject(org.xml.sax.ContentHandler, java.lang.Object, java.lang.String)

getXPath

public java.lang.String getXPath()

setRootElement

public void setRootElement(XMLElementWriterParser root)

getRootElement

public XMLElementWriterParser getRootElement()

getNamespaceAttributes

public org.xml.sax.Attributes getNamespaceAttributes()

addPrefixMapping

public void addPrefixMapping(java.lang.String prefix,
                             java.lang.String namespace)
                      throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException

startPrefixMapping

public void startPrefixMapping(java.lang.String prefix,
                               java.lang.String namespace)
                        throws org.xml.sax.SAXException
Specified by:
startPrefixMapping in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

startDoc

public void startDoc()
              throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException

finishDoc

public void finishDoc()
               throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException

getHandler

public org.xml.sax.ContentHandler getHandler()

getContext

public ObjectContext getContext()

characters

public void characters(char[] chars,
                       int offset,
                       int len)
                throws org.xml.sax.SAXException
Specified by:
characters in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

resetReadText

public void resetReadText()

getReadText

public java.lang.String getReadText()

startElement

public void startElement(java.lang.String uri,
                         java.lang.String localName,
                         java.lang.String qName,
                         org.xml.sax.Attributes attrs)
                  throws org.xml.sax.SAXException
Specified by:
startElement in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

endElement

public void endElement(java.lang.String uri,
                       java.lang.String localName,
                       java.lang.String qName)
                throws org.xml.sax.SAXException
Specified by:
endElement in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

doStartElement

public void doStartElement(java.lang.String uri,
                           java.lang.String localName,
                           java.lang.String qName,
                           org.xml.sax.Attributes attrs)
                    throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException

doEndElement

public void doEndElement(java.lang.String uri,
                         java.lang.String localName,
                         java.lang.String qName)
                  throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException

endDocument

public void endDocument()
                 throws org.xml.sax.SAXException
Specified by:
endDocument in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

endPrefixMapping

public void endPrefixMapping(java.lang.String prefix)
                      throws org.xml.sax.SAXException
Specified by:
endPrefixMapping in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

ignorableWhitespace

public void ignorableWhitespace(char[] values,
                                int param,
                                int param2)
                         throws org.xml.sax.SAXException
Specified by:
ignorableWhitespace in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

processingInstruction

public void processingInstruction(java.lang.String str,
                                  java.lang.String str1)
                           throws org.xml.sax.SAXException
Specified by:
processingInstruction in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

setDocumentLocator

public void setDocumentLocator(org.xml.sax.Locator locator)
Specified by:
setDocumentLocator in interface org.xml.sax.ContentHandler

skippedEntity

public void skippedEntity(java.lang.String str)
                   throws org.xml.sax.SAXException
Specified by:
skippedEntity in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

startDocument

public void startDocument()
                   throws org.xml.sax.SAXException
Specified by:
startDocument in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException


Copyright © 2000 The Hydrate Project. All Rights Reserved.