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
018/*
019 * Copyright (c) 2007, 2008 XStream Committers.
020 * All rights reserved.
021 *
022 * The software in this package is published under the terms of the BSD
023 * style license a copy of which has been included with this distribution in
024 * the LICENSE.txt file.
025 * 
026 * Created on 30. March 2007 by Joerg Schaible
027 */
028package org.apache.activemq.util;
029
030import java.io.InputStream;
031import java.io.OutputStream;
032import java.io.Reader;
033import java.io.Writer;
034
035import javax.xml.stream.XMLStreamException;
036
037import org.codehaus.jettison.mapped.Configuration;
038import org.codehaus.jettison.mapped.MappedNamespaceConvention;
039import org.codehaus.jettison.mapped.MappedXMLInputFactory;
040import org.codehaus.jettison.mapped.MappedXMLOutputFactory;
041
042import com.thoughtworks.xstream.io.HierarchicalStreamDriver;
043import com.thoughtworks.xstream.io.HierarchicalStreamReader;
044import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
045import com.thoughtworks.xstream.io.StreamException;
046import com.thoughtworks.xstream.io.json.JettisonStaxWriter;
047import com.thoughtworks.xstream.io.xml.QNameMap;
048import com.thoughtworks.xstream.io.xml.StaxReader;
049import com.thoughtworks.xstream.io.xml.StaxWriter;
050
051
052/**
053 * 
054 * Temporary used until XStream 1.3.2 is released
055 * 
056 * Simple XStream driver wrapping Jettison's Mapped reader and writer. Serializes object from
057 * and to JSON.
058 * 
059 * @author Dejan Bosanac
060 */
061public class JettisonMappedXmlDriver implements HierarchicalStreamDriver {
062
063    private final MappedXMLOutputFactory mof;
064    private final MappedXMLInputFactory mif;
065    private final MappedNamespaceConvention convention;
066    private boolean useSerializeAsArray = true;
067
068    public JettisonMappedXmlDriver() {
069        this(new Configuration(), true);
070    }
071
072    public JettisonMappedXmlDriver(final Configuration config, final boolean useSerializeAsArray) {
073        mof = new MappedXMLOutputFactory(config);
074        mif = new MappedXMLInputFactory(config);
075        convention = new MappedNamespaceConvention(config);
076        this.useSerializeAsArray = useSerializeAsArray;
077    }
078    
079    public HierarchicalStreamReader createReader(final Reader reader) {
080        try {
081            return new StaxReader(new QNameMap(), mif.createXMLStreamReader(reader));
082        } catch (final XMLStreamException e) {
083            throw new StreamException(e);
084        }
085    }
086
087    public HierarchicalStreamReader createReader(final InputStream input) {
088        try {
089            return new StaxReader(new QNameMap(), mif.createXMLStreamReader(input));
090        } catch (final XMLStreamException e) {
091            throw new StreamException(e);
092        }
093    }
094
095    public HierarchicalStreamWriter createWriter(final Writer writer) {
096        try {
097            if (useSerializeAsArray) {
098                return new JettisonStaxWriter(new QNameMap(), mof.createXMLStreamWriter(writer), convention);
099            } else {
100                return new StaxWriter(new QNameMap(), mof.createXMLStreamWriter(writer));
101            }
102        } catch (final XMLStreamException e) {
103            throw new StreamException(e);
104        }
105    }
106
107    public HierarchicalStreamWriter createWriter(final OutputStream output) {
108        try {
109            if (useSerializeAsArray) {
110                return new JettisonStaxWriter(new QNameMap(), mof.createXMLStreamWriter(output), convention);
111            } else {
112                return new StaxWriter(new QNameMap(), mof.createXMLStreamWriter(output));
113            }
114        } catch (final XMLStreamException e) {
115            throw new StreamException(e);
116        }
117    }
118
119}