/*********************************************************************
* BetwixtAdapter.java
* created on 11.03.2005 by netseeker
* $Source$
* $Date$
* $Revision$
*
* ====================================================================
*
* Copyright 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 org.apache.commons.betwixt.io.BeanReader;
import org.apache.commons.betwixt.io.BeanWriter;
import org.apache.commons.betwixt.registry.DefaultXMLBeanInfoRegistry;
import org.apache.commons.betwixt.registry.XMLBeanInfoRegistry;
import de.netseeker.ejoe.EJConstants;
/**
* SerializeAdapter implementation using Apaches Betwixt
*
* @author netseeker
* @since 0.3.5
* @link http://jakarta.apache.org/commons/betwixt/
*/
/**
* @author netseeker
* @since
*/
public class BetwixtAdapter extends BaseAdapter
{
private static final long serialVersionUID = 1L;
private static final String XML_DECLARATION = "<?xml version=\"1.0\" encoding=\""
+ EJConstants.EJOE_DEFAULT_CHARSET
+ "\" ?><!-- BETWIXT START -->";
private transient XMLBeanInfoRegistry _registry = null;
private XMLBeanInfoRegistry getRegistry()
{
if ( _registry == null )
{
_registry = new DefaultXMLBeanInfoRegistry();
}
return _registry;
}
/**
* @param registry
*/
public void setXMLBeanInfoRegistry( XMLBeanInfoRegistry registry )
{
_registry = registry;
}
/*
* (non-Javadoc)
*
* @see de.netseeker.ejoe.adapter.SerializeAdapter#read(java.io.InputStream)
*/
public Object read( InputStream in ) throws Exception
{
BeanReader beanReader = new BeanReader();
beanReader.getXMLIntrospector().setRegistry( getRegistry() );
beanReader.getBindingConfiguration().setMapIDs( false );
return beanReader.parse( 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
{
BeanWriter beanWriter = new BeanWriter( out );
beanWriter.getXMLIntrospector().setRegistry( getRegistry() );
beanWriter.setWriteEmptyElements( true );
beanWriter.writeXmlDeclaration( XML_DECLARATION );
beanWriter.getBindingConfiguration().setMapIDs( false );
try
{
beanWriter.write( obj.getClass().getName(), obj );
}
finally
{
if ( beanWriter != null ) beanWriter.close();
}
}
/*
* (non-Javadoc)
*
* @see de.netseeker.ejoe.adapter.BaseAdapter#requiresCustomEOFHandling()
*/
public boolean requiresCustomEOFHandling()
{
return true;
}
/*
* (non-Javadoc)
*
* @see de.netseeker.ejoe.adapter.BaseAdapter#getContentType()
*/
public String getContentType()
{
return "text/xml";
}
}