org.opennms.core.xml
Class CastorUtils

java.lang.Object
  extended by org.opennms.core.xml.CastorUtils

public class CastorUtils
extends Object

Utility class for Castor configuration files.

Version:
$Id: $
Author:
DJ Gregor

Method Summary
static
<T> T
duplicateObject(T object, Class<T> clazz)
           
static
<T> org.exolab.castor.xml.Unmarshaller
getUnmarshaller(Class<T> clazz)
           
static void marshalViaString(Object config, File cfgFile)
          Marshall to a string first, then write the string to the file.
static void marshalWithTranslatedExceptions(Object obj, Writer writer)
          Marshal a Castor XML configuration file.
static void marshalWithTranslatedExceptionsViaString(Object obj, org.springframework.core.io.Resource resource)
          Marshal a Castor XML configuration file.
static
<T> T
unmarshal(Class<T> clazz, InputStream in)
          Unmarshal a Castor XML configuration file.
static
<T> T
unmarshal(Class<T> clazz, InputStream in, boolean preserveWhitespace)
          Unmarshal a Castor XML configuration file.
static
<T> T
unmarshal(Class<T> clazz, Reader reader)
          Deprecated. Use a Resource or InputStream-based method instead to avoid character set issues.
static
<T> T
unmarshal(Class<T> clazz, Reader reader, boolean preserveWhitespace)
          Deprecated. Use a Resource or InputStream-based method instead to avoid character set issues.
static
<T> T
unmarshal(Class<T> clazz, org.springframework.core.io.Resource resource)
          Unmarshal a Castor XML configuration file.
static
<T> T
unmarshal(Class<T> clazz, org.springframework.core.io.Resource resource, boolean preserveWhitespace)
          Unmarshal a Castor XML configuration file.
static
<T> T
unmarshalWithTranslatedExceptions(Class<T> clazz, InputStream in)
          Unmarshal a Castor XML configuration file.
static
<T> T
unmarshalWithTranslatedExceptions(Class<T> clazz, InputStream in, boolean preserveWhitespace)
          Unmarshal a Castor XML configuration file.
static
<T> T
unmarshalWithTranslatedExceptions(Class<T> clazz, Reader reader)
          Deprecated. Use a Resource or InputStream-based method instead to avoid character set issues.
static
<T> T
unmarshalWithTranslatedExceptions(Class<T> clazz, Reader reader, boolean preserveWhitespace)
          Deprecated. Use a Resource or InputStream-based method instead to avoid character set issues.
static
<T> T
unmarshalWithTranslatedExceptions(Class<T> clazz, org.springframework.core.io.Resource resource)
          Unmarshal a Castor XML configuration file.
static
<T> T
unmarshalWithTranslatedExceptions(Class<T> clazz, org.springframework.core.io.Resource resource, boolean preserveWhitespace)
          Unmarshal a Castor XML configuration file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

marshalWithTranslatedExceptions

public static void marshalWithTranslatedExceptions(Object obj,
                                                   Writer writer)
                                            throws org.springframework.dao.DataAccessException
Marshal a Castor XML configuration file.

Parameters:
obj - the object representing the objected to be marshalled to XML
writer - where the marshalled XML will be written
Throws:
org.springframework.dao.DataAccessException - if the underlying Castor Marshaller.marshal() call throws a MarshalException or ValidationException. The underlying exception will be translated using MarshallingExceptionTranslator.

marshalWithTranslatedExceptionsViaString

public static void marshalWithTranslatedExceptionsViaString(Object obj,
                                                            org.springframework.core.io.Resource resource)
                                                     throws org.springframework.dao.DataAccessException
Marshal a Castor XML configuration file.

Parameters:
obj - the object representing the objected to be marshalled to XML
resource - a Resource object.
Throws:
org.springframework.dao.DataAccessException - if the underlying Castor Marshaller.marshal() call throws a MarshalException or ValidationException. The underlying exception will be translated using MarshallingExceptionTranslator.

getUnmarshaller

public static <T> org.exolab.castor.xml.Unmarshaller getUnmarshaller(Class<T> clazz)

unmarshal

public static <T> T unmarshal(Class<T> clazz,
                              Reader reader)
                   throws org.exolab.castor.xml.MarshalException,
                          org.exolab.castor.xml.ValidationException
Deprecated. Use a Resource or InputStream-based method instead to avoid character set issues.

Unmarshal a Castor XML configuration file. Uses Java 5 generics for return type.

Parameters:
clazz - the class representing the marshalled XML configuration file
reader - the marshalled XML configuration file to unmarshal
Returns:
Unmarshalled object representing XML file
Throws:
org.exolab.castor.xml.MarshalException - if the underlying Castor Unmarshaller.unmarshal() call throws a org.exolab.castor.xml.MarshalException
org.exolab.castor.xml.ValidationException - if the underlying Castor Unmarshaller.unmarshal() call throws a org.exolab.castor.xml.ValidationException

unmarshal

public static <T> T unmarshal(Class<T> clazz,
                              Reader reader,
                              boolean preserveWhitespace)
                   throws org.exolab.castor.xml.MarshalException,
                          org.exolab.castor.xml.ValidationException
Deprecated. Use a Resource or InputStream-based method instead to avoid character set issues.

Unmarshal a Castor XML configuration file. Uses Java 5 generics for return type.

Parameters:
clazz - the class representing the marshalled XML configuration file
reader - the marshalled XML configuration file to unmarshal
preserveWhitespace - whether or not to preserve whitespace
Returns:
Unmarshalled object representing XML file
Throws:
org.exolab.castor.xml.MarshalException - if the underlying Castor Unmarshaller.unmarshal() call throws a org.exolab.castor.xml.MarshalException
org.exolab.castor.xml.ValidationException - if the underlying Castor Unmarshaller.unmarshal() call throws a org.exolab.castor.xml.ValidationException

unmarshal

public static <T> T unmarshal(Class<T> clazz,
                              InputStream in)
                   throws org.exolab.castor.xml.MarshalException,
                          org.exolab.castor.xml.ValidationException
Unmarshal a Castor XML configuration file. Uses Java 5 generics for return type.

Parameters:
clazz - the class representing the marshalled XML configuration file
in - the marshalled XML configuration file to unmarshal
Returns:
Unmarshalled object representing XML file
Throws:
org.exolab.castor.xml.MarshalException - if the underlying Castor Unmarshaller.unmarshal() call throws a org.exolab.castor.xml.MarshalException
org.exolab.castor.xml.ValidationException - if the underlying Castor Unmarshaller.unmarshal() call throws a org.exolab.castor.xml.ValidationException

unmarshal

public static <T> T unmarshal(Class<T> clazz,
                              InputStream in,
                              boolean preserveWhitespace)
                   throws org.exolab.castor.xml.MarshalException,
                          org.exolab.castor.xml.ValidationException
Unmarshal a Castor XML configuration file. Uses Java 5 generics for return type.

Parameters:
clazz - the class representing the marshalled XML configuration file
in - the marshalled XML configuration file to unmarshal
preserveWhitespace - whether or not to preserve whitespace
Returns:
Unmarshalled object representing XML file
Throws:
org.exolab.castor.xml.MarshalException - if the underlying Castor Unmarshaller.unmarshal() call throws a org.exolab.castor.xml.MarshalException
org.exolab.castor.xml.ValidationException - if the underlying Castor Unmarshaller.unmarshal() call throws a org.exolab.castor.xml.ValidationException

unmarshal

public static <T> T unmarshal(Class<T> clazz,
                              org.springframework.core.io.Resource resource)
                   throws org.exolab.castor.xml.MarshalException,
                          org.exolab.castor.xml.ValidationException,
                          IOException
Unmarshal a Castor XML configuration file. Uses Java 5 generics for return type.

Parameters:
clazz - the class representing the marshalled XML configuration file
resource - the marshalled XML configuration file to unmarshal
Returns:
Unmarshalled object representing XML file
Throws:
org.exolab.castor.xml.MarshalException - if the underlying Castor Unmarshaller.unmarshal() call throws a org.exolab.castor.xml.MarshalException
org.exolab.castor.xml.ValidationException - if the underlying Castor Unmarshaller.unmarshal() call throws a org.exolab.castor.xml.ValidationException
IOException - if the resource could not be opened

unmarshal

public static <T> T unmarshal(Class<T> clazz,
                              org.springframework.core.io.Resource resource,
                              boolean preserveWhitespace)
                   throws org.exolab.castor.xml.MarshalException,
                          org.exolab.castor.xml.ValidationException,
                          IOException
Unmarshal a Castor XML configuration file. Uses Java 5 generics for return type.

Parameters:
clazz - the class representing the marshalled XML configuration file
resource - the marshalled XML configuration file to unmarshal
preserveWhitespace - whether or not to preserve whitespace
Returns:
Unmarshalled object representing XML file
Throws:
org.exolab.castor.xml.MarshalException - if the underlying Castor Unmarshaller.unmarshal() call throws a org.exolab.castor.xml.MarshalException
org.exolab.castor.xml.ValidationException - if the underlying Castor Unmarshaller.unmarshal() call throws a org.exolab.castor.xml.ValidationException
IOException - if the resource could not be opened

unmarshalWithTranslatedExceptions

public static <T> T unmarshalWithTranslatedExceptions(Class<T> clazz,
                                                      Reader reader)
                                           throws org.springframework.dao.DataAccessException
Deprecated. Use a Resource or InputStream-based method instead to avoid character set issues.

Unmarshal a Castor XML configuration file. Uses Java 5 generics for return type and throws DataAccessExceptions.

Parameters:
clazz - the class representing the marshalled XML configuration file
reader - the marshalled XML configuration file to unmarshal
Returns:
Unmarshalled object representing XML file
Throws:
org.springframework.dao.DataAccessException - if the underlying Castor Unmarshaller.unmarshal() call throws a MarshalException or ValidationException. The underlying exception will be translated using MarshallingExceptionTranslator.

unmarshalWithTranslatedExceptions

public static <T> T unmarshalWithTranslatedExceptions(Class<T> clazz,
                                                      Reader reader,
                                                      boolean preserveWhitespace)
                                           throws org.springframework.dao.DataAccessException
Deprecated. Use a Resource or InputStream-based method instead to avoid character set issues.

Unmarshal a Castor XML configuration file. Uses Java 5 generics for return type and throws DataAccessExceptions.

Parameters:
clazz - the class representing the marshalled XML configuration file
reader - the marshalled XML configuration file to unmarshal
preserveWhitespace - Whether to preserve whitespace when unmarshalling.
Returns:
Unmarshalled object representing XML file
Throws:
org.springframework.dao.DataAccessException - if the underlying Castor Unmarshaller.unmarshal() call throws a MarshalException or ValidationException. The underlying exception will be translated using MarshallingExceptionTranslator.

unmarshalWithTranslatedExceptions

public static <T> T unmarshalWithTranslatedExceptions(Class<T> clazz,
                                                      InputStream in)
                                           throws org.springframework.dao.DataAccessException
Unmarshal a Castor XML configuration file. Uses Java 5 generics for return type and throws DataAccessExceptions.

Parameters:
clazz - the class representing the marshalled XML configuration file
in - the marshalled XML configuration file to unmarshal
Returns:
Unmarshalled object representing XML file
Throws:
org.springframework.dao.DataAccessException - if the underlying Castor Unmarshaller.unmarshal() call throws a MarshalException or ValidationException. The underlying exception will be translated using MarshallingExceptionTranslator.

unmarshalWithTranslatedExceptions

public static <T> T unmarshalWithTranslatedExceptions(Class<T> clazz,
                                                      InputStream in,
                                                      boolean preserveWhitespace)
                                           throws org.springframework.dao.DataAccessException
Unmarshal a Castor XML configuration file. Uses Java 5 generics for return type and throws DataAccessExceptions.

Parameters:
clazz - the class representing the marshalled XML configuration file
in - the marshalled XML configuration file to unmarshal
preserveWhitespace - whether to preserve whitespace when unmarshalling.
Returns:
Unmarshalled object representing XML file
Throws:
org.springframework.dao.DataAccessException - if the underlying Castor Unmarshaller.unmarshal() call throws a MarshalException or ValidationException. The underlying exception will be translated using MarshallingExceptionTranslator.

unmarshalWithTranslatedExceptions

public static <T> T unmarshalWithTranslatedExceptions(Class<T> clazz,
                                                      org.springframework.core.io.Resource resource)
Unmarshal a Castor XML configuration file. Uses Java 5 generics for return type and throws DataAccessExceptions.

Parameters:
clazz - the class representing the marshalled XML configuration file
resource - the marshalled XML configuration file to unmarshal
Returns:
Unmarshalled object representing XML file
Throws:
org.springframework.dao.DataAccessException - if the resource could not be opened or the underlying Castor Unmarshaller.unmarshal() call throws a MarshalException or ValidationException. The underlying exception will be translated using MarshallingExceptionTranslator and will include information about the resource from its toString() method.

unmarshalWithTranslatedExceptions

public static <T> T unmarshalWithTranslatedExceptions(Class<T> clazz,
                                                      org.springframework.core.io.Resource resource,
                                                      boolean preserveWhitespace)
Unmarshal a Castor XML configuration file. Uses Java 5 generics for return type and throws DataAccessExceptions.

Parameters:
clazz - the class representing the marshalled XML configuration file
resource - the marshalled XML configuration file to unmarshal
Returns:
Unmarshalled object representing XML file
Throws:
org.springframework.dao.DataAccessException - if the resource could not be opened or the underlying Castor Unmarshaller.unmarshal() call throws a MarshalException or ValidationException. The underlying exception will be translated using MarshallingExceptionTranslator and will include information about the resource from its toString() method.

marshalViaString

public static void marshalViaString(Object config,
                                    File cfgFile)
                             throws org.exolab.castor.xml.MarshalException,
                                    org.exolab.castor.xml.ValidationException,
                                    IOException
Marshall to a string first, then write the string to the file. This way the original config isn't lost if the xml from the marshall is hosed. FIXME: This could still stand to write to a temporary file and/or make a temporary backup of the production configuration file.

Parameters:
config - a Object object.
cfgFile - a File object.
Throws:
org.exolab.castor.xml.MarshalException - if any.
org.exolab.castor.xml.ValidationException - if any.
IOException - if any.

duplicateObject

public static <T> T duplicateObject(T object,
                                    Class<T> clazz)
                         throws org.exolab.castor.xml.MarshalException,
                                org.exolab.castor.xml.ValidationException
Throws:
org.exolab.castor.xml.MarshalException
org.exolab.castor.xml.ValidationException


Copyright © 2011. All Rights Reserved.