Docmosis API 4.5.0

com.docmosis.template.population
Class MemoryDataProvider

java.lang.Object
  extended bycom.docmosis.template.population.MemoryDataProvider
All Implemented Interfaces:
Cleanable, DataProvider, MutableDataProvider, Serializable
Direct Known Subclasses:
JSONDataProvider, SQLDataProvider, XMLDataProvider

public class MemoryDataProvider
extends Object
implements MutableDataProvider, Cleanable, Serializable

An efficient memory based DataProvider. All data is plugged in to an instance of this class which is then immediately available to the population of the template.

This class is provided as a utility to simplify the process of passing data to the population process.

When lookups are done using the get* or has* functions, the key parameter may be treated literally or they may have logic applied depending on how this class was constructed. The period ('.') character may be used to indicate a level of "nesting" of data or it may alternatively be treated purely as another character in the key.

For example, depending on whether nesting is enabled or not, the following call will behave differently:

   dp.getString("my.value");
 
If nesting is enabled, the call above is equivalent to (ignoring nulls):
   dp.getDataProvider("my").getString("value");
 

If nesting is disabled that same call is equivalent to (ignoring nulls):

   dp.getString("my.value");
 

Further, nested elements can be indexed. For example:

   dp.getString("my[1].value");
 
is equivalent to
   dp.getDataProvider("my", 1).getString("value");
 
if nesting is enabled. Otherwise it is treated literally and lookup will be performed with a key with the precise String "my[1].value".

NOTE: the logic of nesting does NOT apply when setting, adding or removing values. This means that the following call:

 
   mdp.setString("abc.def", "hello");
 
always treats the key as literal. It is intended that the DataProviderBuilder be used for creating MemoryDataProviders with nesting in mind.

See Also:
DataProviderBuilder, Serialized Form

Constructor Summary
MemoryDataProvider()
          Basic constructor.
 
Method Summary
 void addDataProvider(String key, DataProvider value)
          Add a data provider of the given key at the next available index.
 void cleanup()
          Perform any cleanup duties such as freeing resources.
protected  void finalize()
           
 boolean getBoolean(String key)
          Determine if the given key hs been set to true.
 Set getBooleanKeys()
          Get all of the available keys representing any boolean values.
 DataProvider getDataProvider(String key, int index)
          Get the data provider contained within this data provider with the given key as a name, and an index.
 int getDataProviderCount(String key)
          Return the number of data providers contained within this data provider with the given key as a name.
 Set getDataProviderKeys()
          Get all of the available keys representing any data providers.
 InputStream getImage(String key)
          Get the input stream over the image with the given key.
 Set getImageKeys()
          Get all of the available keys representing any images.
protected static String getRealKey(String key)
          Depending on whether we are in case-sensitive mode, return a case-edited version of the key.
 String getString(String key)
          Gets the string set to the given value.
 Set getStringKeys()
          Get all of the available keys representing any string values.
 boolean hasBooleanKey(String key)
          Determine if a value for the boolean key has been specified
 boolean hasDataProviderKey(String key)
          NOTE: This method does not support nesting of the key value.
 boolean hasImageKey(String key)
          Determine if the given image has been set (even if to null)
 boolean hasStringKey(String key)
          Determine if the given string has been set (even if to null)
 void setBoolean(String key, boolean value)
          Puts the given value for the given key.
 void setFalse(String key)
          Add a false value at the given key.
 void setImage(String key, File file)
          Puts the given image for the given key.
 void setImage(String key, InputStream in)
          Put the given inputstream for an image against the given key.
 void setString(String key, String value)
          Sets the given value for the given key.
 void setTrue(String key)
          Add a true value at the given key.
 String toDebugString()
          Return the contents represented by a single string, for debug purposes.
protected  String toDebugString(int level)
           
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MemoryDataProvider

public MemoryDataProvider()
Basic constructor.

Method Detail

getString

public String getString(String key)
Gets the string set to the given value.

Specified by:
getString in interface DataProvider
Parameters:
key - the key to find the value.
Returns:
a string value.

hasStringKey

public boolean hasStringKey(String key)
Description copied from interface: DataProvider
Determine if the given string has been set (even if to null)

Specified by:
hasStringKey in interface DataProvider
Parameters:
key - the key for the String
Returns:
true if the key has been set.

setString

public void setString(String key,
                      String value)
Sets the given value for the given key. NOTE: nesting of keys is not supported for set methods. See class description for more information.

Specified by:
setString in interface MutableDataProvider
Parameters:
key - the key to look for.
value - the value to set.

getStringKeys

public Set getStringKeys()
Get all of the available keys representing any string values.

Returns:
a Set of String instances or null.

getImage

public InputStream getImage(String key)
Get the input stream over the image with the given key. A new input stream is returned so it is up to the caller to clean it up.

Specified by:
getImage in interface DataProvider
Parameters:
key - the key under which to locate the image.
Returns:
null if the image cannot be found

hasImageKey

public boolean hasImageKey(String key)
Description copied from interface: DataProvider
Determine if the given image has been set (even if to null)

Specified by:
hasImageKey in interface DataProvider
Parameters:
key - the key for the String
Returns:
true if the key has been set.

setImage

public void setImage(String key,
                     File file)
Puts the given image for the given key. The inputstream created will be cleaned up automatically. NOTE: nesting of keys is not supported for set methods. See class description for more information.

Specified by:
setImage in interface MutableDataProvider
Parameters:
key - the key to use.
file - the image to set.

setImage

public void setImage(String key,
                     InputStream in)
Put the given inputstream for an image against the given key. NOTE: since the contract for DataProvider requires that the image stream be required potentially more than once, the given inputstream is stored in a temporary file and that file is used to generate the stream. NOTE: nesting of keys is not supported for set methods. See class description for more information.

Specified by:
setImage in interface MutableDataProvider
Parameters:
key - the key to use
in - the inputstream to assign

getImageKeys

public Set getImageKeys()
Get all of the available keys representing any images.

Returns:
a Set of String instances or null

getBoolean

public boolean getBoolean(String key)
Description copied from interface: DataProvider
Determine if the given key hs been set to true.

Specified by:
getBoolean in interface DataProvider
Parameters:
key - the key to lookup
Returns:
false if the key is not set or has not been set to true

hasBooleanKey

public boolean hasBooleanKey(String key)
Description copied from interface: DataProvider
Determine if a value for the boolean key has been specified

Specified by:
hasBooleanKey in interface DataProvider
Parameters:
key - the key to lookup
Returns:
true if the boolean value has been set, regardless of what the value is.

setTrue

public void setTrue(String key)
Add a true value at the given key. This is a shorthand for addBoolean(key, true); NOTE: nesting of keys is not supported for set methods. See class description for more information.

Specified by:
setTrue in interface MutableDataProvider
Parameters:
key - the key against which to store a true value

setFalse

public void setFalse(String key)
Add a false value at the given key. This is a shorthand for addBoolean(key, false); NOTE: nesting of keys is not supported for set methods. See class description for more information.

Specified by:
setFalse in interface MutableDataProvider
Parameters:
key - the key against which to store a false value

setBoolean

public void setBoolean(String key,
                       boolean value)
Puts the given value for the given key. If a value is already set, it is overwritten with the new value. NOTE: nesting of keys is not supported for set methods. See class description for more information.

Specified by:
setBoolean in interface MutableDataProvider
Parameters:
key - the key to look for.
value - the value to set.

getBooleanKeys

public Set getBooleanKeys()
Get all of the available keys representing any boolean values.

Returns:
a Set of String instances or null

getDataProviderCount

public int getDataProviderCount(String key)
Return the number of data providers contained within this data provider with the given key as a name. NOTE: This method does not support nesting of the key value.

Specified by:
getDataProviderCount in interface DataProvider
Parameters:
key - the key (name) of the data providers to look for.
Returns:
the number of data providers.

hasDataProviderKey

public boolean hasDataProviderKey(String key)
NOTE: This method does not support nesting of the key value.

Specified by:
hasDataProviderKey in interface DataProvider
Parameters:
key - the key with which to lookup the data provider
Returns:
true if a data provider has been set for the given key, even if null

getDataProvider

public DataProvider getDataProvider(String key,
                                    int index)
Get the data provider contained within this data provider with the given key as a name, and an index. NOTE: This method does not support nesting of the key value.

Specified by:
getDataProvider in interface DataProvider
Parameters:
key - the key (name) of the data provider to look for.
index - the index of the data provider.
Returns:
a data provider.

addDataProvider

public void addDataProvider(String key,
                            DataProvider value)
Add a data provider of the given key at the next available index. NOTE: nesting of keys is not supported for this method. See class description for more information.

Specified by:
addDataProvider in interface MutableDataProvider
Parameters:
key - the key (name) for the data provider.
value - the data provider to add.

getDataProviderKeys

public Set getDataProviderKeys()
Get all of the available keys representing any data providers.

Returns:
a Set of String instances or null

getRealKey

protected static String getRealKey(String key)
Depending on whether we are in case-sensitive mode, return a case-edited version of the key.

Parameters:
key - the key to use.
Returns:
if we are in case-sensisive mode, just return the key; otherwise, return a lowercase version of the key.

toString

public String toString()

toDebugString

public String toDebugString()
Return the contents represented by a single string, for debug purposes.

Returns:
a useful string for debug of this object.

toDebugString

protected String toDebugString(int level)

finalize

protected void finalize()
                 throws Throwable
Throws:
Throwable

cleanup

public void cleanup()
Description copied from interface: Cleanable
Perform any cleanup duties such as freeing resources. This method should be able to be called multiple times and should not throw exceptions.

Specified by:
cleanup in interface Cleanable

Docmosis API 4.5.0

Copyright © 2014 Docmosis Pty Ltd. All Rights Reserved.