com.novell.xml.dom
Class DOMWriter

java.lang.Object
  extended bycom.novell.xml.dom.DOMWriter
All Implemented Interfaces:
XmlWriter

public class DOMWriter
extends Object
implements XmlWriter

This class is a serializer for the DOM. It takes a DOM level-1.0 node and outputs it as XML. If the java.io.Writer object passed to the constructor is of type java.io.OutputStreamWriter or com.novell.io.EncodingStreamWriter then encoding into different character sets can be performed. This class also provides methods to set serialization options corresponding to the XSLT xsl:output element attributes.


Field Summary
protected  int baseIndentLevel
           
protected  boolean cdataOutput
           
protected  Collection cdataSectionElements
           
protected  boolean disableTextEscaping
           
protected  String doctypePublic
           
protected  String doctypeSystem
           
protected  String encoding
           
protected  Node firstDocChild
           
protected  boolean indent
           
protected  int indentLevel
           
protected  String indentString
           
protected  Node root
           
protected  Stack stack
           
protected  boolean standalone
           
protected  boolean writeDeclaration
           
protected  Writer writer
           
protected static int XML_SPACE_DEFAULT
           
protected static int XML_SPACE_PRESERVE
           
protected  int xmlSpace
           
protected static Integer xmlSpaceDefault
           
protected static Integer xmlSpacePreserve
           
 
Constructor Summary
DOMWriter(Node node, OutputStream outputStream, String encoding)
          Construct a DOMWriter for the passed OutputStream, using the passed encoding, if possible.
DOMWriter(Node node, Writer writer)
          Construct a DOMWriter object for a given java.io.Writer object.
 
Method Summary
 void flush()
          Flush the underlying writer attached to this DOMWriter
 Collection getCdataSectionElements()
          Get the list of element tag names whose text content should be output as CDATA sections.
 boolean getDisableTextEscaping()
          Return non-zero if XML text escaping is disabled for this XmlWriter.
 String getDoctypePublic()
          Return the DOCTYPE PUBLIC id to use for the DTD declaration
 String getDoctypeSystem()
          Return the DOCTYPE SYSTEM id to use for the XML-declaration
 String getEncoding()
          Get the encoding string that will be specified in the XML declaration, if a declaration is output
 boolean getIndent()
          Return if this object will inject whitespace to "pretty print" the serialization of a DOM tree
 boolean getStandalone()
          Return if this object will write a "standalone=yes" declaration in the XML-declaration.
 boolean getWriteDeclaration()
          Return if this object will write an XML-declaration when it serializes a DOM tree
protected  void injectWhitespace(Node node)
          Conditionally inject whitespace between elements, comments, and processing instructions to "pretty-print" the serialization.
protected  void popState()
           
protected  void pushState()
           
 void setBaseIndentLevel(int level)
          Set the the starting indentation level during "pretty print" serialization.
 void setCdataSectionElements(Collection cdataSectionElements)
          Set the list of element tag names whose text content should be output as CDATA sections.
 void setDisableTextEscaping(boolean disableTextEscaping)
          Set if XML text escaping is to be disabled for this XmlWriter.
 void setDoctypePublic(String doctypePublic)
          Set the DOCTYPE PUBLIC id to use in the DTD declaration Note that this will only be used if the DOCTYPE SYSTEM id is not null and non-empty (see setDoctypeSystem()), and that the public id overrides the system id.
 void setDoctypeSystem(String doctypeSystem)
          Set the DOCTYPE SYSTEM id to use in the DTD declaration Note that this will be overridden by the DOCTYPE PUBLIC id (see setDoctypePublic()), but must be set to use the public id.
 void setEncoding(String encoding)
          Set the encoding string that will be specified in the XML declaration, if a declaration is output.
 void setIndent(boolean indent)
          Set whether this object should inject whitespace to "pretty print" the serialization of a DOM tree.
 void setIndentString(String indentString)
          Set the string used to indent during "pretty print" serialization of a DOM tree.
 void setStandalone(boolean standalone)
          Set whether this object should write a "standalone=yes" declaration in the XML-declaration
 void setWriteDeclaration(boolean writeDeclaration)
          Set whether this object should output an XML-declaration when it serializes a DOM tree
protected  boolean useCdataOutput(Element element)
          Return whether or not CDATA section output should be used for text children of the passed element's name
 void write()
          Serialize a DOM subtree as XML
protected  void writeDeclaration()
          Write an XML declaration, optionally writing an encoding declaration and a standalone declaration.
protected  void writeDTD(Document node)
          Write a DTD declaration.
protected  void writeTree(Node node)
          Serialize the subtree with the passed node as root.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

root

protected Node root

firstDocChild

protected Node firstDocChild

writer

protected Writer writer

encoding

protected String encoding

indent

protected boolean indent

writeDeclaration

protected boolean writeDeclaration

standalone

protected boolean standalone

doctypePublic

protected String doctypePublic

doctypeSystem

protected String doctypeSystem

indentLevel

protected int indentLevel

disableTextEscaping

protected boolean disableTextEscaping

cdataSectionElements

protected Collection cdataSectionElements

cdataOutput

protected boolean cdataOutput

stack

protected Stack stack

XML_SPACE_DEFAULT

protected static final int XML_SPACE_DEFAULT
See Also:
Constant Field Values

XML_SPACE_PRESERVE

protected static final int XML_SPACE_PRESERVE
See Also:
Constant Field Values

xmlSpaceDefault

protected static final Integer xmlSpaceDefault

xmlSpacePreserve

protected static final Integer xmlSpacePreserve

xmlSpace

protected int xmlSpace

indentString

protected String indentString

baseIndentLevel

protected int baseIndentLevel
Constructor Detail

DOMWriter

public DOMWriter(Node node,
                 Writer writer)
Construct a DOMWriter object for a given java.io.Writer object. Use a com.novell.xml.util.XMLWriter for character encoding. Note that using a buffered writer will result in maximum efficiency.

Parameters:
node - The root of the DOM tree to output
writer - The writer object to use.

DOMWriter

public DOMWriter(Node node,
                 OutputStream outputStream,
                 String encoding)
          throws UnsupportedEncodingException
Construct a DOMWriter for the passed OutputStream, using the passed encoding, if possible.

Parameters:
node - The root of the DOM tree to output
outputStream - The OutputStream to which to write
encoding - The name of the character encoding to use
Method Detail

write

public void write()
           throws IOException
Serialize a DOM subtree as XML

Specified by:
write in interface XmlWriter
Throws:
IOException

flush

public void flush()
           throws IOException
Flush the underlying writer attached to this DOMWriter

Throws:
IOException

getWriteDeclaration

public boolean getWriteDeclaration()
Return if this object will write an XML-declaration when it serializes a DOM tree

Specified by:
getWriteDeclaration in interface XmlWriter
Returns:
True if an XML-declaration will be written

setWriteDeclaration

public void setWriteDeclaration(boolean writeDeclaration)
Set whether this object should output an XML-declaration when it serializes a DOM tree

Specified by:
setWriteDeclaration in interface XmlWriter
Parameters:
writeDeclaration - True if an XML-declaration should be written

getEncoding

public String getEncoding()
Get the encoding string that will be specified in the XML declaration, if a declaration is output

Specified by:
getEncoding in interface XmlWriter
Returns:
The encoding string

setEncoding

public void setEncoding(String encoding)
Set the encoding string that will be specified in the XML declaration, if a declaration is output. Note that it is the caller's responsibility to set up the Writer object passed to the DOMWriter constructor so that it actually outputs using the correct encoding.

Specified by:
setEncoding in interface XmlWriter
Parameters:
encoding - The encoding string to place in the xml declaration

getIndent

public boolean getIndent()
Return if this object will inject whitespace to "pretty print" the serialization of a DOM tree

Specified by:
getIndent in interface XmlWriter
Returns:
True if whitespace will be injected

setIndent

public void setIndent(boolean indent)
Set whether this object should inject whitespace to "pretty print" the serialization of a DOM tree.

Specified by:
setIndent in interface XmlWriter
Parameters:
indent - True if whitespace should be injected to "pretty print" the XML serialization of a DOM tree.

setIndentString

public void setIndentString(String indentString)
Set the string used to indent during "pretty print" serialization of a DOM tree. the default is the tab character. Typically this would either be the tab character or on or more spaces.

Parameters:
indentString - string to use for pretty print

setBaseIndentLevel

public void setBaseIndentLevel(int level)
Set the the starting indentation level during "pretty print" serialization.

Parameters:
level - level to initialize indentation to at start of serialization

getStandalone

public boolean getStandalone()
Return if this object will write a "standalone=yes" declaration in the XML-declaration.

Specified by:
getStandalone in interface XmlWriter
Returns:
True if standalone="yes" will be written in the XML-declaration

setStandalone

public void setStandalone(boolean standalone)
Set whether this object should write a "standalone=yes" declaration in the XML-declaration

Specified by:
setStandalone in interface XmlWriter
Parameters:
standalone - True if a standalone="yes" declaration should be written in the XML-declaration

getDoctypePublic

public String getDoctypePublic()
Return the DOCTYPE PUBLIC id to use for the DTD declaration

Specified by:
getDoctypePublic in interface XmlWriter
Returns:
The DOCTYPE PUBLIC id to use for the DTD declaration, or null if none will be used.

setDoctypePublic

public void setDoctypePublic(String doctypePublic)
Set the DOCTYPE PUBLIC id to use in the DTD declaration Note that this will only be used if the DOCTYPE SYSTEM id is not null and non-empty (see setDoctypeSystem()), and that the public id overrides the system id.

Specified by:
setDoctypePublic in interface XmlWriter
Parameters:
doctypePublic - The DOCTYPE PUBLIC id to use in the DTD declaration or null if none should be used.

getDoctypeSystem

public String getDoctypeSystem()
Return the DOCTYPE SYSTEM id to use for the XML-declaration

Specified by:
getDoctypeSystem in interface XmlWriter
Returns:
The DOCTYPE SYSTEM id to use for the XML-declaration, or null if none will be used.

setDoctypeSystem

public void setDoctypeSystem(String doctypeSystem)
Set the DOCTYPE SYSTEM id to use in the DTD declaration Note that this will be overridden by the DOCTYPE PUBLIC id (see setDoctypePublic()), but must be set to use the public id.

Specified by:
setDoctypeSystem in interface XmlWriter
Parameters:
doctypeSystem - The DOCTYPE SYSTEM id to use in the DTD declaration or null if none should be used.

getDisableTextEscaping

public boolean getDisableTextEscaping()
Return non-zero if XML text escaping is disabled for this XmlWriter. Disabling text escaping means that no character references or entity references will be output for text nodes.

Specified by:
getDisableTextEscaping in interface XmlWriter
Returns:
True if text escaping is disabled

setDisableTextEscaping

public void setDisableTextEscaping(boolean disableTextEscaping)
Set if XML text escaping is to be disabled for this XmlWriter. Disabling text escaping means that no character references or entity references will be output for text nodes.

Specified by:
setDisableTextEscaping in interface XmlWriter
Parameters:
disableTextEscaping - True if text escaping is disabled

getCdataSectionElements

public Collection getCdataSectionElements()
Get the list of element tag names whose text content should be output as CDATA sections.

Specified by:
getCdataSectionElements in interface XmlWriter
Returns:
A Collection of element tag names, each represented by an ExpandedQName object.

setCdataSectionElements

public void setCdataSectionElements(Collection cdataSectionElements)
Set the list of element tag names whose text content should be output as CDATA sections.

Specified by:
setCdataSectionElements in interface XmlWriter
Parameters:
cdataSectionElements - A Collection of ExpandedQName objects, each of which represents an element tag name.

writeDeclaration

protected void writeDeclaration()
                         throws IOException
Write an XML declaration, optionally writing an encoding declaration and a standalone declaration. The encoding declaration will be written if the member encoding is non-null and non-empty, and the standalone declaration will be written if the member "standalone" is true.

Throws:
IOException

writeDTD

protected void writeDTD(Document node)
                 throws IOException
Write a DTD declaration. The declaration will only be written if doctypeSystem is not null and non-empty. If doctypePublic is also non-null and non-empty, it will be used instead of doctypeSystem as the PUBLIC id. The name of the DTD will come from the DocumentType node, if there is one, otherwise will be set to the local name of the first element.

Parameters:
node - The Document node for the DOM tree.
Throws:
IOException

writeTree

protected void writeTree(Node node)
                  throws IOException
Serialize the subtree with the passed node as root. Note: A DTD will only be written if doctypeSystem is non-null and non-empty, and the passed node is of type Node.DOCUMENT_NODE.

Parameters:
node - The root of the subtree to output.
Throws:
IOException

injectWhitespace

protected void injectWhitespace(Node node)
                         throws IOException
Conditionally inject whitespace between elements, comments, and processing instructions to "pretty-print" the serialization.

Parameters:
node - The node before which to inject whitespace
Throws:
IOException

useCdataOutput

protected boolean useCdataOutput(Element element)
Return whether or not CDATA section output should be used for text children of the passed element's name

Parameters:
element - The element to check

pushState

protected void pushState()

popState

protected void popState()