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