1
2
3
4
5
6
7
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
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
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
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
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
170
171
172
173
174
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