Class XmlWriter

  • All Implemented Interfaces:
    IXmlWriter

    public final class XmlWriter
    extends java.lang.Object
    implements IXmlWriter

    Lightweight IXmlWriter implementation.

    Requires a wrapper to be used safely in a multithreaded environment.

    Not intended to be subclassed. Please copy and hack!

    • Field Detail

      • NAME_START_OR_BODY_CHAR

        private static final byte NAME_START_OR_BODY_CHAR
        See Also:
        Constant Field Values
      • ALLOWED_CHARACTERS

        private static final boolean[] ALLOWED_CHARACTERS
      • CHARACTER_CODES

        private static final byte[] CHARACTER_CODES
      • writer

        private final java.io.Writer writer
      • elementNames

        private final org.apache.commons.collections.ArrayStack elementNames
      • currentAttributes

        private final java.util.Set<java.lang.CharSequence> currentAttributes
      • elementsWritten

        boolean elementsWritten
      • inElement

        boolean inElement
      • prologWritten

        boolean prologWritten
    • Constructor Detail

      • XmlWriter

        public XmlWriter​(java.io.Writer writer)
    • Method Detail

      • startDocument

        public IXmlWriter startDocument()
                                 throws java.io.IOException
        Starts a document by writing a prolog. Calling this method is optional. When writing a document fragment, it should not be called.
        Specified by:
        startDocument in interface IXmlWriter
        Returns:
        this object
        Throws:
        OperationNotAllowedException - if called after the first element has been written or once a prolog has already been written
        OperationNotAllowedException - if called after the first element has been written or once a prolog has already been written
        java.io.IOException
      • openElement

        public IXmlWriter openElement​(java.lang.CharSequence elementName)
                               throws java.io.IOException
        Writes the start of an element.
        Specified by:
        openElement in interface IXmlWriter
        Parameters:
        elementName - the name of the element, not null
        Returns:
        this object
        Throws:
        InvalidXmlException - if the name is not valid for an xml element
        OperationNotAllowedException - if called after the first element has been closed
        InvalidXmlException - if the name is not valid for an xml element
        OperationNotAllowedException - if called after the first element has been closed
        java.io.IOException
      • writeAttributeContent

        private void writeAttributeContent​(java.lang.CharSequence content)
                                    throws java.io.IOException
        Throws:
        java.io.IOException
      • content

        public IXmlWriter content​(java.lang.CharSequence content)
                           throws java.io.IOException
        Writes content. Calling this method will automatically Note that this method does not use CDATA.
        Specified by:
        content in interface IXmlWriter
        Parameters:
        content - the content to write
        Returns:
        this object
        Throws:
        OperationNotAllowedException - if called before any call to openElement(java.lang.CharSequence) or after the first element has been closed
        OperationNotAllowedException - if called before any call to #openElement or after the first element has been closed
        java.io.IOException
      • writeBodyContent

        private void writeBodyContent​(java.lang.CharSequence content)
                               throws java.io.IOException
        Throws:
        java.io.IOException
      • writeEscaped

        private void writeEscaped​(java.lang.CharSequence content,
                                  boolean isAttributeContent)
                           throws java.io.IOException
        Throws:
        java.io.IOException
      • isOutOfRange

        private boolean isOutOfRange​(char character)
      • closeDocument

        public IXmlWriter closeDocument()
                                 throws java.io.IOException
        Closes all pending elements. When appropriate, resources are also flushed and closed. No exception is raised when called upon a document whose root element has already been closed.
        Specified by:
        closeDocument in interface IXmlWriter
        Returns:
        this object
        Throws:
        OperationNotAllowedException - if called before any call to openElement(java.lang.CharSequence)
        OperationNotAllowedException - if called before any call to #openElement
        java.io.IOException
      • rawWrite

        private void rawWrite​(java.lang.CharSequence sequence)
                       throws java.io.IOException
        Throws:
        java.io.IOException
      • isInvalidName

        private boolean isInvalidName​(java.lang.CharSequence sequence)
      • isValidNameStart

        private boolean isValidNameStart​(char character)
      • isValidNameBody

        private boolean isValidNameBody​(char character)