1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.module.xml.filters; |
12 | |
|
13 | |
import org.mule.RequestContext; |
14 | |
import org.mule.api.lifecycle.InitialisationException; |
15 | |
import org.mule.api.transport.OutputHandler; |
16 | |
import org.mule.module.xml.transformer.DelayedResult; |
17 | |
import org.mule.module.xml.transformer.XmlToDomDocument; |
18 | |
import org.mule.transformer.types.DataTypeFactory; |
19 | |
|
20 | |
import java.io.ByteArrayInputStream; |
21 | |
import java.io.InputStream; |
22 | |
import java.io.StringReader; |
23 | |
|
24 | |
import javax.xml.parsers.DocumentBuilderFactory; |
25 | |
import javax.xml.stream.XMLStreamConstants; |
26 | |
import javax.xml.stream.XMLStreamReader; |
27 | |
import javax.xml.transform.dom.DOMResult; |
28 | |
|
29 | |
import org.apache.commons.io.output.ByteArrayOutputStream; |
30 | |
import org.dom4j.dom.DOMDocument; |
31 | |
import org.w3c.dom.Document; |
32 | |
import org.w3c.dom.Node; |
33 | |
import org.xml.sax.InputSource; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
public abstract class AbstractJaxpFilter |
39 | |
{ |
40 | |
|
41 | 0 | private XmlToDomDocument xmlToDom = new XmlToDomDocument(); |
42 | |
|
43 | |
private DocumentBuilderFactory documentBuilderFactory; |
44 | |
|
45 | |
public AbstractJaxpFilter() |
46 | |
{ |
47 | 0 | super(); |
48 | 0 | xmlToDom.setReturnDataType(DataTypeFactory.create(Document.class)); |
49 | 0 | } |
50 | |
public void initialise() throws InitialisationException |
51 | |
{ |
52 | 0 | if (getDocumentBuilderFactory() == null) |
53 | |
{ |
54 | 0 | DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); |
55 | 0 | builderFactory.setNamespaceAware(true); |
56 | 0 | setDocumentBuilderFactory(builderFactory); |
57 | |
} |
58 | 0 | } |
59 | |
|
60 | |
public Node toDOMNode(Object src) throws Exception |
61 | |
{ |
62 | 0 | if (src instanceof Node) |
63 | |
{ |
64 | 0 | return (Document) src; |
65 | |
} |
66 | 0 | else if (src instanceof org.dom4j.Document) |
67 | |
{ |
68 | 0 | org.dom4j.Document dom4j = (org.dom4j.Document) src; |
69 | 0 | DOMDocument dom = new DOMDocument(); |
70 | 0 | dom.setDocument(dom4j); |
71 | 0 | return dom; |
72 | |
} |
73 | 0 | else if (src instanceof OutputHandler) |
74 | |
{ |
75 | 0 | OutputHandler handler = ((OutputHandler) src); |
76 | 0 | ByteArrayOutputStream output = new ByteArrayOutputStream(); |
77 | 0 | handler.write(RequestContext.getEvent(), output); |
78 | 0 | InputStream stream = new ByteArrayInputStream(output.toByteArray()); |
79 | 0 | return getDocumentBuilderFactory().newDocumentBuilder().parse(stream); |
80 | |
} |
81 | 0 | else if (src instanceof byte[]) |
82 | |
{ |
83 | 0 | ByteArrayInputStream stream = new ByteArrayInputStream((byte[]) src); |
84 | 0 | return getDocumentBuilderFactory().newDocumentBuilder().parse(stream); |
85 | |
} |
86 | 0 | else if (src instanceof InputStream) |
87 | |
{ |
88 | 0 | return getDocumentBuilderFactory().newDocumentBuilder().parse((InputStream) src); |
89 | |
} |
90 | 0 | else if (src instanceof String) |
91 | |
{ |
92 | 0 | return getDocumentBuilderFactory().newDocumentBuilder().parse( |
93 | |
new InputSource(new StringReader((String) src))); |
94 | |
} |
95 | 0 | else if (src instanceof XMLStreamReader) |
96 | |
{ |
97 | 0 | XMLStreamReader xsr = (XMLStreamReader) src; |
98 | |
|
99 | |
|
100 | 0 | if (!xsr.isStartElement() && xsr.getEventType() != XMLStreamConstants.START_DOCUMENT) |
101 | |
{ |
102 | 0 | xsr.nextTag(); |
103 | |
} |
104 | |
|
105 | 0 | return getDocumentBuilderFactory().newDocumentBuilder().parse(new InputSource()); |
106 | |
} |
107 | 0 | else if (src instanceof DelayedResult) |
108 | |
{ |
109 | 0 | DelayedResult result = ((DelayedResult) src); |
110 | 0 | DOMResult domResult = new DOMResult(); |
111 | 0 | result.write(domResult); |
112 | 0 | return domResult.getNode(); |
113 | |
} |
114 | |
else |
115 | |
{ |
116 | 0 | return (Node) xmlToDom.transform(src); |
117 | |
} |
118 | |
} |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
public DocumentBuilderFactory getDocumentBuilderFactory() |
126 | |
{ |
127 | 0 | return documentBuilderFactory; |
128 | |
} |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
public void setDocumentBuilderFactory(DocumentBuilderFactory documentBuilderFactory) |
137 | |
{ |
138 | 0 | this.documentBuilderFactory = documentBuilderFactory; |
139 | 0 | } |
140 | |
} |