eu.beesoft.gaia.util
Class XmlWriter

java.lang.Object
  extended by eu.beesoft.gaia.util.XmlWriter

public class XmlWriter
extends java.lang.Object

This class is designed to write XML files. The reserved XML characters are replaced by standard entities. Class supports the automatic elements nesting.

Usage:
                PrintWriter pw = ...
                XmlWriter writer = new XmlWriter (pw);
                writer.addElement ("first");
                writer.addAttribute ("value", 50);
                writer.addElement ("second");
                writer.addAttribute ("next", "www");
                writer.closeElement ("second");
                writer.closeElement ("first");
                writer.close ();
 
The code above builds this XML:
                <?xml version="1.0" encoding="UTF-8" ?>
                <first value="50">
                        <second next="www" />
                </first>
 


Constructor Summary
XmlWriter(java.io.PrintWriter output)
          Creates a new XML writer with one-tab indentation and UTF-8 encoding.
XmlWriter(java.io.PrintWriter output, java.lang.String encoding)
          Creates a new XML writer with one-tab indentation.
XmlWriter(java.io.PrintWriter output, java.lang.String encoding, java.lang.String indentation)
          Creates a new XML writer.
 
Method Summary
 void addAttribute(java.lang.String name, boolean value)
          Adds an attribute to the current element.
 void addAttribute(java.lang.String name, double value)
          Adds an attribute to the current element.
 void addAttribute(java.lang.String name, float value)
          Adds an attribute to the current element.
 void addAttribute(java.lang.String name, int value)
          Adds an attribute to the current element.
 void addAttribute(java.lang.String name, long value)
          Adds an attribute to the current element.
 void addAttribute(java.lang.String name, java.lang.Object value)
          Adds an attribute to the current element.
 void addElement(java.lang.String tag)
          Opens a new element in XML output.
 void close()
          Flushes this XML writer and closes the underlaying PrintWriter.
 void closeElement(java.lang.String tag)
          Closes current element in XML output.
 void flush()
          Flushes this XML writer and underlaying PrintWriter.
 java.lang.String getIndentation()
          Returns indentation string.
 void setIndentation(java.lang.String indentation)
          Sets the indentation string.
 void setText(java.lang.String text)
          Sets text to the current element.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XmlWriter

public XmlWriter(java.io.PrintWriter output)
Creates a new XML writer with one-tab indentation and UTF-8 encoding.

Parameters:
output - - writer to which this instance should write

XmlWriter

public XmlWriter(java.io.PrintWriter output,
                 java.lang.String encoding)
Creates a new XML writer with one-tab indentation.

Parameters:
output - - writer to which this instance should write
encoding - - encoding string to write to XML header

XmlWriter

public XmlWriter(java.io.PrintWriter output,
                 java.lang.String encoding,
                 java.lang.String indentation)
Creates a new XML writer.

Parameters:
output - - writer to which this instance should write
encoding - - encoding string to write to XML header
indentation - - string used to indentation of the elements
Method Detail

flush

public void flush()
Flushes this XML writer and underlaying PrintWriter.


close

public void close()
Flushes this XML writer and closes the underlaying PrintWriter.


setIndentation

public void setIndentation(java.lang.String indentation)
Sets the indentation string.

Parameters:
indentation - - the indentation string to set

getIndentation

public java.lang.String getIndentation()
Returns indentation string.

Returns:
indentation string

addElement

public void addElement(java.lang.String tag)
Opens a new element in XML output.

Parameters:
tag - - element name (tag)

closeElement

public void closeElement(java.lang.String tag)
Closes current element in XML output. Given tag must be the same as was used in addElement(String) when opening this element.

Parameters:
tag - - element name (tag)

addAttribute

public void addAttribute(java.lang.String name,
                         boolean value)
Adds an attribute to the current element.

Parameters:
name - - attribute name
value - - attribute value

addAttribute

public void addAttribute(java.lang.String name,
                         int value)
Adds an attribute to the current element.

Parameters:
name - - attribute name
value - - attribute value

addAttribute

public void addAttribute(java.lang.String name,
                         long value)
Adds an attribute to the current element.

Parameters:
name - - attribute name
value - - attribute value

addAttribute

public void addAttribute(java.lang.String name,
                         float value)
Adds an attribute to the current element.

Parameters:
name - - attribute name
value - - attribute value

addAttribute

public void addAttribute(java.lang.String name,
                         double value)
Adds an attribute to the current element.

Parameters:
name - - attribute name
value - - attribute value

addAttribute

public void addAttribute(java.lang.String name,
                         java.lang.Object value)
Adds an attribute to the current element. If given value is null, it writes to XML as attribute value string " <null>".

Parameters:
name - - attribute name
value - - attribute value

setText

public void setText(java.lang.String text)
Sets text to the current element.

Parameters:
text - - text to set to the current element