1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.module.xml.transformer; |
8 | |
|
9 | |
import org.mule.api.transformer.TransformerException; |
10 | |
import org.mule.module.xml.util.XMLUtils; |
11 | |
import org.mule.transformer.AbstractMessageTransformer; |
12 | |
import org.mule.transformer.types.DataTypeFactory; |
13 | |
|
14 | |
import java.io.InputStream; |
15 | |
import java.io.OutputStream; |
16 | |
import java.io.StringWriter; |
17 | |
|
18 | |
import javax.xml.stream.XMLInputFactory; |
19 | |
import javax.xml.stream.XMLOutputFactory; |
20 | |
import javax.xml.transform.OutputKeys; |
21 | |
import javax.xml.transform.Result; |
22 | |
import javax.xml.transform.Source; |
23 | |
import javax.xml.transform.Transformer; |
24 | |
import javax.xml.transform.TransformerFactory; |
25 | |
import javax.xml.transform.TransformerFactoryConfigurationError; |
26 | |
import javax.xml.transform.dom.DOMResult; |
27 | |
import javax.xml.transform.stream.StreamResult; |
28 | |
|
29 | |
import org.apache.commons.io.output.ByteArrayOutputStream; |
30 | |
import org.dom4j.Document; |
31 | |
import org.dom4j.io.DocumentResult; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
public abstract class AbstractXmlTransformer extends AbstractMessageTransformer |
38 | |
{ |
39 | |
private String outputEncoding; |
40 | |
private XMLInputFactory xmlInputFactory; |
41 | |
private XMLOutputFactory xmlOutputFactory; |
42 | 0 | private boolean useStaxSource = false; |
43 | |
|
44 | |
public AbstractXmlTransformer() |
45 | 0 | { |
46 | 0 | registerSourceType(DataTypeFactory.STRING); |
47 | 0 | registerSourceType(DataTypeFactory.BYTE_ARRAY); |
48 | 0 | registerSourceType(DataTypeFactory.create(javax.xml.transform.Source.class)); |
49 | 0 | registerSourceType(DataTypeFactory.create(org.xml.sax.InputSource.class)); |
50 | 0 | registerSourceType(DataTypeFactory.create(org.dom4j.Document.class)); |
51 | 0 | registerSourceType(DataTypeFactory.create(org.w3c.dom.Document.class)); |
52 | 0 | registerSourceType(DataTypeFactory.create(org.w3c.dom.Element.class)); |
53 | 0 | registerSourceType(DataTypeFactory.create(java.io.InputStream.class)); |
54 | 0 | registerSourceType(DataTypeFactory.create(org.mule.api.transport.OutputHandler.class)); |
55 | 0 | registerSourceType(DataTypeFactory.create(javax.xml.stream.XMLStreamReader.class)); |
56 | 0 | registerSourceType(DataTypeFactory.create(org.mule.module.xml.transformer.DelayedResult.class)); |
57 | 0 | setReturnDataType(DataTypeFactory.BYTE_ARRAY); |
58 | |
|
59 | 0 | xmlInputFactory = XMLInputFactory.newInstance(); |
60 | 0 | xmlOutputFactory = XMLOutputFactory.newInstance(); |
61 | 0 | } |
62 | |
|
63 | |
|
64 | |
protected static interface ResultHolder |
65 | |
{ |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
Result getResult(); |
71 | |
|
72 | |
|
73 | |
Object getResultObject(); |
74 | |
} |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
protected static ResultHolder getResultHolder(Class<?> desiredClass) |
82 | |
{ |
83 | 0 | if (desiredClass == null) |
84 | |
{ |
85 | 0 | return null; |
86 | |
} |
87 | 0 | if (byte[].class.equals(desiredClass) || InputStream.class.isAssignableFrom(desiredClass)) |
88 | |
{ |
89 | 0 | return new ResultHolder() |
90 | 0 | { |
91 | 0 | ByteArrayOutputStream resultStream = new ByteArrayOutputStream(); |
92 | 0 | StreamResult result = new StreamResult(resultStream); |
93 | |
|
94 | |
public Result getResult() |
95 | |
{ |
96 | 0 | return result; |
97 | |
} |
98 | |
|
99 | |
public Object getResultObject() |
100 | |
{ |
101 | 0 | return resultStream.toByteArray(); |
102 | |
} |
103 | |
}; |
104 | |
} |
105 | 0 | else if (String.class.equals(desiredClass)) |
106 | |
{ |
107 | 0 | return new ResultHolder() |
108 | 0 | { |
109 | 0 | StringWriter writer = new StringWriter(); |
110 | 0 | StreamResult result = new StreamResult(writer); |
111 | |
|
112 | |
public Result getResult() |
113 | |
{ |
114 | 0 | return result; |
115 | |
} |
116 | |
|
117 | |
public Object getResultObject() |
118 | |
{ |
119 | 0 | return writer.getBuffer().toString(); |
120 | |
} |
121 | |
}; |
122 | |
} |
123 | 0 | else if (org.w3c.dom.Document.class.isAssignableFrom(desiredClass)) |
124 | |
{ |
125 | 0 | return new ResultHolder() |
126 | 0 | { |
127 | 0 | DOMResult result = new DOMResult(); |
128 | |
|
129 | |
public Result getResult() |
130 | |
{ |
131 | 0 | return result; |
132 | |
} |
133 | |
|
134 | |
public Object getResultObject() |
135 | |
{ |
136 | 0 | return result.getNode(); |
137 | |
} |
138 | |
}; |
139 | |
} |
140 | 0 | else if (org.dom4j.io.DocumentResult.class.isAssignableFrom(desiredClass)) |
141 | |
{ |
142 | 0 | return new ResultHolder() |
143 | 0 | { |
144 | 0 | DocumentResult result = new DocumentResult(); |
145 | |
|
146 | |
public Result getResult() |
147 | |
{ |
148 | 0 | return result; |
149 | |
} |
150 | |
|
151 | |
public Object getResultObject() |
152 | |
{ |
153 | 0 | return result; |
154 | |
} |
155 | |
}; |
156 | |
} |
157 | 0 | else if (org.dom4j.Document.class.isAssignableFrom(desiredClass)) |
158 | |
{ |
159 | 0 | return new ResultHolder() |
160 | 0 | { |
161 | 0 | DocumentResult result = new DocumentResult(); |
162 | |
|
163 | |
public Result getResult() |
164 | |
{ |
165 | 0 | return result; |
166 | |
} |
167 | |
|
168 | |
public Object getResultObject() |
169 | |
{ |
170 | 0 | return result.getDocument(); |
171 | |
} |
172 | |
}; |
173 | |
} |
174 | |
|
175 | 0 | return null; |
176 | |
} |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
@Deprecated |
191 | |
protected String convertToText(Object obj) throws Exception |
192 | |
{ |
193 | 0 | return convertToText(obj, null); |
194 | |
} |
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
protected String convertToText(Object obj, String outputEncoding) throws Exception |
213 | |
{ |
214 | |
|
215 | 0 | if (obj instanceof String) |
216 | |
{ |
217 | 0 | return (String) obj; |
218 | |
} |
219 | 0 | else if (obj instanceof Document) |
220 | |
{ |
221 | 0 | return ((Document) obj).asXML(); |
222 | |
} |
223 | |
|
224 | 0 | Source src = XMLUtils.toXmlSource(xmlInputFactory, useStaxSource, obj); |
225 | 0 | if (src == null) |
226 | |
{ |
227 | 0 | return null; |
228 | |
} |
229 | |
|
230 | 0 | StringWriter writer = new StringWriter(); |
231 | 0 | StreamResult result = new StreamResult(writer); |
232 | |
|
233 | 0 | Transformer idTransformer = TransformerFactory.newInstance().newTransformer(); |
234 | 0 | if (outputEncoding != null) |
235 | |
{ |
236 | 0 | idTransformer.setOutputProperty(OutputKeys.ENCODING, outputEncoding); |
237 | |
} |
238 | 0 | idTransformer.transform(src, result); |
239 | 0 | return writer.getBuffer().toString(); |
240 | |
} |
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
protected String convertToBytes(Object obj, String outputEncoding) throws Exception |
257 | |
{ |
258 | |
|
259 | 0 | Source src = XMLUtils.toXmlSource(xmlInputFactory, useStaxSource, obj); |
260 | 0 | if (src == null) |
261 | |
{ |
262 | 0 | return null; |
263 | |
} |
264 | |
|
265 | 0 | StringWriter writer = new StringWriter(); |
266 | 0 | StreamResult result = new StreamResult(writer); |
267 | |
|
268 | 0 | Transformer idTransformer = XMLUtils.getTransformer(); |
269 | 0 | idTransformer.setOutputProperty(OutputKeys.ENCODING, outputEncoding); |
270 | 0 | idTransformer.transform(src, result); |
271 | 0 | return writer.getBuffer().toString(); |
272 | |
} |
273 | |
|
274 | |
protected void writeToStream(Object obj, String outputEncoding, OutputStream output) throws Exception |
275 | |
{ |
276 | |
|
277 | 0 | Source src = XMLUtils.toXmlSource(xmlInputFactory, useStaxSource, obj); |
278 | 0 | if (src == null) |
279 | |
{ |
280 | 0 | return; |
281 | |
} |
282 | |
|
283 | 0 | StreamResult result = new StreamResult(output); |
284 | |
|
285 | 0 | Transformer idTransformer = XMLUtils.getTransformer(); |
286 | 0 | idTransformer.setOutputProperty(OutputKeys.ENCODING, outputEncoding); |
287 | 0 | idTransformer.transform(src, result); |
288 | 0 | } |
289 | |
|
290 | |
|
291 | |
public String getOutputEncoding() |
292 | |
{ |
293 | 0 | return outputEncoding; |
294 | |
} |
295 | |
|
296 | |
|
297 | |
public void setOutputEncoding(String outputEncoding) |
298 | |
{ |
299 | 0 | this.outputEncoding = outputEncoding; |
300 | 0 | } |
301 | |
|
302 | |
public boolean isUseStaxSource() |
303 | |
{ |
304 | 0 | return useStaxSource; |
305 | |
} |
306 | |
|
307 | |
public void setUseStaxSource(boolean useStaxSource) |
308 | |
{ |
309 | 0 | this.useStaxSource = useStaxSource; |
310 | 0 | } |
311 | |
|
312 | |
public XMLInputFactory getXMLInputFactory() |
313 | |
{ |
314 | 0 | return xmlInputFactory; |
315 | |
} |
316 | |
|
317 | |
public void setXMLInputFactory(XMLInputFactory xmlInputFactory) |
318 | |
{ |
319 | 0 | this.xmlInputFactory = xmlInputFactory; |
320 | 0 | } |
321 | |
|
322 | |
public XMLOutputFactory getXMLOutputFactory() |
323 | |
{ |
324 | 0 | return xmlOutputFactory; |
325 | |
} |
326 | |
|
327 | |
public void setXMLOutputFactory(XMLOutputFactory xmlOutputFactory) |
328 | |
{ |
329 | 0 | this.xmlOutputFactory = xmlOutputFactory; |
330 | 0 | } |
331 | |
|
332 | |
} |