Package cdc.util.xml

Class XmlWriter

  • All Implemented Interfaces:
    Closeable, Flushable, AutoCloseable

    public class XmlWriter
    extends Object
    implements Flushable, Closeable
    Basic XML writer.

    This class does not support all XML features. It supports specific features:

    • Pretty printing.
    • Name conversion for elements and attributes.
    • Standard ('\n') or platform specific End of line.
    • Standard compression algorithms.
    • ...
    WARNING: This class does not check full conformity of generated XML.

    Pretty printing
    Pretty printing may produce unexpected results with mixed content elements.
    This implementation tries to produce data as soon as possible.
    Discovering that an element has a mixed content may happen late, so that indentation behavior changes after such discovery.
    If one knows that an element has a mixed content, it is advised to indicate it to the writer by writing an empty string content first in that element.

    Author:
    Damien Carbonne
    • Constructor Detail

      • XmlWriter

        public XmlWriter​(Writer writer,
                         String encoding)
        Creates an XmlWriter to a Writer with a given encoding.
        Parameters:
        writer - The writer.
        encoding - The encoding to use.
      • XmlWriter

        public XmlWriter​(Writer writer)
        Creates an XmlWriter to a Writer, with UTF-8 encoding.
        Parameters:
        writer - The writer.
      • XmlWriter

        public XmlWriter​(PrintStream out)
                  throws IOException
        Creates an XmlWriter to a PrintStream, with UTF-8 encoding.
        Parameters:
        out - The PrintStream to use. Must be closed be the caller.
        Throws:
        UnsupportedEncodingException - If UTF-8 encoding is not supported, which should not happen.
        IOException - When an IO exception occurs.
      • XmlWriter

        public XmlWriter​(OutputStream os,
                         String encoding)
                  throws IOException
        Creates an XmlWriter to an OutputStream with a given encoding.
        Parameters:
        os - The outputStream. Must be closed by caller.
        encoding - The encoding to use.
        Throws:
        UnsupportedEncodingException - If the named encoding is not supported.
        IOException - When an IO exception occurs.
      • XmlWriter

        public XmlWriter​(OutputStream os)
                  throws IOException
        Creates an XmlWriter to an OutputStream with UTF-8 encoding.
        Parameters:
        os - The outputStream. Must be closed by caller.
        Throws:
        UnsupportedEncodingException - If the named encoding is not supported.
        IOException - When an IO exception occurs.
      • XmlWriter

        public XmlWriter​(String filename,
                         String encoding,
                         Compressor compressor)
                  throws IOException
        Creates an XmlWriter to a file with a given encoding and compressor.
        Parameters:
        filename - Name of the file.
        encoding - The encoding to use.
        compressor - The compressor.
        Throws:
        FileNotFoundException - If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason.
        UnsupportedEncodingException - If the named encoding is not supported.
        IOException - When an IO exception occurs.
      • XmlWriter

        public XmlWriter​(String filename,
                         String encoding)
                  throws IOException
        Creates an XmlWriter to a file with a given encoding.
        Parameters:
        filename - Name of the file.
        encoding - The encoding to use.
        Throws:
        FileNotFoundException - If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason.
        UnsupportedEncodingException - If the named encoding is not supported.
        IOException - When an IO exception occurs.
      • XmlWriter

        public XmlWriter​(String filename,
                         Compressor compressor)
                  throws IOException
        Creates an XmlWriter to a file with a UTF-8 encoding and compressor.
        Parameters:
        filename - Name of the file.
        compressor - The compressor.
        Throws:
        FileNotFoundException - If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason.
        UnsupportedEncodingException - If the named encoding is not supported.
        IOException - When an IO exception occurs.
      • XmlWriter

        public XmlWriter​(String filename)
                  throws IOException
        Creates an XmlWriter to a file with UTF-8 encoding.
        Parameters:
        filename - Name of the file.
        Throws:
        FileNotFoundException - If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason.
        UnsupportedEncodingException - If the named encoding is not supported.
        IOException - When an IO exception occurs.
      • XmlWriter

        public XmlWriter​(File file,
                         String encoding,
                         Compressor compressor)
                  throws IOException
        Creates an XmlWriter to a file with a given encoding and compressor.
        Parameters:
        file - File to use.
        encoding - The encoding to use.
        compressor - The compressor to use.
        Throws:
        FileNotFoundException - If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason.
        UnsupportedEncodingException - If the named encoding is not supported.
        IOException - When an IO exception occurs.
      • XmlWriter

        public XmlWriter​(File file,
                         String encoding)
                  throws IOException
        Creates an XmlWriter to a file with a given encoding.
        Parameters:
        file - File to use.
        encoding - The encoding to use.
        Throws:
        FileNotFoundException - If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason.
        UnsupportedEncodingException - If the named encoding is not supported.
        IOException - When an IO exception occurs.
      • XmlWriter

        public XmlWriter​(File file,
                         Compressor compressor)
                  throws IOException
        Creates an XmlWriter to a file with UTF-8 encoding and compressor.
        Parameters:
        file - File to use.
        compressor - The compressor to use.
        Throws:
        FileNotFoundException - If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason.
        UnsupportedEncodingException - If the named encoding is not supported.
        IOException - When an IO exception occurs.
      • XmlWriter

        public XmlWriter​(File file)
                  throws IOException
        Creates an XmlWriter to a file using UTF-8 encoding.
        Parameters:
        file - File to use.
        Throws:
        FileNotFoundException - If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason.
        UnsupportedEncodingException - If UTF-8 encoding is not supported, which should not happen.
        IOException - When an IO exception occurs.
    • Method Detail

      • dataError

        protected static void dataError​(String message)
      • setEnabled

        public final XmlWriter setEnabled​(XmlWriter.Feature feature,
                                          boolean enabled)
        Enables or disables a feature.
        Parameters:
        feature - The feature to enable/disable.
        enabled - If true, enable the feature.
        Returns:
        This writer.
      • isEnabled

        public final boolean isEnabled​(XmlWriter.Feature feature)
        Returns true if a feature is enabled, false otherwise.
        Parameters:
        feature - The feature.
        Returns:
        true if feature is enabled, false otherwise.
      • setIndentString

        public final XmlWriter setIndentString​(String s)
        Set the indent string.
        Parameters:
        s - The indent string.
        Returns:
        This writer.
      • getIndentString

        public final String getIndentString()
      • setTabSize

        public final void setTabSize​(int tabSize)
      • setEndOfLine

        public final void setEndOfLine​(String eol)
        Set the string to use for end of lines.

        This may be useful to force an OS specific symbol.
        This has interactions with XmlWriter.Feature.USE_XML_EOL.
        WARNING: It is the responsibility of programmer to use a valid string.

        Parameters:
        eol - The end of line string.
      • getEndOfLine

        public final String getEndOfLine()
        Returns:
        The end of line string.
      • getConverter

        public final CaseConverter getConverter()
        Returns the case converter. Used if USE_CONVERTER feature is enabled.
        Returns:
        The case converter.
      • setCaseConverter

        public final void setCaseConverter​(CaseConverter converter)
      • beginDocument

        public final void beginDocument​(XmlVersion version)
                                 throws IOException
        Begins an XML document, using writer's encoding.
        Parameters:
        version - The XML version to use.
        Throws:
        IOException - If an I/O error occurs.
      • beginDocument

        public final void beginDocument()
                                 throws IOException
        Begins an XML document, using writer's encoding and version 1.0.
        Throws:
        IOException - If an I/O error occurs.
      • addProcessingInstructionContent

        public final void addProcessingInstructionContent​(String content)
                                                   throws IOException
        Throws:
        IOException
      • endProcessingInstruction

        public final void endProcessingInstruction()
                                            throws IOException
        Throws:
        IOException
      • addStyleSheet

        public final void addStyleSheet​(String xsl)
                                 throws IOException
        Helper function used to insert a StyleSheet Processing Instruction.
        Parameters:
        xsl - The URL of the style sheet to use.
        Throws:
        IOException - If an I/O error occurs.
      • beginComment

        public final void beginComment()
                                throws IOException
        Starts the writing of a new comment.
        Throws:
        IOException - If an I/O error occurs.
      • addCommentContent

        public final void addCommentContent​(String content)
        Adds content to current comment.
        Parameters:
        content - The text to add to current comment.
      • endComment

        public final void endComment()
                              throws IOException
        Closes the current comment.
        Throws:
        IOException - If an I/O error occurs.
      • addComment

        public final void addComment​(String content)
                              throws IOException
        Inserts a comment section with a content.
        Parameters:
        content - The comment content.
        Throws:
        IOException - If an I/O error occurs.
      • beginCData

        public final void beginCData()
                              throws IOException
        Starts the writing of a new CData.
        Throws:
        IOException - If an I/O error occurs.
      • addCDataContent

        public final void addCDataContent​(String content)
        Adds content to current CData.
        Parameters:
        content - The text to add to current CData.
      • endCData

        public final void endCData()
                            throws IOException
        Closes the current CData.
        Throws:
        IOException - If an I/O error occurs.
      • addCData

        public final void addCData​(String content)
                            throws IOException
        Inserts a CData section with a content.
        Parameters:
        content - The CData content.
        Throws:
        IOException - If an I/O error occurs.
      • addAttribute

        public final void addAttribute​(String name,
                                       Object value)
                                throws IOException
        Adds an Object attribute to current element.

        If object is null adds an empty attribute.
        Otherwise, uses toString() to convert value to a String.

        Parameters:
        name - Attribute name.
        value - Attribute value.
        Throws:
        IOException - If an I/O error occurs.
      • addAttribute

        public final void addAttribute​(String name,
                                       String value)
                                throws IOException
        Adds a String attribute to current element.
        Parameters:
        name - Attribute name.
        value - Attribute value.
        Throws:
        IOException - If an I/O error occurs.
      • addAttribute

        public final void addAttribute​(String name,
                                       char value)
                                throws IOException
        Adds a char attribute to current element.
        Parameters:
        name - Attribute name.
        value - Attribute value.
        Throws:
        IOException - If an I/O error occurs.
      • addAttribute

        public final void addAttribute​(String name,
                                       boolean value)
                                throws IOException
        Adds a boolean attribute to current element.
        Parameters:
        name - Attribute name.
        value - Attribute value.
        Throws:
        IOException - If an I/O error occurs.
      • addAttribute

        public final void addAttribute​(String name,
                                       long value)
                                throws IOException
        Adds a long attribute to current element.
        Parameters:
        name - Attribute name.
        value - Attribute value.
        Throws:
        IOException - If an I/O error occurs.
      • addAttribute

        public final void addAttribute​(String name,
                                       int value)
                                throws IOException
        Adds an int attribute to current element.
        Parameters:
        name - Attribute name.
        value - Attribute value.
        Throws:
        IOException - If an I/O error occurs.
      • addAttribute

        public final void addAttribute​(String name,
                                       short value)
                                throws IOException
        Adds a short attribute to current element.
        Parameters:
        name - Attribute name.
        value - Attribute value.
        Throws:
        IOException - If an I/O error occurs.
      • addAttribute

        public final void addAttribute​(String name,
                                       byte value)
                                throws IOException
        Adds a byte attribute to current element.
        Parameters:
        name - Attribute name.
        value - Attribute value.
        Throws:
        IOException - If an I/O error occurs.
      • addAttribute

        public final void addAttribute​(String name,
                                       double value)
                                throws IOException
        Adds a double attribute to current element.
        Parameters:
        name - Attribute name.
        value - Attribute value.
        Throws:
        IOException - If an I/O error occurs.
      • addAttribute

        public final void addAttribute​(String name,
                                       float value)
                                throws IOException
        Adds a float attribute to current element.
        Parameters:
        name - Attribute name.
        value - Attribute value.
        Throws:
        IOException - If an I/O error occurs.
      • addNamespace

        public final void addNamespace​(String prefix,
                                       String uri)
                                throws IOException
        Adds a namespace declaration.
        Parameters:
        prefix - Namespace prefix
        uri - Associated URI.
        Throws:
        IOException - If an I/O error occurs.
      • reset

        public void reset()