Example usage for org.w3c.dom DOMException initCause

List of usage examples for org.w3c.dom DOMException initCause

Introduction

In this page you can find the example usage for org.w3c.dom DOMException initCause.

Prototype

public synchronized Throwable initCause(Throwable cause) 

Source Link

Document

Initializes the cause of this throwable to the specified value.

Usage

From source file:Main.java

/**
 * Serialise the supplied W3C DOM subtree.
 *
 * @param nodeList The DOM subtree as a NodeList.
 * @param format Format the output.//from ww w. j a va2  s  .c  om
 * @param writer The target writer for serialization.
 * @throws DOMException Unable to serialise the DOM.
 */
public static void serialize(NodeList nodeList, boolean format, Writer writer) throws DOMException {

    if (nodeList == null) {
        throw new IllegalArgumentException("null 'subtree' NodeIterator arg in method call.");
    }

    try {
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer;

        if (format) {
            try {
                factory.setAttribute("indent-number", new Integer(4));
            } catch (Exception e) {
                // Ignore... Xalan may throw on this!!
                // We handle Xalan indentation below (yeuckkk) ...
            }
        }
        transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        if (format) {
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "4");
        }

        int listLength = nodeList.getLength();

        // Iterate through the Node List.
        for (int i = 0; i < listLength; i++) {
            Node node = nodeList.item(i);

            if (isTextNode(node)) {
                writer.write(node.getNodeValue());
            } else if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
                writer.write(((Attr) node).getValue());
            } else if (node.getNodeType() == Node.ELEMENT_NODE) {
                transformer.transform(new DOMSource(node), new StreamResult(writer));
            }
        }
    } catch (Exception e) {
        DOMException domExcep = new DOMException(DOMException.INVALID_ACCESS_ERR,
                "Unable to serailise DOM subtree.");
        domExcep.initCause(e);
        throw domExcep;
    }
}