01: package org.objectweb.celtix.bus.jaxws.io;
02:
03: import javax.xml.bind.JAXBContext;
04: import javax.xml.bind.Marshaller;
05: import javax.xml.namespace.QName;
06: import javax.xml.parsers.DocumentBuilder;
07: import javax.xml.parsers.DocumentBuilderFactory;
08: import javax.xml.parsers.ParserConfigurationException;
09: import javax.xml.soap.SOAPBody;
10: import javax.xml.transform.dom.DOMResult;
11: import javax.xml.transform.dom.DOMSource;
12: import javax.xml.transform.sax.SAXSource;
13: import javax.xml.transform.stream.StreamSource;
14:
15: import org.w3c.dom.Document;
16:
17: import org.objectweb.celtix.bindings.DataWriter;
18: import org.objectweb.celtix.bus.jaxws.DynamicDataBindingCallback;
19: import org.objectweb.celtix.context.ObjectMessageContext;
20:
21: public class SOAPBodyDataWriter<T> implements DataWriter<T> {
22:
23: protected SOAPBody dest;
24: final DynamicDataBindingCallback callback;
25:
26: public SOAPBodyDataWriter(DynamicDataBindingCallback cb) {
27: callback = cb;
28: }
29:
30: public void write(Object obj, T output) {
31:
32: dest = (SOAPBody) output;
33: try {
34: if (DOMSource.class.isAssignableFrom(obj.getClass())) {
35: DOMSource domSource = (DOMSource) obj;
36: dest.addDocument((Document) domSource.getNode());
37: } else if (SAXSource.class.isAssignableFrom(obj.getClass())) {
38: SAXSource saxSource = (SAXSource) obj;
39: Document doc = getDocBuilder().parse(
40: saxSource.getInputSource());
41: dest.addDocument(doc);
42: } else if (StreamSource.class.isAssignableFrom(obj
43: .getClass())) {
44: StreamSource streamSource = (StreamSource) obj;
45: Document doc = getDocBuilder().parse(
46: streamSource.getInputStream());
47: dest.addDocument(doc);
48: } else if (Object.class.isAssignableFrom(obj.getClass())) {
49:
50: JAXBContext context = callback.getJAXBContext();
51:
52: Marshaller u = context.createMarshaller();
53: u.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
54: u.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
55: Boolean.TRUE);
56: u.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
57:
58: DOMResult domResult = new DOMResult();
59: u.marshal(obj, domResult);
60: dest.addDocument((Document) domResult.getNode());
61:
62: }
63: } catch (Exception e) {
64: // TODO Auto-generated catch block
65: e.printStackTrace();
66: }
67:
68: }
69:
70: public void write(Object obj, QName elName, T output) {
71: //Complete
72: }
73:
74: public void writeWrapper(ObjectMessageContext objCtx,
75: boolean isOutbound, T output) {
76: // Complete
77: }
78:
79: private DocumentBuilder getDocBuilder()
80: throws ParserConfigurationException {
81:
82: DocumentBuilderFactory dbf = DocumentBuilderFactory
83: .newInstance();
84: dbf.setNamespaceAware(true);
85: return dbf.newDocumentBuilder();
86:
87: }
88:
89: }
|