1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.module.xml.transformer; |
11 | |
|
12 | |
import org.mule.api.MuleEvent; |
13 | |
import org.mule.api.transformer.DiscoverableTransformer; |
14 | |
import org.mule.api.transformer.TransformerException; |
15 | |
import org.mule.api.transport.OutputHandler; |
16 | |
import org.mule.module.xml.util.XMLUtils; |
17 | |
|
18 | |
import java.io.IOException; |
19 | |
import java.io.InputStream; |
20 | |
import java.io.OutputStream; |
21 | |
|
22 | |
import javax.xml.stream.XMLStreamReader; |
23 | |
import javax.xml.stream.XMLStreamWriter; |
24 | |
import javax.xml.transform.Source; |
25 | |
import javax.xml.transform.TransformerFactoryConfigurationError; |
26 | |
import javax.xml.transform.stream.StreamResult; |
27 | |
|
28 | |
import org.dom4j.Document; |
29 | |
|
30 | |
public class XmlToOutputHandler extends AbstractXmlTransformer implements DiscoverableTransformer |
31 | |
{ |
32 | |
|
33 | 232 | private int priorityWeighting = DiscoverableTransformer.DEFAULT_PRIORITY_WEIGHTING; |
34 | |
|
35 | |
public XmlToOutputHandler() |
36 | 232 | { |
37 | 232 | registerSourceType(String.class); |
38 | 232 | registerSourceType(byte[].class); |
39 | 232 | registerSourceType(Source.class); |
40 | 232 | registerSourceType(Document.class); |
41 | 232 | registerSourceType(org.w3c.dom.Document.class); |
42 | 232 | registerSourceType(org.w3c.dom.Element.class); |
43 | 232 | registerSourceType(InputStream.class); |
44 | 232 | registerSourceType(OutputHandler.class); |
45 | 232 | registerSourceType(XMLStreamReader.class); |
46 | 232 | registerSourceType(DelayedResult.class); |
47 | 232 | setReturnClass(OutputHandler.class); |
48 | 232 | } |
49 | |
|
50 | |
public Object doTransform(final Object src, final String encoding) throws TransformerException |
51 | |
{ |
52 | 0 | return new OutputHandler() |
53 | |
{ |
54 | 0 | public void write(MuleEvent event, OutputStream out) throws IOException |
55 | |
{ |
56 | 0 | writeXml(src, encoding, out); |
57 | 0 | } |
58 | |
}; |
59 | |
} |
60 | |
|
61 | |
protected void writeXml(final Object src, final String encoding, OutputStream out) |
62 | |
throws TransformerFactoryConfigurationError, IOException |
63 | |
{ |
64 | |
try |
65 | |
{ |
66 | 0 | if (src instanceof XMLStreamReader) |
67 | |
{ |
68 | |
|
69 | |
|
70 | 0 | XMLStreamReader reader = (XMLStreamReader)src; |
71 | 0 | XMLStreamWriter writer = getXMLOutputFactory().createXMLStreamWriter(out); |
72 | |
|
73 | |
try { |
74 | 0 | writer.writeStartDocument(); |
75 | 0 | XMLUtils.copy(reader, writer); |
76 | 0 | writer.writeEndDocument(); |
77 | |
} finally { |
78 | 0 | writer.close(); |
79 | 0 | reader.close(); |
80 | 0 | } |
81 | 0 | } |
82 | 0 | else if (src instanceof DelayedResult) |
83 | |
{ |
84 | 0 | DelayedResult result = (DelayedResult) src; |
85 | |
|
86 | 0 | StreamResult streamResult = new StreamResult(out); |
87 | 0 | result.write(streamResult); |
88 | 0 | } |
89 | |
else |
90 | |
{ |
91 | 0 | writeToStream(src, encoding, out); |
92 | |
} |
93 | |
} |
94 | 0 | catch (Exception e) |
95 | |
{ |
96 | 0 | IOException ioe = new IOException(e.toString()); |
97 | 0 | ioe.initCause(e); |
98 | 0 | throw ioe; |
99 | 0 | } |
100 | 0 | } |
101 | |
|
102 | |
public int getPriorityWeighting() |
103 | |
{ |
104 | 0 | return priorityWeighting; |
105 | |
} |
106 | |
|
107 | |
public void setPriorityWeighting(int priorityWeighting) |
108 | |
{ |
109 | 0 | this.priorityWeighting = priorityWeighting; |
110 | 0 | } |
111 | |
|
112 | |
} |