View Javadoc

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