1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.module.xml.transformer.jaxb; |
11 | |
|
12 | |
import org.mule.api.MuleEvent; |
13 | |
import org.mule.api.lifecycle.InitialisationException; |
14 | |
import org.mule.api.transformer.DataType; |
15 | |
import org.mule.api.transformer.TransformerException; |
16 | |
import org.mule.api.transport.OutputHandler; |
17 | |
import org.mule.config.i18n.CoreMessages; |
18 | |
import org.mule.transformer.AbstractTransformer; |
19 | |
import org.mule.transformer.types.DataTypeFactory; |
20 | |
|
21 | |
import java.io.ByteArrayOutputStream; |
22 | |
import java.io.IOException; |
23 | |
import java.io.OutputStream; |
24 | |
import java.io.StringWriter; |
25 | |
import java.io.Writer; |
26 | |
|
27 | |
import javax.xml.bind.JAXBContext; |
28 | |
import javax.xml.bind.JAXBException; |
29 | |
import javax.xml.bind.Marshaller; |
30 | |
import javax.xml.parsers.DocumentBuilderFactory; |
31 | |
|
32 | |
import org.w3c.dom.Document; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public class JAXBMarshallerTransformer extends AbstractTransformer |
42 | |
{ |
43 | |
protected JAXBContext jaxbContext; |
44 | |
|
45 | |
protected Class<?> sourceClass; |
46 | |
|
47 | |
public JAXBMarshallerTransformer() |
48 | 0 | { |
49 | 0 | setReturnDataType(DataTypeFactory.create(OutputStream.class)); |
50 | 0 | registerSourceType(DataTypeFactory.OBJECT); |
51 | 0 | } |
52 | |
|
53 | |
public JAXBMarshallerTransformer(JAXBContext jaxbContext, DataType returnType) |
54 | |
{ |
55 | 0 | this(); |
56 | 0 | this.jaxbContext = jaxbContext; |
57 | 0 | setReturnDataType(returnType); |
58 | 0 | } |
59 | |
|
60 | |
@Override |
61 | |
public void initialise() throws InitialisationException |
62 | |
{ |
63 | 0 | super.initialise(); |
64 | 0 | if (jaxbContext == null) |
65 | |
{ |
66 | 0 | throw new InitialisationException(CoreMessages.objectIsNull("jaxbContext"), this); |
67 | |
} |
68 | 0 | } |
69 | |
|
70 | |
@Override |
71 | |
protected Object doTransform(Object src, String encoding) throws TransformerException |
72 | |
{ |
73 | |
try |
74 | |
{ |
75 | 0 | final Marshaller m = jaxbContext.createMarshaller(); |
76 | 0 | if (getReturnClass().equals(String.class)) |
77 | |
{ |
78 | 0 | Writer w = new StringWriter(); |
79 | 0 | m.marshal(src, w); |
80 | 0 | return w.toString(); |
81 | |
} |
82 | 0 | else if (getReturnClass().isAssignableFrom(Writer.class)) |
83 | |
{ |
84 | 0 | Writer w = new StringWriter(); |
85 | 0 | m.marshal(src, w); |
86 | 0 | return w; |
87 | |
} |
88 | 0 | else if (Document.class.isAssignableFrom(getReturnClass())) |
89 | |
{ |
90 | 0 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
91 | 0 | Document doc = factory.newDocumentBuilder().newDocument(); |
92 | 0 | m.marshal(src, doc); |
93 | 0 | return doc; |
94 | |
} |
95 | 0 | else if (OutputStream.class.isAssignableFrom(getReturnClass())) |
96 | |
{ |
97 | 0 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
98 | 0 | m.marshal(src, out); |
99 | 0 | return out; |
100 | |
} |
101 | 0 | else if (OutputHandler.class.equals(getReturnClass())) |
102 | |
{ |
103 | 0 | return new OutputHandler() |
104 | 0 | { |
105 | |
public void write(MuleEvent event, OutputStream out) throws IOException |
106 | |
{ |
107 | |
try |
108 | |
{ |
109 | 0 | m.marshal(event.getMessage().getPayload(), out); |
110 | |
} |
111 | 0 | catch (JAXBException e) |
112 | |
{ |
113 | 0 | IOException iox = new IOException("failed to mashal objec tto XML"); |
114 | 0 | iox.initCause(e); |
115 | 0 | throw iox; |
116 | 0 | } |
117 | 0 | } |
118 | |
}; |
119 | |
} |
120 | |
else |
121 | |
{ |
122 | 0 | throw new TransformerException(CoreMessages.transformerInvalidReturnType(getReturnClass(), getName())); |
123 | |
} |
124 | |
|
125 | |
} |
126 | 0 | catch (Exception e) |
127 | |
{ |
128 | 0 | throw new TransformerException(this, e); |
129 | |
} |
130 | |
} |
131 | |
|
132 | |
public JAXBContext getJaxbContext() |
133 | |
{ |
134 | 0 | return jaxbContext; |
135 | |
} |
136 | |
|
137 | |
public void setJaxbContext(JAXBContext jaxbContext) |
138 | |
{ |
139 | 0 | this.jaxbContext = jaxbContext; |
140 | 0 | } |
141 | |
|
142 | |
public Class<?> getSourceClass() |
143 | |
{ |
144 | 0 | return sourceClass; |
145 | |
} |
146 | |
|
147 | |
public void setSourceClass(Class<?> sourceClass) |
148 | |
{ |
149 | 0 | this.sourceClass = sourceClass; |
150 | 0 | } |
151 | |
} |