Package cdc.util.xml

Class XmlWriter

  • All Implemented Interfaces:
    XmlHandler, Closeable, Flushable, AutoCloseable

    public class XmlWriter
    extends Object
    implements XmlHandler, 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.