Class XMLWriterNSImpl

java.lang.Object
com.topologi.diffx.xml.XMLWriterBase
com.topologi.diffx.xml.XMLWriterNSImpl
All Implemented Interfaces:
XMLWriter

public final class XMLWriterNSImpl extends XMLWriterBase implements XMLWriter
A Namespace-aware writer for XML data.

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"));
 

This class is not synchronised.

Version:
11 December 2011
  • Field Details

  • Constructor Details

    • XMLWriterNSImpl

      public XMLWriterNSImpl(Writer writer) throws NullPointerException

      Creates a new XML writer.

      Sets the depth attribute to 0 and the indentation to true.

      Parameters:
      writer - Where this writer should write the XML data.
      Throws:
      NullPointerException - If the writer is null.
    • XMLWriterNSImpl

      public XMLWriterNSImpl(Writer writer, boolean indent) throws NullPointerException

      Create 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

    • deNude

      void deNude() throws IOException
      Writes the angle bracket if the element open tag is not finished.
      Specified by:
      deNude in class XMLWriterBase
      Throws:
      IOException - If thrown by the wrapped writer.
    • openElement

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

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

      Specified by:
      openElement in interface XMLWriter
      Parameters:
      name - the name of the element
      Throws:
      IOException - If thrown by the wrapped writer.
      See Also:
    • openElement

      public void openElement(String uri, String name) throws IOException
      Write a start element tag correctly indented.

      It is the same as openElement(name, false)

      Parameters:
      uri - The namespace URI of this element.
      name - The name of the element.
      Throws:
      IOException - If thrown by the wrapped writer.
      See Also:
    • openElement

      public void openElement(String name, boolean hasChildren) throws IOException
      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:
      IOException - If thrown by the wrapped writer.
    • openElement

      public void openElement(String uri, String name, boolean hasChildren) throws IOException
      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:
      uri - The namespace URI of this element.
      name - The name of the element.
      hasChildren - true if this element has children.
      Throws:
      IOException - If thrown by the wrapped writer.
    • closeElement

      public void closeElement() throws IOException
      Write an end element tag.
      Specified by:
      closeElement in interface XMLWriter
      Throws:
      IOException - If thrown by the wrapped writer.
    • emptyElement

      public void emptyElement(String element) throws IOException
      Same as emptyElement(null, element);.
      Specified by:
      emptyElement in interface XMLWriter
      Parameters:
      element - the name of the element
      Throws:
      IOException - If thrown by the wrapped writer.
    • emptyElement

      public void emptyElement(String uri, String element) throws IOException
      Write 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:
      uri - The namespace URI for this element.
      element - The name of the element.
      Throws:
      IOException - If thrown by the wrapped writer.
    • peekElement

      private XMLWriterNSImpl.Element peekElement()
      Returns the last element in the list.
      Returns:
      The current element.
    • popElement

      private XMLWriterNSImpl.Element popElement()
      Removes the last element in the list.
      Returns:
      The current element.
    • attribute

      public void attribute(String uri, String name, String value) throws IOException, IllegalStateException
      Writes an attribute.
      Specified by:
      attribute in interface XMLWriter
      Parameters:
      uri - The namespcae URI this attribute belongs to.
      name - The name of the attribute.
      value - The value of the attribute.
      Throws:
      IOException - If thrown by the wrapped writer.
      IllegalStateException - If there is no open element or text has been written.
    • attribute

      public void attribute(String uri, String name, int value) throws IOException, IllegalStateException
      Writes an attribute.

      This method for number does not require escaping.

      Specified by:
      attribute in interface XMLWriter
      Parameters:
      uri - The namespcae URI this attribute belongs to.
      name - The name of the attribute.
      value - The value of the attribute.
      Throws:
      IOException - If thrown by the wrapped writer.
      IllegalStateException - If there is no open element or text has been written.
    • setPrefixMapping

      public void setPrefixMapping(String uri, String prefix) throws NullPointerException
      Description copied from interface: XMLWriter
      Sets a prefix mapping.
      Specified by:
      setPrefixMapping in interface XMLWriter
      Parameters:
      uri - The full namespace URI.
      prefix - The prefix for the namespace uri.
      Throws:
      NullPointerException - if the prefix is null.
      See Also:
    • getQName

      private String getQName(String uri, String name) throws UndeclaredNamespaceException
      Returns the qualified name for this element using the specified namespace URI.
      Parameters:
      uri - The namespace URI for the element.
      name - The name of the element or attribute.
      Returns:
      The qualified element name.
      Throws:
      UndeclaredNamespaceException - If the uri has not being previously declared.
    • handleNamespaceDeclaration

      private void handleNamespaceDeclaration() throws IOException
      Handles the namespace declaration and updates the prefix mappings.
      Throws:
      IOException - If thrown by the wrapped writer.
    • restorePrefixMapping

      private void restorePrefixMapping(XMLWriterNSImpl.Element elt)
      Restores the prefix mapping after closing an element.

      This costly operation need only to be done if the method setPrefixMapping(String, String) have been used immediately before, therefore it should not happen often.

      Parameters:
      elt - The element that had some new mappings.
    • removeIfNeeded

      private void removeIfNeeded(String prefix)
      Removes the mapping associated to the specified prefix.
      Parameters:
      prefix - The prefix which mapping should be removed.
    • close

      public void close() throws IOException, UnclosedElementException
      Close the writer.
      Specified by:
      close in interface XMLWriter
      Throws:
      IOException - If thrown by the wrapped writer.
      UnclosedElementException - If an element has been left open.