1
2
3
4
5
6
7
8
9
10 package org.dom4j.dom;
11
12 import java.util.ArrayList;
13
14 import org.dom4j.DocumentFactory;
15 import org.dom4j.QName;
16 import org.dom4j.tree.DefaultDocument;
17 import org.w3c.dom.DOMException;
18 import org.w3c.dom.NamedNodeMap;
19 import org.w3c.dom.NodeList;
20
21 /*** <p><code>DOMDocument</code> implements an XML document which
22 * supports the W3C DOM API.</p>
23 *
24 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
25 * @version $Revision: 1.15 $
26 */
27 public class DOMDocument extends DefaultDocument implements org.w3c.dom.Document {
28
29 /*** The <code>DocumentFactory</code> instance used by default */
30 private static final DOMDocumentFactory DOCUMENT_FACTORY = (DOMDocumentFactory) DOMDocumentFactory.getInstance();
31
32
33 public DOMDocument() {
34 init();
35 }
36
37 public DOMDocument(String name) {
38 super(name);
39 init();
40 }
41
42 public DOMDocument(DOMElement rootElement) {
43 super(rootElement);
44 init();
45 }
46
47 public DOMDocument(DOMDocumentType docType) {
48 super(docType);
49 init();
50 }
51
52 public DOMDocument(DOMElement rootElement, DOMDocumentType docType) {
53 super(rootElement, docType);
54 init();
55 }
56
57 public DOMDocument(String name, DOMElement rootElement, DOMDocumentType docType) {
58 super(name, rootElement, docType);
59 init();
60 }
61
62 private void init() {
63 setDocumentFactory(DOCUMENT_FACTORY);
64 }
65
66
67
68 public boolean supports(String feature, String version) {
69 return DOMNodeHelper.supports(this, feature, version);
70 }
71
72 public String getNamespaceURI() {
73 return DOMNodeHelper.getNamespaceURI(this);
74 }
75
76 public String getPrefix() {
77 return DOMNodeHelper.getPrefix(this);
78 }
79
80 public void setPrefix(String prefix) throws DOMException {
81 DOMNodeHelper.setPrefix(this, prefix);
82 }
83
84 public String getLocalName() {
85 return DOMNodeHelper.getLocalName(this);
86 }
87
88 public String getNodeName() {
89 return "#document";
90 }
91
92
93
94
95
96
97
98 public String getNodeValue() throws DOMException {
99 return null;
100 }
101
102 public void setNodeValue(String nodeValue) throws DOMException {
103 }
104
105
106 public org.w3c.dom.Node getParentNode() {
107 return DOMNodeHelper.getParentNode(this);
108 }
109
110 public NodeList getChildNodes() {
111 return DOMNodeHelper.createNodeList( content() );
112 }
113
114 public org.w3c.dom.Node getFirstChild() {
115 return DOMNodeHelper.asDOMNode( node(0) );
116 }
117
118 public org.w3c.dom.Node getLastChild() {
119 return DOMNodeHelper.asDOMNode( node( nodeCount() - 1 ) );
120 }
121
122 public org.w3c.dom.Node getPreviousSibling() {
123 return DOMNodeHelper.getPreviousSibling(this);
124 }
125
126 public org.w3c.dom.Node getNextSibling() {
127 return DOMNodeHelper.getNextSibling(this);
128 }
129
130 public NamedNodeMap getAttributes() {
131 return null;
132 }
133
134 public org.w3c.dom.Document getOwnerDocument() {
135 return null;
136 }
137
138 public org.w3c.dom.Node insertBefore(
139 org.w3c.dom.Node newChild,
140 org.w3c.dom.Node refChild
141 ) throws DOMException {
142 checkNewChildNode(newChild);
143 return DOMNodeHelper.insertBefore(this, newChild, refChild);
144 }
145
146 public org.w3c.dom.Node replaceChild(
147 org.w3c.dom.Node newChild,
148 org.w3c.dom.Node oldChild
149 ) throws DOMException {
150 checkNewChildNode(newChild);
151 return DOMNodeHelper.replaceChild(this, newChild, oldChild);
152 }
153
154 public org.w3c.dom.Node removeChild(org.w3c.dom.Node oldChild) throws DOMException {
155 return DOMNodeHelper.removeChild(this, oldChild);
156 }
157
158 public org.w3c.dom.Node appendChild(org.w3c.dom.Node newChild) throws DOMException {
159 checkNewChildNode(newChild);
160 return DOMNodeHelper.appendChild(this, newChild);
161 }
162
163 private void checkNewChildNode(org.w3c.dom.Node newChild) throws DOMException {
164 final int nodeType = newChild.getNodeType();
165 if (!(nodeType == org.w3c.dom.Node.ELEMENT_NODE ||
166 nodeType == org.w3c.dom.Node.COMMENT_NODE ||
167 nodeType == org.w3c.dom.Node.PROCESSING_INSTRUCTION_NODE ||
168 nodeType == org.w3c.dom.Node.DOCUMENT_TYPE_NODE)) {
169 throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR,
170 "Specified node cannot be a child of document");
171 }
172 }
173
174
175 public boolean hasChildNodes() {
176 return nodeCount() > 0;
177 }
178
179 public org.w3c.dom.Node cloneNode(boolean deep) {
180 return DOMNodeHelper.cloneNode(this, deep);
181 }
182
183 public boolean isSupported(String feature, String version) {
184 return DOMNodeHelper.isSupported(this, feature, version);
185 }
186
187 public boolean hasAttributes() {
188 return DOMNodeHelper.hasAttributes(this);
189 }
190
191
192
193
194 public NodeList getElementsByTagName(String name) {
195 ArrayList list = new ArrayList();
196 DOMNodeHelper.appendElementsByTagName( list, this, name );
197 return DOMNodeHelper.createNodeList( list );
198 }
199
200 public NodeList getElementsByTagNameNS(
201 String namespaceURI, String localName
202 ) {
203 ArrayList list = new ArrayList();
204 DOMNodeHelper.appendElementsByTagNameNS(list, this, namespaceURI, localName );
205 return DOMNodeHelper.createNodeList( list );
206 }
207
208
209 public org.w3c.dom.DocumentType getDoctype() {
210 return DOMNodeHelper.asDOMDocumentType( getDocType() );
211 }
212
213 public org.w3c.dom.DOMImplementation getImplementation() {
214 if (getDocumentFactory() instanceof org.w3c.dom.DOMImplementation) {
215 return (org.w3c.dom.DOMImplementation) getDocumentFactory();
216 }
217 else {
218 return DOCUMENT_FACTORY;
219 }
220
221 }
222
223 public org.w3c.dom.Element getDocumentElement() {
224 return DOMNodeHelper.asDOMElement( getRootElement() );
225 }
226
227 public org.w3c.dom.Element createElement(String tagName) throws DOMException {
228 return (org.w3c.dom.Element) getDocumentFactory().createElement(tagName);
229 }
230
231 public org.w3c.dom.DocumentFragment createDocumentFragment() {
232 DOMNodeHelper.notSupported();
233 return null;
234 }
235
236 public org.w3c.dom.Text createTextNode(String data) {
237 return (org.w3c.dom.Text) getDocumentFactory().createText(data);
238 }
239
240 public org.w3c.dom.Comment createComment(String data) {
241 return (org.w3c.dom.Comment) getDocumentFactory().createComment(data);
242 }
243
244 public org.w3c.dom.CDATASection createCDATASection(String data) throws DOMException {
245 return (org.w3c.dom.CDATASection) getDocumentFactory().createCDATA(data);
246 }
247
248 public org.w3c.dom.ProcessingInstruction createProcessingInstruction(
249 String target, String data
250 ) throws DOMException {
251 return (org.w3c.dom.ProcessingInstruction) getDocumentFactory().createProcessingInstruction(target, data);
252 }
253
254 public org.w3c.dom.Attr createAttribute(String name) throws DOMException {
255 QName qname = getDocumentFactory().createQName(name);
256 return (org.w3c.dom.Attr) getDocumentFactory().createAttribute(null, qname, "");
257 }
258
259 public org.w3c.dom.EntityReference createEntityReference(String name) throws DOMException {
260 return (org.w3c.dom.EntityReference) ((DOMDocumentFactory) getDocumentFactory()).createEntity(name);
261 }
262
263 public org.w3c.dom.Node importNode(
264 org.w3c.dom.Node importedNode, boolean deep
265 ) throws DOMException {
266 DOMNodeHelper.notSupported();
267 return null;
268 }
269
270 public org.w3c.dom.Element createElementNS(
271 String namespaceURI, String qualifiedName
272 ) throws DOMException {
273 QName qname = getDocumentFactory().createQName( qualifiedName, namespaceURI );
274 return (org.w3c.dom.Element) getDocumentFactory().createElement(qname);
275 }
276
277 public org.w3c.dom.Attr createAttributeNS(
278 String namespaceURI, String qualifiedName
279 ) throws DOMException {
280 QName qname = getDocumentFactory().createQName( qualifiedName, namespaceURI );
281 return (org.w3c.dom.Attr) getDocumentFactory().createAttribute(null, qname, null);
282 }
283
284
285 public org.w3c.dom.Element getElementById(String elementId) {
286 return DOMNodeHelper.asDOMElement( elementByID( elementId ) );
287 }
288
289
290
291
292
293 protected DocumentFactory getDocumentFactory() {
294 if (super.getDocumentFactory() == null) {
295 return DOCUMENT_FACTORY;
296 }
297 else {
298 return super.getDocumentFactory();
299 }
300 }
301 }
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349