package org.opennms.netmgt.dao.castor;

import java.io.Reader;

import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Unmarshaller;
import org.exolab.castor.xml.ValidationException;
import org.springframework.dao.DataAccessException;

/**
 * Utility class for Castor configuration files.
 * 
 * @author <a href="mailto:dj@opennms.org">DJ Gregor</a>
 */
public class CastorUtils {
    private static final CastorExceptionTranslator CASTOR_EXCEPTION_TRANSLATOR = new CastorExceptionTranslator();

    
    /** Private constructor since this class only has static methods (so far). */
    private CastorUtils() {
    }
    
    /**
     * Unmarshal a Castor XML configuration file.  Uses Java 5 generics for
     * return type. 
     * 
     * @param <T> the class representing the marshalled XML configuration
     *      file.  This will be the return time form the method.
     * @param clazz the class representing the marshalled XML configuration
     *      file
     * @param reader the marshalled XML configuration file to unmarshal
     * @return Unmarshalled object representing XML file
     * @throws MarshalException if the underlying Castor
     *      Unmarshaller.unmarshal() call throws a MarshalException
     * @throws ValidationException if the underlying Castor
     *      Unmarshaller.unmarshal() call throws a ValidationException
     */
    @SuppressWarnings("unchecked")
    public static <T> T unmarshal(Class<T> clazz, Reader reader) throws MarshalException, ValidationException {
        return (T) Unmarshaller.unmarshal(clazz, reader);
    }
    

    /**
     * Unmarshal a Castor XML configuration file.  Uses Java 5 generics for
     * return type and throws DataAccessExceptions.
     * 
     * @param <T> the class representing the marshalled XML configuration
     *      file.  This will be the return time form the method.
     * @param clazz the class representing the marshalled XML configuration
     *      file
     * @param reader the marshalled XML configuration file to unmarshal
     * @return Unmarshalled object representing XML file
     * @throws DataAccessException if the underlying Castor
     *      Unmarshaller.unmarshal() call throws a MarshalException or
     *      ValidationException.  The underlying exception will be translated
     *      using CastorExceptionTranslator.
     */
    @SuppressWarnings("unchecked")
    public static <T> T unmarshalWithTranslatedExceptions(Class<T> clazz, Reader reader) throws DataAccessException {
        try {
            return (T) Unmarshaller.unmarshal(clazz, reader);
        } catch (MarshalException e) {
            throw CASTOR_EXCEPTION_TRANSLATOR.translate("Unmarshalling XML file", e);
        } catch (ValidationException e) {
            throw CASTOR_EXCEPTION_TRANSLATOR.translate("Unmarshalling XML file", e);
        }
    }
}

@@ -55,16 +55,39 @@
 .Reader;
+%0Aimport java.io.Writer;
 %0A%0Aimport
@@ -119,32 +119,73 @@
 rshalException;%0A
+import org.exolab.castor.xml.Marshaller;%0A
 import org.exola
@@ -573,17 +573,16 @@
 ator();%0A
-%0A
     %0A   
@@ -694,20 +694,914 @@
 %7B%0A    %7D%0A
+%0A
     
+/**%0A     * Marshal a Castor XML configuration file.%0A     *%0A     * @param obj the object representing the objected to be marshalled to XML%0A     * @param writer where the marshalled XML will be written%0A     * @throws DataAccessException if the underlying Castor%0A     *      Marshaller.marshal() call throws a MarshalException or%0A     *      ValidationException.  The underlying exception will be translated%0A     *      using CastorExceptionTranslator.%0A     */%0A    public static void marshalWithTranslatedExceptions(Object obj, Writer writer) throws DataAccessException %7B%0A        try %7B%0A            Marshaller.marshal(obj, writer);%0A        %7D catch (MarshalException e) %7B%0A            throw CASTOR_EXCEPTION_TRANSLATOR.translate(%22Marshalling XML file%22, e);%0A        %7D catch (ValidationException e) %7B%0A            throw CASTOR_EXCEPTION_TRANSLATOR.translate(%22Marshalling XML file%22, e);%0A        %7D%0A    %7D%0A
 %0A    /**
@@ -2536,26 +2536,21 @@
 eader);%0A
+
     %7D
-%0A    
 %0A%0A    /*

Add marshalWithTranslatedExceptions.