Docmosis API 4.5.0

com.docmosis.template.population
Class ReflectiveDataProvider

java.lang.Object
  extended bycom.docmosis.template.population.ReflectiveDataProvider
All Implemented Interfaces:
DataProvider, Serializable
Direct Known Subclasses:
ReflectiveCollectionDataProvider

public abstract class ReflectiveDataProvider
extends Object
implements DataProvider, Serializable

This data provider uses reflection to try to obtain data from within an arbitrary given object. The reflection can be forgiving which means that if the contained object cannot provide the information then null values are assumed. If set to unforgiving, Runtime Exceptions will be thrown if the delegate object does not have a method to obtain the required value. Unforgiving might be helpful when debugging or developing.

See Also:
Serialized Form

Nested Class Summary
protected static class ReflectiveDataProvider.DataFormatManager
          A Manager of data formats that are applied to the results of reflective calls to format the data before it is passed into the render process.
 
Field Summary
protected  ReflectiveDataProvider.DataFormatManager dataFormatManager
           
protected  boolean forgiving
           
protected static MethodReflectionHelper METHOD_REFLECTION_HELPER
           
protected  Object reflectedDelegate
           
protected  Class reflectedDelegateClass
           
 
Constructor Summary
protected ReflectiveDataProvider(Object delegate, boolean forgiving, ReflectiveDataProvider.DataFormatManager dataFormatManager)
          Construct a new Reflective Data Provider that will pull data out of the given delegate.
 
Method Summary
abstract  boolean getBoolean(String fieldName)
          Get the boolean value from the reflected delegate.
abstract  DataProvider getDataProvider(String fieldName, int idx)
          Get the data provider from the reflected delegate.
abstract  int getDataProviderCount(String fieldName)
          Get the data provider count from the reflected delegate.
 Object getDelegate()
          Return the delegate inside this Reflective Data Provider.
abstract  InputStream getImage(String fieldName)
          Get the image from the reflected delegate.
static ReflectiveDataProvider getInstance(Object delegate)
          Obtain a new Reflective Data Provider that will pull data out of the given delegate.
static ReflectiveDataProvider getInstance(Object delegate, boolean forgiving)
          obtain a new Reflective Data Provider that will pull data out of the given delegate.
abstract  String getString(String fieldName)
          Get the String from the reflected delegate.
static ReflectiveDataProvider getWrappedInstance(Object delegate)
          Obtain a new Reflective Data Provider that will pull data out of the given delegate.
abstract  boolean hasBooleanKey(String fieldName)
          Determine if the boolean key has been set.
abstract  boolean hasDataProviderKey(String fieldName)
          Determine if the data provider key has been set.
abstract  boolean hasImageKey(String fieldName)
          Determine if the image key has been set.
abstract  boolean hasStringKey(String fieldName)
          Determine if the String key has been set.
 boolean isForgiving()
          Determine if this instance is forgiving or not.
protected static boolean isPrimitive(Object o)
          Determine if the given object is one of the "primitive" java types.
protected static boolean isPrimitiveArray(Object o)
          Determine if the given object is one of the "primitive" java array types.
protected  void logCall(Logger log, String methodName, String fieldName)
          Log a call to any of the retrieval methods.
protected  void logCall(Logger log, String methodName, String fieldName, int index)
          Log a call to any of the retrieval methods.
 void setDataFormatter(Class type, DataFormatter formatter)
          Set the formatter for the given class type.
 void setDateFormat(SimpleDateFormat dateFormat)
          Set the Date Format to use when rendering dates as Strings.
 void setDateFormat(String formatString)
          Set the Date Format to use a SimpleDateFormat using the given format string as the format.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

METHOD_REFLECTION_HELPER

protected static final MethodReflectionHelper METHOD_REFLECTION_HELPER

reflectedDelegate

protected final Object reflectedDelegate

reflectedDelegateClass

protected final Class reflectedDelegateClass

forgiving

protected final boolean forgiving

dataFormatManager

protected final ReflectiveDataProvider.DataFormatManager dataFormatManager
Constructor Detail

ReflectiveDataProvider

protected ReflectiveDataProvider(Object delegate,
                                 boolean forgiving,
                                 ReflectiveDataProvider.DataFormatManager dataFormatManager)
Construct a new Reflective Data Provider that will pull data out of the given delegate. This instance will be forgiving as specified and use the specified data formatter.

Parameters:
delegate - the object from which to retrieve information
forgiving - if true, failure to reflectively locate a method on the delegate fails quietly. If false, Runtime Exceptions will be thrown.
Method Detail

getInstance

public static ReflectiveDataProvider getInstance(Object delegate)
Obtain a new Reflective Data Provider that will pull data out of the given delegate. This instance will be forgiving.

Parameters:
delegate - the object from which to retrieve information

getWrappedInstance

public static ReflectiveDataProvider getWrappedInstance(Object delegate)
Obtain a new Reflective Data Provider that will pull data out of the given delegate. This instance will be forgiving, have no formatter and will force primatives to be wrapped.

Parameters:
delegate - the object from which to retrieve information

getInstance

public static ReflectiveDataProvider getInstance(Object delegate,
                                                 boolean forgiving)
obtain a new Reflective Data Provider that will pull data out of the given delegate. This instance will be forgiving as specified and use the specified data formatter.

Parameters:
delegate - the object from which to retrieve information
forgiving - if true, failure to reflectively locate a method on the delegate fails quietly. If false, Runtime Exceptions will be thrown.

getDelegate

public Object getDelegate()
Return the delegate inside this Reflective Data Provider.

Returns:
the reflected delegate this object was constructed with.

isForgiving

public boolean isForgiving()
Determine if this instance is forgiving or not.

Returns:
true if forgiving.

hasBooleanKey

public abstract boolean hasBooleanKey(String fieldName)
Determine if the boolean key has been set. If a method based on the given fieldName can be reflected, this will return true, otherwise false. NOTE: if fieldName contains period ('.') characters, then this is considered to indicate a nested structure. This method will recursively descend through the reflected delegate attempting to answer the question. If a method along the way returns null, then reflection cannot delve any deeper and false will be returned.

Specified by:
hasBooleanKey in interface DataProvider
Parameters:
fieldName - the name of the field to use for reflection
Returns:
true if a method can be reflected to return a value.

getBoolean

public abstract boolean getBoolean(String fieldName)
Get the boolean value from the reflected delegate. If a method based on the given fieldName can be reflected, this will return the result of calling that method. NOTE: if fieldName contains period ('.') characters, then this is considered to indicate a nested structure. This method will recursively descend through the reflected delegate attempting to obtain data. If a method along the way returns null, then reflection cannot delve any deeper and false will be returned.

Specified by:
getBoolean in interface DataProvider
Parameters:
fieldName - the key to lookup
Returns:
the boolean value or false.
Throws:
DataProvisionNotFoundException - if the reflected method cannot be found and we are not forgiving.
DataProvisionFetchException - if the reflection fails for security or runtime reasons.

hasDataProviderKey

public abstract boolean hasDataProviderKey(String fieldName)
Determine if the data provider key has been set. If a method based on the given fieldName can be reflected, this will return true, otherwise false. NOTE: if fieldName contains period ('.') characters, then this is considered to indicate a nested structure. This method will recursively descend through the reflected delegate attempting to answer the question. If a method along the way returns null, then reflection cannot delve any deeper and false will be returned.

Specified by:
hasDataProviderKey in interface DataProvider
Parameters:
fieldName - the name of the field to use for reflection
Returns:
true if a method can be reflected to return a value.

getDataProvider

public abstract DataProvider getDataProvider(String fieldName,
                                             int idx)
Get the data provider from the reflected delegate. If a method based on the given fieldName can be reflected, this will return the result of calling that method. NOTE: if fieldName contains period ('.') characters, then this is considered to indicate a nested structure. This method will recursively descend through the reflected delegate attempting to obtain data. If a method along the way returns null, then reflection cannot delve any deeper and null will be returned.

Specified by:
getDataProvider in interface DataProvider
Parameters:
fieldName - the key (name) of the data provider to look for.
idx - the index of the data provider.
Returns:
the reflected value or null.
Throws:
DataProvisionNotFoundException - if the reflected method cannot be found and we are not forgiving.
DataProvisionFetchException - if the reflection fails for security or runtime reasons.

getDataProviderCount

public abstract int getDataProviderCount(String fieldName)
Get the data provider count from the reflected delegate. If a method based on the given fieldName can be reflected, this will return the result of calling that method. NOTE: if fieldName contains period ('.') characters, then this is considered to indicate a nested structure. This method will recursively descend through the reflected delegate attempting to obtain data. If a method along the way returns null, then reflection cannot delve any deeper and 0 will be returned.

Specified by:
getDataProviderCount in interface DataProvider
Parameters:
fieldName - the key (name) of the data providers to look for.
Returns:
the reflected value or 0.
Throws:
DataProvisionNotFoundException - if the reflected method cannot be found and we are not forgiving.
DataProvisionFetchException - if the reflection fails for security or runtime reasons.

hasImageKey

public abstract boolean hasImageKey(String fieldName)
Determine if the image key has been set. If a method based on the given fieldName can be reflected, this will return true, otherwise false. NOTE: if fieldName contains period ('.') characters, then this is considered to indicate a nested structure. This method will recursively descend through the reflected delegate attempting to answer the question. If a method along the way returns null, then reflection cannot delve any deeper and false will be returned.

Specified by:
hasImageKey in interface DataProvider
Parameters:
fieldName - the name of the field to use for reflection
Returns:
true if a method can be reflected to return a value.

getImage

public abstract InputStream getImage(String fieldName)
Get the image from the reflected delegate. If a method based on the given fieldName can be reflected, this will return the result of calling that method. NOTE: if fieldName contains period ('.') characters, then this is considered to indicate a nested structure. This method will recursively descend through the reflected delegate attempting to obtain data. If a method along the way returns null, then reflection cannot delve any deeper and null will be returned.

Specified by:
getImage in interface DataProvider
Parameters:
fieldName - the key under which to locate the image.
Returns:
the reflected value or null.
Throws:
DataProvisionNotFoundException - if the reflected method cannot be found and we are not forgiving.
DataProvisionFetchException - if the reflection fails for security or runtime reasons.

hasStringKey

public abstract boolean hasStringKey(String fieldName)
Determine if the String key has been set. If a method based on the given fieldName can be reflected, this will return true, otherwise false. NOTE: if fieldName contains period ('.') characters, then this is considered to indicate a nested structure. This method will recursively descend through the reflected delegate attempting to answer the question. If a method along the way returns null, then reflection cannot delve any deeper and false will be returned.

Specified by:
hasStringKey in interface DataProvider
Parameters:
fieldName - the name of the field to use for reflection
Returns:
true if a method can be reflected to return a value.

getString

public abstract String getString(String fieldName)
                          throws DataProvisionNotFoundException
Get the String from the reflected delegate. If a method based on the given fieldName can be reflected, this will return the result of calling that method. NOTE: if fieldName contains period ('.') characters, then this is considered to indicate a nested structure. This method will recursively descend through the reflected delegate attempting to obtain data. If a method along the way returns null, then reflection cannot delve any deeper and null will be returned.

Specified by:
getString in interface DataProvider
Parameters:
fieldName - the to look up the value
Returns:
the reflected value or null.
Throws:
DataProvisionNotFoundException - if the reflected method cannot be found and we are not forgiving.
DataProvisionFetchException - if the reflection fails for security or runtime reasons.

setDataFormatter

public void setDataFormatter(Class type,
                             DataFormatter formatter)
Set the formatter for the given class type. If the given formatter is null, the formatter for the given class type is cleared. Note: the type is used to match the return type of the reflected methods. This means that boolean and Boolean (and int and Integer etc) are distinct. You might decide to set the format for both the primitive and the Java wrapper class at the same time if you don't wish to check the exact return types of the methods used.

Parameters:
type - the class to register the formatter against
formatter - the formatter to use. Specify null to clear the formatter
See Also:
DataFormatter

setDateFormat

public void setDateFormat(SimpleDateFormat dateFormat)
Set the Date Format to use when rendering dates as Strings. This is a shortcut method which wraps the given SimpleDateFormat in a Date specific DataFormatter and then calls setDataFormatter(Class, DataFormatter).

Parameters:
dateFormat - specify null to use the Java default date format

setDateFormat

public void setDateFormat(String formatString)
Set the Date Format to use a SimpleDateFormat using the given format string as the format.

Parameters:
formatString -

isPrimitive

protected static boolean isPrimitive(Object o)
Determine if the given object is one of the "primitive" java types.

Parameters:
o -
Returns:

isPrimitiveArray

protected static boolean isPrimitiveArray(Object o)
Determine if the given object is one of the "primitive" java array types.

Parameters:
o -
Returns:

logCall

protected void logCall(Logger log,
                       String methodName,
                       String fieldName)
Log a call to any of the retrieval methods. All subclasses of ReflectiveDataProvider should call this method at the start of any of the get(), is() or has() methods.

Parameters:
log - the logger to log to.
methodName - the name of the method being called.
fieldName - the name of the field being called.

logCall

protected void logCall(Logger log,
                       String methodName,
                       String fieldName,
                       int index)
Log a call to any of the retrieval methods. All subclasses of ReflectiveDataProvider should call this method at the start of any of the get(), is() or has() methods.

Parameters:
log - the logger to log to.
methodName - the name of the method being called.
fieldName - the name of the field being called.
index - the index of the field (if applicable).

toString

public String toString()

Docmosis API 4.5.0

Copyright © 2014 Docmosis Pty Ltd. All Rights Reserved.