1 /*
   2  * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.xml.transform.dom;
  27 
  28 import javax.xml.transform.Source;
  29 
  30 import org.w3c.dom.Node;
  31 
  32 /**
  33  * <p>Acts as a holder for a transformation Source tree in the
  34  * form of a Document Object Model (DOM) tree.</p>
  35  *
  36  * <p>Note that XSLT requires namespace support. Attempting to transform a DOM
  37  * that was not contructed with a namespace-aware parser may result in errors.
  38  * Parsers can be made namespace aware by calling
  39  * {@link javax.xml.parsers.DocumentBuilderFactory#setNamespaceAware(boolean awareness)}.</p>
  40  *
  41  * @author <a href="Jeff.Suttor@Sun.com">Jeff Suttor</a>
  42  * @see <a href="http://www.w3.org/TR/DOM-Level-2">Document Object Model (DOM) Level 2 Specification</a>
  43  * @since 1.4
  44  */
  45 public class DOMSource implements Source {
  46 
  47     /**
  48      * <p><code>Node</code> to serve as DOM source.</p>
  49      */
  50     private Node node;
  51 
  52     /**
  53      * <p>The base ID (URL or system ID) from where URLs
  54      * will be resolved.</p>
  55      */
  56     private String systemID;
  57 
  58     /** If {@link javax.xml.transform.TransformerFactory#getFeature}
  59      * returns true when passed this value as an argument,
  60      * the Transformer supports Source input of this type.
  61      */
  62     public static final String FEATURE =
  63         "http://javax.xml.transform.dom.DOMSource/feature";
  64 
  65     /**
  66      * <p>Zero-argument default constructor.  If this constructor is used, and
  67      * no DOM source is set using {@link #setNode(Node node)} , then the
  68      * <code>Transformer</code> will
  69      * create an empty source {@link org.w3c.dom.Document} using
  70      * {@link javax.xml.parsers.DocumentBuilder#newDocument()}.</p>
  71      *
  72      * @see javax.xml.transform.Transformer#transform(Source xmlSource, Result outputTarget)
  73      */
  74     public DOMSource() { }
  75 
  76     /**
  77      * Create a new input source with a DOM node.  The operation
  78      * will be applied to the subtree rooted at this node.  In XSLT,
  79      * a "/" pattern still means the root of the tree (not the subtree),
  80      * and the evaluation of global variables and parameters is done
  81      * from the root node also.
  82      *
  83      * @param n The DOM node that will contain the Source tree.
  84      */
  85     public DOMSource(Node n) {
  86         setNode(n);
  87     }
  88 
  89     /**
  90      * Create a new input source with a DOM node, and with the
  91      * system ID also passed in as the base URI.
  92      *
  93      * @param node The DOM node that will contain the Source tree.
  94      * @param systemID Specifies the base URI associated with node.
  95      */
  96     public DOMSource(Node node, String systemID) {
  97         setNode(node);
  98         setSystemId(systemID);
  99     }
 100 
 101     /**
 102      * Set the node that will represents a Source DOM tree.
 103      *
 104      * @param node The node that is to be transformed.
 105      */
 106     public void setNode(Node node) {
 107         this.node = node;
 108     }
 109 
 110     /**
 111      * Get the node that represents a Source DOM tree.
 112      *
 113      * @return The node that is to be transformed.
 114      */
 115     public Node getNode() {
 116         return node;
 117     }
 118 
 119     /**
 120      * Set the base ID (URL or system ID) from where URLs
 121      * will be resolved.
 122      *
 123      * @param systemID Base URL for this DOM tree.
 124      */
 125     @Override
 126     public void setSystemId(String systemID) {
 127         this.systemID = systemID;
 128     }
 129 
 130     /**
 131      * Get the base ID (URL or system ID) from where URLs
 132      * will be resolved.
 133      *
 134      * @return Base URL for this DOM tree.
 135      */
 136     @Override
 137     public String getSystemId() {
 138         return this.systemID;
 139     }
 140 
 141     /**
 142      * Indicates whether the {@code DOMSource} object is empty. Empty is
 143      * defined as follows:
 144      * <ul>
 145      * <li>if the system identifier and node are {@code null};
 146      * </li>
 147      * <li>if the system identifier is null, and the {@code node} has no child nodes.
 148      * </li>
 149      * </ul>
 150      *
 151      * @return true if the {@code DOMSource} object is empty, false otherwise
 152      */
 153     @Override
 154     public boolean isEmpty() {
 155         return systemID == null && (node == null || !node.hasChildNodes());
 156     }
 157 }
--- EOF ---