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.component.cxf;
018
019 import javax.xml.stream.XMLStreamReader;
020 import javax.xml.stream.XMLStreamWriter;
021
022 import org.w3c.dom.Node;
023
024 import org.apache.cxf.databinding.DataReader;
025 import org.apache.cxf.databinding.DataWriter;
026 import org.apache.cxf.databinding.source.NodeDataReader;
027 import org.apache.cxf.databinding.source.NodeDataWriter;
028 import org.apache.cxf.databinding.source.XMLStreamDataReader;
029 import org.apache.cxf.databinding.source.XMLStreamDataWriter;
030 import org.apache.cxf.jaxb.JAXBDataBinding;
031
032 /**
033 * This is a hybrid DataBinding of {@link JAXBDataBinding} and {@link org.apache.cxf.databinding.source.SourceDataBinding}.
034 * Like the SourceDataBinding, this DataBinding de/serializes parameters as DOMSource objects. And like the JAXBDataBinding, the
035 * {@link #initialize(org.apache.cxf.service.Service)}
036 * method can initialize the service model's message part schema based on the service class in the message part info.
037 * Hence, this DataBinding supports DOMSource object de/serialization without requiring users to provide a WSDL.
038 *
039 * @version @Revision: 789534 $
040 */
041 public class HybridSourceDataBinding extends JAXBDataBinding {
042 private XMLStreamDataReader xsrReader;
043 private XMLStreamDataWriter xswWriter;
044 private NodeDataWriter nodeWriter;
045 private NodeDataReader nodeReader;
046
047 public HybridSourceDataBinding() {
048 super();
049 this.xsrReader = new XMLStreamDataReader();
050 this.xswWriter = new XMLStreamDataWriter();
051
052 this.nodeReader = new NodeDataReader();
053 this.nodeWriter = new NodeDataWriter();
054 }
055
056 @SuppressWarnings("unchecked")
057 @Override
058 public <T> DataReader<T> createReader(Class<T> cls) {
059 if (cls == XMLStreamReader.class) {
060 return (DataReader<T>) xsrReader;
061 } else if (cls == Node.class) {
062 return (DataReader<T>) nodeReader;
063 } else {
064 throw new UnsupportedOperationException("The type " + cls.getName() + " is not supported.");
065 }
066 }
067
068 @Override
069 public Class<?>[] getSupportedReaderFormats() {
070 return new Class[] {XMLStreamReader.class, Node.class};
071 }
072
073 @SuppressWarnings("unchecked")
074 @Override
075 public <T> DataWriter<T> createWriter(Class<T> cls) {
076 if (cls == XMLStreamWriter.class) {
077 return (DataWriter<T>) xswWriter;
078 } else if (cls == Node.class) {
079 return (DataWriter<T>) nodeWriter;
080 } else {
081 throw new UnsupportedOperationException("The type " + cls.getName() + " is not supported.");
082 }
083 }
084
085 @Override
086 public Class<?>[] getSupportedWriterFormats() {
087 return new Class[] {XMLStreamWriter.class, Node.class};
088 }
089 }