001    /**
002     * 
003     * Copyright 2004 Protique Ltd
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License"); 
006     * you may not use this file except in compliance with the License. 
007     * You may obtain a copy of the License at 
008     * 
009     * http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS, 
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
014     * See the License for the specific language governing permissions and 
015     * limitations under the License. 
016     * 
017     **/
018    package org.codehaus.activesoap.handler.xmlbeans;
019    
020    import org.apache.xmlbeans.XmlObject;
021    import org.apache.xmlbeans.XmlOptions;
022    import org.codehaus.activesoap.Handler;
023    import org.codehaus.activesoap.MessageExchange;
024    
025    import javax.xml.stream.XMLStreamReader;
026    import javax.xml.stream.XMLStreamWriter;
027    
028    /**
029     * Processes an XML document using XMLBeans
030     *
031     * @version $Revision: 1.4 $
032     */
033    public class XMLBeansHandler implements Handler {
034        private XmlObject object;
035        private XmlOptions options = new XmlOptions();
036    
037        public XMLBeansHandler() {
038            options.put(XmlOptions.SAVE_NO_XML_DECL, Boolean.TRUE);
039        }
040    
041        public void invoke(MessageExchange exchange) throws Exception {
042            XMLStreamReader in = exchange.getIn();
043            XMLStreamWriter out = exchange.getOut();
044    
045            // lets reset first in case we get a fault
046            object = null;
047            object = XmlObject.Factory.parse(in);
048            handleBody(exchange, object, out);
049        }
050    
051        /**
052         * Sends the reply object to the output body
053         */
054        public void reply(MessageExchange exchange, XmlObject reply, XMLStreamWriter out) throws Exception {
055            XMLBeansHelper.save(reply, out, exchange.isRepairingNamespace());
056        }
057    
058    
059        // Properties
060        //-------------------------------------------------------------------------
061    
062        /**
063         * Returns the body of the SOAP request converted to the native object
064         * (POJO or XmlObject or DOMish tree etc)
065         * if available or null if the body was not parsed
066         */
067        public XmlObject getObject() {
068            return object;
069        }
070    
071        // Implementation methods
072        //-------------------------------------------------------------------------
073        protected void handleBody(MessageExchange exchange, XmlObject body, XMLStreamWriter out) throws Exception {
074        }
075    
076    }