286N/A/*
286N/A * Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
286N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
286N/A *
286N/A * This code is free software; you can redistribute it and/or modify it
286N/A * under the terms of the GNU General Public License version 2 only, as
286N/A * published by the Free Software Foundation. Oracle designates this
286N/A * particular file as subject to the "Classpath" exception as provided
286N/A * by Oracle in the LICENSE file that accompanied this code.
286N/A *
286N/A * This code is distributed in the hope that it will be useful, but WITHOUT
286N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
286N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
286N/A * version 2 for more details (a copy is included in the LICENSE file that
286N/A * accompanied this code).
286N/A *
286N/A * You should have received a copy of the GNU General Public License version
286N/A * 2 along with this work; if not, write to the Free Software Foundation,
286N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
286N/A *
286N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
286N/A * or visit www.oracle.com if you need additional information or have any
286N/A * questions.
286N/A */
286N/A
286N/Apackage javax.xml.transform.dom;
286N/A
286N/Aimport javax.xml.transform.Result;
286N/Aimport org.w3c.dom.Node;
286N/A
286N/A/**
286N/A * <p>Acts as a holder for a transformation result tree in the form of a Document Object Model (DOM) tree.</p>
286N/A *
286N/A * <p>If no output DOM source is set, the transformation will create a Document node as the holder for the result of the transformation,
286N/A * which may be retrieved with {@link #getNode()}.</p>
286N/A *
286N/A * @author <a href="Jeff.Suttor@Sun.com">Jeff Suttor</a>
286N/A */
286N/Apublic class DOMResult implements Result {
286N/A
286N/A /** <p>If {@link javax.xml.transform.TransformerFactory#getFeature}
286N/A * returns <code>true</code> when passed this value as an argument,
286N/A * the <code>Transformer</code> supports <code>Result</code> output of this type.</p>
286N/A */
286N/A public static final String FEATURE = "http://javax.xml.transform.dom.DOMResult/feature";
286N/A
286N/A /**
286N/A * <p>Zero-argument default constructor.</p>
286N/A *
286N/A * <p><code>node</code>,
286N/A * <code>siblingNode</code> and
286N/A * <code>systemId</code>
286N/A * will be set to <code>null</code>.</p>
286N/A */
286N/A public DOMResult() {
286N/A setNode(null);
286N/A setNextSibling(null);
286N/A setSystemId(null);
286N/A }
286N/A
286N/A /**
286N/A * <p>Use a DOM node to create a new output target.</p>
286N/A *
286N/A * <p>In practice, the node should be
286N/A * a {@link org.w3c.dom.Document} node,
286N/A * a {@link org.w3c.dom.DocumentFragment} node, or
286N/A * a {@link org.w3c.dom.Element} node.
286N/A * In other words, a node that accepts children.</p>
286N/A *
286N/A * <p><code>siblingNode</code> and
286N/A * <code>systemId</code>
286N/A * will be set to <code>null</code>.</p>
286N/A *
286N/A * @param node The DOM node that will contain the result tree.
286N/A */
286N/A public DOMResult(Node node) {
286N/A setNode(node);
286N/A setNextSibling(null);
286N/A setSystemId(null);
286N/A }
286N/A
286N/A /**
286N/A * <p>Use a DOM node to create a new output target with the specified System ID.<p>
286N/A *
286N/A * <p>In practice, the node should be
286N/A * a {@link org.w3c.dom.Document} node,
286N/A * a {@link org.w3c.dom.DocumentFragment} node, or
286N/A * a {@link org.w3c.dom.Element} node.
286N/A * In other words, a node that accepts children.</p>
286N/A *
286N/A * <p><code>siblingNode</code> will be set to <code>null</code>.</p>
286N/A *
286N/A * @param node The DOM node that will contain the result tree.
286N/A * @param systemId The system identifier which may be used in association with this node.
286N/A */
286N/A public DOMResult(Node node, String systemId) {
286N/A setNode(node);
286N/A setNextSibling(null);
286N/A setSystemId(systemId);
286N/A }
286N/A
286N/A /**
286N/A * <p>Use a DOM node to create a new output target specifying the child node where the result nodes should be inserted before.</p>
286N/A *
286N/A * <p>In practice, <code>node</code> and <code>nextSibling</code> should be
286N/A * a {@link org.w3c.dom.Document} node,
286N/A * a {@link org.w3c.dom.DocumentFragment} node, or
286N/A * a {@link org.w3c.dom.Element} node.
286N/A * In other words, a node that accepts children.</p>
286N/A *
286N/A * <p>Use <code>nextSibling</code> to specify the child node
286N/A * where the result nodes should be inserted before.
286N/A * If <code>nextSibling</code> is not a sibling of <code>node</code>,
286N/A * then an <code>IllegalArgumentException</code> is thrown.
286N/A * If <code>node</code> is <code>null</code> and <code>nextSibling</code> is not <code>null</code>,
286N/A * then an <code>IllegalArgumentException</code> is thrown.
286N/A * If <code>nextSibling</code> is <code>null</code>,
286N/A * then the behavior is the same as calling {@link #DOMResult(Node node)},
286N/A * i.e. append the result nodes as the last child of the specified <code>node</code>.</p>
286N/A *
286N/A * <p><code>systemId</code> will be set to <code>null</code>.</p>
286N/A *
286N/A * @param node The DOM node that will contain the result tree.
286N/A * @param nextSibling The child node where the result nodes should be inserted before.
286N/A *
286N/A * @throws IllegalArgumentException If <code>nextSibling</code> is not a sibling of <code>node</code> or
286N/A * <code>node</code> is <code>null</code> and <code>nextSibling</code>
286N/A * is not <code>null</code>.
286N/A *
286N/A * @since 1.5
286N/A */
286N/A public DOMResult(Node node, Node nextSibling) {
286N/A
286N/A // does the corrent parent/child relationship exist?
286N/A if (nextSibling != null) {
286N/A // cannot be a sibling of a null node
286N/A if (node == null) {
286N/A throw new IllegalArgumentException("Cannot create a DOMResult when the nextSibling is contained by the \"null\" node.");
286N/A }
286N/A
286N/A // nextSibling contained by node?
286N/A if ((node.compareDocumentPosition(nextSibling)&Node.DOCUMENT_POSITION_CONTAINED_BY)==0) {
286N/A throw new IllegalArgumentException("Cannot create a DOMResult when the nextSibling is not contained by the node.");
286N/A }
286N/A }
286N/A
286N/A setNode(node);
286N/A setNextSibling(nextSibling);
286N/A setSystemId(null);
286N/A }
286N/A
286N/A /**
286N/A * <p>Use a DOM node to create a new output target specifying the child node where the result nodes should be inserted before and
286N/A * the specified System ID.</p>
286N/A *
286N/A * <p>In practice, <code>node</code> and <code>nextSibling</code> should be
286N/A * a {@link org.w3c.dom.Document} node,
286N/A * a {@link org.w3c.dom.DocumentFragment} node, or a
286N/A * {@link org.w3c.dom.Element} node.
286N/A * In other words, a node that accepts children.</p>
286N/A *
286N/A * <p>Use <code>nextSibling</code> to specify the child node
286N/A * where the result nodes should be inserted before.
286N/A * If <code>nextSibling</code> is not a sibling of <code>node</code>,
286N/A * then an <code>IllegalArgumentException</code> is thrown.
286N/A * If <code>node</code> is <code>null</code> and <code>nextSibling</code> is not <code>null</code>,
286N/A * then an <code>IllegalArgumentException</code> is thrown.
286N/A * If <code>nextSibling</code> is <code>null</code>,
286N/A * then the behavior is the same as calling {@link #DOMResult(Node node, String systemId)},
286N/A * i.e. append the result nodes as the last child of the specified node and use the specified System ID.</p>
286N/A *
286N/A * @param node The DOM node that will contain the result tree.
286N/A * @param nextSibling The child node where the result nodes should be inserted before.
286N/A * @param systemId The system identifier which may be used in association with this node.
286N/A *
286N/A * @throws IllegalArgumentException If <code>nextSibling</code> is not a
286N/A * sibling of <code>node</code> or
286N/A * <code>node</code> is <code>null</code> and <code>nextSibling</code>
286N/A * is not <code>null</code>.
286N/A *
286N/A * @since 1.5
286N/A */
286N/A public DOMResult(Node node, Node nextSibling, String systemId) {
286N/A
286N/A // does the corrent parent/child relationship exist?
286N/A if (nextSibling != null) {
286N/A // cannot be a sibling of a null node
286N/A if (node == null) {
286N/A throw new IllegalArgumentException("Cannot create a DOMResult when the nextSibling is contained by the \"null\" node.");
286N/A }
286N/A
286N/A // nextSibling contained by node?
286N/A if ((node.compareDocumentPosition(nextSibling)&Node.DOCUMENT_POSITION_CONTAINED_BY)==0) {
286N/A throw new IllegalArgumentException("Cannot create a DOMResult when the nextSibling is not contained by the node.");
286N/A }
286N/A }
286N/A
286N/A setNode(node);
286N/A setNextSibling(nextSibling);
286N/A setSystemId(systemId);
286N/A }
286N/A
286N/A /**
286N/A * <p>Set the node that will contain the result DOM tree.<p>
286N/A *
286N/A * <p>In practice, the node should be
286N/A * a {@link org.w3c.dom.Document} node,
286N/A * a {@link org.w3c.dom.DocumentFragment} node, or
286N/A * a {@link org.w3c.dom.Element} node.
286N/A * In other words, a node that accepts children.</p>
286N/A *
286N/A * <p>An <code>IllegalStateException</code> is thrown if
286N/A * <code>nextSibling</code> is not <code>null</code> and
286N/A * <code>node</code> is not a parent of <code>nextSibling</code>.
286N/A * An <code>IllegalStateException</code> is thrown if <code>node</code> is <code>null</code> and
286N/A * <code>nextSibling</code> is not <code>null</code>.</p>
286N/A *
286N/A * @param node The node to which the transformation will be appended.
286N/A *
286N/A * @throws IllegalStateException If <code>nextSibling</code> is not
286N/A * <code>null</code> and
286N/A * <code>nextSibling</code> is not a child of <code>node</code> or
286N/A * <code>node</code> is <code>null</code> and
286N/A * <code>nextSibling</code> is not <code>null</code>.
286N/A */
286N/A public void setNode(Node node) {
286N/A // does the corrent parent/child relationship exist?
286N/A if (nextSibling != null) {
286N/A // cannot be a sibling of a null node
286N/A if (node == null) {
286N/A throw new IllegalStateException("Cannot create a DOMResult when the nextSibling is contained by the \"null\" node.");
286N/A }
286N/A
286N/A // nextSibling contained by node?
286N/A if ((node.compareDocumentPosition(nextSibling)&Node.DOCUMENT_POSITION_CONTAINED_BY)==0) {
286N/A throw new IllegalArgumentException("Cannot create a DOMResult when the nextSibling is not contained by the node.");
286N/A }
286N/A }
286N/A
286N/A this.node = node;
286N/A }
286N/A
286N/A /**
286N/A * <p>Get the node that will contain the result DOM tree.</p>
286N/A *
286N/A * <p>If no node was set via
286N/A * {@link #DOMResult(Node node)},
286N/A * {@link #DOMResult(Node node, String systeId)},
286N/A * {@link #DOMResult(Node node, Node nextSibling)},
286N/A * {@link #DOMResult(Node node, Node nextSibling, String systemId)} or
286N/A * {@link #setNode(Node node)},
286N/A * then the node will be set by the transformation, and may be obtained from this method once the transformation is complete.
286N/A * Calling this method before the transformation will return <code>null</code>.</p>
286N/A *
286N/A * @return The node to which the transformation will be appended.
286N/A */
286N/A public Node getNode() {
286N/A return node;
286N/A }
286N/A
286N/A /**
286N/A * <p>Set the child node before which the result nodes will be inserted.</p>
286N/A *
286N/A * <p>Use <code>nextSibling</code> to specify the child node
286N/A * before which the result nodes should be inserted.
286N/A * If <code>nextSibling</code> is not a descendant of <code>node</code>,
286N/A * then an <code>IllegalArgumentException</code> is thrown.
286N/A * If <code>node</code> is <code>null</code> and <code>nextSibling</code> is not <code>null</code>,
286N/A * then an <code>IllegalStateException</code> is thrown.
286N/A * If <code>nextSibling</code> is <code>null</code>,
286N/A * then the behavior is the same as calling {@link #DOMResult(Node node)},
286N/A * i.e. append the result nodes as the last child of the specified <code>node</code>.</p>
286N/A *
286N/A * @param nextSibling The child node before which the result nodes will be inserted.
286N/A *
286N/A * @throws IllegalArgumentException If <code>nextSibling</code> is not a
286N/A * descendant of <code>node</code>.
286N/A * @throws IllegalStateException If <code>node</code> is <code>null</code>
286N/A * and <code>nextSibling</code> is not <code>null</code>.
286N/A *
286N/A * @since 1.5
286N/A */
286N/A public void setNextSibling(Node nextSibling) {
286N/A
286N/A // does the corrent parent/child relationship exist?
286N/A if (nextSibling != null) {
286N/A // cannot be a sibling of a null node
286N/A if (node == null) {
286N/A throw new IllegalStateException("Cannot create a DOMResult when the nextSibling is contained by the \"null\" node.");
286N/A }
286N/A
286N/A // nextSibling contained by node?
286N/A if ((node.compareDocumentPosition(nextSibling)&Node.DOCUMENT_POSITION_CONTAINED_BY)==0) {
286N/A throw new IllegalArgumentException("Cannot create a DOMResult when the nextSibling is not contained by the node.");
286N/A }
286N/A }
286N/A
286N/A this.nextSibling = nextSibling;
286N/A }
286N/A
286N/A /**
286N/A * <p>Get the child node before which the result nodes will be inserted.</p>
286N/A *
286N/A * <p>If no node was set via
286N/A * {@link #DOMResult(Node node, Node nextSibling)},
286N/A * {@link #DOMResult(Node node, Node nextSibling, String systemId)} or
286N/A * {@link #setNextSibling(Node nextSibling)},
286N/A * then <code>null</code> will be returned.</p>
286N/A *
286N/A * @return The child node before which the result nodes will be inserted.
286N/A *
286N/A * @since 1.5
286N/A */
286N/A public Node getNextSibling() {
286N/A return nextSibling;
286N/A }
286N/A
286N/A /**
286N/A * <p>Set the systemId that may be used in association with the node.</p>
286N/A *
286N/A * @param systemId The system identifier as a URI string.
286N/A */
286N/A public void setSystemId(String systemId) {
286N/A this.systemId = systemId;
286N/A }
286N/A
286N/A /**
286N/A * <p>Get the System Identifier.</p>
286N/A *
286N/A * <p>If no System ID was set via
286N/A * {@link #DOMResult(Node node, String systemId)},
286N/A * {@link #DOMResult(Node node, Node nextSibling, String systemId)} or
286N/A * {@link #setSystemId(String systemId)},
286N/A * then <code>null</code> will be returned.</p>
286N/A *
286N/A * @return The system identifier.
286N/A */
286N/A public String getSystemId() {
286N/A return systemId;
286N/A }
286N/A
286N/A //////////////////////////////////////////////////////////////////////
286N/A // Internal state.
286N/A //////////////////////////////////////////////////////////////////////
286N/A
286N/A /**
286N/A * <p>The node to which the transformation will be appended.</p>
286N/A */
286N/A private Node node = null;
286N/A
286N/A /**
286N/A * <p>The child node before which the result nodes will be inserted.</p>
286N/A *
286N/A * @since 1.5
286N/A */
286N/A private Node nextSibling = null;
286N/A
286N/A /**
286N/A * <p>The System ID that may be used in association with the node.</p>
286N/A */
286N/A private String systemId = null;
286N/A}