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