001/* 002 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 003 * 004 * This software is open source. 005 * See the bottom of this file for the licence. 006 * 007 * $Id: DOMDocument.java,v 1.7 2002/02/01 10:13:39 jstrachan Exp $ 008 */ 009 010package org.dom4j.dom; 011 012import java.util.ArrayList; 013import java.util.List; 014 015import org.dom4j.Attribute; 016import org.dom4j.Document; 017import org.dom4j.DocumentFactory; 018import org.dom4j.DocumentType; 019import org.dom4j.Element; 020import org.dom4j.Namespace; 021import org.dom4j.Node; 022import org.dom4j.QName; 023import org.dom4j.tree.DefaultDocument; 024 025import org.w3c.dom.DOMException; 026import org.w3c.dom.NamedNodeMap; 027import org.w3c.dom.NodeList; 028 029/** <p><code>DOMDocument</code> implements an XML document which 030 * supports the W3C DOM API.</p> 031 * 032 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> 033 * @version $Revision: 1.7 $ 034 */ 035public class DOMDocument extends DefaultDocument implements org.w3c.dom.Document { 036 037 /** The <code>DocumentFactory</code> instance used by default */ 038 private static final DOMDocumentFactory DOCUMENT_FACTORY = (DOMDocumentFactory) DOMDocumentFactory.getInstance(); 039 040 041 public DOMDocument() { 042 } 043 044 public DOMDocument(String name) { 045 super(name); 046 } 047 048 public DOMDocument(Element rootElement) { 049 super(rootElement); 050 } 051 052 public DOMDocument(DocumentType docType) { 053 super(docType); 054 } 055 056 public DOMDocument(Element rootElement, DocumentType docType) { 057 super(rootElement, docType); 058 } 059 060 public DOMDocument(String name, Element rootElement, DocumentType docType) { 061 super(name, rootElement, docType); 062 } 063 064 065 // org.w3c.dom.Node interface 066 //------------------------------------------------------------------------- 067 public String getNamespaceURI() { 068 return DOMNodeHelper.getNamespaceURI(this); 069 } 070 071 public String getPrefix() { 072 return DOMNodeHelper.getPrefix(this); 073 } 074 075 public void setPrefix(String prefix) throws DOMException { 076 DOMNodeHelper.setPrefix(this, prefix); 077 } 078 079 public String getLocalName() { 080 return DOMNodeHelper.getLocalName(this); 081 } 082 083 public String getNodeName() { 084 return getName(); 085 } 086 087 //already part of API 088 // 089 //public short getNodeType(); 090 091 092 093 public String getNodeValue() throws DOMException { 094 return DOMNodeHelper.getNodeValue(this); 095 } 096 097 public void setNodeValue(String nodeValue) throws DOMException { 098 DOMNodeHelper.setNodeValue(this, nodeValue); 099 } 100 101 102 public org.w3c.dom.Node getParentNode() { 103 return DOMNodeHelper.getParentNode(this); 104 } 105 106 public NodeList getChildNodes() { 107 return DOMNodeHelper.createNodeList( content() ); 108 } 109 110 public org.w3c.dom.Node getFirstChild() { 111 return DOMNodeHelper.asDOMNode( node(0) ); 112 } 113 114 public org.w3c.dom.Node getLastChild() { 115 return DOMNodeHelper.asDOMNode( node( nodeCount() - 1 ) ); 116 } 117 118 public org.w3c.dom.Node getPreviousSibling() { 119 return DOMNodeHelper.getPreviousSibling(this); 120 } 121 122 public org.w3c.dom.Node getNextSibling() { 123 return DOMNodeHelper.getNextSibling(this); 124 } 125 126 public NamedNodeMap getAttributes() { 127 return DOMNodeHelper.getAttributes(this); 128 } 129 130 public org.w3c.dom.Document getOwnerDocument() { 131 return DOMNodeHelper.getOwnerDocument(this); 132 } 133 134 public org.w3c.dom.Node insertBefore( 135 org.w3c.dom.Node newChild, 136 org.w3c.dom.Node refChild 137 ) throws DOMException { 138 return DOMNodeHelper.insertBefore(this, newChild, refChild); 139 } 140 141 public org.w3c.dom.Node replaceChild( 142 org.w3c.dom.Node newChild, 143 org.w3c.dom.Node oldChild 144 ) throws DOMException { 145 return DOMNodeHelper.replaceChild(this, newChild, oldChild); 146 } 147 148 public org.w3c.dom.Node removeChild(org.w3c.dom.Node oldChild) throws DOMException { 149 return DOMNodeHelper.removeChild(this, oldChild); 150 } 151 152 public org.w3c.dom.Node appendChild(org.w3c.dom.Node newChild) throws DOMException { 153 return DOMNodeHelper.appendChild(this, newChild); 154 } 155 156 public boolean hasChildNodes() { 157 return nodeCount() > 0; 158 } 159 160 public org.w3c.dom.Node cloneNode(boolean deep) { 161 return DOMNodeHelper.cloneNode(this, deep); 162 } 163 164 public boolean isSupported(String feature, String version) { 165 return DOMNodeHelper.isSupported(this, feature, version); 166 } 167 168 public boolean hasAttributes() { 169 return DOMNodeHelper.hasAttributes(this); 170 } 171 172 173 // org.w3c.dom.Document interface 174 //------------------------------------------------------------------------- 175 public NodeList getElementsByTagName(String name) { 176 ArrayList list = new ArrayList(); 177 DOMNodeHelper.appendElementsByTagName( list, this, name ); 178 return DOMNodeHelper.createNodeList( list ); 179 } 180 181 public NodeList getElementsByTagNameNS( 182 String namespaceURI, String localName 183 ) { 184 ArrayList list = new ArrayList(); 185 DOMNodeHelper.appendElementsByTagNameNS(list, this, namespaceURI, localName ); 186 return DOMNodeHelper.createNodeList( list ); 187 } 188 189 190 public org.w3c.dom.DocumentType getDoctype() { 191 return DOMNodeHelper.asDOMDocumentType( getDocType() ); 192 } 193 194 public org.w3c.dom.DOMImplementation getImplementation() { 195 if (getDocumentFactory() instanceof org.w3c.dom.DOMImplementation) { 196 return (org.w3c.dom.DOMImplementation) getDocumentFactory(); 197 } 198 else { 199 return DOCUMENT_FACTORY; 200 } 201 202 } 203 204 public org.w3c.dom.Element getDocumentElement() { 205 return DOMNodeHelper.asDOMElement( getRootElement() ); 206 } 207 208 public org.w3c.dom.Element createElement(String tagName) throws DOMException { 209 return (org.w3c.dom.Element) getDocumentFactory().createElement(tagName); 210 } 211 212 public org.w3c.dom.DocumentFragment createDocumentFragment() { 213 DOMNodeHelper.notSupported(); 214 return null; 215 } 216 217 public org.w3c.dom.Text createTextNode(String data) { 218 return (org.w3c.dom.Text) getDocumentFactory().createText(data); 219 } 220 221 public org.w3c.dom.Comment createComment(String data) { 222 return (org.w3c.dom.Comment) getDocumentFactory().createComment(data); 223 } 224 225 public org.w3c.dom.CDATASection createCDATASection(String data) throws DOMException { 226 return (org.w3c.dom.CDATASection) getDocumentFactory().createCDATA(data); 227 } 228 229 public org.w3c.dom.ProcessingInstruction createProcessingInstruction( 230 String target, String data 231 ) throws DOMException { 232 return (org.w3c.dom.ProcessingInstruction) getDocumentFactory().createProcessingInstruction(target, data); 233 } 234 235 public org.w3c.dom.Attr createAttribute(String name) throws DOMException { 236 QName qname = getDocumentFactory().createQName(name); 237 return (org.w3c.dom.Attr) getDocumentFactory().createAttribute(null, qname, null); 238 } 239 240 public org.w3c.dom.EntityReference createEntityReference(String name) throws DOMException { 241 return (org.w3c.dom.EntityReference) ((DOMDocumentFactory) getDocumentFactory()).createEntity(name); 242 } 243 244 public org.w3c.dom.Node importNode( 245 org.w3c.dom.Node importedNode, boolean deep 246 ) throws DOMException { 247 DOMNodeHelper.notSupported(); 248 return null; 249 } 250 251 public org.w3c.dom.Element createElementNS( 252 String namespaceURI, String qualifiedName 253 ) throws DOMException { 254 QName qname = getDocumentFactory().createQName( qualifiedName, namespaceURI ); 255 return (org.w3c.dom.Element) getDocumentFactory().createElement(qname); 256 } 257 258 public org.w3c.dom.Attr createAttributeNS( 259 String namespaceURI, String qualifiedName 260 ) throws DOMException { 261 QName qname = getDocumentFactory().createQName( qualifiedName, namespaceURI ); 262 return (org.w3c.dom.Attr) getDocumentFactory().createAttribute(null, qname, null); 263 } 264 265 266 public org.w3c.dom.Element getElementById(String elementId) { 267 return DOMNodeHelper.asDOMElement( elementByID( elementId ) ); 268 } 269 270 271 272 // Implementation methods 273 //------------------------------------------------------------------------- 274 protected DocumentFactory getDocumentFactory() { 275 if (super.getDocumentFactory() == null) { 276 return DOCUMENT_FACTORY; 277 } 278 else { 279 return super.getDocumentFactory(); 280 } 281 } 282} 283 284 285 286 287/* 288 * Redistribution and use of this software and associated documentation 289 * ("Software"), with or without modification, are permitted provided 290 * that the following conditions are met: 291 * 292 * 1. Redistributions of source code must retain copyright 293 * statements and notices. Redistributions must also contain a 294 * copy of this document. 295 * 296 * 2. Redistributions in binary form must reproduce the 297 * above copyright notice, this list of conditions and the 298 * following disclaimer in the documentation and/or other 299 * materials provided with the distribution. 300 * 301 * 3. The name "DOM4J" must not be used to endorse or promote 302 * products derived from this Software without prior written 303 * permission of MetaStuff, Ltd. For written permission, 304 * please contact dom4j-info@metastuff.com. 305 * 306 * 4. Products derived from this Software may not be called "DOM4J" 307 * nor may "DOM4J" appear in their names without prior written 308 * permission of MetaStuff, Ltd. DOM4J is a registered 309 * trademark of MetaStuff, Ltd. 310 * 311 * 5. Due credit should be given to the DOM4J Project 312 * (http://dom4j.org/). 313 * 314 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS 315 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 316 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 317 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 318 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 319 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 320 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 321 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 322 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 323 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 324 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 325 * OF THE POSSIBILITY OF SUCH DAMAGE. 326 * 327 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 328 * 329 * $Id: DOMDocument.java,v 1.7 2002/02/01 10:13:39 jstrachan Exp $ 330 */