All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.topologi.diffx.xml.XMLWriter Maven / Gradle / Ivy

Go to download

docx4j is a library which helps you to work with the Office Open XML file format as used in docx documents, pptx presentations, and xlsx spreadsheets.

There is a newer version: 6.1.2
Show newest version
/*
 * This file is part of the DiffX library.
 *
 * For licensing information please see the file license.txt included in the release.
 * A copy of this licence can also be found at
 *   http://www.opensource.org/licenses/artistic-license-2.0.php
 */
package com.topologi.diffx.xml;

import java.io.IOException;

/**
 * Defines a writer for XML data.
 *
 * 

This interface provides simple methods to write XML data onto a writer. * *

Most implementation should wrap a writer or an output stream. Implementations can be * focused on performance, reliability, error reporting, etc... * *

For improved performance, the most efficient solution will generally to have an * implementation write on a buffered writer since the memory usage will generally be * restricted little more than the size of the buffer, and this will keep the I/O * operation to a minimum. * *

Other implementations might want to wrap a SAX content handler. * * @author Christophe Lauret (Allette Systems) * * @version 11 December 2011 */ public interface XMLWriter { // Initialisation methods // ---------------------------------------------------------------------------------------------- /** * Writes the XML declaration. * *

Always: *

   *   <?xml version="1.0" encoding="encoding"?>
   * 
* *

It is followed by a new line character if the indentation is turned on. * * @throws IOException If an I/O exception is thrown by the underlying writer. * @throws IllegalStateException If this method is called after the writer has started * writing elements nodes. */ void xmlDecl() throws IOException; /** * Sets the string to use for indentation. * *

The string must be only composed of valid spaces characters. * *

If the string is null then the indentation is turned off. * * @see Character#isSpaceChar(char) * * @param spaces The indentation string to use. * * @throws IllegalArgumentException If the indent string is not made of spaces. * @throws IllegalStateException If the writer has already been used. */ void setIndentChars(String spaces); // Text methods // ---------------------------------------------------------------------------------------------- /** * Writes the given character correctly for the encoding of this document. * * @param c The character to write. * * @throws IOException If an I/O exception is thrown by the underlying writer. */ void writeText(char c) throws IOException; /** * Writes the given text correctly for the encoding of this document. * *

Does nothing if the text is null. * * @param text The text to write * * @throws IOException If an I/O exception is thrown by the underlying writer. */ void writeText(String text) throws IOException; /** * Write the given text correctly for the encoding of this document. * * @param text The text to write. * @param off The offset where we should start writing the string. * @param len The length of the character subarray to write. * * @throws IOException If an I/O exception is thrown by the underlying writer. */ void writeText(char[] text, int off, int len) throws IOException; /** * Writes the given text as a CDATA section. * *

Does nothing if the text is null. * * @param data The data to write inside the CDATA section. * * @throws IOException If an I/O exception is thrown by the underlying writer. * @throws IllegalArgumentException If the implementation does not support CDATA nesting */ void writeCDATA(String data) throws IOException; // XML methods // ---------------------------------------------------------------------------------------------- /** * Writes the given XML data. * *

The text is appended as is, therefore it should be escaped properly for the * encoding used by the underlying stream writer. * *

Does nothing if the text is null. * * @param text The text to write. * * @throws IOException If an I/O exception is thrown by the underlying writer. */ void writeXML(String text) throws IOException; /** * Write the given XML data. * *

The text is appended as is, therefore it should be escaped properly for the * encoding used by the underlying stream writer. * * @param text The text to write. * @param off The offset where we should start writing the string. * @param len The length of the character subarray to write. * * @throws IOException If an I/O exception is thrown by the underlying writer. */ void writeXML(char[] text, int off, int len) throws IOException; // Comments and PIs // ---------------------------------------------------------------------------------------------- /** * Writes an XML comment. * *

An XML comment is:
*

   *   <!-- comment -->
   * 
* *

Comments are not indented. * *

Does not write anything if the comment if null. * * @param comment The comment to be written * * @throws IOException If thrown by the wrapped writer. * @throws IllegalArgumentException If the comment contains "--". */ void writeComment(String comment) throws IOException; /** * Writes an XML processing instruction. * *

An XML processing intruction is:
*

   *   <?target data?>
   * 
* * @param target The PI's target. * @param data The PI's data. * * @throws IOException If an I/O exception occurs. */ void writePI(String target, String data) throws IOException; // Open/close elements // ---------------------------------------------------------------------------------------------- /** * Writes a start element tag correctly indented. * *

It is the same as openElement(name, false) * * @see #openElement(java.lang.String, boolean) * * @param name the name of the element * * @throws IOException If an I/O exception occurs. */ void openElement(String name) throws IOException; /** * Writes a start element tag correctly indented. * *

Use the hasChildren parameter to specify whether this element is terminal * node or not, note: this affects the indenting. To produce correctly indented XML, you * should use the same value for this flag when closing the element. * *

The name can contain attributes and should be a valid xml name. * * @param name The name of the element * @param hasChildren true if this element has children * * @throws IOException If an I/O exception occurs. */ void openElement(String name, boolean hasChildren) throws IOException; /** * Writes a start element tag correctly indented. * *

Use the hasChildren parameter to specify whether this element is terminal * node or not, note: this affects the indenting. To produce correctly indented XML, you * should use the same value for this flag when closing the element. * *

The name can contain attributes and should be a valid xml name. * * @param uri The namespace URI of the element. * @param name The name of the element. * @param hasChildren true if this element has children. * * @throws IOException If an I/O exception occurs. * @throws UnsupportedOperationException If the implementing class does not handle namespace. */ void openElement(String uri, String name, boolean hasChildren) throws IOException; /** * Close the element automatically. * *

The element is closed symmetrically to the * {@link #openElement(String, String, boolean)} method if the XML writer is namespace * aware or the {@link #openElement(String, boolean)}method. * * @throws IOException If an I/O exception occurs. */ void closeElement() throws IOException; // Element shortcuts // ---------------------------------------------------------------------------------------------- /** * Opens element, inserts text node and closes. * *

This method should behave like: *

   *   this.openElement(name, false);
   *   this.writeText(text);
   *   this.closeElement();
   * 
* * @param name The name of the element. * @param text The text of the element. * * @throws IOException If thrown by the wrapped writer. */ void element(String name, String text) throws IOException; /** * Writes an empty element. * *

It is possible for the element to contain attributes, however, since there is no character * escaping, great care must be taken not to introduce invalid characters. For example: *

   *    <example test="yes"/>
   * 
* * @param element the name of the element * * @throws IOException If an I/O exception occurs. * @throws UnsupportedOperationException If the implementing class does not handle namespace. */ void emptyElement(String element) throws IOException; /** * Writes an empty element. * *

It is possible for the element to contain attributes, however, since there is no character * escaping, great care must be taken not to introduce invalid characters. For example: *

   *    <example test="yes"/>
   * 
* * @param uri The namespace URI of the element. * @param element The name of the element. * * @throws IOException If an I/O exception occurs. * @throws UnsupportedOperationException If the implementing class does not handle namespace. */ void emptyElement(String uri, String element) throws IOException; // Attributes // ---------------------------------------------------------------------------------------------- /** * Writes an attribute. * * @param name The name of the attribute. * @param value The value of the attribute. * * @throws IOException If thrown by the wrapped writer. * @throws IllegalStateException If there is no open element or text has been written. */ void attribute(String name, String value) throws IOException; /** * Writes an attribute. * *

This method for number does not require escaping. * * @param name The name of the attribute. * @param value The value of the attribute. * * @throws IOException If thrown by the wrapped writer. * @throws IllegalStateException If there is no open element or text has been written. */ void attribute(String name, int value) throws IOException; /** * Writes an attribute. * * @param uri The uri of the attribute. * @param name The name of the attribute. * @param value The value of the attribute. * * @throws IOException If thrown by the wrapped writer. * @throws IllegalStateException If there is no open element or text has been written. * @throws UnsupportedOperationException If the implementing class does not handle namespace. */ void attribute(String uri, String name, String value) throws IOException; /** * Writes an attribute. * *

This method for number does not require escaping. * * @param uri The uri of the attribute. * @param name The name of the attribute. * @param value The value of the attribute. * * @throws IOException If thrown by the wrapped writer. * @throws IllegalStateException If there is no open element or text has been written. * @throws UnsupportedOperationException If the implementing class does not handle namespace. */ void attribute(String uri, String name, int value) throws IOException; // Namespace handling // ---------------------------------------------------------------------------------------------- /** * Sets a prefix mapping. * * @param uri The namespace URI. * @param prefix The new prefix for the namespace URI. * * @throws UnsupportedOperationException If the implementing class does not handle namespace. */ void setPrefixMapping(String uri, String prefix); // Direct access to the writer // ---------------------------------------------------------------------------------------------- /** * Flush the writer. * * @throws IOException If thrown by the wrapped writer. */ void flush() throws IOException; /** * Close the writer. * * @throws IOException If thrown by the wrapped writer. * @throws UnclosedElementException If there is still an open element. */ void close() throws IOException; }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy