Class XMLWriter

    • Field Detail

      • LEXICAL_HANDLER_NAMES

        protected static final String[] LEXICAL_HANDLER_NAMES
      • DEFAULT_FORMAT

        protected static final org.dom4j.io.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 Writer writer
        The Writer used to output to
    • Method Detail

      • setWriter

        public void setWriter​(Writer writer)
      • isEscapeText

        public boolean isEscapeText()
        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 - true to escape text, otherwise false
      • 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:
        the maximum character code
      • 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 IOException
        Flushes the underlying Writer
        Throws:
        IOException - if the writer could not be flushed
      • close

        public void close()
                   throws IOException
        Closes the underlying Writer
        Throws:
        IOException - if the writer could not be closed
      • println

        public void println()
                     throws IOException
        Writes the new line text to the underlying Writer
        Throws:
        IOException - if the new line could not be written
      • write

        public void write​(org.dom4j.Attribute attribute)
                   throws IOException
        Writes the given Attribute.
        Parameters:
        attribute - Attribute to output.
        Throws:
        IOException - if the attribute could not be written
      • write

        public void write​(org.dom4j.Document doc)
                   throws 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:
        IOException - - if there's any problem writing.
      • write

        public void write​(org.dom4j.Element element)
                   throws IOException

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

        Parameters:
        element - Element to output.
        Throws:
        IOException - if the element could not be written
      • write

        public void write​(org.dom4j.CDATA cdata)
                   throws IOException
        Writes the given CDATA.
        Parameters:
        cdata - CDATA to output.
        Throws:
        IOException - if the cdata could not be written
      • write

        public void write​(org.dom4j.Comment comment)
                   throws IOException
        Writes the given Comment.
        Parameters:
        comment - Comment to output.
        Throws:
        IOException - if the comment could not be written
      • write

        public void write​(org.dom4j.DocumentType docType)
                   throws IOException
        Writes the given DocumentType.
        Parameters:
        docType - DocumentType to output.
        Throws:
        IOException - if the docType could not be written
      • write

        public void write​(org.dom4j.Entity entity)
                   throws IOException
        Writes the given Entity.
        Parameters:
        entity - Entity to output.
        Throws:
        IOException - if the entity could not be written
      • write

        public void write​(org.dom4j.Namespace namespace)
                   throws IOException
        Writes the given Namespace.
        Parameters:
        namespace - Namespace to output.
        Throws:
        IOException - if the namespace could not be written
      • write

        public void write​(org.dom4j.ProcessingInstruction processingInstruction)
                   throws IOException
        Writes the given ProcessingInstruction.
        Parameters:
        processingInstruction - ProcessingInstruction to output.
        Throws:
        IOException - if the instruction could not be written
      • write

        public void write​(String text)
                   throws IOException

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

        Parameters:
        text - is the text to output
        Throws:
        IOException - if the text could not be written
      • write

        public void write​(org.dom4j.Text text)
                   throws IOException
        Writes the given Text.
        Parameters:
        text - Text to output.
        Throws:
        IOException - if the text could not be written
      • write

        public void write​(org.dom4j.Node node)
                   throws IOException
        Writes the given Node.
        Parameters:
        node - Node to output.
        Throws:
        IOException - if the node could not be written
      • write

        public void write​(Object object)
                   throws 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:
        IOException - if the object could not be written
      • writeOpen

        public void writeOpen​(org.dom4j.Element element)
                       throws IOException

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

        Parameters:
        element - Element to output.
        Throws:
        IOException - if the element could not be written
      • writeClose

        public void writeClose​(org.dom4j.Element element)
                        throws IOException

        Writes the closing tag of an Element

        Parameters:
        element - Element to output.
        Throws:
        IOException - if the element could not be written
      • setLexicalHandler

        public void setLexicalHandler​(LexicalHandler handler)
      • writeElement

        protected void writeElement​(org.dom4j.Element element)
                             throws IOException
        Throws:
        IOException
      • isElementSpacePreserved

        protected final boolean isElementSpacePreserved​(org.dom4j.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 - the element to check
        Returns:
        true if whitespace should be preserved, otherwise false
      • writeElementContent

        protected void writeElementContent​(org.dom4j.Element element)
                                    throws 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 - the element to write
        Throws:
        IOException - if the element could not be written
      • writeDocType

        protected void writeDocType​(org.dom4j.DocumentType docType)
                             throws IOException
        Throws:
        IOException
      • writeNamespace

        protected void writeNamespace​(org.dom4j.Namespace namespace)
                               throws IOException
        Throws:
        IOException
      • writeNamespaces

        protected void writeNamespaces()
                                throws IOException
        Writes the SAX namepsaces
        Throws:
        IOException - if the namespaces could not be written
      • writeNamespace

        protected void writeNamespace​(String prefix,
                                      String uri)
                               throws IOException
        Writes the SAX namepsaces
        Parameters:
        prefix - the namespace prefix
        uri - the URL of the namespace
        Throws:
        IOException - if the namespace could not be written
      • writeProcessingInstruction

        protected void writeProcessingInstruction​(org.dom4j.ProcessingInstruction processingInstruction)
                                           throws IOException
        Throws:
        IOException
      • writeNodeText

        protected void writeNodeText​(org.dom4j.Node node)
                              throws IOException
        This method is used to write out Nodes that contain text and still allow for xml:space to be handled properly.
        Parameters:
        node - the node to write
        Throws:
        IOException - if the node could not be written
      • installLexicalHandler

        protected void installLexicalHandler()
      • writeEntity

        protected void writeEntity​(org.dom4j.Entity entity)
                            throws IOException
        Throws:
        IOException
      • writeAttributes

        protected void writeAttributes​(org.dom4j.Element element)
                                throws IOException
        Writes the attributes of the given element
        Parameters:
        element - the element whose attributes should be written
        Throws:
        IOException - if the element could not be written
      • writeAttribute

        protected void writeAttribute​(org.dom4j.Attribute attribute)
                               throws IOException
        Throws:
        IOException
      • writePrintln

        protected void writePrintln()
                             throws IOException

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

        Throws:
        IOException - if the new line could not be written
      • writeDeclaration

        protected void writeDeclaration()
                                 throws IOException

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

        Throws:
        IOException - if the declaration could not be written
      • isExpandEmptyElements

        protected boolean isExpandEmptyElements()
      • escapeElementEntities

        protected String escapeElementEntities​(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 - the entities to escale
        Returns:
        the escaped entities
      • escapeAttributeEntities

        protected String escapeAttributeEntities​(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 - the entitie to escape
        Returns:
        the escaped entity
      • shouldEncodeChar

        protected boolean shouldEncodeChar​(char c)
        Should the given character be escaped. This depends on the encoding of the document.
        Parameters:
        c - the character to check
        Returns:
        true to escape the character, otherwise false
      • 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:
        the maximum allow character code
      • isNamespaceDeclaration

        protected boolean isNamespaceDeclaration​(org.dom4j.Namespace ns)
      • getOutputFormat

        protected org.dom4j.io.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:
        the output format
      • resolveEntityRefs

        public boolean resolveEntityRefs()
      • setResolveEntityRefs

        public void setResolveEntityRefs​(boolean resolve)