View Javadoc

1   /*
2    * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
3    *
4    * This software is open source.
5    * See the bottom of this file for the licence.
6    */
7   
8   package org.dom4j.dom;
9   
10  import java.util.ArrayList;
11  
12  import org.dom4j.DocumentFactory;
13  import org.dom4j.QName;
14  import org.dom4j.tree.DefaultDocument;
15  
16  import org.w3c.dom.Attr;
17  import org.w3c.dom.CDATASection;
18  import org.w3c.dom.DOMException;
19  import org.w3c.dom.Document;
20  import org.w3c.dom.EntityReference;
21  import org.w3c.dom.NamedNodeMap;
22  import org.w3c.dom.NodeList;
23  import org.w3c.dom.ProcessingInstruction;
24  
25  /***
26   * <p>
27   * <code>DOMDocument</code> implements an XML document which supports the W3C
28   * DOM API.
29   * </p>
30   * 
31   * @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
32   * @version $Revision: 1.17 $
33   */
34  public class DOMDocument extends DefaultDocument implements Document {
35      /*** The <code>DocumentFactory</code> instance used by default */
36      private static final DOMDocumentFactory DOCUMENT_FACTORY
37              = (DOMDocumentFactory) DOMDocumentFactory.getInstance();
38  
39      public DOMDocument() {
40          init();
41      }
42  
43      public DOMDocument(String name) {
44          super(name);
45          init();
46      }
47  
48      public DOMDocument(DOMElement rootElement) {
49          super(rootElement);
50          init();
51      }
52  
53      public DOMDocument(DOMDocumentType docType) {
54          super(docType);
55          init();
56      }
57  
58      public DOMDocument(DOMElement rootElement, DOMDocumentType docType) {
59          super(rootElement, docType);
60          init();
61      }
62  
63      public DOMDocument(String name, DOMElement rootElement,
64              DOMDocumentType docType) {
65          super(name, rootElement, docType);
66          init();
67      }
68  
69      private void init() {
70          setDocumentFactory(DOCUMENT_FACTORY);
71      }
72  
73      // org.w3c.dom.Node interface
74      // -------------------------------------------------------------------------
75      public boolean supports(String feature, String version) {
76          return DOMNodeHelper.supports(this, feature, version);
77      }
78  
79      public String getNamespaceURI() {
80          return DOMNodeHelper.getNamespaceURI(this);
81      }
82  
83      public String getPrefix() {
84          return DOMNodeHelper.getPrefix(this);
85      }
86  
87      public void setPrefix(String prefix) throws DOMException {
88          DOMNodeHelper.setPrefix(this, prefix);
89      }
90  
91      public String getLocalName() {
92          return DOMNodeHelper.getLocalName(this);
93      }
94  
95      public String getNodeName() {
96          return "#document";
97      }
98  
99      // already part of API
100     //
101     // public short getNodeType();
102     public String getNodeValue() throws DOMException {
103         return null;
104     }
105 
106     public void setNodeValue(String nodeValue) throws DOMException {
107     }
108 
109     public org.w3c.dom.Node getParentNode() {
110         return DOMNodeHelper.getParentNode(this);
111     }
112 
113     public NodeList getChildNodes() {
114         return DOMNodeHelper.createNodeList(content());
115     }
116 
117     public org.w3c.dom.Node getFirstChild() {
118         return DOMNodeHelper.asDOMNode(node(0));
119     }
120 
121     public org.w3c.dom.Node getLastChild() {
122         return DOMNodeHelper.asDOMNode(node(nodeCount() - 1));
123     }
124 
125     public org.w3c.dom.Node getPreviousSibling() {
126         return DOMNodeHelper.getPreviousSibling(this);
127     }
128 
129     public org.w3c.dom.Node getNextSibling() {
130         return DOMNodeHelper.getNextSibling(this);
131     }
132 
133     public NamedNodeMap getAttributes() {
134         return null;
135     }
136 
137     public org.w3c.dom.Document getOwnerDocument() {
138         return null;
139     }
140 
141     public org.w3c.dom.Node insertBefore(org.w3c.dom.Node newChild,
142             org.w3c.dom.Node refChild) throws DOMException {
143         checkNewChildNode(newChild);
144 
145         return DOMNodeHelper.insertBefore(this, newChild, refChild);
146     }
147 
148     public org.w3c.dom.Node replaceChild(org.w3c.dom.Node newChild,
149             org.w3c.dom.Node oldChild) throws DOMException {
150         checkNewChildNode(newChild);
151 
152         return DOMNodeHelper.replaceChild(this, newChild, oldChild);
153     }
154 
155     public org.w3c.dom.Node removeChild(org.w3c.dom.Node oldChild)
156             throws DOMException {
157         return DOMNodeHelper.removeChild(this, oldChild);
158     }
159 
160     public org.w3c.dom.Node appendChild(org.w3c.dom.Node newChild)
161             throws DOMException {
162         checkNewChildNode(newChild);
163 
164         return DOMNodeHelper.appendChild(this, newChild);
165     }
166 
167     private void checkNewChildNode(org.w3c.dom.Node newChild)
168             throws DOMException {
169         final int nodeType = newChild.getNodeType();
170 
171         if (!((nodeType == org.w3c.dom.Node.ELEMENT_NODE)
172                 || (nodeType == org.w3c.dom.Node.COMMENT_NODE)
173                 || (nodeType == org.w3c.dom.Node.PROCESSING_INSTRUCTION_NODE) 
174                 || (nodeType == org.w3c.dom.Node.DOCUMENT_TYPE_NODE))) {
175             throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR,
176                     "Given node cannot be a child of document");
177         }
178     }
179 
180     public boolean hasChildNodes() {
181         return nodeCount() > 0;
182     }
183 
184     public org.w3c.dom.Node cloneNode(boolean deep) {
185         return DOMNodeHelper.cloneNode(this, deep);
186     }
187 
188     public boolean isSupported(String feature, String version) {
189         return DOMNodeHelper.isSupported(this, feature, version);
190     }
191 
192     public boolean hasAttributes() {
193         return DOMNodeHelper.hasAttributes(this);
194     }
195 
196     // org.w3c.dom.Document interface
197     // -------------------------------------------------------------------------
198     public NodeList getElementsByTagName(String name) {
199         ArrayList list = new ArrayList();
200         DOMNodeHelper.appendElementsByTagName(list, this, name);
201 
202         return DOMNodeHelper.createNodeList(list);
203     }
204 
205     public NodeList getElementsByTagNameNS(String namespace, String name) {
206         ArrayList list = new ArrayList();
207         DOMNodeHelper.appendElementsByTagNameNS(list, this, namespace, name);
208 
209         return DOMNodeHelper.createNodeList(list);
210     }
211 
212     public org.w3c.dom.DocumentType getDoctype() {
213         return DOMNodeHelper.asDOMDocumentType(getDocType());
214     }
215 
216     public org.w3c.dom.DOMImplementation getImplementation() {
217         if (getDocumentFactory() instanceof org.w3c.dom.DOMImplementation) {
218             return (org.w3c.dom.DOMImplementation) getDocumentFactory();
219         } else {
220             return DOCUMENT_FACTORY;
221         }
222     }
223 
224     public org.w3c.dom.Element getDocumentElement() {
225         return DOMNodeHelper.asDOMElement(getRootElement());
226     }
227 
228     public org.w3c.dom.Element createElement(String name) throws DOMException {
229         return (org.w3c.dom.Element) getDocumentFactory().createElement(name);
230     }
231 
232     public org.w3c.dom.DocumentFragment createDocumentFragment() {
233         DOMNodeHelper.notSupported();
234 
235         return null;
236     }
237 
238     public org.w3c.dom.Text createTextNode(String data) {
239         return (org.w3c.dom.Text) getDocumentFactory().createText(data);
240     }
241 
242     public org.w3c.dom.Comment createComment(String data) {
243         return (org.w3c.dom.Comment) getDocumentFactory().createComment(data);
244     }
245 
246     public CDATASection createCDATASection(String data) throws DOMException {
247         return (CDATASection) getDocumentFactory().createCDATA(data);
248     }
249 
250     public ProcessingInstruction createProcessingInstruction(String target,
251             String data) throws DOMException {
252         return (ProcessingInstruction) getDocumentFactory()
253                 .createProcessingInstruction(target, data);
254     }
255 
256     public Attr createAttribute(String name) throws DOMException {
257         QName qname = getDocumentFactory().createQName(name);
258 
259         return (Attr) getDocumentFactory().createAttribute(null, qname, "");
260     }
261 
262     public EntityReference createEntityReference(String name)
263             throws DOMException {
264         return (EntityReference) getDocumentFactory().createEntity(name, null);
265     }
266 
267     public org.w3c.dom.Node importNode(org.w3c.dom.Node importedNode,
268             boolean deep) throws DOMException {
269         DOMNodeHelper.notSupported();
270 
271         return null;
272     }
273 
274     public org.w3c.dom.Element createElementNS(String namespaceURI,
275             String qualifiedName) throws DOMException {
276         QName qname = getDocumentFactory().createQName(qualifiedName,
277                 namespaceURI);
278 
279         return (org.w3c.dom.Element) getDocumentFactory().createElement(qname);
280     }
281 
282     public org.w3c.dom.Attr createAttributeNS(String namespaceURI,
283             String qualifiedName) throws DOMException {
284         QName qname = getDocumentFactory().createQName(qualifiedName,
285                 namespaceURI);
286 
287         return (org.w3c.dom.Attr) getDocumentFactory().createAttribute(null,
288                 qname, null);
289     }
290 
291     public org.w3c.dom.Element getElementById(String elementId) {
292         return DOMNodeHelper.asDOMElement(elementByID(elementId));
293     }
294 
295     // Implementation methods
296     // -------------------------------------------------------------------------
297     protected DocumentFactory getDocumentFactory() {
298         if (super.getDocumentFactory() == null) {
299             return DOCUMENT_FACTORY;
300         } else {
301             return super.getDocumentFactory();
302         }
303     }
304 }
305 
306 /*
307  * Redistribution and use of this software and associated documentation
308  * ("Software"), with or without modification, are permitted provided that the
309  * following conditions are met:
310  * 
311  * 1. Redistributions of source code must retain copyright statements and
312  * notices. Redistributions must also contain a copy of this document.
313  * 
314  * 2. Redistributions in binary form must reproduce the above copyright notice,
315  * this list of conditions and the following disclaimer in the documentation
316  * and/or other materials provided with the distribution.
317  * 
318  * 3. The name "DOM4J" must not be used to endorse or promote products derived
319  * from this Software without prior written permission of MetaStuff, Ltd. For
320  * written permission, please contact dom4j-info@metastuff.com.
321  * 
322  * 4. Products derived from this Software may not be called "DOM4J" nor may
323  * "DOM4J" appear in their names without prior written permission of MetaStuff,
324  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
325  * 
326  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
327  * 
328  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
329  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
330  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
331  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
332  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
333  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
334  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
335  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
336  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
337  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
338  * POSSIBILITY OF SUCH DAMAGE.
339  * 
340  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
341  */