Class XMLWriterBase

java.lang.Object
com.topologi.diffx.xml.XMLWriterBase
All Implemented Interfaces:
XMLWriter
Direct Known Subclasses:
XMLWriterImpl, XMLWriterNSImpl

abstract class XMLWriterBase extends Object implements XMLWriter
A base implementation for XML writers.

Provides methods to generate well-formed XML data easily. wrapping a writer.

This version only supports utf-8 encoding, if writing to a file make sure that the encoding of the file output stream is "utf-8".

The recommended implementation is to use a BufferedWriter to write.

  Writer writer =
     new BufferedWriter(new OutputStreamWriter(new FileOutputStream("foo.out"),"utf-8"));
 
Version:
17 May 2005
  • Field Details

    • writer

      final Writer writer
      Where the XML data goes.
    • encoding

      String encoding
      Encoding of the output xml.
    • writerEscape

      XMLEscapeWriter writerEscape
      Encoding of the output xml.
    • depth

      int depth
      Level of the depth of the xml document currently produced.

      This attribute changes depending on the state of the instance.

    • indent

      boolean indent
      Indicates whether the xml should be indented or not.

      The default is true (indented).

      The indentation is 2 white-spaces.

    • indentChars

      private String indentChars
      The default indentation spaces used.
    • isNude

      boolean isNude
      Flag to indicate that the element open tag is not finished yet.
  • Constructor Details

    • XMLWriterBase

      public XMLWriterBase(Writer writer, boolean indent) throws NullPointerException

      Creates a new XML writer.

      Parameters:
      writer - Where this writer should write the XML data.
      indent - Set the indentation flag.
      Throws:
      NullPointerException - If the writer is null.
  • Method Details

    • xmlDecl

      public final void xmlDecl() throws IOException
      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
      Throws:
      IOException - If an I/O exception is thrown by the underlying writer.
    • setIndentChars

      public final void setIndentChars(String spaces) throws IllegalStateException, IllegalArgumentException
      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.
      Throws:
      IllegalStateException - If the writer has already been used.
      IllegalArgumentException - If the indent string is not made of spaces.
      See Also:
    • setEncoding

      public final void setEncoding(String encoding) throws IllegalStateException, IllegalArgumentException
      Sets the encoding to use.

      The encoding must match the encoding used if there is an underlying OutputStreamWriter.

      Parameters:
      encoding - The encoding to use.
      Throws:
      IllegalArgumentException - If the encoding is not valid.
      IllegalStateException - If the writer has already been used.
    • writeText

      public final void writeText(String text) throws IOException
      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
      Throws:
      IOException - If an I/O exception is thrown by the underlying writer.
    • writeText

      public final void writeText(char[] text, int off, int len) throws IOException
      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.
      Throws:
      IOException - If an I/O exception is thrown by the underlying writer.
    • writeText

      public final void writeText(char c) throws IOException
      Writes the given character correctly for the encoding of this document.
      Specified by:
      writeText in interface XMLWriter
      Parameters:
      c - The character to write.
      Throws:
      IOException - If an I/O exception is thrown by the underlying writer.
    • writeText

      public final void writeText(Object o) throws IOException
      Writes the string value of an object.

      Does nothing if the object is null.

      Parameters:
      o - The object that should be written as text.
      Throws:
      IOException - If thrown by the wrapped writer.
      See Also:
    • writeXML

      public final void writeXML(String text) throws IOException
      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.
      Throws:
      IOException - If an I/O exception is thrown by the underlying writer.
    • writeXML

      public final void writeXML(char[] text, int off, int len) throws IOException
      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.
      Throws:
      IOException - If an I/O exception is thrown by the underlying writer.
    • writeComment

      public final void writeComment(String comment) throws IOException, IllegalArgumentException
      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
      Throws:
      IOException - If thrown by the wrapped writer.
      IllegalArgumentException - If the comment contains "--".
    • writePI

      public final void writePI(String target, String data) throws IOException
      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.
      Throws:
      IOException - If an I/O exception occurs.
    • writeCDATA

      public final void writeCDATA(String data) throws IOException
      Writes the given text as a CDATA section.

      Does nothing if the text is null.

      Specified by:
      writeCDATA in interface XMLWriter
      Parameters:
      data - The data to write inside the CDATA section.
      Throws:
      IOException - If an I/O exception is thrown by the underlying writer.
    • attribute

      public final void attribute(String name, String value) throws IOException
      Writes an attribute.
      Specified by:
      attribute in interface XMLWriter
      Parameters:
      name - The name of the attribute.
      value - The value of the attribute.
      Throws:
      IOException - If thrown by the wrapped writer.
    • attribute

      public final void attribute(String name, int value) throws IOException
      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.
      Throws:
      IOException - If thrown by the wrapped writer.
    • element

      public void element(String name, String text) throws IOException
      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.
      Throws:
      IOException - If thrown by the wrapped writer.
    • flush

      public final void flush() throws IOException
      Flush the writer.
      Specified by:
      flush in interface XMLWriter
      Throws:
      IOException - If thrown by the wrapped writer.
    • deNude

      abstract void deNude() throws IOException
      Writes the end of the open element tag.

      After this method is invoked it is not possible to write attributes for an element.

      Throws:
      IOException - If thrown by the wrapped writer.
    • indent

      void indent() throws IOException
      Insert the correct amount of space characterss depending on the depth and if the indent flag is set to true.
      Throws:
      IOException - If thrown by the wrapped writer.
    • doNothing

      static final void doNothing()
      Does nothing.

      This method exists so that we can explicitly say that we should do nothing in certain conditions.