1 package org.codehaus.xfire.jaxb;
2
3 import javanet.staxutils.StAXSource;
4
5 import javax.xml.bind.JAXBContext;
6 import javax.xml.bind.JAXBException;
7 import javax.xml.bind.Marshaller;
8 import javax.xml.bind.Unmarshaller;
9 import javax.xml.namespace.QName;
10 import javax.xml.stream.XMLStreamReader;
11
12 import org.codehaus.xfire.MessageContext;
13 import org.codehaus.xfire.XFireRuntimeException;
14 import org.codehaus.xfire.aegis.MessageReader;
15 import org.codehaus.xfire.aegis.MessageWriter;
16 import org.codehaus.xfire.aegis.stax.ElementReader;
17 import org.codehaus.xfire.aegis.stax.ElementWriter;
18 import org.codehaus.xfire.aegis.type.Type;
19 import org.codehaus.xfire.fault.XFireFault;
20
21 public class JaxbType extends Type
22 {
23 private QName mySchemaType;
24
25 public JaxbType(Class clazz, JAXBContext jaxbContext)
26 {
27 mySchemaType = new JaxbIntrospector(jaxbContext).introspect(clazz);
28 try
29 {
30 unmarshaller = jaxbContext.createUnmarshaller();
31 unmarshaller.setValidating(false);
32 } catch (JAXBException e)
33 {
34 throw new XFireRuntimeException("error creating unmarshaller", e);
35 }
36 try
37 {
38 marshaller = jaxbContext.createMarshaller();
39 } catch (JAXBException e)
40 {
41 throw new XFireRuntimeException("error creating marshaller", e);
42 }
43 }
44
45 Unmarshaller unmarshaller;
46 Marshaller marshaller;
47
48 public boolean isAbstract()
49 {
50
51 return false;
52 }
53
54 public boolean isComplex()
55 {
56 return true;
57 }
58
59 public Object readObject(MessageReader reader, MessageContext context) throws XFireFault
60 {
61 try
62 {
63 XMLStreamReader xmlStreamReader = ((ElementReader) reader).getXMLStreamReader();
64 System.out.println(xmlStreamReader.hasNext());
65 StAXSource stAXSource = new StAXSource(xmlStreamReader);
66 System.out.println(stAXSource.toString());
67 return unmarshaller.unmarshal(stAXSource);
68 } catch (Exception e)
69 {
70 e.printStackTrace();
71 throw new XFireFault("Could not unmarshall type.", e, XFireFault.SENDER);
72 }
73 }
74
75 public void writeObject(Object object, MessageWriter writer, MessageContext context) throws XFireFault
76 {
77 try
78 {
79
80 marshaller.marshal(object, new FilteringStaxResult(((ElementWriter) writer).getXMLStreamWriter()));
81 } catch (JAXBException e)
82 {
83 throw new XFireFault("Could not marshall type.", e, XFireFault.RECEIVER);
84 }
85
86 }
87
88 public QName getSchemaType()
89 {
90 return mySchemaType;
91 }
92
93 public boolean isWriteOuter()
94 {
95 return false;
96 }
97 }