Package org.dom4j.io

Class XMLWriter

  • All Implemented Interfaces:
    org.xml.sax.ContentHandler, org.xml.sax.DTDHandler, org.xml.sax.EntityResolver, org.xml.sax.ErrorHandler, org.xml.sax.ext.LexicalHandler, org.xml.sax.XMLFilter, org.xml.sax.XMLReader
    Direct Known Subclasses:
    HTMLWriter

    public class XMLWriter
    extends org.xml.sax.helpers.XMLFilterImpl
    implements org.xml.sax.ext.LexicalHandler

    XMLWriter takes a DOM4J tree and formats it to a stream as XML. It can also take SAX events too so can be used by SAX clients as this object implements the ContentHandlerand LexicalHandler interfaces. as well. This formatter performs typical document formatting. The XML declaration and processing instructions are always on their own lines. An OutputFormatobject can be used to define how whitespace is handled when printing and allows various configuration options, such as to allow suppression of the XML declaration, the encoding declaration or whether empty documents are collapsed.

    There are write(...) methods to print any of the standard DOM4J classes, including Document and Element, to either a Writer or an OutputStream. Warning: using your own Writer may cause the writer's preferred character encoding to be ignored. If you use encodings other than UTF8, we recommend using the method that takes an OutputStream instead.

    • Field Detail

      • LEXICAL_HANDLER_NAMES

        protected static final java.lang.String[] LEXICAL_HANDLER_NAMES
      • DEFAULT_FORMAT

        protected static final OutputFormat DEFAULT_FORMAT
      • lastOutputNodeType

        protected int lastOutputNodeType
        Stores the last type of node written so algorithms can refer to the previous node type
      • preserve

        protected boolean preserve
        Stores the xml:space attribute value of preserve for whitespace flag
      • writer

        protected java.io.Writer writer
        The Writer used to output to
    • Constructor Detail

      • XMLWriter

        public XMLWriter(java.io.Writer writer)
      • XMLWriter

        public XMLWriter(java.io.Writer writer,
                         OutputFormat format)
      • XMLWriter

        public XMLWriter()
      • XMLWriter

        public XMLWriter(java.io.OutputStream out)
                  throws java.io.UnsupportedEncodingException
        Throws:
        java.io.UnsupportedEncodingException
      • XMLWriter

        public XMLWriter(java.io.OutputStream out,
                         OutputFormat format)
                  throws java.io.UnsupportedEncodingException
        Throws:
        java.io.UnsupportedEncodingException
      • XMLWriter

        public XMLWriter(OutputFormat format)
                  throws java.io.UnsupportedEncodingException
        Throws:
        java.io.UnsupportedEncodingException
    • Method Detail

      • setWriter

        public void setWriter(java.io.Writer writer)
      • setOutputStream

        public void setOutputStream(java.io.OutputStream out)
                             throws java.io.UnsupportedEncodingException
        Throws:
        java.io.UnsupportedEncodingException
      • isEscapeText

        public boolean isEscapeText()
        DOCUMENT ME!
        Returns:
        true if text thats output should be escaped. This is enabled by default. It could be disabled if the output format is textual, like in XSLT where we can have xml, html or text output.
      • setEscapeText

        public void setEscapeText(boolean escapeText)
        Sets whether text output should be escaped or not. This is enabled by default. It could be disabled if the output format is textual, like in XSLT where we can have xml, html or text output.
        Parameters:
        escapeText - DOCUMENT ME!
      • setIndentLevel

        public void setIndentLevel(int indentLevel)
        Set the initial indentation level. This can be used to output a document (or, more likely, an element) starting at a given indent level, so it's not always flush against the left margin. Default: 0
        Parameters:
        indentLevel - the number of indents to start with
      • getMaximumAllowedCharacter

        public int getMaximumAllowedCharacter()
        Returns the maximum allowed character code that should be allowed unescaped which defaults to 127 in US-ASCII (7 bit) or 255 in ISO- (8 bit).
        Returns:
        DOCUMENT ME!
      • setMaximumAllowedCharacter

        public void setMaximumAllowedCharacter(int maximumAllowedCharacter)
        Sets the maximum allowed character code that should be allowed unescaped such as 127 in US-ASCII (7 bit) or 255 in ISO- (8 bit) or -1 to not escape any characters (other than the special XML characters like < > &) If this is not explicitly set then it is defaulted from the encoding.
        Parameters:
        maximumAllowedCharacter - The maximumAllowedCharacter to set
      • flush

        public void flush()
                   throws java.io.IOException
        Flushes the underlying Writer
        Throws:
        java.io.IOException - DOCUMENT ME!
      • close

        public void close()
                   throws java.io.IOException
        Closes the underlying Writer
        Throws:
        java.io.IOException - DOCUMENT ME!
      • println

        public void println()
                     throws java.io.IOException
        Writes the new line text to the underlying Writer
        Throws:
        java.io.IOException - DOCUMENT ME!
      • write

        public void write(Attribute attribute)
                   throws java.io.IOException
        Writes the given Attribute.
        Parameters:
        attribute - Attribute to output.
        Throws:
        java.io.IOException - DOCUMENT ME!
      • write

        public void write(Document doc)
                   throws java.io.IOException

        This will print the Document to the current Writer.

        Warning: using your own Writer may cause the writer's preferred character encoding to be ignored. If you use encodings other than UTF8, we recommend using the method that takes an OutputStream instead.

        Note: as with all Writers, you may need to flush() yours after this method returns.

        Parameters:
        doc - Document to format.
        Throws:
        java.io.IOException - if there's any problem writing.
      • write

        public void write(Element element)
                   throws java.io.IOException

        Writes the Element, including its Attribute s, and its value, and all its content (child nodes) to the current Writer.

        Parameters:
        element - Element to output.
        Throws:
        java.io.IOException - DOCUMENT ME!
      • write

        public void write(CDATA cdata)
                   throws java.io.IOException
        Writes the given CDATA.
        Parameters:
        cdata - CDATA to output.
        Throws:
        java.io.IOException - DOCUMENT ME!
      • write

        public void write(Comment comment)
                   throws java.io.IOException
        Writes the given Comment.
        Parameters:
        comment - Comment to output.
        Throws:
        java.io.IOException - DOCUMENT ME!
      • write

        public void write(DocumentType docType)
                   throws java.io.IOException
        Writes the given DocumentType.
        Parameters:
        docType - DocumentType to output.
        Throws:
        java.io.IOException - DOCUMENT ME!
      • write

        public void write(Entity entity)
                   throws java.io.IOException
        Writes the given Entity.
        Parameters:
        entity - Entity to output.
        Throws:
        java.io.IOException - DOCUMENT ME!
      • write

        public void write(Namespace namespace)
                   throws java.io.IOException
        Writes the given Namespace.
        Parameters:
        namespace - Namespace to output.
        Throws:
        java.io.IOException - DOCUMENT ME!
      • write

        public void write(ProcessingInstruction processingInstruction)
                   throws java.io.IOException
        Writes the given ProcessingInstruction.
        Parameters:
        processingInstruction - ProcessingInstruction to output.
        Throws:
        java.io.IOException - DOCUMENT ME!
      • write

        public void write(java.lang.String text)
                   throws java.io.IOException

        Print out a String, Perfoms the necessary entity escaping and whitespace stripping.

        Parameters:
        text - is the text to output
        Throws:
        java.io.IOException - DOCUMENT ME!
      • write

        public void write(Text text)
                   throws java.io.IOException
        Writes the given Text.
        Parameters:
        text - Text to output.
        Throws:
        java.io.IOException - DOCUMENT ME!
      • write

        public void write(Node node)
                   throws java.io.IOException
        Writes the given Node.
        Parameters:
        node - Node to output.
        Throws:
        java.io.IOException - DOCUMENT ME!
      • write

        public void write(java.lang.Object object)
                   throws java.io.IOException
        Writes the given object which should be a String, a Node or a List of Nodes.
        Parameters:
        object - is the object to output.
        Throws:
        java.io.IOException - DOCUMENT ME!
      • writeOpen

        public void writeOpen(Element element)
                       throws java.io.IOException

        Writes the opening tag of an Element, including its Attributes but without its content.

        Parameters:
        element - Element to output.
        Throws:
        java.io.IOException - DOCUMENT ME!
      • writeClose

        public void writeClose(Element element)
                        throws java.io.IOException

        Writes the closing tag of an Element

        Parameters:
        element - Element to output.
        Throws:
        java.io.IOException - DOCUMENT ME!
      • parse

        public void parse(org.xml.sax.InputSource source)
                   throws java.io.IOException,
                          org.xml.sax.SAXException
        Specified by:
        parse in interface org.xml.sax.XMLReader
        Overrides:
        parse in class org.xml.sax.helpers.XMLFilterImpl
        Throws:
        java.io.IOException
        org.xml.sax.SAXException
      • setProperty

        public void setProperty(java.lang.String name,
                                java.lang.Object value)
                         throws org.xml.sax.SAXNotRecognizedException,
                                org.xml.sax.SAXNotSupportedException
        Specified by:
        setProperty in interface org.xml.sax.XMLReader
        Overrides:
        setProperty in class org.xml.sax.helpers.XMLFilterImpl
        Throws:
        org.xml.sax.SAXNotRecognizedException
        org.xml.sax.SAXNotSupportedException
      • getProperty

        public java.lang.Object getProperty(java.lang.String name)
                                     throws org.xml.sax.SAXNotRecognizedException,
                                            org.xml.sax.SAXNotSupportedException
        Specified by:
        getProperty in interface org.xml.sax.XMLReader
        Overrides:
        getProperty in class org.xml.sax.helpers.XMLFilterImpl
        Throws:
        org.xml.sax.SAXNotRecognizedException
        org.xml.sax.SAXNotSupportedException
      • setLexicalHandler

        public void setLexicalHandler(org.xml.sax.ext.LexicalHandler handler)
      • getLexicalHandler

        public org.xml.sax.ext.LexicalHandler getLexicalHandler()
      • setDocumentLocator

        public void setDocumentLocator(org.xml.sax.Locator locator)
        Specified by:
        setDocumentLocator in interface org.xml.sax.ContentHandler
        Overrides:
        setDocumentLocator in class org.xml.sax.helpers.XMLFilterImpl
      • startDocument

        public void startDocument()
                           throws org.xml.sax.SAXException
        Specified by:
        startDocument in interface org.xml.sax.ContentHandler
        Overrides:
        startDocument in class org.xml.sax.helpers.XMLFilterImpl
        Throws:
        org.xml.sax.SAXException
      • endDocument

        public void endDocument()
                         throws org.xml.sax.SAXException
        Specified by:
        endDocument in interface org.xml.sax.ContentHandler
        Overrides:
        endDocument in class org.xml.sax.helpers.XMLFilterImpl
        Throws:
        org.xml.sax.SAXException
      • startPrefixMapping

        public void startPrefixMapping(java.lang.String prefix,
                                       java.lang.String uri)
                                throws org.xml.sax.SAXException
        Specified by:
        startPrefixMapping in interface org.xml.sax.ContentHandler
        Overrides:
        startPrefixMapping in class org.xml.sax.helpers.XMLFilterImpl
        Throws:
        org.xml.sax.SAXException
      • endPrefixMapping

        public void endPrefixMapping(java.lang.String prefix)
                              throws org.xml.sax.SAXException
        Specified by:
        endPrefixMapping in interface org.xml.sax.ContentHandler
        Overrides:
        endPrefixMapping in class org.xml.sax.helpers.XMLFilterImpl
        Throws:
        org.xml.sax.SAXException
      • startElement

        public void startElement(java.lang.String namespaceURI,
                                 java.lang.String localName,
                                 java.lang.String qName,
                                 org.xml.sax.Attributes attributes)
                          throws org.xml.sax.SAXException
        Specified by:
        startElement in interface org.xml.sax.ContentHandler
        Overrides:
        startElement in class org.xml.sax.helpers.XMLFilterImpl
        Throws:
        org.xml.sax.SAXException
      • endElement

        public void endElement(java.lang.String namespaceURI,
                               java.lang.String localName,
                               java.lang.String qName)
                        throws org.xml.sax.SAXException
        Specified by:
        endElement in interface org.xml.sax.ContentHandler
        Overrides:
        endElement in class org.xml.sax.helpers.XMLFilterImpl
        Throws:
        org.xml.sax.SAXException
      • characters

        public void characters(char[] ch,
                               int start,
                               int length)
                        throws org.xml.sax.SAXException
        Specified by:
        characters in interface org.xml.sax.ContentHandler
        Overrides:
        characters in class org.xml.sax.helpers.XMLFilterImpl
        Throws:
        org.xml.sax.SAXException
      • ignorableWhitespace

        public void ignorableWhitespace(char[] ch,
                                        int start,
                                        int length)
                                 throws org.xml.sax.SAXException
        Specified by:
        ignorableWhitespace in interface org.xml.sax.ContentHandler
        Overrides:
        ignorableWhitespace in class org.xml.sax.helpers.XMLFilterImpl
        Throws:
        org.xml.sax.SAXException
      • processingInstruction

        public void processingInstruction(java.lang.String target,
                                          java.lang.String data)
                                   throws org.xml.sax.SAXException
        Specified by:
        processingInstruction in interface org.xml.sax.ContentHandler
        Overrides:
        processingInstruction in class org.xml.sax.helpers.XMLFilterImpl
        Throws:
        org.xml.sax.SAXException
      • notationDecl

        public void notationDecl(java.lang.String name,
                                 java.lang.String publicID,
                                 java.lang.String systemID)
                          throws org.xml.sax.SAXException
        Specified by:
        notationDecl in interface org.xml.sax.DTDHandler
        Overrides:
        notationDecl in class org.xml.sax.helpers.XMLFilterImpl
        Throws:
        org.xml.sax.SAXException
      • unparsedEntityDecl

        public void unparsedEntityDecl(java.lang.String name,
                                       java.lang.String publicID,
                                       java.lang.String systemID,
                                       java.lang.String notationName)
                                throws org.xml.sax.SAXException
        Specified by:
        unparsedEntityDecl in interface org.xml.sax.DTDHandler
        Overrides:
        unparsedEntityDecl in class org.xml.sax.helpers.XMLFilterImpl
        Throws:
        org.xml.sax.SAXException
      • startDTD

        public void startDTD(java.lang.String name,
                             java.lang.String publicID,
                             java.lang.String systemID)
                      throws org.xml.sax.SAXException
        Specified by:
        startDTD in interface org.xml.sax.ext.LexicalHandler
        Throws:
        org.xml.sax.SAXException
      • endDTD

        public void endDTD()
                    throws org.xml.sax.SAXException
        Specified by:
        endDTD in interface org.xml.sax.ext.LexicalHandler
        Throws:
        org.xml.sax.SAXException
      • startCDATA

        public void startCDATA()
                        throws org.xml.sax.SAXException
        Specified by:
        startCDATA in interface org.xml.sax.ext.LexicalHandler
        Throws:
        org.xml.sax.SAXException
      • endCDATA

        public void endCDATA()
                      throws org.xml.sax.SAXException
        Specified by:
        endCDATA in interface org.xml.sax.ext.LexicalHandler
        Throws:
        org.xml.sax.SAXException
      • startEntity

        public void startEntity(java.lang.String name)
                         throws org.xml.sax.SAXException
        Specified by:
        startEntity in interface org.xml.sax.ext.LexicalHandler
        Throws:
        org.xml.sax.SAXException
      • endEntity

        public void endEntity(java.lang.String name)
                       throws org.xml.sax.SAXException
        Specified by:
        endEntity in interface org.xml.sax.ext.LexicalHandler
        Throws:
        org.xml.sax.SAXException
      • comment

        public void comment(char[] ch,
                            int start,
                            int length)
                     throws org.xml.sax.SAXException
        Specified by:
        comment in interface org.xml.sax.ext.LexicalHandler
        Throws:
        org.xml.sax.SAXException
      • writeElement

        protected void writeElement(Element element)
                             throws java.io.IOException
        Throws:
        java.io.IOException
      • isElementSpacePreserved

        protected final boolean isElementSpacePreserved(Element element)
        Determines if element is a special case of XML elements where it contains an xml:space attribute of "preserve". If it does, then retain whitespace.
        Parameters:
        element - DOCUMENT ME!
        Returns:
        DOCUMENT ME!
      • writeElementContent

        protected void writeElementContent(Element element)
                                    throws java.io.IOException
        Outputs the content of the given element. If whitespace trimming is enabled then all adjacent text nodes are appended together before the whitespace trimming occurs to avoid problems with multiple text nodes being created due to text content that spans parser buffers in a SAX parser.
        Parameters:
        element - DOCUMENT ME!
        Throws:
        java.io.IOException - DOCUMENT ME!
      • writeCDATA

        protected void writeCDATA(java.lang.String text)
                           throws java.io.IOException
        Throws:
        java.io.IOException
      • writeDocType

        protected void writeDocType(DocumentType docType)
                             throws java.io.IOException
        Throws:
        java.io.IOException
      • writeNamespace

        protected void writeNamespace(Namespace namespace)
                               throws java.io.IOException
        Throws:
        java.io.IOException
      • writeNamespaces

        protected void writeNamespaces()
                                throws java.io.IOException
        Writes the SAX namepsaces
        Throws:
        java.io.IOException - DOCUMENT ME!
      • writeNamespace

        protected void writeNamespace(java.lang.String prefix,
                                      java.lang.String uri)
                               throws java.io.IOException
        Writes the SAX namepsaces
        Parameters:
        prefix - the prefix
        uri - the namespace uri
        Throws:
        java.io.IOException - DOCUMENT ME!
      • writeNamespaces

        protected void writeNamespaces(Element element)
                                throws java.io.IOException
        Writes all namespaces declared directly on element.
        Throws:
        java.io.IOException
      • writeProcessingInstruction

        protected void writeProcessingInstruction(ProcessingInstruction pi)
                                           throws java.io.IOException
        Throws:
        java.io.IOException
      • writeString

        protected void writeString(java.lang.String text)
                            throws java.io.IOException
        Throws:
        java.io.IOException
      • writeNodeText

        protected void writeNodeText(Node node)
                              throws java.io.IOException
        This method is used to write out Nodes that contain text and still allow for xml:space to be handled properly.
        Parameters:
        node - DOCUMENT ME!
        Throws:
        java.io.IOException - DOCUMENT ME!
      • writeNode

        protected void writeNode(Node node)
                          throws java.io.IOException
        Throws:
        java.io.IOException
      • installLexicalHandler

        protected void installLexicalHandler()
      • writeDocType

        protected void writeDocType(java.lang.String name,
                                    java.lang.String publicID,
                                    java.lang.String systemID)
                             throws java.io.IOException
        Throws:
        java.io.IOException
      • writeEntity

        protected void writeEntity(Entity entity)
                            throws java.io.IOException
        Throws:
        java.io.IOException
      • writeEntityRef

        protected void writeEntityRef(java.lang.String name)
                               throws java.io.IOException
        Throws:
        java.io.IOException
      • writeComment

        protected void writeComment(java.lang.String text)
                             throws java.io.IOException
        Throws:
        java.io.IOException
      • writeAttributes

        protected void writeAttributes(Element element)
                                throws java.io.IOException
        Writes the attributes of the given element
        Parameters:
        element - DOCUMENT ME!
        Throws:
        java.io.IOException - DOCUMENT ME!
      • writeAttribute

        protected void writeAttribute(Attribute attribute)
                               throws java.io.IOException
        Throws:
        java.io.IOException
      • writeAttributes

        protected void writeAttributes(org.xml.sax.Attributes attributes)
                                throws java.io.IOException
        Throws:
        java.io.IOException
      • writeAttribute

        protected void writeAttribute(org.xml.sax.Attributes attributes,
                                      int index)
                               throws java.io.IOException
        Throws:
        java.io.IOException
      • indent

        protected void indent()
                       throws java.io.IOException
        Throws:
        java.io.IOException
      • writePrintln

        protected void writePrintln()
                             throws java.io.IOException

        This will print a new line only if the newlines flag was set to true

        Throws:
        java.io.IOException - DOCUMENT ME!
      • createWriter

        protected java.io.Writer createWriter(java.io.OutputStream outStream,
                                              java.lang.String encoding)
                                       throws java.io.UnsupportedEncodingException
        Get an OutputStreamWriter, use preferred encoding.
        Parameters:
        outStream - DOCUMENT ME!
        encoding - DOCUMENT ME!
        Returns:
        DOCUMENT ME!
        Throws:
        java.io.UnsupportedEncodingException - DOCUMENT ME!
      • writeDeclaration

        protected void writeDeclaration()
                                 throws java.io.IOException

        This will write the declaration to the given Writer. Assumes XML version 1.0 since we don't directly know.

        Throws:
        java.io.IOException - DOCUMENT ME!
      • writeClose

        protected void writeClose(java.lang.String qualifiedName)
                           throws java.io.IOException
        Throws:
        java.io.IOException
      • writeEmptyElementClose

        protected void writeEmptyElementClose(java.lang.String qualifiedName)
                                       throws java.io.IOException
        Throws:
        java.io.IOException
      • isExpandEmptyElements

        protected boolean isExpandEmptyElements()
      • escapeElementEntities

        protected java.lang.String escapeElementEntities(java.lang.String text)
        This will take the pre-defined entities in XML 1.0 and convert their character representation to the appropriate entity reference, suitable for XML attributes.
        Parameters:
        text - DOCUMENT ME!
        Returns:
        DOCUMENT ME!
      • writeEscapeAttributeEntities

        protected void writeEscapeAttributeEntities(java.lang.String txt)
                                             throws java.io.IOException
        Throws:
        java.io.IOException
      • escapeAttributeEntities

        protected java.lang.String escapeAttributeEntities(java.lang.String text)
        This will take the pre-defined entities in XML 1.0 and convert their character representation to the appropriate entity reference, suitable for XML attributes.
        Parameters:
        text - DOCUMENT ME!
        Returns:
        DOCUMENT ME!
      • shouldEncodeChar

        protected boolean shouldEncodeChar(int codepoint)
        Should the given character be escaped. This depends on the encoding of the document.
        Parameters:
        codepoint - Unicode codepoint. DOCUMENT ME!
        Returns:
        boolean
      • defaultMaximumAllowedCharacter

        protected int defaultMaximumAllowedCharacter()
        Returns the maximum allowed character code that should be allowed unescaped which defaults to 127 in US-ASCII (7 bit) or 255 in ISO- (8 bit).
        Returns:
        DOCUMENT ME!
      • isNamespaceDeclaration

        protected boolean isNamespaceDeclaration(Namespace ns)
      • handleException

        protected void handleException(java.io.IOException e)
                                throws org.xml.sax.SAXException
        Throws:
        org.xml.sax.SAXException
      • getOutputFormat

        protected OutputFormat getOutputFormat()
        Lets subclasses get at the current format object, so they can call setTrimText, setNewLines, etc. Put in to support the HTMLWriter, in the way that it pushes the current newline/trim state onto a stack and overrides the state within preformatted tags.
        Returns:
        DOCUMENT ME!
      • resolveEntityRefs

        public boolean resolveEntityRefs()
      • setResolveEntityRefs

        public void setResolveEntityRefs(boolean resolve)