org.freecompany.xmlwriter

Class XmlWriter

public class XmlWriter extends FilterWriter implements ContentHandler, LexicalHandler

A Writer for XML documents. In concept, an XmlWriter is a SAX ContentHandler that simply writes the corresponding events as an XML document.
Field Summary
protected intdepth
protected CharSequenceencoding
protected booleanendElementGenerated
protected booleanindented
protected Locatorlocator
static intNO_AUTOFLUSH
A value for the autoflush threshold that disables the feature.
protected Map<String,CharSequence>pending
protected Map<String,CharSequence>prefixes
protected Booleanstandalone
protected booleanstartElementPending
State flags for internal use.
protected intthreshold
protected CharSequenceversion
protected static StringXMLNS
Constructor Summary
XmlWriter(Writer out)
Creates an XmlWriter that wraps another writer.
Method Summary
XmlWriterappend(Node root)
Writes a DOM subtree.
XmlWriterappendInternal(char value)
Override to convert exception types to SAX.
XmlWriterappendInternal(CharSequence value)
Override to convert exception types to SAX.
voidcData(CharSequence s)
Creates a CDATA section in the output.
voidcharacters(char[] characters, int offset, int length)
Writes text to the output.
voidcharacters(CharSequence s)
Writes character data while performing escapes as defined in the XML 1.0 specification, section 2.4.
voidcomment(char[] characters, int offset, int index)
voidcreateElement(QName qname)
Creates an empty element with no attributes.
voidcreateElement(QName qname, CharSequence content)
Creates an element with no attributes and the specified text content.
voidcreateElement(QName qname, Attributes attrs)
Creates an empty element with the specified attributes.
voidcreateElement(QName qname, Attributes attrs, CharSequence content)
Creates an element with the specified attributes and text content.
voidcreateElement(CharSequence uri, CharSequence name)
Creates an empty element with no attributes.
voidcreateElement(CharSequence uri, CharSequence name, CharSequence content)
Creates an element with no attributes and the specified text content.
voidcreateElement(CharSequence uri, CharSequence name, Attributes attrs)
Creates an empty element with the specified attributes.
voidcreateElement(CharSequence uri, CharSequence name, Attributes attrs, CharSequence content)
Creates an element with the specified attributes and text content.
voidendCDATA()
Ends a CDATA section in the output.
voidendDocument()
voidendDTD()
voidendElement(QName qname)
Ends an element.
voidendElement(String uri, String name)
Ends an element.
voidendElement(String uri, String name, String qname)
Ends an element.
voidendEntity(String name)
voidendPrefixMapping(String prefix)
Adds a set of namespace mappings to the current namespace context.
protected voidfinishPendingStartElement()
protected voidflushInternal()
intgetAutoFlush()
Gets the autoflush threshold.
intgetDepth()
Gets the current output tree depth.
CharSequencegetEncoding()
BooleangetStandalone()
CharSequencegetVersion()
voidignorableWhitespace(char[] characters, int offset, int length)
protected voidindent(int depth)
booleanisIndented()
voidprocessingInstruction(String target, String data)
voidsetAutoFlush(int threshold)
Sets the document depth at which the underlying writer is automatically flushed.
voidsetDocumentLocator(Locator locator)
voidsetEncoding(CharSequence encoding)
voidsetIndented(boolean indented)
voidsetStandalone(Boolean standalone)
voidsetVersion(CharSequence version)
voidskippedEntity(String name)
voidstartCDATA()
Starts a CDATA section in the output.
voidstartDocument()
voidstartDTD(String name, String publicid, String systemid)
voidstartElement(QName qname)
Start an element with no attributes.
voidstartElement(QName qname, Attributes attrs)
Start an element with the specified attributes.
voidstartElement(String uri, String name)
Start an element with no attributes.
voidstartElement(String uri, String name, String qname)
Start an element with no attributes.
voidstartElement(String uri, String name, String qname, Attributes attrs)
Start an element with the specified attributes.
voidstartEntity(String name)
voidstartPrefixMapping(String prefix, String uri)
Adds a namespace mapping to the current namespace context.
protected voidwriteAttributes(Attributes attrs)
protected voidwriteNamespace(CharSequence uri, CharSequence prefix)
protected voidwriteQName(CharSequence uri, CharSequence name)
protected voidwriteWithEscapes(CharSequence value, CharSequence charsToEscape)

Field Detail

depth

protected int depth

encoding

protected CharSequence encoding

endElementGenerated

protected boolean endElementGenerated

indented

protected boolean indented

locator

protected Locator locator

NO_AUTOFLUSH

public static final int NO_AUTOFLUSH
A value for the autoflush threshold that disables the feature.

pending

protected final Map<String,CharSequence> pending

prefixes

protected final Map<String,CharSequence> prefixes

standalone

protected Boolean standalone

startElementPending

protected boolean startElementPending
State flags for internal use.

threshold

protected int threshold

version

protected CharSequence version

XMLNS

protected static final String XMLNS

Constructor Detail

XmlWriter

public XmlWriter(Writer out)
Creates an XmlWriter that wraps another writer.

Parameters: out the wrapped writer

Method Detail

append

public XmlWriter append(Node root)
Writes a DOM subtree.

Parameters: root the root of the DOM subtree

Throws: IOException when the underlying writer does

appendInternal

public XmlWriter appendInternal(char value)
Override to convert exception types to SAX.

appendInternal

public XmlWriter appendInternal(CharSequence value)
Override to convert exception types to SAX.

cData

public void cData(CharSequence s)
Creates a CDATA section in the output.

Throws: IOException when the underlying writer does

characters

public void characters(char[] characters, int offset, int length)
Writes text to the output.

Throws: IOException when the underlying writer does

characters

public void characters(CharSequence s)
Writes character data while performing escapes as defined in the XML 1.0 specification, section 2.4.

comment

public void comment(char[] characters, int offset, int index)

createElement

public void createElement(QName qname)
Creates an empty element with no attributes.

Parameters: qname the qualified name of the element

Throws: IOException when the underlying writer does

createElement

public void createElement(QName qname, CharSequence content)
Creates an element with no attributes and the specified text content.

Parameters: qname the qualified name of the element content the text value of the element

Throws: IOException when the underlying writer does

createElement

public void createElement(QName qname, Attributes attrs)
Creates an empty element with the specified attributes.

Parameters: qname the qualified name of the element attrs the attribute names mapped to their corresponding values

Throws: IOException when the underlying writer does

createElement

public void createElement(QName qname, Attributes attrs, CharSequence content)
Creates an element with the specified attributes and text content.

Parameters: qname the qualified name of the element attrs the attribute names mapped to their corresponding values content the text value of the element

Throws: IOException when the underlying writer does

createElement

public void createElement(CharSequence uri, CharSequence name)
Creates an empty element with no attributes.

Parameters: uri the namespace URI of the element name the local name of the element

Throws: IOException when the underlying writer does

createElement

public void createElement(CharSequence uri, CharSequence name, CharSequence content)
Creates an element with no attributes and the specified text content.

Parameters: uri the namespace URI of the element name the local name of the element content the text value of the element

Throws: IOException when the underlying writer does

createElement

public void createElement(CharSequence uri, CharSequence name, Attributes attrs)
Creates an empty element with the specified attributes.

Parameters: uri the namespace URI of the element name the local name of the element attrs the attribute names mapped to their corresponding values

Throws: IOException when the underlying writer does

createElement

public void createElement(CharSequence uri, CharSequence name, Attributes attrs, CharSequence content)
Creates an element with the specified attributes and text content.

Parameters: uri the namespace URI of the element name the local name of the element attrs the attribute names mapped to their corresponding values content the text value of the element

Throws: IOException when the underlying writer does

endCDATA

public void endCDATA()
Ends a CDATA section in the output.

Throws: IOException when the underlying writer does

endDocument

public void endDocument()

endDTD

public void endDTD()

endElement

public void endElement(QName qname)
Ends an element.

Parameters: qname the qualified name of the element

Throws: IOException when the underlying writer does

endElement

public void endElement(String uri, String name)
Ends an element.

Parameters: uri the namespace URI of the element name the local name of the element

Throws: IOException when the underlying writer does

endElement

public void endElement(String uri, String name, String qname)
Ends an element.

Parameters: uri the namespace URI of the element name the local name of the element

Throws: IOException when the underlying writer does

endEntity

public void endEntity(String name)

endPrefixMapping

public void endPrefixMapping(String prefix)
Adds a set of namespace mappings to the current namespace context.

Parameters: map a map of namespace URIs to prefixes.

finishPendingStartElement

protected void finishPendingStartElement()

flushInternal

protected void flushInternal()

getAutoFlush

public int getAutoFlush()
Gets the autoflush threshold.

Returns: the current autoflush threshold

getDepth

public int getDepth()
Gets the current output tree depth.

getEncoding

public CharSequence getEncoding()

getStandalone

public Boolean getStandalone()

getVersion

public CharSequence getVersion()

ignorableWhitespace

public void ignorableWhitespace(char[] characters, int offset, int length)

indent

protected void indent(int depth)

isIndented

public boolean isIndented()

processingInstruction

public void processingInstruction(String target, String data)

setAutoFlush

public void setAutoFlush(int threshold)
Sets the document depth at which the underlying writer is automatically flushed.

Parameters: threshold the new threshold value

setDocumentLocator

public void setDocumentLocator(Locator locator)

setEncoding

public void setEncoding(CharSequence encoding)

setIndented

public void setIndented(boolean indented)

setStandalone

public void setStandalone(Boolean standalone)

setVersion

public void setVersion(CharSequence version)

skippedEntity

public void skippedEntity(String name)

startCDATA

public void startCDATA()
Starts a CDATA section in the output. The caller is responsible for ensuring that the content of the CDATA section does not contain the string "]]>"

Throws: IOException when the underlying writer does

startDocument

public void startDocument()

startDTD

public void startDTD(String name, String publicid, String systemid)

startElement

public void startElement(QName qname)
Start an element with no attributes.

Parameters: qname the qualified name of the element

Throws: IOException when the underlying writer does

startElement

public void startElement(QName qname, Attributes attrs)
Start an element with the specified attributes.

Parameters: qname the qualified name of the element attrs the attribute names mapped to their corresponding values

Throws: IOException when the underlying writer does

startElement

public void startElement(String uri, String name)
Start an element with no attributes.

Parameters: uri the namespace URI of the element name the local name of the element

Throws: IOException when the underlying writer does

startElement

public void startElement(String uri, String name, String qname)
Start an element with no attributes.

Parameters: uri the namespace URI of the element name the local name of the element

Throws: IOException when the underlying writer does

startElement

public void startElement(String uri, String name, String qname, Attributes attrs)
Start an element with the specified attributes.

Parameters: uri the namespace URI of the element name the local name of the element attrs the attribute names mapped to their corresponding values

Throws: IOException when the underlying writer does

startEntity

public void startEntity(String name)

startPrefixMapping

public void startPrefixMapping(String prefix, String uri)
Adds a namespace mapping to the current namespace context. The next start tag written to the output will include the specified namespace prefix mapping attribute.

Parameters: uri the URI of the new namespace prefix the prefix used when writing tags in the namespace

writeAttributes

protected void writeAttributes(Attributes attrs)

writeNamespace

protected void writeNamespace(CharSequence uri, CharSequence prefix)

writeQName

protected void writeQName(CharSequence uri, CharSequence name)

writeWithEscapes

protected void writeWithEscapes(CharSequence value, CharSequence charsToEscape)