1 /*
   2  * Copyright (c) 2000, 2005, 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  */
  44 public class DOMSource implements Source {
  45 
  46     /**
  47      * <p><code>Node</code> to serve as DOM source.</p>
  48      */
  49     private Node node;
  50 
  51     /**
  52      * <p>The base ID (URL or system ID) from where URLs
  53      * will be resolved.</p>
  54      */
  55     private String systemID;
  56 
  57     /** If {@link javax.xml.transform.TransformerFactory#getFeature}
  58      * returns true when passed this value as an argument,
  59      * the Transformer supports Source input of this type.
  60      */
  61     public static final String FEATURE =
  62         "http://javax.xml.transform.dom.DOMSource/feature";
  63 
  64     /**
  65      * <p>Zero-argument default constructor.  If this constructor is used, and
  66      * no DOM source is set using {@link #setNode(Node node)} , then the
  67      * <code>Transformer</code> will
  68      * create an empty source {@link org.w3c.dom.Document} using
  69      * {@link javax.xml.parsers.DocumentBuilder#newDocument()}.</p>
  70      *
  71      * @see javax.xml.transform.Transformer#transform(Source xmlSource, Result outputTarget)
  72      */
  73     public DOMSource() { }
  74 
  75     /**
  76      * Create a new input source with a DOM node.  The operation
  77      * will be applied to the subtree rooted at this node.  In XSLT,
  78      * a "/" pattern still means the root of the tree (not the subtree),
  79      * and the evaluation of global variables and parameters is done
  80      * from the root node also.
  81      *
  82      * @param n The DOM node that will contain the Source tree.
  83      */
  84     public DOMSource(Node n) {
  85         setNode(n);
  86     }
  87 
  88     /**
  89      * Create a new input source with a DOM node, and with the
  90      * system ID also passed in as the base URI.
  91      *
  92      * @param node The DOM node that will contain the Source tree.
  93      * @param systemID Specifies the base URI associated with node.
  94      */
  95     public DOMSource(Node node, String systemID) {
  96         setNode(node);
  97         setSystemId(systemID);
  98     }
  99 
 100     /**
 101      * Set the node that will represents a Source DOM tree.
 102      *
 103      * @param node The node that is to be transformed.
 104      */
 105     public void setNode(Node node) {
 106         this.node = node;
 107     }
 108 
 109     /**
 110      * Get the node that represents a Source DOM tree.
 111      *
 112      * @return The node that is to be transformed.
 113      */
 114     public Node getNode() {
 115         return node;
 116     }
 117 
 118     /**
 119      * Set the base ID (URL or system ID) from where URLs
 120      * will be resolved.
 121      *
 122      * @param systemID Base URL for this DOM tree.
 123      */
 124     public void setSystemId(String systemID) {
 125         this.systemID = systemID;
 126     }
 127 
 128     /**
 129      * Get the base ID (URL or system ID) from where URLs
 130      * will be resolved.
 131      *
 132      * @return Base URL for this DOM tree.
 133      */
 134     public String getSystemId() {
 135         return this.systemID;
 136     }
 137 }