com.topologi.diffx.xml
Class XMLStringWriter

java.lang.Object
  extended by com.topologi.diffx.xml.XMLStringWriter
All Implemented Interfaces:
XMLWriter

public final class XMLStringWriter
extends Object
implements XMLWriter

An XML which writes on to a string.

This XML writer is backed by a StringWriter and will defer the XML writer's method to either a XMLWriterImpl or XMLWriterNSImpl depending on whether namespace support is required.

The write methods do not throw any IOException.

If the write is not set to support namespaces, the method which require a namespace URI will throw an UnsupportedOperationException.

Version:
7 March 2012
Author:
Christophe Lauret

Constructor Summary
XMLStringWriter(boolean namespaces)
          Creates a new XML string writer.
XMLStringWriter(boolean namespaces, boolean indent)
          Create a new XML string writer.
 
Method Summary
 void attribute(String name, int value)
          Writes an attribute.
 void attribute(String name, String value)
          Writes an attribute.
 void attribute(String uri, String name, int value)
          Writes an attribute.
 void attribute(String uri, String name, String value)
          Writes an attribute.
 void close()
          Close the writer.
 void closeElement()
          Close the element automatically.
 void element(String name, String text)
          Opens element, inserts text node and closes.
 void emptyElement(String element)
          Writes an empty element.
 void emptyElement(String uri, String element)
          Writes an empty element.
 void flush()
          Flush the writer.
 void openElement(String name)
          Writes a start element tag correctly indented.
 void openElement(String name, boolean hasChildren)
          Writes a start element tag correctly indented.
 void openElement(String uri, String name, boolean hasChildren)
          Writes a start element tag correctly indented.
 void setIndentChars(String spaces)
          Sets the string to use for indentation.
 void setPrefixMapping(String uri, String prefix)
          Sets a prefix mapping.
 String toString()
          Returns the XML content as a String.
 void writeCDATA(String cdata)
          Writes the given text as a CDATA section.
 void writeComment(String comment)
          Writes an XML comment.
 void writePI(String target, String data)
          Writes an XML processing instruction.
 void writeText(char c)
          Writes the given character correctly for the encoding of this document.
 void writeText(char[] text, int off, int len)
          Write the given text correctly for the encoding of this document.
 void writeText(String text)
          Writes the given text correctly for the encoding of this document.
 void writeXML(char[] text, int off, int len)
          Write the given XML data.
 void writeXML(String text)
          Writes the given XML data.
 void xmlDecl()
          Writes the XML declaration.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

XMLStringWriter

public XMLStringWriter(boolean namespaces)

Creates a new XML string writer.

Parameters:
namespaces - Whether this XML writer should use namespaces.

XMLStringWriter

public XMLStringWriter(boolean namespaces,
                       boolean indent)

Create a new XML string writer.

Parameters:
namespaces - Whether this XML writer should use namespaces.
indent - Set the indentation flag.
Method Detail

xmlDecl

public void xmlDecl()
Description copied from interface: XMLWriter
Writes the XML declaration.

Always:

   <?xml version="1.0" encoding="encoding"?>
 

It is followed by a new line character if the indentation is turned on.

Specified by:
xmlDecl in interface XMLWriter

setIndentChars

public void setIndentChars(String spaces)
Description copied from interface: XMLWriter
Sets the string to use for indentation.

The string must be only composed of valid spaces characters.

If the string is null then the indentation is turned off.

Specified by:
setIndentChars in interface XMLWriter
Parameters:
spaces - The indentation string to use.
See Also:
Character.isSpaceChar(char)

writeText

public void writeText(char c)
Description copied from interface: XMLWriter
Writes the given character correctly for the encoding of this document.

Specified by:
writeText in interface XMLWriter
Parameters:
c - The character to write.

writeText

public void writeText(String text)
Description copied from interface: XMLWriter
Writes the given text correctly for the encoding of this document.

Does nothing if the text is null.

Specified by:
writeText in interface XMLWriter
Parameters:
text - The text to write

writeText

public void writeText(char[] text,
                      int off,
                      int len)
Description copied from interface: XMLWriter
Write the given text correctly for the encoding of this document.

Specified by:
writeText in interface XMLWriter
Parameters:
text - The text to write.
off - The offset where we should start writing the string.
len - The length of the character subarray to write.

writeCDATA

public void writeCDATA(String cdata)
Description copied from interface: XMLWriter
Writes the given text as a CDATA section.

Does nothing if the text is null.

Specified by:
writeCDATA in interface XMLWriter
Parameters:
cdata - The data to write inside the CDATA section.

writeXML

public void writeXML(String text)
Description copied from interface: XMLWriter
Writes the given XML data.

The text is appended as is, therefore it should be escaped properly for the encoding used by the underlying stream writer.

Does nothing if the text is null.

Specified by:
writeXML in interface XMLWriter
Parameters:
text - The text to write.

writeXML

public void writeXML(char[] text,
                     int off,
                     int len)
Description copied from interface: XMLWriter
Write the given XML data.

The text is appended as is, therefore it should be escaped properly for the encoding used by the underlying stream writer.

Specified by:
writeXML in interface XMLWriter
Parameters:
text - The text to write.
off - The offset where we should start writing the string.
len - The length of the character subarray to write.

writeComment

public void writeComment(String comment)
Description copied from interface: XMLWriter
Writes an XML comment.

An XML comment is:

   <!-- comment -->
 

Comments are not indented.

Does not write anything if the comment if null.

Specified by:
writeComment in interface XMLWriter
Parameters:
comment - The comment to be written

writePI

public void writePI(String target,
                    String data)
Description copied from interface: XMLWriter
Writes an XML processing instruction.

An XML processing intruction is:

   <?target data?>
 

Specified by:
writePI in interface XMLWriter
Parameters:
target - The PI's target.
data - The PI's data.

openElement

public void openElement(String name)
Description copied from interface: XMLWriter
Writes a start element tag correctly indented.

It is the same as openElement(name, false)

Specified by:
openElement in interface XMLWriter
Parameters:
name - the name of the element
See Also:
XMLWriter.openElement(java.lang.String, boolean)

openElement

public void openElement(String name,
                        boolean hasChildren)
Description copied from interface: XMLWriter
Writes a start element tag correctly indented.

Use the hasChildren parameter to specify whether this element is terminal node or not, note: this affects the indenting. To produce correctly indented XML, you should use the same value for this flag when closing the element.

The name can contain attributes and should be a valid xml name.

Specified by:
openElement in interface XMLWriter
Parameters:
name - The name of the element
hasChildren - true if this element has children

openElement

public void openElement(String uri,
                        String name,
                        boolean hasChildren)
Description copied from interface: XMLWriter
Writes a start element tag correctly indented.

Use the hasChildren parameter to specify whether this element is terminal node or not, note: this affects the indenting. To produce correctly indented XML, you should use the same value for this flag when closing the element.

The name can contain attributes and should be a valid xml name.

Specified by:
openElement in interface XMLWriter
Parameters:
uri - The namespace URI of the element.
name - The name of the element.
hasChildren - true if this element has children.

closeElement

public void closeElement()
Description copied from interface: XMLWriter
Close the element automatically.

The element is closed symmetrically to the XMLWriter.openElement(String, String, boolean) method if the XML writer is namespace aware or the XMLWriter.openElement(String, boolean)method.

Specified by:
closeElement in interface XMLWriter

element

public void element(String name,
                    String text)
Description copied from interface: XMLWriter
Opens element, inserts text node and closes.

This method should behave like:

   this.openElement(name, false);
   this.writeText(text);
   this.closeElement();
 

Specified by:
element in interface XMLWriter
Parameters:
name - The name of the element.
text - The text of the element.

emptyElement

public void emptyElement(String element)
Description copied from interface: XMLWriter
Writes an empty element.

It is possible for the element to contain attributes, however, since there is no character escaping, great care must be taken not to introduce invalid characters. For example:

    <example test="yes"/>
 

Specified by:
emptyElement in interface XMLWriter
Parameters:
element - the name of the element

emptyElement

public void emptyElement(String uri,
                         String element)
Description copied from interface: XMLWriter
Writes an empty element.

It is possible for the element to contain attributes, however, since there is no character escaping, great care must be taken not to introduce invalid characters. For example:

    <example test="yes"/>
 

Specified by:
emptyElement in interface XMLWriter
Parameters:
uri - The namespace URI of the element.
element - The name of the element.

attribute

public void attribute(String name,
                      String value)
Description copied from interface: XMLWriter
Writes an attribute.

Specified by:
attribute in interface XMLWriter
Parameters:
name - The name of the attribute.
value - The value of the attribute.

attribute

public void attribute(String name,
                      int value)
Description copied from interface: XMLWriter
Writes an attribute.

This method for number does not require escaping.

Specified by:
attribute in interface XMLWriter
Parameters:
name - The name of the attribute.
value - The value of the attribute.

attribute

public void attribute(String uri,
                      String name,
                      String value)
Description copied from interface: XMLWriter
Writes an attribute.

Specified by:
attribute in interface XMLWriter
Parameters:
uri - The uri of the attribute.
name - The name of the attribute.
value - The value of the attribute.

attribute

public void attribute(String uri,
                      String name,
                      int value)
Description copied from interface: XMLWriter
Writes an attribute.

This method for number does not require escaping.

Specified by:
attribute in interface XMLWriter
Parameters:
uri - The uri of the attribute.
name - The name of the attribute.
value - The value of the attribute.

setPrefixMapping

public void setPrefixMapping(String uri,
                             String prefix)
Description copied from interface: XMLWriter
Sets a prefix mapping.

Specified by:
setPrefixMapping in interface XMLWriter
Parameters:
uri - The namespace URI.
prefix - The new prefix for the namespace URI.

flush

public void flush()
Description copied from interface: XMLWriter
Flush the writer.

Specified by:
flush in interface XMLWriter

close

public void close()
           throws UnclosedElementException
Description copied from interface: XMLWriter
Close the writer.

Specified by:
close in interface XMLWriter
Throws:
UnclosedElementException - If there is still an open element.

toString

public String toString()
Returns the XML content as a String.

Overrides:
toString in class Object
Returns:
the XML content as a String.