Class DOMWriterImpl

java.lang.Object
com.topologi.diffx.xml.dom.DOMWriterImpl
All Implemented Interfaces:
DOMWriter, XMLWriter

public final class DOMWriterImpl extends Object implements DOMWriter
A simple implementation of a DOM writer

Provides methods to generate well-formed XML data easily via DOM.

Version:
11 December 2011
  • Field Details

    • document

      private final Document document
      The DOM document on which we write.
    • newline

      private final Node newline
      The new line used.
    • indent

      private 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.
    • depth

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

      This attribute changes depending on the state of the instance.

    • isNude

      private transient boolean isNude
      Flag to indicate that the element open tag is not finished yet.
    • currentElement

      private transient Node currentElement
      The current node being written onto.

      This node should always be an element except before and after writing where it is the document node itself.

    • childrenFlags

      private transient List<Boolean> childrenFlags
      An array to indicate which elements have children.
  • Constructor Details

    • DOMWriterImpl

      public DOMWriterImpl() throws ParserConfigurationException

      Creates a new XML writer for DOM using the default implementation on the system.

      Attempts to create the DOM document using:

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance()
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.newDocument();
       
      Throws:
      ParserConfigurationException - If thrown by the document builder factory.
    • DOMWriterImpl

      public DOMWriterImpl(Document document)

      Creates a new XML writer for DOM.

      Parameters:
      document - The DOM provided.
      Throws:
      NullPointerException - If the handler is null.
  • Method Details

    • xmlDecl

      public void xmlDecl()
      Does nothing.
      Specified by:
      xmlDecl in interface XMLWriter
    • setIndentChars

      public void setIndentChars(String spaces)
      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.
      See Also:
    • writeText

      public void writeText(String text)
      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:
      DOMException - If thrown by method invoked on the underlying DOM document
    • writeText

      public void writeText(char[] text, int off, int len)
      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:
      DOMException - If thrown by method invoked on the underlying DOM document
    • writeText

      public void writeText(char c)
      This method is expensive as the character has to be converted to a String for DOM. Writes the given character correctly for the encoding of this document.
      Specified by:
      writeText in interface XMLWriter
      Parameters:
      c - The character to write.
      Throws:
      DOMException - If thrown by method invoked on the underlying DOM document
    • writeText

      public void writeText(Object o)
      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:
      DOMException - If thrown by method invoked on the underlying DOM document
      See Also:
    • writeCDATA

      public void writeCDATA(String data)
      Writes the CDATA section to the DOM.

      Does nothing if the object is null.

      Specified by:
      writeCDATA in interface XMLWriter
      Parameters:
      data - The data to write to the section.
      Throws:
      DOMException - If thrown by method invoked on the underlying DOM document
    • writeXML

      public void writeXML(String text)
      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:
      UnsupportedOperationException - XML cannot be written to the DOM
    • writeXML

      public void writeXML(char[] text, int off, int len) throws UnsupportedOperationException
      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:
      UnsupportedOperationException - XML cannot be written to the DOM
    • writeComment

      public void writeComment(String comment) throws DOMException
      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:
      DOMException - If thrown by method invoked on the underlying DOM document
    • writePI

      public void writePI(String target, String data) throws DOMException
      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:
      DOMException - If thrown by method invoked on the underlying DOM document
    • attribute

      public void attribute(String name, String value) throws DOMException
      Writes an attribute.
      Specified by:
      attribute in interface XMLWriter
      Parameters:
      name - The name of the attribute.
      value - The value of the attribute.
      Throws:
      DOMException - If thrown by method invoked on the underlying DOM document
    • attribute

      public void attribute(String name, int value) throws DOMException
      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:
      DOMException - If thrown by method invoked on the underlying DOM document
    • openElement

      public void openElement(String name) throws DOMException
      Writes a start element tag correctly indented.

      It is the same as openElement("", name, false)

      Specified by:
      openElement in interface XMLWriter
      Parameters:
      name - the name of the element
      Throws:
      DOMException - If thrown by method invoked on the underlying DOM document
      See Also:
    • openElement

      public void openElement(String name, boolean hasChildren) throws DOMException
      Writes a start element tag correctly indented.

      Use the hasChildren parameter to specify whether this element is terminal node or not, note: this affects the indenting. To produce correctly indented XML, you should use the same value for this flag when closing the element.

      The name can contain attributes and should be a valid xml name.

      Specified by:
      openElement in interface XMLWriter
      Parameters:
      name - The name of the element.
      hasChildren - true if this element has children.
      Throws:
      DOMException - If thrown by method invoked on the underlying DOM document
    • element

      public void element(String name, String text) throws DOMException
      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:
      DOMException - If thrown by method invoked on the underlying DOM document
    • closeElement

      public void closeElement() throws DOMException, IllegalCloseElementException
      Close the element automatically.

      The element is closed symmetrically to the XMLWriter.openElement(String, String, boolean) method if the XML writer is namespace aware or the XMLWriter.openElement(String, boolean)method.

      Specified by:
      closeElement in interface XMLWriter
      Throws:
      DOMException - If thrown by method invoked on the underlying DOM document
      IllegalCloseElementException
    • emptyElement

      public void emptyElement(String name) throws DOMException
      Writes an empty element.

      It is possible for the element to contain attributes, however, since there is no character escaping, great care must be taken not to introduce invalid characters. For example:

          <example test="yes"/>
       
      Specified by:
      emptyElement in interface XMLWriter
      Parameters:
      name - the name of the element
      Throws:
      DOMException - If thrown by method invoked on the underlying DOM document
    • close

      public void close()
      Does nothing. Close the writer.
      Specified by:
      close in interface XMLWriter
    • flush

      public void flush()
      Normalises the current element.
      Specified by:
      flush in interface XMLWriter
    • getDocument

      public Document getDocument()
      Returns the DOM document produced by the XML Writer.
      Specified by:
      getDocument in interface DOMWriter
      Returns:
      The DOM document produced by the XML Writer.
    • openElement

      public void openElement(String uri, String name) throws UnsupportedOperationException
      Not supported.
      Parameters:
      uri - This parameter is ignored.
      name - This parameter is ignored.
      Throws:
      UnsupportedOperationException - This class does not handle namespaces.
    • openElement

      public void openElement(String uri, String name, boolean hasChildren) throws UnsupportedOperationException
      Not supported.
      Specified by:
      openElement in interface XMLWriter
      Parameters:
      uri - This parameter is ignored.
      name - This parameter is ignored.
      hasChildren - This parameter is ignored.
      Throws:
      UnsupportedOperationException - This class does not handle namespaces.
    • emptyElement

      public void emptyElement(String uri, String element) throws UnsupportedOperationException
      Not supported.
      Specified by:
      emptyElement in interface XMLWriter
      Parameters:
      uri - This parameter is ignored.
      element - This parameter is ignored.
      Throws:
      UnsupportedOperationException - This class does not handle namespaces.
    • setPrefixMapping

      public void setPrefixMapping(String uri, String prefix) throws UnsupportedOperationException
      Not supported.
      Specified by:
      setPrefixMapping in interface XMLWriter
      Parameters:
      uri - This parameter is ignored.
      prefix - This parameter is ignored.
      Throws:
      UnsupportedOperationException - This class does not handle namespaces.
    • attribute

      public void attribute(String uri, String name, String value) throws UnsupportedOperationException
      Not supported.
      Specified by:
      attribute in interface XMLWriter
      Parameters:
      uri - This parameter is ignored.
      name - The name of the attribute.
      value - The value of the attribute.
      Throws:
      UnsupportedOperationException - This class does not handle namespaces.
    • attribute

      public void attribute(String uri, String name, int value) throws UnsupportedOperationException
      Not supported.
      Specified by:
      attribute in interface XMLWriter
      Parameters:
      uri - This parameter is ignored.
      name - The name of the attribute.
      value - The value of the attribute.
      Throws:
      UnsupportedOperationException - This class does not handle namespaces.
    • indent

      void indent()
      Insert the correct amount of space characterss depending on the depth and if the indent flag is set to true.
    • deNude

      private void deNude()
      Writes the angle bracket if the element open tag is not finished.
    • newLine

      private void newLine()
      Adds a new line to the DOM.
      Throws:
      DOMException - If thrown by method invoked on the underlying DOM document
    • newDocument

      private static Document newDocument() throws ParserConfigurationException
      Returns a new DOM document.

      Attempts to create the DOM document using:

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance()
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.newDocument();
       
      Returns:
      A new DOM document.
      Throws:
      ParserConfigurationException - If thrown by the document builder factory.