1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transformers.xml; |
12 | |
|
13 | |
import org.mule.umo.UMOEventContext; |
14 | |
import org.mule.umo.transformer.TransformerException; |
15 | |
|
16 | |
import java.io.ByteArrayInputStream; |
17 | |
import java.io.InputStreamReader; |
18 | |
import java.io.Reader; |
19 | |
import java.io.UnsupportedEncodingException; |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
public class XmlToObject extends AbstractXStreamTransformer |
31 | |
{ |
32 | |
|
33 | 38 | private final DomDocumentToXml domTransformer = new DomDocumentToXml(); |
34 | |
|
35 | |
public XmlToObject() |
36 | 38 | { |
37 | 46 | registerSourceType(String.class); |
38 | 38 | registerSourceType(byte[].class); |
39 | 38 | registerSourceType(org.w3c.dom.Document.class); |
40 | 38 | registerSourceType(org.dom4j.Document.class); |
41 | 38 | } |
42 | |
|
43 | |
public Object transform(Object src, String encoding, UMOEventContext context) throws TransformerException |
44 | |
{ |
45 | 16 | if (src instanceof byte[]) |
46 | |
{ |
47 | |
try |
48 | |
{ |
49 | 4 | Reader xml = new InputStreamReader(new ByteArrayInputStream((byte[]) src), encoding); |
50 | 4 | return getXStream().fromXML(xml); |
51 | |
} |
52 | 0 | catch (UnsupportedEncodingException uee) |
53 | |
{ |
54 | 0 | throw new TransformerException(this, uee); |
55 | |
} |
56 | |
} |
57 | 12 | else if (src instanceof String) |
58 | |
{ |
59 | 12 | return getXStream().fromXML(src.toString()); |
60 | |
} |
61 | |
else |
62 | |
{ |
63 | 0 | return getXStream().fromXML((String) domTransformer.transform(src)); |
64 | |
} |
65 | |
} |
66 | |
|
67 | |
} |