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 import java.io.InputStream;
020 import java.io.OutputStream;
021
022 import javax.xml.stream.XMLStreamException;
023 import javax.xml.stream.XMLStreamReader;
024 import javax.xml.stream.XMLStreamWriter;
025
026 import com.thoughtworks.xstream.XStream;
027 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
028 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
029 import com.thoughtworks.xstream.io.xml.QNameMap;
030 import com.thoughtworks.xstream.io.xml.StaxReader;
031 import com.thoughtworks.xstream.io.xml.StaxWriter;
032
033 import org.apache.camel.Exchange;
034 import org.apache.camel.spi.ClassResolver;
035 import org.apache.camel.spi.DataFormat;
036
037 /**
038 * A <a href="http://camel.apache.org/data-format.html">data format</a>
039 * ({@link DataFormat}) using XStream to marshal to and from XML
040 *
041 * @version $Revision: 961922 $
042 */
043 public class XStreamDataFormat extends AbstractXStreamWrapper {
044 String encoding;
045
046 public XStreamDataFormat() {
047 }
048
049 public XStreamDataFormat(XStream xstream) {
050 super(xstream);
051 }
052
053 public void setEncoding(String encoding) {
054 this.encoding = encoding;
055 }
056
057 public String getEncoding() {
058 return encoding;
059 }
060
061 /**
062 * A factory method which takes a collection of types to be annotated
063 */
064 public static XStreamDataFormat processAnnotations(ClassResolver resolver, Iterable<Class<?>> types) {
065 XStreamDataFormat answer = new XStreamDataFormat();
066 XStream xstream = answer.getXStream(resolver);
067 for (Class<?> type : types) {
068 xstream.processAnnotations(type);
069 }
070 return answer;
071 }
072
073 /**
074 * A factory method which takes a number of types to be annotated
075 */
076 public static XStreamDataFormat processAnnotations(ClassResolver resolver, Class<?>... types) {
077 XStreamDataFormat answer = new XStreamDataFormat();
078 XStream xstream = answer.getXStream(resolver);
079 for (Class<?> type : types) {
080 xstream.processAnnotations(type);
081 }
082 return answer;
083 }
084
085 // just make sure the exchange property can override the xmlstream encoding setting
086 protected void updateCharactorEncodingInfo(Exchange exchange) {
087 if (exchange.getProperty(Exchange.CHARSET_NAME) == null && encoding != null) {
088 exchange.setProperty(Exchange.CHARSET_NAME, encoding);
089 }
090 }
091
092 protected HierarchicalStreamWriter createHierarchicalStreamWriter(Exchange exchange, Object body, OutputStream stream) throws XMLStreamException {
093 updateCharactorEncodingInfo(exchange);
094 XMLStreamWriter xmlWriter = getStaxConverter().createXMLStreamWriter(stream, exchange);
095 return new StaxWriter(new QNameMap(), xmlWriter);
096 }
097
098 protected HierarchicalStreamReader createHierarchicalStreamReader(Exchange exchange, InputStream stream) throws XMLStreamException {
099 updateCharactorEncodingInfo(exchange);
100 XMLStreamReader xmlReader = getStaxConverter().createXMLStreamReader(stream, exchange);
101 return new StaxReader(new QNameMap(), xmlReader);
102 }
103 }