KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dom4j > dom > DOMDocument


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 JavaDoc;
11
12 import org.dom4j.DocumentFactory;
13 import org.dom4j.QName;
14 import org.dom4j.tree.DefaultDocument;
15
16 import org.w3c.dom.Attr JavaDoc;
17 import org.w3c.dom.CDATASection JavaDoc;
18 import org.w3c.dom.DOMException JavaDoc;
19 import org.w3c.dom.Document JavaDoc;
20 import org.w3c.dom.EntityReference JavaDoc;
21 import org.w3c.dom.NamedNodeMap JavaDoc;
22 import org.w3c.dom.NodeList JavaDoc;
23 import org.w3c.dom.ProcessingInstruction JavaDoc;
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 JavaDoc 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 JavaDoc 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 JavaDoc feature, String JavaDoc version) {
76         return DOMNodeHelper.supports(this, feature, version);
77     }
78
79     public String JavaDoc getNamespaceURI() {
80         return DOMNodeHelper.getNamespaceURI(this);
81     }
82
83     public String JavaDoc getPrefix() {
84         return DOMNodeHelper.getPrefix(this);
85     }
86
87     public void setPrefix(String JavaDoc prefix) throws DOMException JavaDoc {
88         DOMNodeHelper.setPrefix(this, prefix);
89     }
90
91     public String JavaDoc getLocalName() {
92         return DOMNodeHelper.getLocalName(this);
93     }
94
95     public String JavaDoc getNodeName() {
96         return "#document";
97     }
98
99     // already part of API
100
//
101
// public short getNodeType();
102
public String JavaDoc getNodeValue() throws DOMException JavaDoc {
103         return null;
104     }
105
106     public void setNodeValue(String JavaDoc nodeValue) throws DOMException JavaDoc {
107     }
108
109     public org.w3c.dom.Node JavaDoc getParentNode() {
110         return DOMNodeHelper.getParentNode(this);
111     }
112
113     public NodeList JavaDoc getChildNodes() {
114         return DOMNodeHelper.createNodeList(content());
115     }
116
117     public org.w3c.dom.Node JavaDoc getFirstChild() {
118         return DOMNodeHelper.asDOMNode(node(0));
119     }
120
121     public org.w3c.dom.Node JavaDoc getLastChild() {
122         return DOMNodeHelper.asDOMNode(node(nodeCount() - 1));
123     }
124
125     public org.w3c.dom.Node JavaDoc getPreviousSibling() {
126         return DOMNodeHelper.getPreviousSibling(this);
127     }
128
129     public org.w3c.dom.Node JavaDoc getNextSibling() {
130         return DOMNodeHelper.getNextSibling(this);
131     }
132
133     public NamedNodeMap JavaDoc getAttributes() {
134         return null;
135     }
136
137     public org.w3c.dom.Document JavaDoc getOwnerDocument() {
138         return null;
139     }
140
141     public org.w3c.dom.Node JavaDoc insertBefore(org.w3c.dom.Node JavaDoc newChild,
142             org.w3c.dom.Node JavaDoc refChild) throws DOMException JavaDoc {
143         checkNewChildNode(newChild);
144
145         return DOMNodeHelper.insertBefore(this, newChild, refChild);
146     }
147
148     public org.w3c.dom.Node JavaDoc replaceChild(org.w3c.dom.Node JavaDoc newChild,
149             org.w3c.dom.Node JavaDoc oldChild) throws DOMException JavaDoc {
150         checkNewChildNode(newChild);
151
152         return DOMNodeHelper.replaceChild(this, newChild, oldChild);
153     }
154
155     public org.w3c.dom.Node JavaDoc removeChild(org.w3c.dom.Node JavaDoc oldChild)
156             throws DOMException JavaDoc {
157         return DOMNodeHelper.removeChild(this, oldChild);
158     }
159
160     public org.w3c.dom.Node JavaDoc appendChild(org.w3c.dom.Node JavaDoc newChild)
161             throws DOMException JavaDoc {
162         checkNewChildNode(newChild);
163
164         return DOMNodeHelper.appendChild(this, newChild);
165     }
166
167     private void checkNewChildNode(org.w3c.dom.Node JavaDoc newChild)
168             throws DOMException JavaDoc {
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 JavaDoc(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 JavaDoc cloneNode(boolean deep) {
185         return DOMNodeHelper.cloneNode(this, deep);
186     }
187
188     public boolean isSupported(String JavaDoc feature, String JavaDoc 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 JavaDoc getElementsByTagName(String JavaDoc name) {
199         ArrayList JavaDoc list = new ArrayList JavaDoc();
200         DOMNodeHelper.appendElementsByTagName(list, this, name);
201
202         return DOMNodeHelper.createNodeList(list);
203     }
204
205     public NodeList JavaDoc getElementsByTagNameNS(String JavaDoc namespace, String JavaDoc name) {
206         ArrayList JavaDoc list = new ArrayList JavaDoc();
207         DOMNodeHelper.appendElementsByTagNameNS(list, this, namespace, name);
208
209         return DOMNodeHelper.createNodeList(list);
210     }
211
212     public org.w3c.dom.DocumentType JavaDoc getDoctype() {
213         return DOMNodeHelper.asDOMDocumentType(getDocType());
214     }
215
216     public org.w3c.dom.DOMImplementation JavaDoc getImplementation() {
217         if (getDocumentFactory() instanceof org.w3c.dom.DOMImplementation JavaDoc) {
218             return (org.w3c.dom.DOMImplementation JavaDoc) getDocumentFactory();
219         } else {
220             return DOCUMENT_FACTORY;
221         }
222     }
223
224     public org.w3c.dom.Element JavaDoc getDocumentElement() {
225         return DOMNodeHelper.asDOMElement(getRootElement());
226     }
227
228     public org.w3c.dom.Element JavaDoc createElement(String JavaDoc name) throws DOMException JavaDoc {
229         return (org.w3c.dom.Element JavaDoc) getDocumentFactory().createElement(name);
230     }
231
232     public org.w3c.dom.DocumentFragment JavaDoc createDocumentFragment() {
233         DOMNodeHelper.notSupported();
234
235         return null;
236     }
237
238     public org.w3c.dom.Text JavaDoc createTextNode(String JavaDoc data) {
239         return (org.w3c.dom.Text JavaDoc) getDocumentFactory().createText(data);
240     }
241
242     public org.w3c.dom.Comment JavaDoc createComment(String JavaDoc data) {
243         return (org.w3c.dom.Comment JavaDoc) getDocumentFactory().createComment(data);
244     }
245
246     public CDATASection JavaDoc createCDATASection(String JavaDoc data) throws DOMException JavaDoc {
247         return (CDATASection JavaDoc) getDocumentFactory().createCDATA(data);
248     }
249
250     public ProcessingInstruction JavaDoc createProcessingInstruction(String JavaDoc target,
251             String JavaDoc data) throws DOMException JavaDoc {
252         return (ProcessingInstruction JavaDoc) getDocumentFactory()
253                 .createProcessingInstruction(target, data);
254     }
255
256     public Attr JavaDoc createAttribute(String JavaDoc name) throws DOMException JavaDoc {
257         QName qname = getDocumentFactory().createQName(name);
258
259         return (Attr JavaDoc) getDocumentFactory().createAttribute(null, qname, "");
260     }
261
262     public EntityReference JavaDoc createEntityReference(String JavaDoc name)
263             throws DOMException JavaDoc {
264         return (EntityReference JavaDoc) getDocumentFactory().createEntity(name, null);
265     }
266
267     public org.w3c.dom.Node JavaDoc importNode(org.w3c.dom.Node JavaDoc importedNode,
268             boolean deep) throws DOMException JavaDoc {
269         DOMNodeHelper.notSupported();
270
271         return null;
272     }
273
274     public org.w3c.dom.Element JavaDoc createElementNS(String JavaDoc namespaceURI,
275             String JavaDoc qualifiedName) throws DOMException JavaDoc {
276         QName qname = getDocumentFactory().createQName(qualifiedName,
277                 namespaceURI);
278
279         return (org.w3c.dom.Element JavaDoc) getDocumentFactory().createElement(qname);
280     }
281
282     public org.w3c.dom.Attr JavaDoc createAttributeNS(String JavaDoc namespaceURI,
283             String JavaDoc qualifiedName) throws DOMException JavaDoc {
284         QName qname = getDocumentFactory().createQName(qualifiedName,
285                 namespaceURI);
286
287         return (org.w3c.dom.Attr JavaDoc) getDocumentFactory().createAttribute(null,
288                 qname, null);
289     }
290
291     public org.w3c.dom.Element JavaDoc getElementById(String JavaDoc 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  */

342
Popular Tags