Last update: 2011-06-23

org.proteios.io
Class XMLCrudeWriter3Impl

java.lang.Object
  extended by org.proteios.io.XMLCrudeWriter3Impl
All Implemented Interfaces:
XMLCrudeWriter, XMLCrudeWriter2, XMLCrudeWriter3

public class XMLCrudeWriter3Impl
extends Object
implements XMLCrudeWriter, XMLCrudeWriter3

This class supports crude XML writing, with some added convenience methods. This class contains some methods that may help writing XML files. It was created as support for e.g. writing single tags with XMLStreamWriter could not be found. To get consistent indentation and change to a new line, the following rules are used: 1. A start element or comment starts a new line. 2. An end element starts a new line, unless following data. 3. Indentation level is increased by one before start element. 4. Indentation level is decreased by one after end element. To avoid having the outermost element block indented, the indentation level starts at -1 instead of 0.

Version:
2.0
Author:
Olle
Last modified
$Date: 2009-04-07 11:17:04 +0200 (Tue, 07 Apr 2009) $

Constructor Summary
XMLCrudeWriter3Impl(OutputStream outStream)
          Constructor that takes an output stream as parameter.
 
Method Summary
 void close()
          Close (not) output stream.
 void flush()
          Flushes output stream.
 int getByteBufferSize()
          Get size of byte buffer for writing a String to an OutputStream.
 int getIndentationLevel()
          Get indentation level.
 OutputStream getOutStream()
          Get output stream.
 boolean isIndentationUsed()
          Get indentation use flag.
 void setByteBufferSize(int byteBufferSize)
          Set size of byte buffer for writing a String to an OutputStream.
 void setIndentationLevel(int level)
          Set indentation level.
 void setIndentationUsed(boolean indentationUsed)
          Set indentation use flag.
 void setOutStream(OutputStream outStream)
          Set output stream.
 void writeAttribute(String name, String value)
          Writes XML attribute.
 void writeCharacters(String text)
          Writes text between XML start and end tags.
 void writeComment(String comment)
          Writes XML comment.
 void writeEndDocument()
          Writes end of XML document.
 void writeEndElement(String element)
          Writes XML end element.
 void writeEndTag(String element)
          Writes XML end tag for XML block.
 void writeIndentedText(String text, int extraIndentation)
          Writes indented text with optional extra indentation.
 void writeNamespace(String name, String value)
          Writes XML namespace.
 void writeSimpleElementPair(String elementName, String data)
          Convenience method for writing simple element pair.
 void writeSingleElementEnd()
          Writes XML single element end.
 void writeStartDocument(String encoding, String version)
          Writes XML file header.
 void writeStartElement(String element)
          Writes XML start element.
 void writeStartElementEnd()
          Writes XML start element end.
 void writeStartListTag(String element, int count)
          Writes XML start list tag for XML block.
 void writeStartTag(String element)
          Writes XML start tag for XML block.
 void writeStyleSheetHeader(String type, String href)
          Writes XML style sheet header.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLCrudeWriter3Impl

public XMLCrudeWriter3Impl(OutputStream outStream)
Constructor that takes an output stream as parameter.

Parameters:
outStream - The FileWriter output stream to use.
Method Detail

getOutStream

public OutputStream getOutStream()
Get output stream.

Specified by:
getOutStream in interface XMLCrudeWriter
Returns:
outStream output stream.

setOutStream

public void setOutStream(OutputStream outStream)
Set output stream.

Specified by:
setOutStream in interface XMLCrudeWriter
Parameters:
outStream - output stream to use.

isIndentationUsed

public boolean isIndentationUsed()
Get indentation use flag.

Specified by:
isIndentationUsed in interface XMLCrudeWriter
Returns:
indentationUsed boolean indentation use flag.

setIndentationUsed

public void setIndentationUsed(boolean indentationUsed)
Set indentation use flag.

Specified by:
setIndentationUsed in interface XMLCrudeWriter
Parameters:
indentationUsed - boolean indentation use flag.

getByteBufferSize

public int getByteBufferSize()
Get size of byte buffer for writing a String to an OutputStream.

Returns:
byteBufferSize int byte buffer size for writing a string to an OutputStream.

setByteBufferSize

public void setByteBufferSize(int byteBufferSize)
Set size of byte buffer for writing a String to an OutputStream.

Parameters:
byteBufferSize - int byte buffer size for writing a string to an OutputStream.

writeStartDocument

public void writeStartDocument(String encoding,
                               String version)
                        throws javax.xml.stream.XMLStreamException
Writes XML file header.

Specified by:
writeStartDocument in interface XMLCrudeWriter
Parameters:
encoding - String with XML encoding.
version - String with XML version.
Throws:
javax.xml.stream.XMLStreamException - If there is an error

writeStyleSheetHeader

public void writeStyleSheetHeader(String type,
                                  String href)
                           throws javax.xml.stream.XMLStreamException
Writes XML style sheet header.

Parameters:
type - String XML style sheet type, e.g. "text/xsl", .
href - String Name of XSL file.
Throws:
javax.xml.stream.XMLStreamException - If there is an error

writeEndDocument

public void writeEndDocument()
                      throws javax.xml.stream.XMLStreamException
Writes end of XML document. Note that unlike e.g. XMLStreamWriter writeEndDocument() method, this does not close any open tags, but simple writes a line separator to finish the last line. All tags must be closed by "hand".

Specified by:
writeEndDocument in interface XMLCrudeWriter
Throws:
javax.xml.stream.XMLStreamException - If there is an error

writeComment

public void writeComment(String comment)
                  throws javax.xml.stream.XMLStreamException
Writes XML comment.

Specified by:
writeComment in interface XMLCrudeWriter
Parameters:
comment - String with comment.
Throws:
javax.xml.stream.XMLStreamException - If there is an error

writeStartElement

public void writeStartElement(String element)
                       throws javax.xml.stream.XMLStreamException
Writes XML start element.

Specified by:
writeStartElement in interface XMLCrudeWriter
Parameters:
element - String with element name.
Throws:
javax.xml.stream.XMLStreamException - If there is an error

writeAttribute

public void writeAttribute(String name,
                           String value)
                    throws javax.xml.stream.XMLStreamException
Writes XML attribute.

Specified by:
writeAttribute in interface XMLCrudeWriter
Parameters:
name - String with attribute name.
value - String with attribute value.
Throws:
javax.xml.stream.XMLStreamException - If there is an error

writeNamespace

public void writeNamespace(String name,
                           String value)
                    throws javax.xml.stream.XMLStreamException
Writes XML namespace.

Specified by:
writeNamespace in interface XMLCrudeWriter
Parameters:
name - String with namespace name.
value - String with namespace value.
Throws:
javax.xml.stream.XMLStreamException - If there is an error

writeSingleElementEnd

public void writeSingleElementEnd()
                           throws javax.xml.stream.XMLStreamException
Writes XML single element end.

Specified by:
writeSingleElementEnd in interface XMLCrudeWriter
Throws:
javax.xml.stream.XMLStreamException - If there is an error

writeStartElementEnd

public void writeStartElementEnd()
                          throws javax.xml.stream.XMLStreamException
Writes XML start element end.

Specified by:
writeStartElementEnd in interface XMLCrudeWriter
Throws:
javax.xml.stream.XMLStreamException - If there is an error

writeEndElement

public void writeEndElement(String element)
                     throws javax.xml.stream.XMLStreamException
Writes XML end element.

Specified by:
writeEndElement in interface XMLCrudeWriter
Parameters:
element - String with element name.
Throws:
javax.xml.stream.XMLStreamException - If there is an error

writeCharacters

public void writeCharacters(String text)
                     throws javax.xml.stream.XMLStreamException
Writes text between XML start and end tags.

Specified by:
writeCharacters in interface XMLCrudeWriter
Parameters:
text - String with element text.
Throws:
javax.xml.stream.XMLStreamException - If there is an error

flush

public void flush()
           throws javax.xml.stream.XMLStreamException
Flushes output stream.

Specified by:
flush in interface XMLCrudeWriter
Throws:
javax.xml.stream.XMLStreamException - If there is an error

close

public void close()
           throws javax.xml.stream.XMLStreamException
Close (not) output stream.

Specified by:
close in interface XMLCrudeWriter
Throws:
javax.xml.stream.XMLStreamException - If there is an error

writeStartTag

public void writeStartTag(String element)
                   throws javax.xml.stream.XMLStreamException
Writes XML start tag for XML block.

Specified by:
writeStartTag in interface XMLCrudeWriter2
Parameters:
element - String with element name.
Throws:
javax.xml.stream.XMLStreamException - If there is an XML Stream related error

writeStartListTag

public void writeStartListTag(String element,
                              int count)
                       throws javax.xml.stream.XMLStreamException
Writes XML start list tag for XML block.

Specified by:
writeStartListTag in interface XMLCrudeWriter2
Parameters:
element - String with element name.
count - int the number of XML blocks in the list.
Throws:
javax.xml.stream.XMLStreamException - If there is an XML Stream related error

writeEndTag

public void writeEndTag(String element)
                 throws javax.xml.stream.XMLStreamException
Writes XML end tag for XML block.

Specified by:
writeEndTag in interface XMLCrudeWriter2
Parameters:
element - String with element name.
Throws:
javax.xml.stream.XMLStreamException - If there is an XML Stream related error

writeSimpleElementPair

public void writeSimpleElementPair(String elementName,
                                   String data)
                            throws javax.xml.stream.XMLStreamException
Convenience method for writing simple element pair. Writes XMl expression like: data

Specified by:
writeSimpleElementPair in interface XMLCrudeWriter2
Parameters:
elementName - String with element name.
data - String with element data.
Throws:
javax.xml.stream.XMLStreamException - If there is an XML Stream related error

writeIndentedText

public void writeIndentedText(String text,
                              int extraIndentation)
                       throws javax.xml.stream.XMLStreamException
Writes indented text with optional extra indentation. The indentation level counter is not updated.

Specified by:
writeIndentedText in interface XMLCrudeWriter3
Parameters:
text - String with text.
extraIndentation - int optional extra indentation.
Throws:
javax.xml.stream.XMLStreamException - If there is an error

getIndentationLevel

public int getIndentationLevel()
Get indentation level.

Specified by:
getIndentationLevel in interface XMLCrudeWriter3
Returns:
int level the indentation level.

setIndentationLevel

public void setIndentationLevel(int level)
Set indentation level.

Specified by:
setIndentationLevel in interface XMLCrudeWriter3
Parameters:
level - int The indentation level to set.

Last update: 2011-06-23