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: DOMDocumentFactory.java,v 1.10 2001/07/03 14:42:42 jstrachan Exp $
008 */
009
010package org.dom4j.dom;
011
012import java.util.Map;
013
014import org.dom4j.Attribute;
015import org.dom4j.CDATA;
016import org.dom4j.Comment;
017import org.dom4j.Document;
018import org.dom4j.DocumentFactory;
019import org.dom4j.DocumentType;
020import org.dom4j.Element;
021import org.dom4j.Entity;
022import org.dom4j.Namespace;
023import org.dom4j.ProcessingInstruction;
024import org.dom4j.QName;
025import org.dom4j.Text;
026
027/** <p><code>DOMDocumentFactory</code> is a factory of DOM4J objects
028  * which implement the W3C DOM API.</p>
029  *
030  * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
031  * @version $Revision: 1.10 $
032  */
033public class DOMDocumentFactory extends DocumentFactory implements org.w3c.dom.DOMImplementation {
034
035    /** The Singleton instance */
036    protected static transient DOMDocumentFactory singleton = new DOMDocumentFactory();
037    
038    
039    /** <p>Access to the singleton instance of this factory.</p>
040      *
041      * @return the default singleon instance
042      */
043    public static DocumentFactory getInstance() {
044        return singleton;
045    }
046    
047    
048    // Factory methods
049    
050    public Document createDocument() {
051        DOMDocument answer = new DOMDocument();
052        answer.setDocumentFactory( this );
053        return answer;
054    }
055    
056    public DocumentType createDocType(String name, String publicId, String systemId) {
057        return new DOMDocumentType( name, publicId, systemId );
058    }
059    
060    public Element createElement(QName qname) {
061        return new DOMElement(qname);
062    }
063
064    public Element createElement(QName qname, int attributeCount) {
065        return new DOMElement(qname, attributeCount);
066    }
067    
068    public Attribute createAttribute(Element owner, QName qname, String value) {
069        return new DOMAttribute(qname, value);
070    }
071    
072    public CDATA createCDATA(String text) {
073        return new DOMCDATA(text);
074    }
075    
076    public Comment createComment(String text) {
077        return new DOMComment(text);
078    }
079    
080    public Text createText(String text) {
081        return new DOMText(text);
082    }
083    
084    public Entity createEntity(String name) {
085        return new DOMEntityReference(name);
086    }
087    
088    public Entity createEntity(String name, String text) {
089        return new DOMEntityReference(name, text);
090    }
091
092    public Namespace createNamespace(String prefix, String uri) {
093        return new DOMNamespace(prefix, uri);
094    }
095    
096    
097    public ProcessingInstruction createProcessingInstruction(String target, String data) {
098        return new DOMProcessingInstruction(target, data);
099    }
100    
101    public ProcessingInstruction createProcessingInstruction(String target, Map data) {
102        return new DOMProcessingInstruction(target, data);
103    }
104    
105    // org.w3c.dom.DOMImplementation interface
106    
107    public boolean hasFeature(String feature, String version) {
108        return false;
109    }
110
111    public org.w3c.dom.DocumentType createDocumentType(
112        String qualifiedName, String publicId, String systemId
113    ) throws org.w3c.dom.DOMException {
114        return new DOMDocumentType( qualifiedName, publicId, systemId );
115    }
116
117    public org.w3c.dom.Document createDocument(
118        String namespaceURI, 
119        String qualifiedName, 
120        org.w3c.dom.DocumentType documentType
121    ) throws org.w3c.dom.DOMException {
122        DocumentType docType = asDocumentType( documentType );
123        DOMDocument document = new DOMDocument( docType );
124        document.addElement( createQName( qualifiedName, namespaceURI ) );
125        return document;
126   }
127
128
129    // Implementation methods 
130    
131    protected DocumentType asDocumentType( org.w3c.dom.DocumentType documentType ) {
132        if ( documentType instanceof DocumentType ) {
133            return (DocumentType) documentType;
134        }
135        else {
136            return new DOMDocumentType( 
137                documentType.getName(), 
138                documentType.getPublicId(), 
139                documentType.getSystemId() 
140            );
141        }
142    }
143    
144}
145
146
147
148
149/*
150 * Redistribution and use of this software and associated documentation
151 * ("Software"), with or without modification, are permitted provided
152 * that the following conditions are met:
153 *
154 * 1. Redistributions of source code must retain copyright
155 *    statements and notices.  Redistributions must also contain a
156 *    copy of this document.
157 *
158 * 2. Redistributions in binary form must reproduce the
159 *    above copyright notice, this list of conditions and the
160 *    following disclaimer in the documentation and/or other
161 *    materials provided with the distribution.
162 *
163 * 3. The name "DOM4J" must not be used to endorse or promote
164 *    products derived from this Software without prior written
165 *    permission of MetaStuff, Ltd.  For written permission,
166 *    please contact dom4j-info@metastuff.com.
167 *
168 * 4. Products derived from this Software may not be called "DOM4J"
169 *    nor may "DOM4J" appear in their names without prior written
170 *    permission of MetaStuff, Ltd. DOM4J is a registered
171 *    trademark of MetaStuff, Ltd.
172 *
173 * 5. Due credit should be given to the DOM4J Project
174 *    (http://dom4j.org/).
175 *
176 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
177 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
178 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
179 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
180 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
181 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
182 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
183 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
184 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
185 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
186 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
187 * OF THE POSSIBILITY OF SUCH DAMAGE.
188 *
189 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
190 *
191 * $Id: DOMDocumentFactory.java,v 1.10 2001/07/03 14:42:42 jstrachan Exp $
192 */