Top Description Fields Constructors Methods
org.dom4j.io

public Class SAXWriter

extends Object
implements XMLReader
Class Inheritance
All Implemented Interfaces
org.xml.sax.XMLReader
Imports
java.io.IOException, java.util.HashMap, .Iterator, .List, .Map, org.dom4j.Attribute, .Branch, .CDATA, .CharacterData, .Comment, .Document, .DocumentType, .Element, .Entity, .Namespace, .Node, .ProcessingInstruction, .Text, org.dom4j.tree.NamespaceStack, org.xml.sax.Attributes, .ContentHandler, .DTDHandler, .EntityResolver, .ErrorHandler, .InputSource, .SAXException, .SAXNotRecognizedException, .SAXNotSupportedException, .XMLReader, org.xml.sax.ext.LexicalHandler, org.xml.sax.helpers.AttributesImpl, .LocatorImpl

SAXWriter writes a DOM4J tree to a SAX ContentHandler.

Author
James Strachan
Version
$Revision: 1.24 $

Field Summary

Modifier and TypeField and Description
private AttributesImpl
attributes

AttributesImpl used when generating the Attributes

private ContentHandler
contentHandler

ContentHandler to which SAX events are raised

private boolean
declareNamespaceAttributes

Whether namespace declarations are exported as attributes or not

private DTDHandler
dtdHandler

DTDHandler fired when a document has a DTD

private EntityResolver
entityResolver

EntityResolver fired when a document has a DTD

private ErrorHandler
protected static final String
protected static final String
private Map<String, Boolean>
features

Stores the features

protected static final String[]
private LexicalHandler
lexicalHandler

LexicalHandler fired on Entity and CDATA sections

private Map<String, Object>
properties

Stores the properties

Constructor Summary

AccessConstructor and Description
public
public
SAXWriter(ContentHandler contentHandler)

public
SAXWriter(ContentHandler contentHandler, LexicalHandler lexicalHandler)

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

Method Summary

Modifier and TypeMethod and Description
protected AttributesImpl

Returns:

DOCUMENT ME!
addNamespaceAttribute
(AttributesImpl
DOCUMENT ME!
attrs
,
Namespace
DOCUMENT ME!
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 ME!
document
)

The org.xml.sax.Locatoris 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 ME!
document
)

We do not yet support DTD or XML Schemas so this method does nothing right now.

protected void
protected void
protected void
endPrefixMapping(NamespaceStack
DOCUMENT ME!
stack
,
int
DOCUMENT ME!
stackSize
)

Fires a SAX endPrefixMapping event for all the namespaceStack which have gone out of scope

protected void
public ContentHandler

Returns:

the ContentHandler called when SAX events are raised
getContentHandler
()

Implements org.xml.sax.XMLReader.getContentHandler.

DOCUMENT ME!
public DTDHandler

Returns:

the DTDHandler
getDTDHandler
()

Implements org.xml.sax.XMLReader.getDTDHandler.

DOCUMENT ME!
public EntityResolver

Returns:

the EntityResolver used when a Document contains a DTD
getEntityResolver
()

Implements org.xml.sax.XMLReader.getEntityResolver.

DOCUMENT ME!
public ErrorHandler

Returns:

the ErrorHandler
getErrorHandler
()

Implements org.xml.sax.XMLReader.getErrorHandler.

DOCUMENT ME!
public boolean

Returns:

DOCUMENT ME!
getFeature
(String
DOCUMENT ME!
name
)

Implements org.xml.sax.XMLReader.getFeature.

Looks up the value of a feature.
public LexicalHandler

Returns:

the LexicalHandler used when a Document contains a DTD
getLexicalHandler
()

DOCUMENT ME!

public Object

Returns:

DOCUMENT ME!
getProperty
(String
DOCUMENT ME!
name
)

Implements org.xml.sax.XMLReader.getProperty.

Gets the given SAX property
public boolean

Returns:

DOCUMENT ME!
isDeclareNamespaceAttributes
()

Should namespace declarations be converted to "xmlns" attributes.

protected boolean

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
isIgnoreableNamespace
(Namespace
DOCUMENT ME!
namespace
,
NamespaceStack
DOCUMENT ME!
namespaceStack
)

DOCUMENT ME!

public void
parse(String
DOCUMENT ME!
systemId
)

Implements org.xml.sax.XMLReader.parse.

This method is not supported.
public void
parse(InputSource
DOCUMENT ME!
input
)

Implements org.xml.sax.XMLReader.parse.

Parses an XML document.
public void
setContentHandler(ContentHandler
is the ContentHandler called when SAX events are raised
contentHandler
)

Implements org.xml.sax.XMLReader.setContentHandler.

Sets the ContentHandler called when SAX events are raised
public void
setDeclareNamespaceAttributes(boolean
DOCUMENT ME!
declareNamespaceAttrs
)

Sets whether namespace declarations should be exported as "xmlns" attributes or not.

public void
setDTDHandler(DTDHandler
DOCUMENT ME!
handler
)

Implements org.xml.sax.XMLReader.setDTDHandler.

Sets the DTDHandler.
public void
setEntityResolver(EntityResolver
is the EntityResolver
entityResolver
)

Implements org.xml.sax.XMLReader.setEntityResolver.

Sets the EntityResolver.
public void
setErrorHandler(ErrorHandler
DOCUMENT ME!
errorHandler
)

Implements org.xml.sax.XMLReader.setErrorHandler.

Sets the ErrorHandler.
public void
setFeature(String
DOCUMENT ME!
name
,
boolean
DOCUMENT ME!
value
)

Implements org.xml.sax.XMLReader.setFeature.

This implementation does actually use any features but just stores them for later retrieval
public void
setLexicalHandler(LexicalHandler
is the LexicalHandler
lexicalHandler
)

Sets the LexicalHandler.

public void
setProperty(String
DOCUMENT ME!
name
,
Object
DOCUMENT ME!
value
)

Implements org.xml.sax.XMLReader.setProperty.

Sets the given SAX property
public void
setXMLReader(XMLReader
is the XMLReader
xmlReader
)

Sets the XMLReader used to write SAX events to

protected void
protected void
startElement(Element element, AttributesImpl namespaceAttributes)

protected AttributesImpl

Returns:

DOCUMENT ME!
startPrefixMapping
(Element
DOCUMENT ME!
element
,
NamespaceStack
DOCUMENT ME!
namespaceStack
)

Fires a SAX startPrefixMapping event for all the namespaceStack which have just come into scope

public void
write(Node
DOCUMENT ME!
node
)

A polymorphic method to write any Node to this SAX stream

public void
write(Document
is the Document to parse
document
)

Generates SAX events for the given Document and all its content

public void
write(Element
is the Element to parse
element
)

Generates SAX events for the given Element and all its content

public void
write(String
is the text to send to the SAX ContentHandler
text
)

Generates SAX events for the given text

public void
write(CDATA
is the CDATA to parse
cdata
)

Generates SAX events for the given CDATA

public void
write(Comment
is the Comment to parse
comment
)

Generates SAX events for the given Comment

public void
write(Entity
is the Entity to parse
entity
)

Generates SAX events for the given Entity

public void
write(ProcessingInstruction
is the ProcessingInstruction to parse
pi
)

Generates SAX events for the given ProcessingInstruction

protected void
write(Element element, NamespaceStack namespaceStack)

public void
writeClose(Element
Element to output.
element
)

Writes the closing tag of an Element

protected void
writeContent(Branch branch, NamespaceStack namespaceStack)

public void
writeOpen(Element
Element to output.
element
)

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

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

attributesback to summary
private AttributesImpl attributes

AttributesImpl used when generating the Attributes

contentHandlerback to summary
private ContentHandler contentHandler

ContentHandler to which SAX events are raised

declareNamespaceAttributesback to summary
private boolean declareNamespaceAttributes

Whether namespace declarations are exported as attributes or not

dtdHandlerback to summary
private DTDHandler dtdHandler

DTDHandler fired when a document has a DTD

entityResolverback to summary
private EntityResolver entityResolver

EntityResolver fired when a document has a DTD

errorHandlerback to summary
private ErrorHandler errorHandler
FEATURE_NAMESPACE_PREFIXESback to summary
protected static final String FEATURE_NAMESPACE_PREFIXES
FEATURE_NAMESPACESback to summary
protected static final String FEATURE_NAMESPACES
featuresback to summary
private Map<String, Boolean> features

Stores the features

LEXICAL_HANDLER_NAMESback to summary
protected static final String[] LEXICAL_HANDLER_NAMES
lexicalHandlerback to summary
private LexicalHandler lexicalHandler

LexicalHandler fired on Entity and CDATA sections

propertiesback to summary
private Map<String, Object> properties

Stores the properties

Constructor Detail

SAXWriterback to summary
public SAXWriter()
SAXWriterback to summary
public SAXWriter(ContentHandler contentHandler)
SAXWriterback to summary
public SAXWriter(ContentHandler contentHandler, LexicalHandler lexicalHandler)
SAXWriterback to summary
public SAXWriter(ContentHandler contentHandler, LexicalHandler lexicalHandler, EntityResolver entityResolver)

Method Detail

addNamespaceAttributeback to summary
protected AttributesImpl addNamespaceAttribute(AttributesImpl attrs, 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.

Parameters
attrs:AttributesImpl

DOCUMENT ME!

namespace:Namespace

DOCUMENT ME!

Returns:AttributesImpl

DOCUMENT ME!

checkForNullHandlersback to summary
protected void checkForNullHandlers()

Ensures non-null content handlers?

createAttributesback to summary
protected Attributes createAttributes(Element element, Attributes namespaceAttributes) throws SAXException
documentLocatorback to summary
protected void documentLocator(Document document) throws SAXException

The org.xml.sax.Locatoris 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.

Parameters
document:Document

DOCUMENT ME!

Exceptions
SAXException:
DOCUMENT ME!
dtdHandlerback to summary
protected void dtdHandler(Document document) throws SAXException

We do not yet support DTD or XML Schemas so this method does nothing right now.

Parameters
document:Document

DOCUMENT ME!

Exceptions
SAXException:
DOCUMENT ME!
endDocumentback to summary
protected void endDocument() throws SAXException
endElementback to summary
protected void endElement(Element element) throws SAXException
endPrefixMappingback to summary
protected void endPrefixMapping(NamespaceStack stack, int stackSize) throws SAXException

Fires a SAX endPrefixMapping event for all the namespaceStack which have gone out of scope

Parameters
stack:NamespaceStack

DOCUMENT ME!

stackSize:int

DOCUMENT ME!

Exceptions
SAXException:
DOCUMENT ME!
entityResolverback to summary
protected void entityResolver(Document document) throws SAXException
getContentHandlerback to summary
public ContentHandler getContentHandler()

Implements org.xml.sax.XMLReader.getContentHandler.

DOCUMENT ME!

Returns:ContentHandler

the ContentHandler called when SAX events are raised

getDTDHandlerback to summary
public DTDHandler getDTDHandler()

Implements org.xml.sax.XMLReader.getDTDHandler.

DOCUMENT ME!

Returns:DTDHandler

the DTDHandler

getEntityResolverback to summary
public EntityResolver getEntityResolver()

Implements org.xml.sax.XMLReader.getEntityResolver.

DOCUMENT ME!

Returns:EntityResolver

the EntityResolver used when a Document contains a DTD

getErrorHandlerback to summary
public ErrorHandler getErrorHandler()

Implements org.xml.sax.XMLReader.getErrorHandler.

DOCUMENT ME!

Returns:ErrorHandler

the ErrorHandler

getFeatureback to summary
public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException

Implements org.xml.sax.XMLReader.getFeature.

Looks up the value of a feature.

Parameters
name:String

DOCUMENT ME!

Returns:boolean

DOCUMENT ME!

Exceptions
SAXNotRecognizedException:
DOCUMENT ME!
SAXNotSupportedException:
DOCUMENT ME!
getLexicalHandlerback to summary
public LexicalHandler getLexicalHandler()

DOCUMENT ME!

Returns:LexicalHandler

the LexicalHandler used when a Document contains a DTD

getPropertyback to summary
public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException

Implements org.xml.sax.XMLReader.getProperty.

Gets the given SAX property

Parameters
name:String

DOCUMENT ME!

Returns:Object

DOCUMENT ME!

Exceptions
SAXNotRecognizedException:
DOCUMENT ME!
SAXNotSupportedException:
DOCUMENT ME!
isDeclareNamespaceAttributesback to summary
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"

Returns:boolean

DOCUMENT ME!

isIgnoreableNamespaceback to summary
protected boolean isIgnoreableNamespace(Namespace namespace, NamespaceStack namespaceStack)

DOCUMENT ME!

Parameters
namespace:Namespace

DOCUMENT ME!

namespaceStack:NamespaceStack

DOCUMENT ME!

Returns:boolean

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

parseback to summary
public void parse(String systemId) throws SAXNotSupportedException

Implements org.xml.sax.XMLReader.parse.

This method is not supported.

Parameters
systemId:String

DOCUMENT ME!

Exceptions
SAXNotSupportedException:
DOCUMENT ME!
parseback to summary
public void parse(InputSource input) throws SAXException

Implements org.xml.sax.XMLReader.parse.

Parses an XML document. This method can only accept DocumentInputSource inputs otherwise a SAXNotSupportedExceptionexception is thrown.

Parameters
input:InputSource

DOCUMENT ME!

Exceptions
SAXException:
DOCUMENT ME!
SAXNotSupportedException:
if the input source is not wrapping a dom4j document
setContentHandlerback to summary
public void setContentHandler(ContentHandler contentHandler)

Implements org.xml.sax.XMLReader.setContentHandler.

Sets the ContentHandler called when SAX events are raised

Parameters
contentHandler:ContentHandler

is the ContentHandler called when SAX events are raised

setDeclareNamespaceAttributesback to summary
public void setDeclareNamespaceAttributes(boolean declareNamespaceAttrs)

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"

Parameters
declareNamespaceAttrs:boolean

DOCUMENT ME!

setDTDHandlerback to summary
public void setDTDHandler(DTDHandler handler)

Implements org.xml.sax.XMLReader.setDTDHandler.

Sets the DTDHandler.

Parameters
handler:DTDHandler

DOCUMENT ME!

setEntityResolverback to summary
public void setEntityResolver(EntityResolver entityResolver)

Implements org.xml.sax.XMLReader.setEntityResolver.

Sets the EntityResolver.

Parameters
entityResolver:EntityResolver

is the EntityResolver

setErrorHandlerback to summary
public void setErrorHandler(ErrorHandler errorHandler)

Implements org.xml.sax.XMLReader.setErrorHandler.

Sets the ErrorHandler.

Parameters
errorHandler:ErrorHandler

DOCUMENT ME!

setFeatureback to summary
public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException

Implements org.xml.sax.XMLReader.setFeature.

This implementation does actually use any features but just stores them for later retrieval

Parameters
name:String

DOCUMENT ME!

value:boolean

DOCUMENT ME!

Exceptions
SAXNotRecognizedException:
DOCUMENT ME!
SAXNotSupportedException:
DOCUMENT ME!
setLexicalHandlerback to summary
public void setLexicalHandler(LexicalHandler lexicalHandler)

Sets the LexicalHandler.

Parameters
lexicalHandler:LexicalHandler

is the LexicalHandler

setPropertyback to summary
public void setProperty(String name, Object value)

Implements org.xml.sax.XMLReader.setProperty.

Sets the given SAX property

Parameters
name:String

DOCUMENT ME!

value:Object

DOCUMENT ME!

setXMLReaderback to summary
public void setXMLReader(XMLReader xmlReader)

Sets the XMLReader used to write SAX events to

Parameters
xmlReader:XMLReader

is the XMLReader

startDocumentback to summary
protected void startDocument() throws SAXException
startElementback to summary
protected void startElement(Element element, AttributesImpl namespaceAttributes) throws SAXException
startPrefixMappingback to summary
protected AttributesImpl startPrefixMapping(Element element, NamespaceStack namespaceStack) throws SAXException

Fires a SAX startPrefixMapping event for all the namespaceStack which have just come into scope

Parameters
element:Element

DOCUMENT ME!

namespaceStack:NamespaceStack

DOCUMENT ME!

Returns:AttributesImpl

DOCUMENT ME!

Exceptions
SAXException:
DOCUMENT ME!
writeback to summary
public void write(Node node) throws SAXException

A polymorphic method to write any Node to this SAX stream

Parameters
node:Node

DOCUMENT ME!

Exceptions
SAXException:
DOCUMENT ME!
writeback to summary
public void write(Document document) throws SAXException

Generates SAX events for the given Document and all its content

Parameters
document:Document

is the Document to parse

Exceptions
SAXException:
if there is a SAX error processing the events
writeback to summary
public void write(Element element) throws SAXException

Generates SAX events for the given Element and all its content

Parameters
element:Element

is the Element to parse

Exceptions
SAXException:
if there is a SAX error processing the events
writeback to summary
public void write(String text) throws SAXException

Generates SAX events for the given text

Parameters
text:String

is the text to send to the SAX ContentHandler

Exceptions
SAXException:
if there is a SAX error processing the events
writeback to summary
public void write(CDATA cdata) throws SAXException

Generates SAX events for the given CDATA

Parameters
cdata:CDATA

is the CDATA to parse

Exceptions
SAXException:
if there is a SAX error processing the events
writeback to summary
public void write(Comment comment) throws SAXException

Generates SAX events for the given Comment

Parameters
comment:Comment

is the Comment to parse

Exceptions
SAXException:
if there is a SAX error processing the events
writeback to summary
public void write(Entity entity) throws SAXException

Generates SAX events for the given Entity

Parameters
entity:Entity

is the Entity to parse

Exceptions
SAXException:
if there is a SAX error processing the events
writeback to summary
public void write(ProcessingInstruction pi) throws SAXException

Generates SAX events for the given ProcessingInstruction

Parameters
pi:ProcessingInstruction

is the ProcessingInstruction to parse

Exceptions
SAXException:
if there is a SAX error processing the events
writeback to summary
protected void write(Element element, NamespaceStack namespaceStack) throws SAXException
writeCloseback to summary
public void writeClose(Element element) throws SAXException

Writes the closing tag of an Element

Parameters
element:Element

Element to output.

Exceptions
SAXException:
DOCUMENT ME!
writeContentback to summary
protected void writeContent(Branch branch, NamespaceStack namespaceStack) throws SAXException
writeOpenback to summary
public void writeOpen(Element element) throws SAXException

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

Parameters
element:Element

Element to output.

Exceptions
SAXException:
DOCUMENT ME!