All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.javakaffee.web.msm.serializer.javolution.HibernateCollectionsXMLFormat Maven / Gradle / Ivy

There is a newer version: 2.1.1
Show newest version
/*
 * Copyright 2010 Martin Grotzke
 *
 * 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.
 *
 */
package de.javakaffee.web.msm.serializer.javolution;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javolution.xml.XMLFormat;
import javolution.xml.stream.XMLStreamException;

import org.hibernate.collection.internal.AbstractPersistentCollection;

/**
 * A {@link CustomXMLFormat} that handles hibernate mapped collections (subclasses
 * of {@link AbstractPersistentCollection}).
 *
 * @author Martin Grotzke
 */
public class HibernateCollectionsXMLFormat extends CustomXMLFormat {

    private final Map, XMLFormat> _formats = new ConcurrentHashMap, XMLFormat>();

    @Override
    public boolean canConvert( final Class cls ) {
        return AbstractPersistentCollection.class.isAssignableFrom( cls );
    }

    @Override
    public void read( final XMLFormat.InputElement input, final AbstractPersistentCollection obj ) throws XMLStreamException {
        getFormat( obj.getClass() ).read( input, obj );
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void write( final AbstractPersistentCollection obj, final javolution.xml.XMLFormat.OutputElement output ) throws XMLStreamException {
        getFormat( obj.getClass() ).write( obj, output );
    }

    @SuppressWarnings( "unchecked" )
    private XMLFormat getFormat( final Class clazz ) {
        XMLFormat format = _formats.get( clazz );
        if ( format == null ) {
            format = new ReflectionFormat( clazz, clazz.getClassLoader() );
            _formats.put( clazz, format );
        }
        return format;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy