View Javadoc

1   package org.codehaus.xfire.xmlbeans;
2   
3   import javax.xml.namespace.QName;
4   import javax.xml.stream.XMLStreamException;
5   
6   import org.apache.xmlbeans.SchemaType;
7   import org.apache.xmlbeans.XmlCursor;
8   import org.apache.xmlbeans.XmlException;
9   import org.apache.xmlbeans.XmlObject;
10  import org.apache.xmlbeans.XmlOptions;
11  import org.codehaus.xfire.MessageContext;
12  import org.codehaus.xfire.fault.XFireFault;
13  import org.codehaus.xfire.message.MessageReader;
14  import org.codehaus.xfire.message.MessageWriter;
15  import org.codehaus.xfire.type.Type;
16  import org.codehaus.xfire.util.STAXUtils;
17  import org.dom4j.Element;
18  
19  /***
20   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
21   * @since Nov 13, 2004
22   */
23  public class XMLBeansType
24  	extends Type
25  {
26      private SchemaType schemaType;
27  
28      final static XmlOptions options = new XmlOptions();
29      static
30      {
31          options.setSaveInner();
32      }
33      
34      public XMLBeansType()
35      {
36      }
37      
38      public XMLBeansType(SchemaType schemaType)
39      {
40          this.schemaType = schemaType;
41      }
42  
43      public Object readObject(MessageReader reader, MessageContext context) 
44      	throws XFireFault
45      {
46          try
47          {
48              return XmlObject.Factory.parse(reader.getXMLStreamReader());
49          }
50          catch( XmlException e )
51          {
52              throw new XFireFault("Could not read request.", e, XFireFault.SENDER);
53          }
54      }
55  
56      public void writeObject(Object o, MessageWriter writer, MessageContext context)
57      	throws XFireFault
58      {
59          try
60          {
61              XmlObject obj = (XmlObject) o; 
62  
63              XmlCursor cursor = obj.newCursor();
64              if (cursor.toFirstChild() && cursor.toFirstChild())
65              {
66                  do
67                  {
68                      STAXUtils.copy(cursor.newXMLStreamReader(), 
69                                     writer.getXMLStreamWriter());
70                  }
71                  while(cursor.toNextSibling());
72              }
73          } 
74          catch (XMLStreamException e)
75          {
76              throw new XFireFault("Could not write response.", e, XFireFault.SENDER);
77          }
78      }
79  
80      public void writeSchema(Element element)
81      {
82          // todo:
83      }
84      
85      public boolean isComplex()
86      {
87          return true;
88      }
89      
90      /***
91       * @return Returns the schemaType.
92       */
93      public QName getSchemaType()
94      {
95          return schemaType.getDocumentElementName();
96      }
97  }