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