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
024 import com.thoughtworks.xstream.XStream;
025 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
026 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
027
028 import org.apache.camel.Exchange;
029 import org.apache.camel.converter.jaxp.StaxConverter;
030 import org.apache.camel.spi.DataFormat;
031
032 /**
033 * An abstract class which implement <a href="http://camel.apache.org/data-format.html">data format</a>
034 * ({@link DataFormat}) interface which leverage the XStream library for XML or JSON's marshaling and unmarshaling
035 *
036 * @version $Revision: 738604 $
037 */
038
039 public abstract class AbstractXStreamWrapper implements DataFormat {
040
041 private XStream xstream;
042 private StaxConverter staxConverter;
043
044 public AbstractXStreamWrapper() {
045
046 }
047
048 public AbstractXStreamWrapper(XStream xstream) {
049 this.xstream = xstream;
050 }
051
052 public XStream getXStream() {
053 if (xstream == null) {
054 xstream = createXStream();
055 }
056 return xstream;
057 }
058
059 public void setXStream(XStream xstream) {
060 this.xstream = xstream;
061 }
062
063 protected XStream createXStream() {
064 return new XStream();
065 }
066
067 public StaxConverter getStaxConverter() {
068 if (staxConverter == null) {
069 staxConverter = new StaxConverter();
070 }
071 return staxConverter;
072 }
073
074 public void setStaxConverter(StaxConverter staxConverter) {
075 this.staxConverter = staxConverter;
076 }
077
078 public void marshal(Exchange exchange, Object body, OutputStream stream) throws Exception {
079 HierarchicalStreamWriter writer = createHierarchicalStreamWriter(exchange, body, stream);
080 try {
081 getXStream().marshal(body, writer);
082 } finally {
083 writer.close();
084 }
085 }
086
087 public Object unmarshal(Exchange exchange, InputStream stream) throws Exception {
088 HierarchicalStreamReader reader = createHierarchicalStreamReader(exchange, stream);
089 try {
090 return getXStream().unmarshal(reader);
091 } finally {
092 reader.close();
093 }
094 }
095
096 protected abstract HierarchicalStreamWriter createHierarchicalStreamWriter(Exchange exchange, Object body, OutputStream stream) throws XMLStreamException;
097
098 protected abstract HierarchicalStreamReader createHierarchicalStreamReader(Exchange exchange, InputStream stream) throws XMLStreamException;
099 }