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.dataformat.xstream;
018
019
020 import java.io.InputStream;
021 import java.io.OutputStream;
022
023 import javax.xml.stream.XMLStreamException;
024 import javax.xml.stream.XMLStreamReader;
025 import javax.xml.stream.XMLStreamWriter;
026
027 import com.thoughtworks.xstream.XStream;
028 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
029 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
030 import com.thoughtworks.xstream.io.xml.QNameMap;
031 import com.thoughtworks.xstream.io.xml.StaxReader;
032 import com.thoughtworks.xstream.io.xml.StaxWriter;
033
034 import org.apache.camel.Exchange;
035 import org.apache.camel.converter.jaxp.StaxConverter;
036 import org.apache.camel.spi.DataFormat;
037
038 /**
039 * A <a href="http://activemq.apache.org/camel/data-format.html">data format</a>
040 * ({@link DataFormat}) using XStream to marshal to and from XML
041 *
042 * @version $Revision: 711862 $
043 */
044 public class XStreamDataFormat extends AbstractXStreamWrapper {
045
046 public XStreamDataFormat() {
047 super();
048 }
049
050 public XStreamDataFormat(XStream xstream) {
051 super(xstream);
052 }
053
054 /**
055 * A factory method which takes a collection of types to be annotated
056 */
057 public static XStreamDataFormat processAnnotations(Iterable<Class<?>> types) {
058 XStreamDataFormat answer = new XStreamDataFormat();
059 XStream xstream = answer.getXStream();
060 for (Class<?> type : types) {
061 xstream.processAnnotations(type);
062 }
063 return answer;
064 }
065
066 /**
067 * A factory method which takes a number of types to be annotated
068 */
069 public static XStreamDataFormat processAnnotations(Class<?>... types) {
070 XStreamDataFormat answer = new XStreamDataFormat();
071 XStream xstream = answer.getXStream();
072 for (Class<?> type : types) {
073 xstream.processAnnotations(type);
074 }
075 return answer;
076 }
077
078 protected HierarchicalStreamWriter createHierarchicalStreamWriter(Exchange exchange, Object body, OutputStream stream) throws XMLStreamException {
079 XMLStreamWriter xmlWriter = getStaxConverter().createXMLStreamWriter(stream);
080 return new StaxWriter(new QNameMap(), xmlWriter);
081 }
082
083 protected HierarchicalStreamReader createHierarchicalStreamReader(Exchange exchange, InputStream stream) throws XMLStreamException {
084 XMLStreamReader xmlReader = getStaxConverter().createXMLStreamReader(stream);
085 return new StaxReader(new QNameMap(), xmlReader);
086 }
087 }