001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  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    package org.apache.camel.converter.xmlbeans;
018    
019    import java.io.File;
020    import java.io.IOException;
021    import java.io.InputStream;
022    import java.io.Reader;
023    import java.nio.ByteBuffer;
024    
025    import javax.xml.parsers.ParserConfigurationException;
026    import javax.xml.transform.Source;
027    import javax.xml.transform.TransformerException;
028    
029    import org.w3c.dom.Document;
030    import org.w3c.dom.Node;
031    import org.xml.sax.SAXException;
032    
033    import org.apache.camel.Converter;
034    import org.apache.camel.Exchange;
035    import org.apache.camel.converter.IOConverter;
036    import org.apache.camel.converter.NIOConverter;
037    import org.apache.camel.converter.jaxp.XmlConverter;
038    import org.apache.xmlbeans.XmlException;
039    import org.apache.xmlbeans.XmlObject;
040    import org.apache.xmlbeans.impl.piccolo.xml.XMLStreamReader;
041    
042    /**
043     * A <a href="http://camel.apache.org/type-coverter.html">Type Converter</a>
044     * of XMLBeans objects
045     *
046     * @version $Revision: 2431 $
047     */
048    @Converter
049    public class XmlBeansConverter {
050        private XmlConverter xmlConverter = new XmlConverter();
051    
052        @Converter
053        public static XmlObject toXmlObject(File value) throws IOException, XmlException {
054            return XmlObject.Factory.parse(value);
055        }
056    
057        @Converter
058        public static XmlObject toXmlObject(Reader value) throws IOException, XmlException {
059            return XmlObject.Factory.parse(value);
060        }
061    
062        @Converter
063        public static XmlObject toXmlObject(Node value) throws IOException, XmlException {
064            return XmlObject.Factory.parse(value);
065        }
066    
067        @Converter
068        public static XmlObject toXmlObject(InputStream value) throws IOException, XmlException {
069            return XmlObject.Factory.parse(value);
070        }
071    
072        @Converter
073        public static XmlObject toXmlObject(String value, Exchange exchange) throws IOException, XmlException {
074            return toXmlObject(IOConverter.toInputStream(value, exchange));
075        }
076    
077        @Converter
078        public static XmlObject toXmlObject(byte[] value) throws IOException, XmlException {
079            return toXmlObject(IOConverter.toInputStream(value));
080        }
081    
082        @Converter
083        public static XmlObject toXmlObject(ByteBuffer value) throws IOException, XmlException {
084            return toXmlObject(NIOConverter.toInputStream(value));
085        }
086    
087        @Converter
088        public static XmlObject toXmlObject(XMLStreamReader value) throws IOException, XmlException {
089            return XmlObject.Factory.parse(value);
090        }
091    
092        @Converter
093        public XmlObject toXmlObject(Source value) throws IOException, XmlException, TransformerException, ParserConfigurationException, SAXException {
094            Document document = getXmlConverter().toDOMDocument(value);
095            return toXmlObject(document);
096        }
097    
098        // Properties
099        //-------------------------------------------------------------------------
100        public XmlConverter getXmlConverter() {
101            return xmlConverter;
102        }
103    
104        public void setXmlConverter(XmlConverter xmlConverter) {
105            this.xmlConverter = xmlConverter;
106        }
107    }