View Javadoc

1   /*
2    * Copyright 2001-2004 (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    * $Id: DOMDocumentFactory.java,v 1.17 2004/06/25 08:03:35 maartenc Exp $
8    */
9   
10  package org.dom4j.dom;
11  
12  import java.util.Map;
13  
14  import org.dom4j.Attribute;
15  import org.dom4j.CDATA;
16  import org.dom4j.Comment;
17  import org.dom4j.Document;
18  import org.dom4j.DocumentFactory;
19  import org.dom4j.DocumentType;
20  import org.dom4j.Element;
21  import org.dom4j.Entity;
22  import org.dom4j.Namespace;
23  import org.dom4j.ProcessingInstruction;
24  import org.dom4j.QName;
25  import org.dom4j.Text;
26  
27  /*** <p><code>DOMDocumentFactory</code> is a factory of DOM4J objects
28    * which implement the W3C DOM API.</p>
29    *
30    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
31    * @version $Revision: 1.17 $
32    */
33  public class DOMDocumentFactory extends DocumentFactory implements org.w3c.dom.DOMImplementation {
34  
35      /*** The Singleton instance */
36      //protected static transient DOMDocumentFactory singleton = new DOMDocumentFactory();
37      private final static ThreadLocal singlePerThread=new ThreadLocal();
38      private static String domDocumentFactoryClassName=null;
39  
40  
41  
42      /*** <p>Access to the singleton instance of this factory.</p>
43        *
44        * @return the default singleon instance
45        */
46      public static DocumentFactory getInstance() {
47        DOMDocumentFactory fact =(DOMDocumentFactory)singlePerThread.get();
48         if (fact==null) {
49           fact=  new DOMDocumentFactory();
50           singlePerThread.set(fact);
51          }
52         if (fact==null){
53         }
54         return fact;
55      }
56  
57  
58      // Factory methods
59  
60      public Document createDocument() {
61          DOMDocument answer = new DOMDocument();
62          answer.setDocumentFactory( this );
63          return answer;
64      }
65  
66      public DocumentType createDocType(String name, String publicId, String systemId) {
67          return new DOMDocumentType( name, publicId, systemId );
68      }
69  
70      public Element createElement(QName qname) {
71          return new DOMElement(qname);
72      }
73  
74      public Element createElement(QName qname, int attributeCount) {
75          return new DOMElement(qname, attributeCount);
76      }
77  
78      public Attribute createAttribute(Element owner, QName qname, String value) {
79          return new DOMAttribute(qname, value);
80      }
81  
82      public CDATA createCDATA(String text) {
83          return new DOMCDATA(text);
84      }
85  
86      public Comment createComment(String text) {
87          return new DOMComment(text);
88      }
89  
90      public Text createText(String text) {
91          return new DOMText(text);
92      }
93  
94      public Entity createEntity(String name) {
95          return new DOMEntityReference(name);
96      }
97  
98      public Entity createEntity(String name, String text) {
99          return new DOMEntityReference(name, text);
100     }
101 
102     public Namespace createNamespace(String prefix, String uri) {
103         return new DOMNamespace(prefix, uri);
104     }
105 
106 
107     public ProcessingInstruction createProcessingInstruction(String target, String data) {
108         return new DOMProcessingInstruction(target, data);
109     }
110 
111     public ProcessingInstruction createProcessingInstruction(String target, Map data) {
112         return new DOMProcessingInstruction(target, data);
113     }
114 
115     // org.w3c.dom.DOMImplementation interface
116 
117     public boolean hasFeature(String feature, String version) {
118         if ("XML".equalsIgnoreCase(feature) || "Core".equalsIgnoreCase(feature)) {
119           return (version == null || version.length() == 0 || "1.0".equals(version) || "2.0".equals(version));
120         }
121         return false;
122     }
123 
124     public org.w3c.dom.DocumentType createDocumentType(
125         String qualifiedName, String publicId, String systemId
126     ) throws org.w3c.dom.DOMException {
127         return new DOMDocumentType( qualifiedName, publicId, systemId );
128     }
129 
130     public org.w3c.dom.Document createDocument(
131         String namespaceURI,
132         String qualifiedName,
133         org.w3c.dom.DocumentType documentType
134     ) throws org.w3c.dom.DOMException {
135         DOMDocument document;
136         if (documentType != null) {
137             DOMDocumentType docType = asDocumentType( documentType );
138             document = new DOMDocument( docType );
139         } else {
140             document = new DOMDocument();
141         }
142 
143         document.addElement( createQName( qualifiedName, namespaceURI ) );
144         return document;
145    }
146 
147 
148     // Implementation methods
149 
150     protected DOMDocumentType asDocumentType( org.w3c.dom.DocumentType documentType ) {
151         if ( documentType instanceof DOMDocumentType ) {
152             return (DOMDocumentType) documentType;
153         }
154         else {
155             return new DOMDocumentType(
156                 documentType.getName(),
157                 documentType.getPublicId(),
158                 documentType.getSystemId()
159             );
160         }
161     }
162 
163 }
164 
165 
166 
167 
168 /*
169  * Redistribution and use of this software and associated documentation
170  * ("Software"), with or without modification, are permitted provided
171  * that the following conditions are met:
172  *
173  * 1. Redistributions of source code must retain copyright
174  *    statements and notices.  Redistributions must also contain a
175  *    copy of this document.
176  *
177  * 2. Redistributions in binary form must reproduce the
178  *    above copyright notice, this list of conditions and the
179  *    following disclaimer in the documentation and/or other
180  *    materials provided with the distribution.
181  *
182  * 3. The name "DOM4J" must not be used to endorse or promote
183  *    products derived from this Software without prior written
184  *    permission of MetaStuff, Ltd.  For written permission,
185  *    please contact dom4j-info@metastuff.com.
186  *
187  * 4. Products derived from this Software may not be called "DOM4J"
188  *    nor may "DOM4J" appear in their names without prior written
189  *    permission of MetaStuff, Ltd. DOM4J is a registered
190  *    trademark of MetaStuff, Ltd.
191  *
192  * 5. Due credit should be given to the DOM4J Project - 
193  *    http://www.dom4j.org
194  *
195  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
196  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
197  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
198  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
199  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
200  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
201  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
202  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
203  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
204  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
205  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
206  * OF THE POSSIBILITY OF SUCH DAMAGE.
207  *
208  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
209  *
210  * $Id: DOMDocumentFactory.java,v 1.17 2004/06/25 08:03:35 maartenc Exp $
211  */