1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| package org.dom4j; |
11 |
| |
12 |
| import java.io.IOException; |
13 |
| import java.io.ObjectInputStream; |
14 |
| import java.io.Serializable; |
15 |
| import java.util.List; |
16 |
| import java.util.Map; |
17 |
| |
18 |
| import org.dom4j.rule.Pattern; |
19 |
| import org.dom4j.tree.DefaultAttribute; |
20 |
| import org.dom4j.tree.DefaultCDATA; |
21 |
| import org.dom4j.tree.DefaultComment; |
22 |
| import org.dom4j.tree.DefaultDocument; |
23 |
| import org.dom4j.tree.DefaultDocumentType; |
24 |
| import org.dom4j.tree.DefaultElement; |
25 |
| import org.dom4j.tree.DefaultEntity; |
26 |
| import org.dom4j.tree.DefaultProcessingInstruction; |
27 |
| import org.dom4j.tree.DefaultText; |
28 |
| import org.dom4j.tree.QNameCache; |
29 |
| import org.dom4j.xpath.DefaultXPath; |
30 |
| import org.dom4j.xpath.XPathPattern; |
31 |
| import org.jaxen.VariableContext; |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| public class DocumentFactory implements Serializable { |
44 |
| |
45 |
| |
46 |
| |
47 |
| private final static ThreadLocal singlePerThread=new ThreadLocal(); |
48 |
| private static String documentFactoryClassName=null; |
49 |
| |
50 |
| protected transient QNameCache cache; |
51 |
| |
52 |
| |
53 |
| private Map xpathNamespaceURIs; |
54 |
| |
55 |
| static { |
56 |
170
| try {
|
57 |
170
| documentFactoryClassName = System.getProperty(
|
58 |
| "org.dom4j.factory", |
59 |
| "org.dom4j.DocumentFactory" |
60 |
| ); |
61 |
| } |
62 |
| catch (Exception e) { |
63 |
0
| documentFactoryClassName = "org.dom4j.DocumentFactory";
|
64 |
| } |
65 |
170
| getInstance();
|
66 |
| } |
67 |
| |
68 |
| |
69 |
| |
70 |
| |
71 |
| |
72 |
| |
73 |
| |
74 |
53060
| public static DocumentFactory getInstance() {
|
75 |
53060
| DocumentFactory fact = (DocumentFactory)singlePerThread.get();
|
76 |
53060
| if (fact==null) {
|
77 |
188
| fact=createSingleton( documentFactoryClassName );
|
78 |
188
| singlePerThread.set(fact);
|
79 |
| } |
80 |
53060
| return fact;
|
81 |
| } |
82 |
| |
83 |
1098
| public DocumentFactory() {
|
84 |
1098
| init();
|
85 |
| } |
86 |
| |
87 |
| |
88 |
| |
89 |
| |
90 |
11986
| public Document createDocument() {
|
91 |
11986
| DefaultDocument answer = new DefaultDocument();
|
92 |
11986
| answer.setDocumentFactory( this );
|
93 |
11986
| return answer;
|
94 |
| } |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
11602
| public Document createDocument(String encoding) {
|
100 |
| |
101 |
| |
102 |
| |
103 |
11602
| Document answer = createDocument();
|
104 |
11602
| if (answer instanceof DefaultDocument) {
|
105 |
11602
| ((DefaultDocument) answer).setXMLEncoding(encoding);
|
106 |
| } |
107 |
11602
| return answer;
|
108 |
| } |
109 |
| |
110 |
2
| public Document createDocument(Element rootElement) {
|
111 |
2
| Document answer = createDocument();
|
112 |
2
| answer.setRootElement(rootElement);
|
113 |
2
| return answer;
|
114 |
| } |
115 |
| |
116 |
22
| public DocumentType createDocType(String name, String publicId, String systemId) {
|
117 |
22
| return new DefaultDocumentType( name, publicId, systemId );
|
118 |
| } |
119 |
| |
120 |
181638
| public Element createElement(QName qname) {
|
121 |
181638
| return new DefaultElement(qname);
|
122 |
| } |
123 |
| |
124 |
392
| public Element createElement(String name) {
|
125 |
392
| return createElement(createQName(name));
|
126 |
| } |
127 |
| |
128 |
8
| public Element createElement(String qualifiedName, String namespaceURI) {
|
129 |
8
| return createElement(createQName(qualifiedName, namespaceURI));
|
130 |
| } |
131 |
| |
132 |
23922
| public Attribute createAttribute(Element owner, QName qname, String value) {
|
133 |
23922
| return new DefaultAttribute(qname, value);
|
134 |
| } |
135 |
| |
136 |
1188
| public Attribute createAttribute(Element owner, String name, String value) {
|
137 |
1188
| return createAttribute(owner, createQName(name), value);
|
138 |
| } |
139 |
| |
140 |
170
| public CDATA createCDATA(String text) {
|
141 |
170
| return new DefaultCDATA(text);
|
142 |
| } |
143 |
| |
144 |
944
| public Comment createComment(String text) {
|
145 |
944
| return new DefaultComment(text);
|
146 |
| } |
147 |
| |
148 |
212538
| public Text createText(String text) {
|
149 |
212538
| if ( text == null ) {
|
150 |
0
| throw new IllegalArgumentException( "Adding text to an XML document must not be null" );
|
151 |
| } |
152 |
212538
| return new DefaultText(text);
|
153 |
| } |
154 |
| |
155 |
| |
156 |
8
| public Entity createEntity(String name, String text) {
|
157 |
8
| return new DefaultEntity(name, text);
|
158 |
| } |
159 |
| |
160 |
196356
| public Namespace createNamespace(String prefix, String uri) {
|
161 |
196356
| return Namespace.get(prefix, uri);
|
162 |
| } |
163 |
| |
164 |
30
| public ProcessingInstruction createProcessingInstruction(String target, String data) {
|
165 |
30
| return new DefaultProcessingInstruction(target, data);
|
166 |
| } |
167 |
| |
168 |
0
| public ProcessingInstruction createProcessingInstruction(String target, Map data) {
|
169 |
0
| return new DefaultProcessingInstruction(target, data);
|
170 |
| } |
171 |
| |
172 |
201910
| public QName createQName(String localName, Namespace namespace) {
|
173 |
201910
| return cache.get(localName, namespace);
|
174 |
| } |
175 |
| |
176 |
22910
| public QName createQName(String localName) {
|
177 |
22910
| return cache.get(localName);
|
178 |
| } |
179 |
| |
180 |
32
| public QName createQName(String name, String prefix, String uri) {
|
181 |
32
| return cache.get(name, Namespace.get( prefix, uri ));
|
182 |
| } |
183 |
| |
184 |
28
| public QName createQName(String qualifiedName, String uri) {
|
185 |
28
| return cache.get(qualifiedName, uri);
|
186 |
| } |
187 |
| |
188 |
| |
189 |
| |
190 |
| |
191 |
| |
192 |
| |
193 |
| |
194 |
| |
195 |
1612
| public XPath createXPath(String xpathExpression) throws InvalidXPathException {
|
196 |
1612
| DefaultXPath xpath = new DefaultXPath( xpathExpression );
|
197 |
1604
| if ( xpathNamespaceURIs != null ) {
|
198 |
6
| xpath.setNamespaceURIs( xpathNamespaceURIs );
|
199 |
| } |
200 |
1604
| return xpath;
|
201 |
| } |
202 |
| |
203 |
| |
204 |
| |
205 |
| |
206 |
| |
207 |
| |
208 |
| |
209 |
| |
210 |
| |
211 |
12
| public XPath createXPath(String xpathExpression, VariableContext variableContext) {
|
212 |
12
| XPath xpath = createXPath( xpathExpression );
|
213 |
12
| xpath.setVariableContext( variableContext );
|
214 |
12
| return xpath;
|
215 |
| } |
216 |
| |
217 |
| |
218 |
| |
219 |
| |
220 |
| |
221 |
| |
222 |
| |
223 |
| |
224 |
| |
225 |
| |
226 |
| |
227 |
0
| public NodeFilter createXPathFilter(String xpathFilterExpression, VariableContext variableContext) {
|
228 |
0
| XPath answer = createXPath( xpathFilterExpression );
|
229 |
| |
230 |
0
| answer.setVariableContext( variableContext );
|
231 |
0
| return answer;
|
232 |
| } |
233 |
| |
234 |
| |
235 |
| |
236 |
| |
237 |
| |
238 |
| |
239 |
| |
240 |
| |
241 |
| |
242 |
| |
243 |
44
| public NodeFilter createXPathFilter(String xpathFilterExpression) {
|
244 |
44
| return createXPath( xpathFilterExpression );
|
245 |
| |
246 |
| } |
247 |
| |
248 |
| |
249 |
| |
250 |
| |
251 |
| |
252 |
| |
253 |
| |
254 |
| |
255 |
| |
256 |
72
| public Pattern createPattern(String xpathPattern) {
|
257 |
72
| return new XPathPattern( xpathPattern );
|
258 |
| } |
259 |
| |
260 |
| |
261 |
| |
262 |
| |
263 |
| |
264 |
| |
265 |
| |
266 |
2
| public List getQNames() {
|
267 |
2
| return cache.getQNames();
|
268 |
| } |
269 |
| |
270 |
| |
271 |
| |
272 |
| |
273 |
| |
274 |
| |
275 |
0
| public Map getXPathNamespaceURIs() {
|
276 |
0
| return xpathNamespaceURIs;
|
277 |
| } |
278 |
| |
279 |
| |
280 |
| |
281 |
| |
282 |
| |
283 |
4
| public void setXPathNamespaceURIs(Map xpathNamespaceURIs) {
|
284 |
4
| this.xpathNamespaceURIs = xpathNamespaceURIs;
|
285 |
| } |
286 |
| |
287 |
| |
288 |
| |
289 |
| |
290 |
| |
291 |
| |
292 |
| |
293 |
| |
294 |
| |
295 |
| |
296 |
| |
297 |
188
| protected static DocumentFactory createSingleton(String className) {
|
298 |
| |
299 |
188
| try {
|
300 |
| |
301 |
| |
302 |
188
| Class theClass = Class.forName(
|
303 |
| className, |
304 |
| true, |
305 |
| DocumentFactory.class.getClassLoader() |
306 |
| ); |
307 |
188
| return (DocumentFactory) theClass.newInstance();
|
308 |
| } |
309 |
| catch (Throwable e) { |
310 |
0
| System.out.println( "WARNING: Cannot load DocumentFactory: " + className );
|
311 |
0
| return new DocumentFactory();
|
312 |
| } |
313 |
| } |
314 |
| |
315 |
| |
316 |
| |
317 |
| |
318 |
0
| protected QName intern(QName qname) {
|
319 |
0
| return cache.intern(qname);
|
320 |
| } |
321 |
| |
322 |
| |
323 |
| |
324 |
| |
325 |
1104
| protected QNameCache createQNameCache() {
|
326 |
1104
| return new QNameCache(this);
|
327 |
| } |
328 |
| |
329 |
| |
330 |
| |
331 |
6
| private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
|
332 |
6
| in.defaultReadObject();
|
333 |
6
| init();
|
334 |
| } |
335 |
| |
336 |
1104
| protected void init() {
|
337 |
1104
| cache = createQNameCache();
|
338 |
| } |
339 |
| } |
340 |
| |
341 |
| |
342 |
| |
343 |
| |
344 |
| |
345 |
| |
346 |
| |
347 |
| |
348 |
| |
349 |
| |
350 |
| |
351 |
| |
352 |
| |
353 |
| |
354 |
| |
355 |
| |
356 |
| |
357 |
| |
358 |
| |
359 |
| |
360 |
| |
361 |
| |
362 |
| |
363 |
| |
364 |
| |
365 |
| |
366 |
| |
367 |
| |
368 |
| |
369 |
| |
370 |
| |
371 |
| |
372 |
| |
373 |
| |
374 |
| |
375 |
| |
376 |
| |
377 |
| |
378 |
| |
379 |
| |
380 |
| |
381 |
| |
382 |
| |
383 |
| |
384 |
| |
385 |
| |
386 |
| |
387 |
| |