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 javanet.staxutils.ContentHandlerToXMLStreamWriter;
021    import org.apache.commons.logging.Log;
022    import org.apache.commons.logging.LogFactory;
023    import org.apache.xmlbeans.SchemaType;
024    import org.apache.xmlbeans.XmlCursor;
025    import org.apache.xmlbeans.XmlObject;
026    import org.apache.xmlbeans.XmlOptions;
027    import org.codehaus.activesoap.RestClient;
028    import org.codehaus.activesoap.RestService;
029    import org.codehaus.activesoap.SoapClient;
030    import org.codehaus.activesoap.SoapService;
031    import org.codehaus.activesoap.transport.LocalTransportClient;
032    import org.codehaus.activesoap.transport.TransportClient;
033    import org.codehaus.activesoap.util.XMLStreamHelper;
034    import org.xml.sax.SAXException;
035    
036    import javax.xml.namespace.QName;
037    import javax.xml.stream.XMLStreamException;
038    import javax.xml.stream.XMLStreamWriter;
039    import javax.xml.stream.XMLStreamReader;
040    
041    /**
042     * @version $Revision: 1.7 $
043     */
044    public class XMLBeansHelper {
045    
046        public static RestClient createRestClient(TransportClient transportClient) {
047            RestService service = new RestService();
048            RestClient client = new RestClient(transportClient, service);
049            useXMLBeans(client);
050            return client;
051        }
052    
053        public static SoapClient createSoapClient(TransportClient transportClient) {
054            SoapService service = new SoapService();
055            SoapClient client = new SoapClient(transportClient, service);
056            useXMLBeans(client);
057            return client;
058        }
059    
060        public static RestClient newLocalRestClient(RestService service) {
061            RestClient client = new RestClient(new LocalTransportClient(service), service);
062            useXMLBeans(client);
063            return client;
064        }
065    
066        public static SoapClient newLocalSoapClient(SoapService service) {
067            SoapClient client = new SoapClient(new LocalTransportClient(service), service);
068            useXMLBeans(client);
069            return client;
070        }
071    
072        /**
073         * Configures the given {@link RestClient} to use XMLBeans as the default
074         * client side marshalling layer
075         */
076        public static void useXMLBeans(RestClient client) {
077            client.setClientHandler(new XMLBeansClientHandler());
078        }
079    
080    
081    
082        /**
083         * Saves the XMLBean to the given StAX output stream  until XMLBeans natively supports
084         * this in which case this method could be retired.
085         */
086        public static void save(XmlObject xmlObject, XMLStreamWriter out, boolean repairing) throws XMLStreamException {
087            XMLStreamReader reader = xmlObject.newXMLStreamReader();
088            XMLStreamHelper.copy(reader, out, repairing);
089        }
090    
091        /**
092         * Returns true if the given object is an element type
093         */
094        public static boolean isElement(XmlObject xmlObject) {
095            SchemaType schemaType = xmlObject.schemaType();
096    
097            // TODO there must be an easier way?
098            return !schemaType.isDocumentType() && !schemaType.isAttributeType() && !schemaType.isSimpleType();
099        }
100    }