Tag: Castor

CastorHelper

CodeDevFrançais

Dans la série code à garder sous la main : Une classe pour travailler Castor

package com.ledruide.utils.xml;

import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;

import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.Unmarshaller;
import org.exolab.castor.xml.ValidationException;
import org.xml.sax.InputSource;

public class CastorHelper {

    private CastorHelper() {}

    private static final String ENCODING = "UTF-16";

    /**
     * Permet de creer un fichier contenant le XML genere
     * 
     * @param monObjet la classe contenant les donnees a 'castoriser'
     * @param mappingFile le fichier XML de mapping pour savoir quoi faire avec l'Objet
     * @throws Exception si une erreur survient.
     */
    public static String saveXMLFile( Object monObjet , String mappingFile ) throws Exception {
        try {

            StringWriter content = new StringWriter();
            // First look into the classpath
            InputStream is = CastorHelper.class.getResourceAsStream( mappingFile );
            // If the input stream is not OK I look into the filesystem
            if ( is == null ) {
                is = new FileInputStream( mappingFile );
            }
            // Chargement du mapping
            Mapping mapping = new Mapping();
            mapping.loadMapping( new InputSource( is ) );
            // Marshal l'object -> La transformation de Java en XML !
            final Marshaller marshaller = new Marshaller( content );
            marshaller.setValidation( false );
            marshaller.setMapping( mapping );
            marshaller.setEncoding( ENCODING );
            marshaller.marshal( monObjet );

            return content.toString();
        }
        catch ( Exception e ) {
            throw e;
        }
    }

    /**
     * Permet de lire un fichier XML contenant les donnees
     * 
     * @param aClasse la classe d'implémentation des donnees a 'decastoriser'
     * @throws Exception si une erreur survient.
     */
    public static void loadXMLFile( Object monObjet , String mappingFile , String xml ) throws Exception {
        try {
            // First llok into the classpath
            InputStream is = CastorHelper.class.getResourceAsStream( mappingFile );
            // If the input stream is not OK I look into the filesystem
            if ( is == null ) {
                is = new FileInputStream( mappingFile );
            }

            // Chargement du mapping
            Mapping mapping = new Mapping();
            mapping.loadMapping( new InputSource( is ) );
            // UnMarshal l'object -> La transformation XML vers Java !
            Unmarshaller unmarshaller = new Unmarshaller( monObjet );
            unmarshaller.setWhitespacePreserve( true );
            unmarshaller.setMapping( mapping );
            monObjet = unmarshaller.unmarshal( new FileReader( xml ) );
        }
        catch ( Exception e ) {
            throw e;
        }
    }

    /**
     * @param result
     * @return
     * @throws IOException
     * @throws MarshalException
     * @throws ValidationException
     */
    public static String dynamicXmlFromObject( Object object ) throws Exception {
        StringWriter content = new StringWriter();
        final Marshaller marshaller = new Marshaller( content );
        marshaller.setValidation( false );
        marshaller.setEncoding( ENCODING );
        marshaller.setSuppressXSIType(true);
        marshaller.marshal( object );

        return content.toString();
    }

}