com.virtuosotechnologies.lib.xml
Class XMLUnparser

java.lang.Object
  extended bycom.virtuosotechnologies.lib.xml.XMLUnparser
Direct Known Subclasses:
XMLDocumentUnparser

public class XMLUnparser
extends Object

Helper for XML unparsing. This creates an indented tree representation of the elements. Elements may be single-line or multi-line. Single-line elements may only contain text or other single-line elements. Multi-line elements may contain text or any other element. Output inside a multi-line element is indented.

This class is not thread-safe. Access should be serialized into one thread.


Constructor Summary
XMLUnparser(Writer writer, int indent, boolean escape16BitChars)
          Constructor
 
Method Summary
 void addAttribute(String name, String value)
          Add an attribute to the current element.
 void addString(String value)
          Add a string to the current element
protected  String createIndentString(int indent)
          Create an indent string given an indent level.
 void endElement(String element)
          End the innermost element.
 Writer getWriter()
          Returns the writer being written to
 boolean isDefunct()
          Returns true if the unparser is defunct.
 void makeDefunct()
          Finish the unparser.
 void startMultiLineElement(String element)
          Start a multi-line element.
 void startSingleLineElement(String element)
          Start a single-line element.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLUnparser

public XMLUnparser(Writer writer,
                   int indent,
                   boolean escape16BitChars)
            throws IOException
Constructor

Parameters:
writer - writer to write to
indent - base indent
escape16BitChars - true to generate escape sequences for 16-bit characters
Method Detail

createIndentString

protected String createIndentString(int indent)
Create an indent string given an indent level. The default implementation concatenates tabs together. This can be overridden.

Parameters:
indent - number of indents
Returns:
string to perform the indent

getWriter

public Writer getWriter()
Returns the writer being written to

Returns:
the writer

isDefunct

public boolean isDefunct()
Returns true if the unparser is defunct.

Returns:
true if the unparser is defunct

makeDefunct

public void makeDefunct()
                 throws IOException
Finish the unparser. This can only be called once all elements are ended.

Throws:
IOException - i/o error
IllegalStateException - there are unterminated elements or unparser is already defunct

startMultiLineElement

public void startMultiLineElement(String element)
                           throws IOException
Start a multi-line element.

Parameters:
element - element name
Throws:
IOException - i/o error
IllegalStateException - Surrounding element is a single-line element or unparser is defunct
IllegalArgumentException - Element name is illegal

startSingleLineElement

public void startSingleLineElement(String element)
                            throws IOException
Start a single-line element.

Parameters:
element - element name
Throws:
IOException - i/o error
IllegalStateException - unparser is defunct
IllegalArgumentException - Element name is illegal

endElement

public void endElement(String element)
                throws IOException
End the innermost element.

Parameters:
element - element name, which must match the current element name
Throws:
IOException - i/o error
IllegalStateException - no current element or unparser is defunct
IllegalArgumentException - element name mismatch

addAttribute

public void addAttribute(String name,
                         String value)
                  throws IOException
Add an attribute to the current element.

Parameters:
name - attribute name
value - attribute value
Throws:
IOException - i/o error
IllegalStateException - no current element, the current element has already had subelements or strings added, or unparser is defunct
IllegalArgumentException - attribute name is illegal

addString

public void addString(String value)
               throws IOException
Add a string to the current element

Parameters:
value - string to add
Throws:
IOException - i/o error
IllegalStateException - unparser is defunct