nu.xom.samples
Class SourceCodeSerializer

java.lang.Object
  extended by nu.xom.Serializer
      extended by nu.xom.samples.SourceCodeSerializer

public class SourceCodeSerializer
extends nu.xom.Serializer

This class converts an XML document into XOM source code that creates the same XML document. It's often useful for building self-contained unit tests.

Version:
1.0
Author:
Elliotte Rusty Harold

Constructor Summary
SourceCodeSerializer(java.io.OutputStream out)
           
SourceCodeSerializer(java.io.OutputStream out, java.lang.String encoding)
           
 
Method Summary
static void main(java.lang.String[] args)
           
protected  void write(nu.xom.Attribute attribute)
           Writes an attribute in the form name="value".
protected  void write(nu.xom.Comment comment)
           Writes a comment onto the output stream using the current options.
protected  void write(nu.xom.DocType doctype)
           Writes a DocType object onto the output stream using the current options.
 void write(nu.xom.Document doc)
           Serializes a document onto the output stream using the current options.
protected  void write(nu.xom.ProcessingInstruction instruction)
           Writes a processing instruction onto the output stream using the current options.
protected  void write(nu.xom.Text text)
           Writes a Text object onto the output stream using the current options.
protected  void writeAttributes(nu.xom.Element element)
           Writes all the attributes of the specified element onto the output stream, one at a time, separated by white space.
protected  void writeEmptyElementTag(nu.xom.Element element)
           Writes an empty-element tag for the element including all its namespace declarations and attributes.
protected  void writeEndTag(nu.xom.Element element)
           Writes the end-tag for an element in the form </name>.
protected  void writeNamespaceDeclarations(nu.xom.Element element)
           Writes all the namespace declaration attributes of the specified element onto the output stream, one at a time, separated by white space.
protected  void writeStartTag(nu.xom.Element element)
           Writes the start-tag for the element including all its namespace declarations and attributes.
 
Methods inherited from class nu.xom.Serializer
breakLine, flush, getColumnNumber, getEncoding, getIndent, getLineSeparator, getMaxLength, getPreserveBaseURI, getUnicodeNormalizationFormC, setIndent, setLineSeparator, setMaxLength, setOutputStream, setPreserveBaseURI, setUnicodeNormalizationFormC, write, writeAttributeValue, writeChild, writeEscaped, writeNamespaceDeclaration, writeRaw, writeXMLDeclaration
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SourceCodeSerializer

public SourceCodeSerializer(java.io.OutputStream out)

SourceCodeSerializer

public SourceCodeSerializer(java.io.OutputStream out,
                            java.lang.String encoding)
                     throws java.io.UnsupportedEncodingException
Throws:
java.io.UnsupportedEncodingException
Method Detail

write

public void write(nu.xom.Document doc)
           throws java.io.IOException
Description copied from class: nu.xom.Serializer

Serializes a document onto the output stream using the current options.

Overrides:
write in class nu.xom.Serializer
Parameters:
doc - the Document to serialize
Throws:
java.io.IOException - if the underlying output stream encounters an I/O error

writeStartTag

protected void writeStartTag(nu.xom.Element element)
                      throws java.io.IOException
Description copied from class: nu.xom.Serializer

Writes the start-tag for the element including all its namespace declarations and attributes.

The writeAttributes method is called to write all the non-namespace-declaration attributes. The writeNamespaceDeclarations method is called to write all the namespace declaration attributes.

Overrides:
writeStartTag in class nu.xom.Serializer
Parameters:
element - the element whose start-tag is written
Throws:
java.io.IOException - if the underlying output stream encounters an I/O error

writeEndTag

protected void writeEndTag(nu.xom.Element element)
                    throws java.io.IOException
Description copied from class: nu.xom.Serializer

Writes the end-tag for an element in the form </name>.

Overrides:
writeEndTag in class nu.xom.Serializer
Parameters:
element - the element whose end-tag is written
Throws:
java.io.IOException - if the underlying output stream encounters an I/O error

writeEmptyElementTag

protected void writeEmptyElementTag(nu.xom.Element element)
                             throws java.io.IOException
Description copied from class: nu.xom.Serializer

Writes an empty-element tag for the element including all its namespace declarations and attributes.

The writeAttributes method is called to write all the non-namespace-declaration attributes. The writeNamespaceDeclarations method is called to write all the namespace declaration attributes.

If subclasses don't wish empty-element tags to be used, they can override this method to simply invoke writeStartTag followed by writeEndTag.

Overrides:
writeEmptyElementTag in class nu.xom.Serializer
Parameters:
element - the element whose empty-element tag is written
Throws:
java.io.IOException - if the underlying output stream encounters an I/O error

writeAttributes

protected void writeAttributes(nu.xom.Element element)
                        throws java.io.IOException
Description copied from class: nu.xom.Serializer

Writes all the attributes of the specified element onto the output stream, one at a time, separated by white space. If preserveBaseURI is true, and it is necessary to add an xml:base attribute to the element in order to preserve the base URI, then that attribute is also written here. Each individual attribute is written by invoking write(Attribute).

Overrides:
writeAttributes in class nu.xom.Serializer
Parameters:
element - the Element whose attributes are written
Throws:
java.io.IOException - if the underlying output stream encounters an I/O error

write

protected void write(nu.xom.Attribute attribute)
              throws java.io.IOException
Description copied from class: nu.xom.Serializer

Writes an attribute in the form name="value". Characters in the attribute value are escaped as necessary.

Overrides:
write in class nu.xom.Serializer
Parameters:
attribute - the Attribute to write
Throws:
java.io.IOException - if the underlying output stream encounters an I/O error

writeNamespaceDeclarations

protected void writeNamespaceDeclarations(nu.xom.Element element)
                                   throws java.io.IOException
Description copied from class: nu.xom.Serializer

Writes all the namespace declaration attributes of the specified element onto the output stream, one at a time, separated by white space. Each individual declaration is written by invoking writeNamespaceDeclaration.

Overrides:
writeNamespaceDeclarations in class nu.xom.Serializer
Parameters:
element - the Element whose namespace declarations are written
Throws:
java.io.IOException - if the underlying output stream encounters an I/O error

write

protected void write(nu.xom.ProcessingInstruction instruction)
              throws java.io.IOException
Description copied from class: nu.xom.Serializer

Writes a processing instruction onto the output stream using the current options. Since character and entity references are not resolved in processing instructions, processing instructions can only be serialized when all characters they contain are available in the current encoding.

Overrides:
write in class nu.xom.Serializer
Parameters:
instruction - the ProcessingInstruction to serialize
Throws:
java.io.IOException - if the underlying output stream encounters an I/O error

write

protected void write(nu.xom.DocType doctype)
              throws java.io.IOException
Description copied from class: nu.xom.Serializer

Writes a DocType object onto the output stream using the current options.

Overrides:
write in class nu.xom.Serializer
Parameters:
doctype - the document type declaration to serialize
Throws:
java.io.IOException - if the underlying output stream encounters an I/O error

write

protected void write(nu.xom.Comment comment)
              throws java.io.IOException
Description copied from class: nu.xom.Serializer

Writes a comment onto the output stream using the current options. Since character and entity references are not resolved in comments, comments can only be serialized when all characters they contain are available in the current encoding.

Overrides:
write in class nu.xom.Serializer
Parameters:
comment - the Comment to serialize
Throws:
java.io.IOException - if the underlying output stream encounters an I/O error

write

protected void write(nu.xom.Text text)
              throws java.io.IOException
Description copied from class: nu.xom.Serializer

Writes a Text object onto the output stream using the current options. Reserved characters such as <, > and " are escaped using the standard entity references such as &lt;, &gt;, and &quot;.

Characters which cannot be encoded in the current character set (for example, Ω in ISO-8859-1) are encoded using character references.

Overrides:
write in class nu.xom.Serializer
Parameters:
text - the Text to serialize
Throws:
java.io.IOException - if the underlying output stream encounters an I/O error

main

public static void main(java.lang.String[] args)


Copyright 2002-2005 Elliotte Rusty Harold
elharo@metalab.unc.edu