/*********************************************************************
* XStreamAdapter.java
* created on 08.08.2004 by netseeker
* $Source: /cvsroot/ejoe/EJOE/src/de/netseeker/ejoe/adapter/XStreamAdapter.java,v $
* $Date: 2007/11/17 10:59:40 $
* $Revision: 1.37 $
*
* ====================================================================
*
* Copyright 2005-2006 netseeker aka Michael Manske
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*
* This file is part of the ejoe framework.
* For more information on the author, please see
* <http://www.manskes.de/>.
*
*********************************************************************/
package de.netseeker.ejoe.adapter;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.HierarchicalStreamDriver;
import com.thoughtworks.xstream.io.xml.CompactWriter;
import de.netseeker.ejoe.EJConstants;
/**
* An adapter for (de)serializing objects via the great XStream library
*
* @author netseeker aka Michael Manske
* @since 0.3.0
* @link http://xstream.codehaus.org
*/
public class XStreamAdapter extends BaseAdapter
{
private static final long serialVersionUID = 1L;
protected XStream _xstream = null;
private boolean _isCompact = true;
/**
* Creates a new instance of XStreamAdapter using compact XML
*/
public XStreamAdapter()
{
this( true );
}
/**
* Creates a new instance of XStreamAdapter
*
* @param isCompact true if this Adapter should use compact xml otherwise false
* @see com.thoughtworks.xstream.io.xml.CompactWriter
*/
public XStreamAdapter(boolean isCompact)
{
this._isCompact = isCompact;
this._xstream = new XStream();
}
/**
* Creates a new instance of XStreamAdapter using the given HierarchicalStreamDriver
*/
public XStreamAdapter(HierarchicalStreamDriver driver)
{
this._xstream = new XStream( driver );
}
/*
* (non-Javadoc)
*
* @see de.netseeker.ejoe.adapter.SerializeAdapter#read(java.io.InputStream)
*/
public Object read( InputStream in ) throws Exception
{
return _xstream.fromXML( in );
}
/*
* (non-Javadoc)
*
* @see de.netseeker.ejoe.adapter.SerializeAdapter#write(java.lang.Object, java.io.OutputStream)
*/
public void write( Object obj, OutputStream out ) throws Exception
{
if ( this._isCompact )
{
_xstream
.marshal( obj, new CompactWriter( new OutputStreamWriter( out, EJConstants.EJOE_DEFAULT_CHARSET ) ) );
}
else
{
_xstream.toXML( obj, out );
}
}
/*
* (non-Javadoc)
*
* @see de.netseeker.ejoe.adapter.SerializeAdapter#setClassLoader(java.lang.ClassLoader)
*/
public void handleClassLoaderChange( ClassLoader classLoader )
{
_xstream.setClassLoader( classLoader );
}
/*
* (non-Javadoc)
*
* @see de.netseeker.ejoe.adapter.BaseAdapter#getContentType()
*/
public String getContentType()
{
return "text/xml";
}
}