1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.module.xml.transformer; |
8 | |
|
9 | |
import org.mule.api.MuleMessage; |
10 | |
import org.mule.api.transformer.TransformerException; |
11 | |
import org.mule.transformer.types.DataTypeFactory; |
12 | |
import org.mule.util.store.DeserializationPostInitialisable; |
13 | |
|
14 | |
import java.io.ByteArrayInputStream; |
15 | |
import java.io.IOException; |
16 | |
import java.io.InputStream; |
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 | 0 | private final DomDocumentToXml domTransformer = new DomDocumentToXml(); |
34 | |
|
35 | |
public XmlToObject() |
36 | 0 | { |
37 | 0 | registerSourceType(DataTypeFactory.STRING); |
38 | 0 | registerSourceType(DataTypeFactory.BYTE_ARRAY); |
39 | 0 | registerSourceType(DataTypeFactory.INPUT_STREAM); |
40 | 0 | registerSourceType(DataTypeFactory.create(org.w3c.dom.Document.class)); |
41 | 0 | registerSourceType(DataTypeFactory.create(org.dom4j.Document.class)); |
42 | 0 | setReturnDataType(DataTypeFactory.OBJECT); |
43 | 0 | } |
44 | |
|
45 | |
@Override |
46 | |
public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException |
47 | |
{ |
48 | 0 | Object src = message.getPayload(); |
49 | |
Object result; |
50 | 0 | if (src instanceof byte[]) |
51 | |
{ |
52 | |
try |
53 | |
{ |
54 | 0 | Reader xml = new InputStreamReader(new ByteArrayInputStream((byte[]) src), outputEncoding); |
55 | 0 | result = getXStream().fromXML(xml); |
56 | |
} |
57 | 0 | catch (UnsupportedEncodingException e) |
58 | |
{ |
59 | 0 | throw new TransformerException(this, e); |
60 | 0 | } |
61 | |
} |
62 | 0 | else if (src instanceof InputStream) |
63 | |
{ |
64 | 0 | InputStream input = (InputStream) src; |
65 | |
try |
66 | |
{ |
67 | 0 | Reader xml = new InputStreamReader(input, outputEncoding); |
68 | 0 | result = getXStream().fromXML(xml); |
69 | |
} |
70 | 0 | catch (Exception e) |
71 | |
{ |
72 | 0 | throw new TransformerException(this, e); |
73 | |
} |
74 | |
finally |
75 | |
{ |
76 | 0 | try |
77 | |
{ |
78 | 0 | input.close(); |
79 | |
} |
80 | 0 | catch (IOException e) |
81 | |
{ |
82 | 0 | logger.warn("Exception closing stream: ", e); |
83 | 0 | } |
84 | 0 | } |
85 | 0 | } |
86 | 0 | else if (src instanceof String) |
87 | |
{ |
88 | 0 | result = getXStream().fromXML(src.toString()); |
89 | |
} |
90 | |
else |
91 | |
{ |
92 | 0 | result = getXStream().fromXML((String) domTransformer.transform(src)); |
93 | |
} |
94 | |
|
95 | |
try |
96 | |
{ |
97 | 0 | postDeserialisationInit(result); |
98 | 0 | return result; |
99 | |
} |
100 | 0 | catch (Exception e) |
101 | |
{ |
102 | 0 | throw new TransformerException(this, e); |
103 | |
} |
104 | |
} |
105 | |
|
106 | |
protected void postDeserialisationInit(final Object object) throws Exception |
107 | |
{ |
108 | 0 | if (object instanceof DeserializationPostInitialisable) |
109 | |
{ |
110 | 0 | DeserializationPostInitialisable.Implementation.init(object, muleContext); |
111 | |
} |
112 | 0 | } |
113 | |
|
114 | |
} |