TUM CCSM Commons

edu.tum.cs.commons.xml
Class XMLWriter<E extends java.lang.Enum<E>,A extends java.lang.Enum<A>>

java.lang.Object
  extended by edu.tum.cs.commons.xml.XMLWriter<E,A>
Direct Known Subclasses:
HTMLWriter

public class XMLWriter<E extends java.lang.Enum<E>,A extends java.lang.Enum<A>>
extends java.lang.Object

Utility class for creating XML documents. Please consult test case XMLWriterTest to see how this class is intended to be used.

Version:
$Rev: 26963 $
Author:
Florian Deissenboeck, $Author: hummelb $
Rating:
GREEN Hash: E346D576888FEF33DA193173CEE8337B

Field Summary
protected  IXMLResolver<E,A> xmlResolver
          Resolver used by the writer.
 
Constructor Summary
XMLWriter(java.io.OutputStream stream, IXMLResolver<E,A> xmlResolver)
          Create a new writer.
XMLWriter(java.io.PrintWriter writer, IXMLResolver<E,A> xmlResolver)
          Create a new writer.
 
Method Summary
 void addAttribute(A attribute, java.lang.Object value)
          Add a attribute.
 void addCDataSection(java.lang.String cdata)
          Add CDATA section.
 void addClosedElement(E element, java.lang.Object... attributes)
          Convenience method for adding an element together with (some of) its attributes.
 void addClosedTextElement(E element, java.lang.String text, java.lang.Object... attributes)
          Convenience method for adding an element together with (some of) its attributes and text inbetween.
 void addComment(java.lang.String text)
          Add an XML comment.
 void addHeader(java.lang.String version, java.lang.String encoding)
          Add an XML header.
 void addNewLine()
          Add new line.
 void addPublicDocTypeDefintion(E rootElement, java.lang.String publicId, java.lang.String systemId)
          Add public document type definiton
protected  void addRawString(java.lang.String text)
          Adds the given text unprocessed to the writer.
 void addText(java.lang.String text)
          Add a text element to an element.
 void close()
          Close the writer.
 void closeElement(E element)
          Close an element.
static java.lang.String escape(java.lang.String text)
          Escape text for XML.
 void flush()
          Flushes the underlying writer.
protected  E getCurrentElement()
          Returns the element we are currently in.
protected  java.io.PrintWriter getWriter()
          Get writer this writer writes to.
 void openElement(E element)
          Start a new element
 void openElement(E element, java.lang.Object... attributes)
          Convenience method for adding an element together with (some of) its attributes.
 void setSuppressLineBreaks(boolean supressLineBreaks)
          Toogle line break behavior.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

xmlResolver

protected final IXMLResolver<E extends java.lang.Enum<E>,A extends java.lang.Enum<A>> xmlResolver
Resolver used by the writer.

Constructor Detail

XMLWriter

public XMLWriter(java.io.OutputStream stream,
                 IXMLResolver<E,A> xmlResolver)
Create a new writer.

Parameters:
stream - the stream to write to.
xmlResolver - resolvers used by this writer

XMLWriter

public XMLWriter(java.io.PrintWriter writer,
                 IXMLResolver<E,A> xmlResolver)
Create a new writer.

Parameters:
writer - the writer to write to.
xmlResolver - resolvers used by this writer
Method Detail

setSuppressLineBreaks

public void setSuppressLineBreaks(boolean supressLineBreaks)
Toogle line break behavior. If set to true the writer does not write line breaks. If set to false (default) line breaks are written.

This can, for example, be used for HTML where line breaks sometimes change the layout.


addHeader

public void addHeader(java.lang.String version,
                      java.lang.String encoding)
Add an XML header.

Parameters:
version - version string
encoding - encoding definition

addPublicDocTypeDefintion

public void addPublicDocTypeDefintion(E rootElement,
                                      java.lang.String publicId,
                                      java.lang.String systemId)
Add public document type definiton

Parameters:
rootElement - root element
publicId - public id
systemId - sytem id

openElement

public void openElement(E element)
Start a new element

Parameters:
element - the element to start.

addAttribute

public void addAttribute(A attribute,
                         java.lang.Object value)
Add a attribute. This only works if a element was started but no other elements were added yet.

Parameters:
attribute - the attribute to create
value - its value
Throws:
XMLWriterException - if there's no element to add attributes to ( EXMLWriterExceptionType.ATTRIBUTE_OUTSIDE_ELEMENT) or if an attribute is added twice ( EXMLWriterExceptionType.DUPLICATE_ATTRIBUTE).

openElement

public void openElement(E element,
                        java.lang.Object... attributes)
Convenience method for adding an element together with (some of) its attributes.

Parameters:
element - The element to be opened (using openElement(Enum)).
attributes - the attributes to be added. The number of arguments must be even, where the first, third, etc. argument is an attribute enum.

addClosedElement

public void addClosedElement(E element,
                             java.lang.Object... attributes)
Convenience method for adding an element together with (some of) its attributes. This is the same as openElement(Enum, Object[]), but also closes the element.


addClosedTextElement

public void addClosedTextElement(E element,
                                 java.lang.String text,
                                 java.lang.Object... attributes)
Convenience method for adding an element together with (some of) its attributes and text inbetween. This is the same as openElement(Enum, Object[]), but then adds the provided text and closes the element.


closeElement

public void closeElement(E element)
Close an element.

Parameters:
element - the element to close.
Throws:
XMLWriterException - on attempt to close the wrong element ( EXMLWriterExceptionType.UNCLOSED_ELEMENT).

addText

public void addText(java.lang.String text)
Add a text element to an element.

Parameters:
text - the text to add.

addCDataSection

public void addCDataSection(java.lang.String cdata)
Add CDATA section. Added text is not escaped.

Throws:
XMLWriterException - If the added text contains the CDATA closing tag ]]>. This is not automatically escaped as some parsers do not automatically unescape it when reading.

addComment

public void addComment(java.lang.String text)
Add an XML comment.

Parameters:
text - comment text.

addNewLine

public void addNewLine()
Add new line.


close

public void close()
Close the writer.

Throws:
XMLWriterException - if there is a remaining open element.

flush

public void flush()
Flushes the underlying writer.


addRawString

protected void addRawString(java.lang.String text)
Adds the given text unprocessed to the writer. This is useful for adding chunks of generated XML to avoid having the brackets escaped.


getWriter

protected java.io.PrintWriter getWriter()
Get writer this writer writes to.


getCurrentElement

protected E getCurrentElement()
Returns the element we are currently in.

Throws:
java.util.EmptyStackException - if there is no unclosed element.

escape

public static java.lang.String escape(java.lang.String text)
Escape text for XML. Creates empty string for null value.


TUM CCSM Commons

TUM CCSM Commons - 2.7