|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.hydrateframework.XMLDocWriterParser
public class XMLDocWriterParser
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();
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 |
---|
public static final java.lang.String xsiUri
protected static final char[] listSeparator
protected static final org.apache.commons.logging.Log m_log
Constructor Detail |
---|
public XMLDocWriterParser(ObjectContext ctx, org.xml.sax.ContentHandler hnd, XMLDocWriterParser.TestOutput testOut) throws org.xml.sax.SAXException
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
org.xml.sax.SAXException
XMLDocWriterParser.TestOutput
Method Detail |
---|
public void writeXML(java.lang.Object[] o) throws org.xml.sax.SAXException
org.xml.sax.SAXException
public void setOutputFilter(XMLDocWriterParser.TestOutput testOut)
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.
testOut
- The object that will filter the xml output.public java.util.Comparator getComparator(java.lang.String elemName)
XMLElementWriterParser
in order to determine the
ordering of elements in the SAX output stream.
elemName
- the name of the element that is about to be written
XMLDocWriterParser.TestOutput.getComparator(java.lang.String)
public java.lang.String findPrefixFromNamespace(java.lang.String namespace)
namespace
- the namespace uri to search for.
public int getBuildType()
public void setBuildType(int bldType)
bldType
- the build type - one of the Assembler.BUILD_.. values.public org.xml.sax.Attributes beforeObject(java.lang.Object o, org.xml.sax.Attributes attrs)
XMLElementWriterParser
before an object is to be written
to the SAX output stream.
o
- the object that is about to be writtenattrs
- the attributes that will be attached to the object element
XMLDocWriterParser.TestOutput.beforeObject(org.xml.sax.ContentHandler, java.lang.Object, java.lang.String, org.xml.sax.Attributes)
public java.lang.String[] beforeAttributes(java.lang.Object o, java.lang.String[] exclude)
XMLElementWriterParser
before object attributes are
written to the SAX output stream.
o
- the object whose attributes are about to be writtenexclude
- a list of attributes to be excluded
XMLDocWriterParser.TestOutput.beforeAttributes(org.xml.sax.ContentHandler, java.lang.Object, java.lang.String, java.lang.String[])
public void afterAttributes(java.lang.Object o)
XMLElementWriterParser
after object attributes are
written to the SAX output stream.
o
- the object whose attributes have been writtenXMLDocWriterParser.TestOutput.afterAttributes(org.xml.sax.ContentHandler, java.lang.Object, java.lang.String)
public void afterObject(java.lang.Object o)
XMLElementWriterParser
after an object has been
written to the SAX output stream.
o
- the object which has been writtenXMLDocWriterParser.TestOutput.afterObject(org.xml.sax.ContentHandler, java.lang.Object, java.lang.String)
public java.lang.String getXPath()
public void setRootElement(XMLElementWriterParser root)
public XMLElementWriterParser getRootElement()
public org.xml.sax.Attributes getNamespaceAttributes()
public void addPrefixMapping(java.lang.String prefix, java.lang.String namespace) throws org.xml.sax.SAXException
org.xml.sax.SAXException
public void startPrefixMapping(java.lang.String prefix, java.lang.String namespace) throws org.xml.sax.SAXException
startPrefixMapping
in interface org.xml.sax.ContentHandler
org.xml.sax.SAXException
public void startDoc() throws org.xml.sax.SAXException
org.xml.sax.SAXException
public void finishDoc() throws org.xml.sax.SAXException
org.xml.sax.SAXException
public org.xml.sax.ContentHandler getHandler()
public ObjectContext getContext()
public void characters(char[] chars, int offset, int len) throws org.xml.sax.SAXException
characters
in interface org.xml.sax.ContentHandler
org.xml.sax.SAXException
public void resetReadText()
public java.lang.String getReadText()
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
startElement
in interface org.xml.sax.ContentHandler
org.xml.sax.SAXException
public void endElement(java.lang.String uri, java.lang.String localName, java.lang.String qName) throws org.xml.sax.SAXException
endElement
in interface org.xml.sax.ContentHandler
org.xml.sax.SAXException
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
org.xml.sax.SAXException
public void doEndElement(java.lang.String uri, java.lang.String localName, java.lang.String qName) throws org.xml.sax.SAXException
org.xml.sax.SAXException
public void endDocument() throws org.xml.sax.SAXException
endDocument
in interface org.xml.sax.ContentHandler
org.xml.sax.SAXException
public void endPrefixMapping(java.lang.String prefix) throws org.xml.sax.SAXException
endPrefixMapping
in interface org.xml.sax.ContentHandler
org.xml.sax.SAXException
public void ignorableWhitespace(char[] values, int param, int param2) throws org.xml.sax.SAXException
ignorableWhitespace
in interface org.xml.sax.ContentHandler
org.xml.sax.SAXException
public void processingInstruction(java.lang.String str, java.lang.String str1) throws org.xml.sax.SAXException
processingInstruction
in interface org.xml.sax.ContentHandler
org.xml.sax.SAXException
public void setDocumentLocator(org.xml.sax.Locator locator)
setDocumentLocator
in interface org.xml.sax.ContentHandler
public void skippedEntity(java.lang.String str) throws org.xml.sax.SAXException
skippedEntity
in interface org.xml.sax.ContentHandler
org.xml.sax.SAXException
public void startDocument() throws org.xml.sax.SAXException
startDocument
in interface org.xml.sax.ContentHandler
org.xml.sax.SAXException
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |