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