1   
2   
3   
4   
5   
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      
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     
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     
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 
183 
184 
185 
186 
187 
188 
189 
190 
191 
192 
193 
194 
195 
196 
197 
198 
199 
200 
201 
202 
203 
204 
205 
206 
207 
208 
209 
210 
211 
212 
213 
214 
215 
216