org.freecompany.xmlwriter
Class XmlWriter

java.lang.Object
  extended by java.io.Writer
      extended by java.io.FilterWriter
          extended by org.freecompany.xmlwriter.XmlWriter
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable, java.lang.Appendable, org.xml.sax.ContentHandler, org.xml.sax.ext.LexicalHandler

public class XmlWriter
extends java.io.FilterWriter
implements org.xml.sax.ContentHandler, org.xml.sax.ext.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  int depth
           
protected  java.lang.CharSequence encoding
           
protected  boolean endElementGenerated
           
protected  boolean indented
           
protected  org.xml.sax.Locator locator
           
static int NO_AUTOFLUSH
          A value for the autoflush threshold that disables the feature.
protected  java.util.Map<java.lang.String,java.lang.CharSequence> pending
           
protected  java.util.Map<java.lang.String,java.lang.CharSequence> prefixes
           
protected  java.lang.Boolean standalone
           
protected  boolean startElementPending
          State flags for internal use.
protected  int threshold
           
protected  java.lang.CharSequence version
           
protected static java.lang.String XMLNS
           
 
Fields inherited from class java.io.FilterWriter
out
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
XmlWriter(java.io.Writer out)
          Creates an XmlWriter that wraps another writer.
 
Method Summary
 XmlWriter append(org.w3c.dom.Node root)
          Writes a DOM subtree.
 XmlWriter appendInternal(char value)
          Override to convert exception types to SAX.
 XmlWriter appendInternal(java.lang.CharSequence value)
          Override to convert exception types to SAX.
 void cData(java.lang.CharSequence s)
          Creates a CDATA section in the output.
 void characters(char[] characters, int offset, int length)
          Writes text to the output.
 void characters(java.lang.CharSequence s)
          Writes character data while performing escapes as defined in the XML 1.0 specification, section 2.4.
 void comment(char[] characters, int offset, int index)
           
 void createElement(java.lang.CharSequence uri, java.lang.CharSequence name)
          Creates an empty element with no attributes.
 void createElement(java.lang.CharSequence uri, java.lang.CharSequence name, org.xml.sax.Attributes attrs)
          Creates an empty element with the specified attributes.
 void createElement(java.lang.CharSequence uri, java.lang.CharSequence name, org.xml.sax.Attributes attrs, java.lang.CharSequence content)
          Creates an element with the specified attributes and text content.
 void createElement(java.lang.CharSequence uri, java.lang.CharSequence name, java.lang.CharSequence content)
          Creates an element with no attributes and the specified text content.
 void createElement(javax.xml.namespace.QName qname)
          Creates an empty element with no attributes.
 void createElement(javax.xml.namespace.QName qname, org.xml.sax.Attributes attrs)
          Creates an empty element with the specified attributes.
 void createElement(javax.xml.namespace.QName qname, org.xml.sax.Attributes attrs, java.lang.CharSequence content)
          Creates an element with the specified attributes and text content.
 void createElement(javax.xml.namespace.QName qname, java.lang.CharSequence content)
          Creates an element with no attributes and the specified text content.
 void endCDATA()
          Ends a CDATA section in the output.
 void endDocument()
           
 void endDTD()
           
 void endElement(javax.xml.namespace.QName qname)
          Ends an element.
 void endElement(java.lang.String uri, java.lang.String name)
          Ends an element.
 void endElement(java.lang.String uri, java.lang.String name, java.lang.String qname)
          Ends an element.
 void endEntity(java.lang.String name)
           
 void endPrefixMapping(java.lang.String prefix)
          Adds a set of namespace mappings to the current namespace context.
protected  void finishPendingStartElement()
           
protected  void flushInternal()
           
 int getAutoFlush()
          Gets the autoflush threshold.
 int getDepth()
          Gets the current output tree depth.
 java.lang.CharSequence getEncoding()
           
 java.lang.Boolean getStandalone()
           
 java.lang.CharSequence getVersion()
           
 void ignorableWhitespace(char[] characters, int offset, int length)
           
protected  void indent(int depth)
           
 boolean isIndented()
           
 void processingInstruction(java.lang.String target, java.lang.String data)
           
 void setAutoFlush(int threshold)
          Sets the document depth at which the underlying writer is automatically flushed.
 void setDocumentLocator(org.xml.sax.Locator locator)
           
 void setEncoding(java.lang.CharSequence encoding)
           
 void setIndented(boolean indented)
           
 void setStandalone(java.lang.Boolean standalone)
           
 void setVersion(java.lang.CharSequence version)
           
 void skippedEntity(java.lang.String name)
           
 void startCDATA()
          Starts a CDATA section in the output.
 void startDocument()
           
 void startDTD(java.lang.String name, java.lang.String publicid, java.lang.String systemid)
           
 void startElement(javax.xml.namespace.QName qname)
          Start an element with no attributes.
 void startElement(javax.xml.namespace.QName qname, org.xml.sax.Attributes attrs)
          Start an element with the specified attributes.
 void startElement(java.lang.String uri, java.lang.String name)
          Start an element with no attributes.
 void startElement(java.lang.String uri, java.lang.String name, java.lang.String qname)
          Start an element with no attributes.
 void startElement(java.lang.String uri, java.lang.String name, java.lang.String qname, org.xml.sax.Attributes attrs)
          Start an element with the specified attributes.
 void startEntity(java.lang.String name)
           
 void startPrefixMapping(java.lang.String prefix, java.lang.String uri)
          Adds a namespace mapping to the current namespace context.
protected  void writeAttributes(org.xml.sax.Attributes attrs)
           
protected  void writeNamespace(java.lang.CharSequence uri, java.lang.CharSequence prefix)
           
protected  void writeQName(java.lang.CharSequence uri, java.lang.CharSequence name)
           
protected  void writeWithEscapes(java.lang.CharSequence value, java.lang.CharSequence charsToEscape)
           
 
Methods inherited from class java.io.FilterWriter
close, flush, write, write, write
 
Methods inherited from class java.io.Writer
append, append, append, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NO_AUTOFLUSH

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

See Also:
Constant Field Values

XMLNS

protected static final java.lang.String XMLNS
See Also:
Constant Field Values

prefixes

protected final java.util.Map<java.lang.String,java.lang.CharSequence> prefixes

pending

protected final java.util.Map<java.lang.String,java.lang.CharSequence> pending

locator

protected org.xml.sax.Locator locator

version

protected java.lang.CharSequence version

encoding

protected java.lang.CharSequence encoding

standalone

protected java.lang.Boolean standalone

indented

protected boolean indented

threshold

protected int threshold

startElementPending

protected boolean startElementPending
State flags for internal use.


endElementGenerated

protected boolean endElementGenerated

depth

protected int depth
Constructor Detail

XmlWriter

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

Parameters:
out - the wrapped writer
Method Detail

startPrefixMapping

public void startPrefixMapping(java.lang.String prefix,
                               java.lang.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.

Specified by:
startPrefixMapping in interface org.xml.sax.ContentHandler
Parameters:
uri - the URI of the new namespace
prefix - the prefix used when writing tags in the namespace

endPrefixMapping

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

Specified by:
endPrefixMapping in interface org.xml.sax.ContentHandler
Parameters:
map - a map of namespace URIs to prefixes.

setAutoFlush

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

Parameters:
threshold - the new threshold value

getAutoFlush

public int getAutoFlush()
Gets the autoflush threshold.

Returns:
the current autoflush threshold

getDepth

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


startElement

public void startElement(javax.xml.namespace.QName qname)
                  throws org.xml.sax.SAXException
Start an element with no attributes.

Parameters:
qname - the qualified name of the element
Throws:
java.io.IOException - when the underlying writer does
org.xml.sax.SAXException

startElement

public void startElement(javax.xml.namespace.QName qname,
                         org.xml.sax.Attributes attrs)
                  throws org.xml.sax.SAXException
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:
java.io.IOException - when the underlying writer does
org.xml.sax.SAXException

startElement

public void startElement(java.lang.String uri,
                         java.lang.String name)
                  throws org.xml.sax.SAXException
Start an element with no attributes.

Parameters:
uri - the namespace URI of the element
name - the local name of the element
Throws:
java.io.IOException - when the underlying writer does
org.xml.sax.SAXException

startElement

public void startElement(java.lang.String uri,
                         java.lang.String name,
                         java.lang.String qname)
                  throws org.xml.sax.SAXException
Start an element with no attributes.

Parameters:
uri - the namespace URI of the element
name - the local name of the element
Throws:
java.io.IOException - when the underlying writer does
org.xml.sax.SAXException

startElement

public void startElement(java.lang.String uri,
                         java.lang.String name,
                         java.lang.String qname,
                         org.xml.sax.Attributes attrs)
                  throws org.xml.sax.SAXException
Start an element with the specified attributes.

Specified by:
startElement in interface org.xml.sax.ContentHandler
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:
java.io.IOException - when the underlying writer does
org.xml.sax.SAXException

indent

protected void indent(int depth)
               throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException

endElement

public void endElement(javax.xml.namespace.QName qname)
                throws org.xml.sax.SAXException
Ends an element.

Parameters:
qname - the qualified name of the element
Throws:
java.io.IOException - when the underlying writer does
org.xml.sax.SAXException

endElement

public void endElement(java.lang.String uri,
                       java.lang.String name)
                throws org.xml.sax.SAXException
Ends an element.

Parameters:
uri - the namespace URI of the element
name - the local name of the element
Throws:
java.io.IOException - when the underlying writer does
org.xml.sax.SAXException

endElement

public void endElement(java.lang.String uri,
                       java.lang.String name,
                       java.lang.String qname)
                throws org.xml.sax.SAXException
Ends an element.

Specified by:
endElement in interface org.xml.sax.ContentHandler
Parameters:
uri - the namespace URI of the element
name - the local name of the element
Throws:
java.io.IOException - when the underlying writer does
org.xml.sax.SAXException

finishPendingStartElement

protected void finishPendingStartElement()
                                  throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException

createElement

public void createElement(javax.xml.namespace.QName qname)
                   throws org.xml.sax.SAXException
Creates an empty element with no attributes.

Parameters:
qname - the qualified name of the element
Throws:
java.io.IOException - when the underlying writer does
org.xml.sax.SAXException

createElement

public void createElement(javax.xml.namespace.QName qname,
                          java.lang.CharSequence content)
                   throws org.xml.sax.SAXException
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:
java.io.IOException - when the underlying writer does
org.xml.sax.SAXException

createElement

public void createElement(javax.xml.namespace.QName qname,
                          org.xml.sax.Attributes attrs)
                   throws org.xml.sax.SAXException
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:
java.io.IOException - when the underlying writer does
org.xml.sax.SAXException

createElement

public void createElement(javax.xml.namespace.QName qname,
                          org.xml.sax.Attributes attrs,
                          java.lang.CharSequence content)
                   throws org.xml.sax.SAXException
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:
java.io.IOException - when the underlying writer does
org.xml.sax.SAXException

createElement

public void createElement(java.lang.CharSequence uri,
                          java.lang.CharSequence name)
                   throws org.xml.sax.SAXException
Creates an empty element with no attributes.

Parameters:
uri - the namespace URI of the element
name - the local name of the element
Throws:
java.io.IOException - when the underlying writer does
org.xml.sax.SAXException

createElement

public void createElement(java.lang.CharSequence uri,
                          java.lang.CharSequence name,
                          java.lang.CharSequence content)
                   throws org.xml.sax.SAXException
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:
java.io.IOException - when the underlying writer does
org.xml.sax.SAXException

createElement

public void createElement(java.lang.CharSequence uri,
                          java.lang.CharSequence name,
                          org.xml.sax.Attributes attrs)
                   throws org.xml.sax.SAXException
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:
java.io.IOException - when the underlying writer does
org.xml.sax.SAXException

createElement

public void createElement(java.lang.CharSequence uri,
                          java.lang.CharSequence name,
                          org.xml.sax.Attributes attrs,
                          java.lang.CharSequence content)
                   throws org.xml.sax.SAXException
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:
java.io.IOException - when the underlying writer does
org.xml.sax.SAXException

writeQName

protected void writeQName(java.lang.CharSequence uri,
                          java.lang.CharSequence name)
                   throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException

writeNamespace

protected void writeNamespace(java.lang.CharSequence uri,
                              java.lang.CharSequence prefix)
                       throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException

writeAttributes

protected void writeAttributes(org.xml.sax.Attributes attrs)
                        throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException

writeWithEscapes

protected void writeWithEscapes(java.lang.CharSequence value,
                                java.lang.CharSequence charsToEscape)
                         throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException

append

public XmlWriter append(org.w3c.dom.Node root)
                 throws org.xml.sax.SAXException
Writes a DOM subtree.

Parameters:
root - the root of the DOM subtree
Throws:
java.io.IOException - when the underlying writer does
org.xml.sax.SAXException

flushInternal

protected void flushInternal()
                      throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException

startCDATA

public void startCDATA()
                throws org.xml.sax.SAXException
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 "]]>"

Specified by:
startCDATA in interface org.xml.sax.ext.LexicalHandler
Throws:
java.io.IOException - when the underlying writer does
org.xml.sax.SAXException

endCDATA

public void endCDATA()
              throws org.xml.sax.SAXException
Ends a CDATA section in the output.

Specified by:
endCDATA in interface org.xml.sax.ext.LexicalHandler
Throws:
java.io.IOException - when the underlying writer does
org.xml.sax.SAXException

cData

public void cData(java.lang.CharSequence s)
           throws org.xml.sax.SAXException
Creates a CDATA section in the output.

Throws:
java.io.IOException - when the underlying writer does
org.xml.sax.SAXException

comment

public void comment(char[] characters,
                    int offset,
                    int index)
             throws org.xml.sax.SAXException
Specified by:
comment in interface org.xml.sax.ext.LexicalHandler
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

endDocument

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

setDocumentLocator

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

ignorableWhitespace

public void ignorableWhitespace(char[] characters,
                                int offset,
                                int length)
                         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 target,
                                  java.lang.String data)
                           throws org.xml.sax.SAXException
Specified by:
processingInstruction in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

skippedEntity

public void skippedEntity(java.lang.String name)
Specified by:
skippedEntity in interface org.xml.sax.ContentHandler

startEntity

public void startEntity(java.lang.String name)
Specified by:
startEntity in interface org.xml.sax.ext.LexicalHandler

endEntity

public void endEntity(java.lang.String name)
Specified by:
endEntity in interface org.xml.sax.ext.LexicalHandler

startDTD

public void startDTD(java.lang.String name,
                     java.lang.String publicid,
                     java.lang.String systemid)
Specified by:
startDTD in interface org.xml.sax.ext.LexicalHandler

endDTD

public void endDTD()
Specified by:
endDTD in interface org.xml.sax.ext.LexicalHandler

characters

public void characters(char[] characters,
                       int offset,
                       int length)
                throws org.xml.sax.SAXException
Writes text to the output.

Specified by:
characters in interface org.xml.sax.ContentHandler
Throws:
java.io.IOException - when the underlying writer does
org.xml.sax.SAXException

characters

public void characters(java.lang.CharSequence s)
                throws org.xml.sax.SAXException
Writes character data while performing escapes as defined in the XML 1.0 specification, section 2.4.

Throws:
org.xml.sax.SAXException

setVersion

public void setVersion(java.lang.CharSequence version)

getVersion

public java.lang.CharSequence getVersion()

setEncoding

public void setEncoding(java.lang.CharSequence encoding)

getEncoding

public java.lang.CharSequence getEncoding()

setStandalone

public void setStandalone(java.lang.Boolean standalone)

getStandalone

public java.lang.Boolean getStandalone()

setIndented

public void setIndented(boolean indented)

isIndented

public boolean isIndented()

appendInternal

public XmlWriter appendInternal(char value)
                         throws org.xml.sax.SAXException
Override to convert exception types to SAX.

Throws:
org.xml.sax.SAXException

appendInternal

public XmlWriter appendInternal(java.lang.CharSequence value)
                         throws org.xml.sax.SAXException
Override to convert exception types to SAX.

Throws:
org.xml.sax.SAXException