Package de.netseeker.ejoe.adapter

Source Code of de.netseeker.ejoe.adapter.SkaringaAdapter

/*********************************************************************
* SkaringaAdapter.java
* created on 27.05.2007 by netseeker
* $Id: SkaringaAdapter.java,v 1.2 2007/11/17 10:56:38 netseeker Exp $
* $Log: SkaringaAdapter.java,v $
* Revision 1.2  2007/11/17 10:56:38  netseeker
* *** empty log message ***
*
* Revision 1.1  2007/05/27 22:13:09  netseeker
* *** empty log message ***
*
*
* ====================================================================
*
*  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 javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import com.skaringa.javaxml.NoImplementationException;
import com.skaringa.javaxml.ObjectTransformer;
import com.skaringa.javaxml.ObjectTransformerFactory;

/**
* An adapter for (de)serializing objects via the great Skaringa library
*
* @author netseeker aka Michael Manske
* @since 0.3.9.3
* @link http://skaringa.sourceforge.net/
*/
public class SkaringaAdapter extends BaseAdapter
{
    /**
     *
     */
    private static final long           serialVersionUID = 1L;

    private transient ObjectTransformer _transformer;

    private boolean                     compact          = false;

    /**
     * Creates a new instance of SkaringaAdapter using compact XML
     */
    public SkaringaAdapter()
    {
        this( true );
    }

    /**
     * Creates a new instance of SkaringaAdapter
     *
     * @param isCompact true if this Adapter should use compact xml otherwise false
     */
    public SkaringaAdapter(boolean isCompact)
    {
        this.compact = isCompact;
        init();
    }

    private void init()
    {
        try
        {
            _transformer = ObjectTransformerFactory.getInstance().getImplementation();
            _transformer.setProperty( com.skaringa.javaxml.PropertyKeys.SORT_FIELDS, "yes" );
            _transformer.setProperty( com.skaringa.javaxml.PropertyKeys.SKIP_UNKNOWN_FIELDS, "yes" );
            if ( this.compact )
            {
                _transformer.setProperty( javax.xml.transform.OutputKeys.INDENT, "no" );
            }
            else
            {
                _transformer.setProperty( javax.xml.transform.OutputKeys.INDENT, "yes" );
            }
        }
        catch ( NoImplementationException e )
        {
            RuntimeException re = new RuntimeException( e.getMessage() );
            re.setStackTrace( e.getStackTrace() );
            throw re;
        }
    }

    /*
     * (non-Javadoc)
     *
     * @see de.netseeker.ejoe.adapter.SerializeAdapter#read(java.io.InputStream)
     */
    public Object read( InputStream in ) throws Exception
    {
        return _transformer.deserialize( new StreamSource( 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
    {
        _transformer.serialize( obj, new StreamResult( out ) );
    }

    /*
     * (non-Javadoc)
     *
     * @see de.netseeker.ejoe.adapter.BaseAdapter#getContentType()
     */
    public String getContentType()
    {
        return "text/xml";
    }

    /*
     * (non-Javadoc)
     *
     * @see de.netseeker.ejoe.adapter.BaseAdapter#handleClassLoaderChange(java.lang.ClassLoader)
     */
    public void handleClassLoaderChange( ClassLoader classLoader )
    {
        this._transformer.setClassLoader( classLoader );
    }

}
TOP

Related Classes of de.netseeker.ejoe.adapter.SkaringaAdapter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.