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