View Javadoc

1   /***
2    * 
3    * Copyright 2004 Protique Ltd
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * You may obtain a copy of the License at 
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License. 
16   * 
17   **/
18  package org.codehaus.xfire.xmlbeans;
19  
20  import javax.xml.stream.XMLStreamConstants;
21  import javax.xml.stream.XMLStreamReader;
22  import org.apache.xmlbeans.XmlObject;
23  import org.apache.xmlbeans.XmlOptions;
24  import org.codehaus.xfire.MessageContext;
25  import org.codehaus.xfire.handler.AbstractHandler;
26  import org.codehaus.xfire.handler.Handler;
27  
28  /***
29   * Processes an XML document using XMLBeans
30   * 
31   * @version $Revision: 1.2 $
32   */
33  public class XMLBeansHandler
34      extends AbstractHandler
35      implements Handler
36  {
37      private XmlObject object;
38      private XmlOptions options = new XmlOptions();
39      
40      public XMLBeansHandler()
41      {
42          options.put( XmlOptions.SAVE_NO_XML_DECL, Boolean.TRUE );
43      }
44  
45      public void invoke( MessageContext context, XMLStreamReader in ) throws Exception
46      {
47          object = null;
48          if ( in.getEventType() != XMLStreamConstants.END_ELEMENT )
49          {
50              object = XmlObject.Factory.parse( in );
51          }
52          
53          handleBody( context, object );
54      }
55      
56      /***
57       * Sends the reply object to the output body
58       */
59      public void reply( MessageContext context, 
60                         XmlObject reply ) throws Exception
61      {
62          // also possible to do getStreamWriter() and use that.
63          reply.save( context.getResponseStream() );
64      }
65      
66      // Properties
67      //-------------------------------------------------------------------------
68      /***
69       * Returns the body of the SOAP request converted to the native object (POJO
70       * or XmlObject or DOMish tree etc) if available or null if the body was not
71       * parsed
72       */
73      public XmlObject getObject()
74      {
75          return object;
76      }
77      
78      // Implementation methods
79      //-------------------------------------------------------------------------
80      protected void handleBody( MessageContext context, 
81                                 XmlObject body )
82          throws Exception
83      {
84      }
85  }