Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/javax/xml/transform/dom/DOMResult.java
32288 views
/*1* Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.xml.transform.dom;2627import javax.xml.transform.Result;28import org.w3c.dom.Node;2930/**31* <p>Acts as a holder for a transformation result tree in the form of a Document Object Model (DOM) tree.</p>32*33* <p>If no output DOM source is set, the transformation will create a Document node as the holder for the result of the transformation,34* which may be retrieved with {@link #getNode()}.</p>35*36* @author <a href="[email protected]">Jeff Suttor</a>37*/38public class DOMResult implements Result {3940/** <p>If {@link javax.xml.transform.TransformerFactory#getFeature}41* returns <code>true</code> when passed this value as an argument,42* the <code>Transformer</code> supports <code>Result</code> output of this type.</p>43*/44public static final String FEATURE = "http://javax.xml.transform.dom.DOMResult/feature";4546/**47* <p>Zero-argument default constructor.</p>48*49* <p><code>node</code>,50* <code>siblingNode</code> and51* <code>systemId</code>52* will be set to <code>null</code>.</p>53*/54public DOMResult() {55setNode(null);56setNextSibling(null);57setSystemId(null);58}5960/**61* <p>Use a DOM node to create a new output target.</p>62*63* <p>In practice, the node should be64* a {@link org.w3c.dom.Document} node,65* a {@link org.w3c.dom.DocumentFragment} node, or66* a {@link org.w3c.dom.Element} node.67* In other words, a node that accepts children.</p>68*69* <p><code>siblingNode</code> and70* <code>systemId</code>71* will be set to <code>null</code>.</p>72*73* @param node The DOM node that will contain the result tree.74*/75public DOMResult(Node node) {76setNode(node);77setNextSibling(null);78setSystemId(null);79}8081/**82* <p>Use a DOM node to create a new output target with the specified System ID.<p>83*84* <p>In practice, the node should be85* a {@link org.w3c.dom.Document} node,86* a {@link org.w3c.dom.DocumentFragment} node, or87* a {@link org.w3c.dom.Element} node.88* In other words, a node that accepts children.</p>89*90* <p><code>siblingNode</code> will be set to <code>null</code>.</p>91*92* @param node The DOM node that will contain the result tree.93* @param systemId The system identifier which may be used in association with this node.94*/95public DOMResult(Node node, String systemId) {96setNode(node);97setNextSibling(null);98setSystemId(systemId);99}100101/**102* <p>Use a DOM node to create a new output target specifying the child node where the result nodes should be inserted before.</p>103*104* <p>In practice, <code>node</code> and <code>nextSibling</code> should be105* a {@link org.w3c.dom.Document} node,106* a {@link org.w3c.dom.DocumentFragment} node, or107* a {@link org.w3c.dom.Element} node.108* In other words, a node that accepts children.</p>109*110* <p>Use <code>nextSibling</code> to specify the child node111* where the result nodes should be inserted before.112* If <code>nextSibling</code> is not a sibling of <code>node</code>,113* then an <code>IllegalArgumentException</code> is thrown.114* If <code>node</code> is <code>null</code> and <code>nextSibling</code> is not <code>null</code>,115* then an <code>IllegalArgumentException</code> is thrown.116* If <code>nextSibling</code> is <code>null</code>,117* then the behavior is the same as calling {@link #DOMResult(Node node)},118* i.e. append the result nodes as the last child of the specified <code>node</code>.</p>119*120* <p><code>systemId</code> will be set to <code>null</code>.</p>121*122* @param node The DOM node that will contain the result tree.123* @param nextSibling The child node where the result nodes should be inserted before.124*125* @throws IllegalArgumentException If <code>nextSibling</code> is not a sibling of <code>node</code> or126* <code>node</code> is <code>null</code> and <code>nextSibling</code>127* is not <code>null</code>.128*129* @since 1.5130*/131public DOMResult(Node node, Node nextSibling) {132133// does the corrent parent/child relationship exist?134if (nextSibling != null) {135// cannot be a sibling of a null node136if (node == null) {137throw new IllegalArgumentException("Cannot create a DOMResult when the nextSibling is contained by the \"null\" node.");138}139140// nextSibling contained by node?141if ((node.compareDocumentPosition(nextSibling)&Node.DOCUMENT_POSITION_CONTAINED_BY)==0) {142throw new IllegalArgumentException("Cannot create a DOMResult when the nextSibling is not contained by the node.");143}144}145146setNode(node);147setNextSibling(nextSibling);148setSystemId(null);149}150151/**152* <p>Use a DOM node to create a new output target specifying the child node where the result nodes should be inserted before and153* the specified System ID.</p>154*155* <p>In practice, <code>node</code> and <code>nextSibling</code> should be156* a {@link org.w3c.dom.Document} node,157* a {@link org.w3c.dom.DocumentFragment} node, or a158* {@link org.w3c.dom.Element} node.159* In other words, a node that accepts children.</p>160*161* <p>Use <code>nextSibling</code> to specify the child node162* where the result nodes should be inserted before.163* If <code>nextSibling</code> is not a sibling of <code>node</code>,164* then an <code>IllegalArgumentException</code> is thrown.165* If <code>node</code> is <code>null</code> and <code>nextSibling</code> is not <code>null</code>,166* then an <code>IllegalArgumentException</code> is thrown.167* If <code>nextSibling</code> is <code>null</code>,168* then the behavior is the same as calling {@link #DOMResult(Node node, String systemId)},169* i.e. append the result nodes as the last child of the specified node and use the specified System ID.</p>170*171* @param node The DOM node that will contain the result tree.172* @param nextSibling The child node where the result nodes should be inserted before.173* @param systemId The system identifier which may be used in association with this node.174*175* @throws IllegalArgumentException If <code>nextSibling</code> is not a176* sibling of <code>node</code> or177* <code>node</code> is <code>null</code> and <code>nextSibling</code>178* is not <code>null</code>.179*180* @since 1.5181*/182public DOMResult(Node node, Node nextSibling, String systemId) {183184// does the corrent parent/child relationship exist?185if (nextSibling != null) {186// cannot be a sibling of a null node187if (node == null) {188throw new IllegalArgumentException("Cannot create a DOMResult when the nextSibling is contained by the \"null\" node.");189}190191// nextSibling contained by node?192if ((node.compareDocumentPosition(nextSibling)&Node.DOCUMENT_POSITION_CONTAINED_BY)==0) {193throw new IllegalArgumentException("Cannot create a DOMResult when the nextSibling is not contained by the node.");194}195}196197setNode(node);198setNextSibling(nextSibling);199setSystemId(systemId);200}201202/**203* <p>Set the node that will contain the result DOM tree.<p>204*205* <p>In practice, the node should be206* a {@link org.w3c.dom.Document} node,207* a {@link org.w3c.dom.DocumentFragment} node, or208* a {@link org.w3c.dom.Element} node.209* In other words, a node that accepts children.</p>210*211* <p>An <code>IllegalStateException</code> is thrown if212* <code>nextSibling</code> is not <code>null</code> and213* <code>node</code> is not a parent of <code>nextSibling</code>.214* An <code>IllegalStateException</code> is thrown if <code>node</code> is <code>null</code> and215* <code>nextSibling</code> is not <code>null</code>.</p>216*217* @param node The node to which the transformation will be appended.218*219* @throws IllegalStateException If <code>nextSibling</code> is not220* <code>null</code> and221* <code>nextSibling</code> is not a child of <code>node</code> or222* <code>node</code> is <code>null</code> and223* <code>nextSibling</code> is not <code>null</code>.224*/225public void setNode(Node node) {226// does the corrent parent/child relationship exist?227if (nextSibling != null) {228// cannot be a sibling of a null node229if (node == null) {230throw new IllegalStateException("Cannot create a DOMResult when the nextSibling is contained by the \"null\" node.");231}232233// nextSibling contained by node?234if ((node.compareDocumentPosition(nextSibling)&Node.DOCUMENT_POSITION_CONTAINED_BY)==0) {235throw new IllegalArgumentException("Cannot create a DOMResult when the nextSibling is not contained by the node.");236}237}238239this.node = node;240}241242/**243* <p>Get the node that will contain the result DOM tree.</p>244*245* <p>If no node was set via246* {@link #DOMResult(Node node)},247* {@link #DOMResult(Node node, String systeId)},248* {@link #DOMResult(Node node, Node nextSibling)},249* {@link #DOMResult(Node node, Node nextSibling, String systemId)} or250* {@link #setNode(Node node)},251* then the node will be set by the transformation, and may be obtained from this method once the transformation is complete.252* Calling this method before the transformation will return <code>null</code>.</p>253*254* @return The node to which the transformation will be appended.255*/256public Node getNode() {257return node;258}259260/**261* <p>Set the child node before which the result nodes will be inserted.</p>262*263* <p>Use <code>nextSibling</code> to specify the child node264* before which the result nodes should be inserted.265* If <code>nextSibling</code> is not a descendant of <code>node</code>,266* then an <code>IllegalArgumentException</code> is thrown.267* If <code>node</code> is <code>null</code> and <code>nextSibling</code> is not <code>null</code>,268* then an <code>IllegalStateException</code> is thrown.269* If <code>nextSibling</code> is <code>null</code>,270* then the behavior is the same as calling {@link #DOMResult(Node node)},271* i.e. append the result nodes as the last child of the specified <code>node</code>.</p>272*273* @param nextSibling The child node before which the result nodes will be inserted.274*275* @throws IllegalArgumentException If <code>nextSibling</code> is not a276* descendant of <code>node</code>.277* @throws IllegalStateException If <code>node</code> is <code>null</code>278* and <code>nextSibling</code> is not <code>null</code>.279*280* @since 1.5281*/282public void setNextSibling(Node nextSibling) {283284// does the corrent parent/child relationship exist?285if (nextSibling != null) {286// cannot be a sibling of a null node287if (node == null) {288throw new IllegalStateException("Cannot create a DOMResult when the nextSibling is contained by the \"null\" node.");289}290291// nextSibling contained by node?292if ((node.compareDocumentPosition(nextSibling)&Node.DOCUMENT_POSITION_CONTAINED_BY)==0) {293throw new IllegalArgumentException("Cannot create a DOMResult when the nextSibling is not contained by the node.");294}295}296297this.nextSibling = nextSibling;298}299300/**301* <p>Get the child node before which the result nodes will be inserted.</p>302*303* <p>If no node was set via304* {@link #DOMResult(Node node, Node nextSibling)},305* {@link #DOMResult(Node node, Node nextSibling, String systemId)} or306* {@link #setNextSibling(Node nextSibling)},307* then <code>null</code> will be returned.</p>308*309* @return The child node before which the result nodes will be inserted.310*311* @since 1.5312*/313public Node getNextSibling() {314return nextSibling;315}316317/**318* <p>Set the systemId that may be used in association with the node.</p>319*320* @param systemId The system identifier as a URI string.321*/322public void setSystemId(String systemId) {323this.systemId = systemId;324}325326/**327* <p>Get the System Identifier.</p>328*329* <p>If no System ID was set via330* {@link #DOMResult(Node node, String systemId)},331* {@link #DOMResult(Node node, Node nextSibling, String systemId)} or332* {@link #setSystemId(String systemId)},333* then <code>null</code> will be returned.</p>334*335* @return The system identifier.336*/337public String getSystemId() {338return systemId;339}340341//////////////////////////////////////////////////////////////////////342// Internal state.343//////////////////////////////////////////////////////////////////////344345/**346* <p>The node to which the transformation will be appended.</p>347*/348private Node node = null;349350/**351* <p>The child node before which the result nodes will be inserted.</p>352*353* @since 1.5354*/355private Node nextSibling = null;356357/**358* <p>The System ID that may be used in association with the node.</p>359*/360private String systemId = null;361}362363364