org.dom4j.io
Class SAXWriter

java.lang.Object
  extended byorg.dom4j.io.SAXWriter
All Implemented Interfaces:
XMLReader

public class SAXWriter
extends java.lang.Object
implements XMLReader

SAXWriter writes a DOM4J tree to a SAX ContentHandler.

Version:
$Revision: 1.22 $
Author:
James Strachan

Field Summary
protected static java.lang.String FEATURE_NAMESPACE_PREFIXES
           
protected static java.lang.String FEATURE_NAMESPACES
           
protected static java.lang.String[] LEXICAL_HANDLER_NAMES
           
 
Constructor Summary
SAXWriter()
           
SAXWriter(ContentHandler contentHandler)
           
SAXWriter(ContentHandler contentHandler, LexicalHandler lexicalHandler)
           
SAXWriter(ContentHandler contentHandler, LexicalHandler lexicalHandler, EntityResolver entityResolver)
           
 
Method Summary
protected  AttributesImpl addNamespaceAttribute(AttributesImpl namespaceAttributes, Namespace namespace)
          If isDelcareNamespaceAttributes() is enabled then this method will add the given namespace declaration to the supplied attributes object, creating one if it does not exist.
protected  void checkForNullHandlers()
          Ensures non-null content handlers?
protected  Attributes createAttributes(Element element, Attributes namespaceAttributes)
           
protected  void documentLocator(Document document)
          The Locator is only really useful when parsing a textual document as its main purpose is to identify the line and column number.
protected  void dtdHandler(Document document)
          We do not yet support DTD or XML Schemas so this method does nothing right now.
protected  void endDocument()
           
protected  void endElement(Element element)
           
protected  void endPrefixMapping(NamespaceStack namespaceStack, int stackSize)
          Fires a SAX endPrefixMapping event for all the namespaceStack which have gone out of scope
protected  void entityResolver(Document document)
           
 ContentHandler getContentHandler()
          Return the current content handler.
 DTDHandler getDTDHandler()
          Return the current DTD handler.
 EntityResolver getEntityResolver()
          Return the current entity resolver.
 ErrorHandler getErrorHandler()
          Return the current error handler.
 boolean getFeature(java.lang.String name)
          Looks up the value of a feature.
 LexicalHandler getLexicalHandler()
           
 java.lang.Object getProperty(java.lang.String name)
          Gets the given SAX property
 boolean isDeclareNamespaceAttributes()
          Should namespace declarations be converted to "xmlns" attributes.
protected  boolean isIgnoreableNamespace(Namespace namespace, NamespaceStack namespaceStack)
           
 void parse(InputSource input)
          Parses an XML document.
 void parse(java.lang.String systemId)
          This method is not supported.
 void setContentHandler(ContentHandler contentHandler)
          Sets the ContentHandler called when SAX events are raised
 void setDeclareNamespaceAttributes(boolean declareNamespaceAttributes)
          Sets whether namespace declarations should be exported as "xmlns" attributes or not.
 void setDTDHandler(DTDHandler dtdHandler)
          Sets the DTDHandler.
 void setEntityResolver(EntityResolver entityResolver)
          Sets the EntityResolver .
 void setErrorHandler(ErrorHandler errorHandler)
          Sets the ErrorHandler.
 void setFeature(java.lang.String name, boolean value)
          This implementation does actually use any features but just stores them for later retrieval
 void setLexicalHandler(LexicalHandler lexicalHandler)
          Sets the LexicalHandler .
 void setProperty(java.lang.String name, java.lang.Object value)
          Sets the given SAX property
 void setXMLReader(XMLReader xmlReader)
          Sets the XMLReader used to write SAX events to
protected  void startDocument()
           
protected  void startElement(Element element, AttributesImpl namespaceAttributes)
           
protected  AttributesImpl startPrefixMapping(Element element, NamespaceStack namespaceStack)
          Fires a SAX startPrefixMapping event for all the namespaceStack which have just come into scope
 void write(CDATA cdata)
          Generates SAX events for the given CDATA
 void write(Comment comment)
          Generates SAX events for the given Comment
 void write(Document document)
          Generates SAX events for the given Document and all its content
 void write(Element element)
          Generates SAX events for the given Element and all its content
protected  void write(Element element, NamespaceStack namespaceStack)
           
 void write(Entity entity)
          Generates SAX events for the given Entity
 void write(Node node)
          A polymorphic method to write any Node to this SAX stream
 void write(ProcessingInstruction pi)
          Generates SAX events for the given ProcessingInstruction
 void write(java.lang.String text)
          Generates SAX events for the given text
 void writeClose(Element element)
          Writes the closing tag of an Element
protected  void writeContent(Branch branch, NamespaceStack namespaceStack)
           
 void writeOpen(Element element)
          Writes the opening tag of an Element, including its Attributes but without its content.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LEXICAL_HANDLER_NAMES

protected static final java.lang.String[] LEXICAL_HANDLER_NAMES

FEATURE_NAMESPACE_PREFIXES

protected static java.lang.String FEATURE_NAMESPACE_PREFIXES

FEATURE_NAMESPACES

protected static java.lang.String FEATURE_NAMESPACES
Constructor Detail

SAXWriter

public SAXWriter()

SAXWriter

public SAXWriter(ContentHandler contentHandler)

SAXWriter

public SAXWriter(ContentHandler contentHandler,
                 LexicalHandler lexicalHandler)

SAXWriter

public SAXWriter(ContentHandler contentHandler,
                 LexicalHandler lexicalHandler,
                 EntityResolver entityResolver)
Method Detail

write

public void write(Node node)
           throws SAXException
A polymorphic method to write any Node to this SAX stream

Throws:
SAXException

write

public void write(Document document)
           throws SAXException
Generates SAX events for the given Document and all its content

Parameters:
document - is the Document to parse
Throws:
SAXException - if there is a SAX error processing the events

write

public void write(Element element)
           throws SAXException
Generates SAX events for the given Element and all its content

Parameters:
element - is the Element to parse
Throws:
SAXException - if there is a SAX error processing the events

writeOpen

public void writeOpen(Element element)
               throws SAXException

Writes the opening tag of an Element, including its Attributes but without its content.

Parameters:
element - Element to output.
Throws:
SAXException

writeClose

public void writeClose(Element element)
                throws SAXException

Writes the closing tag of an Element

Parameters:
element - Element to output.
Throws:
SAXException

write

public void write(java.lang.String text)
           throws SAXException
Generates SAX events for the given text

Parameters:
text - is the text to send to the SAX ContentHandler
Throws:
SAXException - if there is a SAX error processing the events

write

public void write(CDATA cdata)
           throws SAXException
Generates SAX events for the given CDATA

Parameters:
cdata - is the CDATA to parse
Throws:
SAXException - if there is a SAX error processing the events

write

public void write(Comment comment)
           throws SAXException
Generates SAX events for the given Comment

Parameters:
comment - is the Comment to parse
Throws:
SAXException - if there is a SAX error processing the events

write

public void write(Entity entity)
           throws SAXException
Generates SAX events for the given Entity

Parameters:
entity - is the Entity to parse
Throws:
SAXException - if there is a SAX error processing the events

write

public void write(ProcessingInstruction pi)
           throws SAXException
Generates SAX events for the given ProcessingInstruction

Parameters:
pi - is the ProcessingInstruction to parse
Throws:
SAXException - if there is a SAX error processing the events

isDeclareNamespaceAttributes

public boolean isDeclareNamespaceAttributes()
Should namespace declarations be converted to "xmlns" attributes. This property defaults to false as per the SAX specification. This property is set via the SAX feature "http://xml.org/sax/features/namespace-prefixes"


setDeclareNamespaceAttributes

public void setDeclareNamespaceAttributes(boolean declareNamespaceAttributes)
Sets whether namespace declarations should be exported as "xmlns" attributes or not. This property is set from the SAX feature "http://xml.org/sax/features/namespace-prefixes"


getContentHandler

public ContentHandler getContentHandler()
Description copied from interface: XMLReader
Return the current content handler.

Specified by:
getContentHandler in interface XMLReader
Returns:
the ContentHandler called when SAX events are raised
See Also:
XMLReader.setContentHandler(org.xml.sax.ContentHandler)

setContentHandler

public void setContentHandler(ContentHandler contentHandler)
Sets the ContentHandler called when SAX events are raised

Specified by:
setContentHandler in interface XMLReader
Parameters:
contentHandler - is the ContentHandler called when SAX events are raised
See Also:
XMLReader.getContentHandler()

getDTDHandler

public DTDHandler getDTDHandler()
Description copied from interface: XMLReader
Return the current DTD handler.

Specified by:
getDTDHandler in interface XMLReader
Returns:
the DTDHandler
See Also:
XMLReader.setDTDHandler(org.xml.sax.DTDHandler)

setDTDHandler

public void setDTDHandler(DTDHandler dtdHandler)
Sets the DTDHandler.

Specified by:
setDTDHandler in interface XMLReader
Parameters:
dtdHandler - The DTD handler.
See Also:
XMLReader.getDTDHandler()

getErrorHandler

public ErrorHandler getErrorHandler()
Description copied from interface: XMLReader
Return the current error handler.

Specified by:
getErrorHandler in interface XMLReader
Returns:
the ErrorHandler
See Also:
XMLReader.setErrorHandler(org.xml.sax.ErrorHandler)

setErrorHandler

public void setErrorHandler(ErrorHandler errorHandler)
Sets the ErrorHandler.

Specified by:
setErrorHandler in interface XMLReader
Parameters:
errorHandler - The error handler.
See Also:
XMLReader.getErrorHandler()

getEntityResolver

public EntityResolver getEntityResolver()
Description copied from interface: XMLReader
Return the current entity resolver.

Specified by:
getEntityResolver in interface XMLReader
Returns:
the EntityResolver used when a Document contains a DTD
See Also:
XMLReader.setEntityResolver(org.xml.sax.EntityResolver)

setEntityResolver

public void setEntityResolver(EntityResolver entityResolver)
Sets the EntityResolver .

Specified by:
setEntityResolver in interface XMLReader
Parameters:
entityResolver - is the EntityResolver
See Also:
XMLReader.getEntityResolver()

getLexicalHandler

public LexicalHandler getLexicalHandler()
Returns:
the LexicalHandler used when a Document contains a DTD

setLexicalHandler

public void setLexicalHandler(LexicalHandler lexicalHandler)
Sets the LexicalHandler .

Parameters:
lexicalHandler - is the LexicalHandler

setXMLReader

public void setXMLReader(XMLReader xmlReader)
Sets the XMLReader used to write SAX events to

Parameters:
xmlReader - is the XMLReader

getFeature

public boolean getFeature(java.lang.String name)
                   throws SAXNotRecognizedException,
                          SAXNotSupportedException
Looks up the value of a feature.

Specified by:
getFeature in interface XMLReader
Parameters:
name - The feature name, which is a fully-qualified URI.
Returns:
The current value of the feature (true or false).
Throws:
SAXNotRecognizedException - If the feature value can't be assigned or retrieved.
SAXNotSupportedException - When the XMLReader recognizes the feature name but cannot determine its value at this time.
See Also:
XMLReader.setFeature(java.lang.String, boolean)

setFeature

public void setFeature(java.lang.String name,
                       boolean value)
                throws SAXNotRecognizedException,
                       SAXNotSupportedException
This implementation does actually use any features but just stores them for later retrieval

Specified by:
setFeature in interface XMLReader
Parameters:
name - The feature name, which is a fully-qualified URI.
value - The requested value of the feature (true or false).
Throws:
SAXNotRecognizedException - If the feature value can't be assigned or retrieved.
SAXNotSupportedException - When the XMLReader recognizes the feature name but cannot set the requested value.
See Also:
XMLReader.getFeature(java.lang.String)

setProperty

public void setProperty(java.lang.String name,
                        java.lang.Object value)
                 throws SAXNotRecognizedException,
                        SAXNotSupportedException
Sets the given SAX property

Specified by:
setProperty in interface XMLReader
Parameters:
name - The property name, which is a fully-qualified URI.
value - The requested value for the property.
Throws:
SAXNotRecognizedException - If the property value can't be assigned or retrieved.
SAXNotSupportedException - When the XMLReader recognizes the property name but cannot set the requested value.

getProperty

public java.lang.Object getProperty(java.lang.String name)
                             throws SAXNotRecognizedException,
                                    SAXNotSupportedException
Gets the given SAX property

Specified by:
getProperty in interface XMLReader
Parameters:
name - The property name, which is a fully-qualified URI.
Returns:
The current value of the property.
Throws:
SAXNotSupportedException - When the XMLReader recognizes the property name but cannot determine its value at this time.
SAXNotRecognizedException - If the property value can't be assigned or retrieved.
See Also:
XMLReader.setProperty(java.lang.String, java.lang.Object)

parse

public void parse(java.lang.String systemId)
           throws SAXNotSupportedException
This method is not supported.

Specified by:
parse in interface XMLReader
Parameters:
systemId - The system identifier (URI).
Throws:
SAXNotSupportedException
See Also:
XMLReader.parse(org.xml.sax.InputSource)

parse

public void parse(InputSource input)
           throws SAXException
Parses an XML document. This method can only accept DocumentInputSource inputs otherwise a SAXNotSupportedException exception is thrown.

Specified by:
parse in interface XMLReader
Throws:
SAXNotSupportedException - if the input source is not wrapping a dom4j document
SAXException - Any SAX exception, possibly wrapping another exception.
See Also:
InputSource, XMLReader.parse(java.lang.String), XMLReader.setEntityResolver(org.xml.sax.EntityResolver), XMLReader.setDTDHandler(org.xml.sax.DTDHandler), XMLReader.setContentHandler(org.xml.sax.ContentHandler), XMLReader.setErrorHandler(org.xml.sax.ErrorHandler)

writeContent

protected void writeContent(Branch branch,
                            NamespaceStack namespaceStack)
                     throws SAXException
Throws:
SAXException

documentLocator

protected void documentLocator(Document document)
                        throws SAXException
The Locator is only really useful when parsing a textual document as its main purpose is to identify the line and column number. Since we are processing an in memory tree which will probably have its line number information removed, we'll just use -1 for the line and column numbers.

Throws:
SAXException

entityResolver

protected void entityResolver(Document document)
                       throws SAXException
Throws:
SAXException

dtdHandler

protected void dtdHandler(Document document)
                   throws SAXException
We do not yet support DTD or XML Schemas so this method does nothing right now.

Throws:
SAXException

startDocument

protected void startDocument()
                      throws SAXException
Throws:
SAXException

endDocument

protected void endDocument()
                    throws SAXException
Throws:
SAXException

write

protected void write(Element element,
                     NamespaceStack namespaceStack)
              throws SAXException
Throws:
SAXException

startPrefixMapping

protected AttributesImpl startPrefixMapping(Element element,
                                            NamespaceStack namespaceStack)
                                     throws SAXException
Fires a SAX startPrefixMapping event for all the namespaceStack which have just come into scope

Throws:
SAXException

endPrefixMapping

protected void endPrefixMapping(NamespaceStack namespaceStack,
                                int stackSize)
                         throws SAXException
Fires a SAX endPrefixMapping event for all the namespaceStack which have gone out of scope

Throws:
SAXException

startElement

protected void startElement(Element element,
                            AttributesImpl namespaceAttributes)
                     throws SAXException
Throws:
SAXException

endElement

protected void endElement(Element element)
                   throws SAXException
Throws:
SAXException

createAttributes

protected Attributes createAttributes(Element element,
                                      Attributes namespaceAttributes)
                               throws SAXException
Throws:
SAXException

addNamespaceAttribute

protected AttributesImpl addNamespaceAttribute(AttributesImpl namespaceAttributes,
                                               Namespace namespace)
If isDelcareNamespaceAttributes() is enabled then this method will add the given namespace declaration to the supplied attributes object, creating one if it does not exist.


isIgnoreableNamespace

protected boolean isIgnoreableNamespace(Namespace namespace,
                                        NamespaceStack namespaceStack)
Returns:
true if the given namespace is an ignorable namespace (such as Namespace.NO_NAMESPACE or Namespace.XML_NAMESPACE) or if the namespace has already been declared in the current scope

checkForNullHandlers

protected void checkForNullHandlers()
Ensures non-null content handlers?



Copyright © 2004 MetaStuff Ltd. All Rights Reserved. Hosted by

SourceForge Logo