|
Docmosis API 3.0.1 | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.docmosis.template.population.DataProviderBuilder
This class helps to collect the data together for insertion into a document. Data can be added from a variety of sources such as key value pairs, files, Java Objects and SQL ResultSets. The usage pattern is as follows:
DataProviderBuilder dpb = new DataProviderBuilder(); dpb.add(...); dpb.add(...); dpb.add(...); ... // use the data for a render DocumentProcessor.renderDoc(...., dpb.getDataProvider(), ...);When populating templates, the structure of the data needs to match the structure of the template. This is the case no matter the source of data (Java, Strings, SQL etc). Look to the Docmosis Developer Guide for more information about lining up data and templates. Some specific notes about how this is achieved when adding Strings to the data follows. When adding Strings and files of delimited Strings, a "dot" notation is used to indicate a level of nesting in the data. For example
dpb.add("hotel.floor.room", "101");Corresponds to a structure:
hotel floor room = "101"meaning that hotel "contains" floor which "contains" room with a value of "101. The following rules apply to String data being added:
Typical examples of use are: 1. Create a data provider from simple delimited strings DataProviderBuilder dpb = new DataProviderBuilder(); dpb.add("name", "Me Mo"); dpb.add("age", "101"); ... 2. Add some nested data to an existing data set. This example will create a nested structure of subjects containing details about subjects results: DataProviderBuilder dpb = new DataProviderBuilder(); dpb.add("Report Title", "Student Subject Breakdown"); dpb.addAll(new String[][]{ {"subject.0.name", "English"}, {"subject.0.comment", ""}, {"subject.0.teacher", "J. Test"}, {"subject.0.outcomes.0.outcome", "Listening and Speaking"}, {"subject.0.outcomes.0.passed", "Y"}, {"subject.0.outcomes.1.outcome", "Reading"}, {"subject.0.outcomes.1.passed", "Y"}, {"subject.0.outcomes.2.outcome", "Viewing"}, {"subject.0.outcomes.2.passed", "Y"}, {"subject.0.outcomes.3.outcome", "Writing"}, {"subject.0.outcomes.3.passed", "N"}, {"subject.1.name", "Mathematics"}, {"subject.1.comment", ""}, {"subject.1.teacher", "P. Test"}, {"subject.1.outcomes.0.outcome", "Algebra"}, {"subject.0.outcomes.0.passed", "Y"}, {"subject.1.outcomes.1.outcome", "Chance and Data"}, {"subject.0.outcomes.1.passed", "Y"}, {"subject.1.outcomes.2.outcome", "Measurement"}, {"subject.0.outcomes.2.passed", "Y"}, {"subject.1.outcomes.3.outcome", "Number"}, {"subject.0.outcomes.3.passed", "Y"}, {"subject.1.outcomes.4.outcome", "Space"}, {"subject.0.outcomes.4.passed", "Y"}}); ...
Constructor Summary | |
DataProviderBuilder()
Construct a new DataProviderBuilder |
|
DataProviderBuilder(MutableDataProvider addTo)
Construct a new DataProvider Builder using the given MutableDataProvider as the starting point. |
Method Summary | |
DataProviderBuilder |
add(DataProvider dp,
String key)
Add the given data provider to our current data under the given key. |
DataProviderBuilder |
add(String[] keyValuePair)
Add a key value pair to the memory data provider. |
DataProviderBuilder |
add(String key,
String value)
Add a new key-value pair to the current data. |
DataProviderBuilder |
addAll(Map map)
Add all key value pairs from a map to the memory data provider. |
DataProviderBuilder |
addAll(String[][] keyValuePairs)
Add all key value pairs to the memory data provider. |
DataProviderBuilder |
addFile(File keyValuePairFile)
Add a key-value pair file that using a comma as a delimiter. |
DataProviderBuilder |
addFile(File keyValuePairFile,
char delimiter)
Add a key-value pair file using the given delimiter to distinguish key from value. |
DataProviderBuilder |
addFile(File keyValuePairFile,
char delimiter,
String charSetName)
Add a key-value pair file using the given delimiter to distinguish key from value. |
DataProviderBuilder |
addImage(String key,
File imageFile)
Add the given image (in a file) to the current data. |
DataProviderBuilder |
addImage(String key,
InputStream imageStream)
Add the given image (in an InputStream) to the current data. |
DataProviderBuilder |
addJavaObject(Object o)
Deprecated. in favour of addJavaObject(Object, String). See warning. |
DataProviderBuilder |
addJavaObject(Object o,
String key)
Add the given Java object to our current data so that it may be referenced by the given key. |
DataProviderBuilder |
addJavaObject(Object o,
String key,
boolean forgiving)
Add the given Java object to our current data so that it may be referenced by the given key, specifying the forgiving behaviour (see below). |
DataProviderBuilder |
addJSONFile(File jsonFile)
Add a JSON file to the existing data provider. |
DataProviderBuilder |
addJSONString(String jsonString)
Add a JSON string to the existing data provider. |
DataProviderBuilder |
addSQL(ResultSet rs,
DataProviderGrouping[] grouping)
Add the given ResultSet to our data using the given grouping definition to transform the ResultSet grid into a hierarchy. |
DataProviderBuilder |
addSQL(ResultSet rs,
String key)
Add the given ResultSet to our data. |
void |
addStringInterceptor(StringInterceptor newInterceptor)
Add a StringInterceptor to this builder. |
DataProviderBuilder |
addXMLDocument(Document xmlDocument)
Add an XML document to the existing data provider. |
DataProviderBuilder |
addXMLDocument(Document xmlDocument,
XMLNodeFilter filter,
boolean includeRoot)
Add an XML document to the existing data provider. |
DataProviderBuilder |
addXMLFile(File xmlFile)
Add an XML file to the existing data provider. |
DataProviderBuilder |
addXMLFile(File xmlFile,
boolean includeRoot)
Add an XML file to the existing data provider. |
DataProviderBuilder |
addXMLFile(File xmlFile,
XMLNodeFilter filter,
boolean includeRoot)
Add an XML file to the existing data provider applying the given filter to selectively exclude some of the XML content. |
DataProviderBuilder |
addXMLStream(InputStream source,
XMLNodeFilter filter,
boolean includeRoot)
Add the given input stream to this data provider, optionally filtering out content using the given filter and optionally including the root node. |
DataProviderBuilder |
addXMLString(String xml)
Add the given XML string to this data provider. |
DataProviderBuilder |
addXMLString(String xml,
XMLNodeFilter filter)
Add the given XML string to this data provider, optionally filtering out content using the given filter. |
DataProviderBuilder |
addXMLString(String xml,
XMLNodeFilter filter,
boolean includeRoot)
Add the given XML string to this data provider, optionally filtering out content using the given filter and optionally including the root node. |
DataProvider |
getDataProvider()
Get the data provider that has been built so far. |
String |
toString()
|
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
public DataProviderBuilder()
public DataProviderBuilder(MutableDataProvider addTo)
addTo
- the starting point of data.Method Detail |
public void addStringInterceptor(StringInterceptor newInterceptor)
newInterceptor
- an interceptor which may change interpretation of strings.
IllegalArgumentException
- if the interceptor is nulladd(String, String)
public DataProviderBuilder addXMLDocument(Document xmlDocument) throws DOMException
xmlDocument
- the XML document containing the data to add to the
data provider.
DOMException
- if the root node is missing from the document.public DataProviderBuilder addXMLDocument(Document xmlDocument, XMLNodeFilter filter, boolean includeRoot) throws DOMException
xmlDocument
- the XML document containing the data to add to the
data provider.filter
- an XML filter to only allow nodes of interest. May be null.includeRoot
- if true the root node of the XML will form the root of
the created data. Otherwise the data is build starting with the children
of the root node.
DOMException
- if the root node is missing from the document.public DataProviderBuilder addXMLFile(File xmlFile) throws IOException, SAXException, ParserConfigurationException, DOMException
xmlFile
- the XML file containing the data to add to the
data provider.
DOMException
- if the root node is missing from the document.
IOException
SAXException
ParserConfigurationException
public DataProviderBuilder addXMLFile(File xmlFile, boolean includeRoot) throws IOException, SAXException, ParserConfigurationException, DOMException
xmlFile
- the XML file containing the data to add to the
data provider.includeRoot
- if true the root node of the XML will form the root of
the created data. Otherwise the data is build starting with the children
of the root node.
DOMException
- if the root node is missing from the document.
IOException
SAXException
ParserConfigurationException
public DataProviderBuilder addXMLFile(File xmlFile, XMLNodeFilter filter, boolean includeRoot) throws IOException, SAXException, ParserConfigurationException, DOMException
xmlFile
- the XML file containing the data to add to the
data provider.filter
- an XML filter to only allow nodes of interest. May be null.includeRoot
- if true the root node of the XML will form the root of
the created data. Otherwise the data is build starting with the children
of the root node.
DOMException
- if the root node is missing from the document.
IOException
SAXException
ParserConfigurationException
public DataProviderBuilder addXMLString(String xml) throws ParserConfigurationException, SAXException, IOException
xml
- the XML text containing the data to add
ParserConfigurationException
- if there is a problem preparing XML parsing
SAXException
- if there is a problem with the XML
IOException
- if an IO problem occurspublic DataProviderBuilder addXMLString(String xml, XMLNodeFilter filter) throws ParserConfigurationException, SAXException, IOException
xml
- the XML text containing the data to addfilter
- a filter (or null) that can be used to stop content from the given
XML being included in the data.
ParserConfigurationException
- if there is a problem preparing XML parsing
SAXException
- if there is a problem with the XML
IOException
- if an IO problem occurspublic DataProviderBuilder addXMLString(String xml, XMLNodeFilter filter, boolean includeRoot) throws ParserConfigurationException, SAXException, IOException
xml
- the XML text containing the data to addfilter
- a filter (or null) that can be used to stop content from the given
XML being included in the data.includeRoot
- if true the root node of the XML will form the root of
the created data. Otherwise the data is build starting with the children
of the root node.
ParserConfigurationException
- if there is a problem preparing XML parsing
SAXException
- if there is a problem with the XML
IOException
- if an IO problem occurspublic DataProviderBuilder addXMLStream(InputStream source, XMLNodeFilter filter, boolean includeRoot) throws ParserConfigurationException, SAXException, IOException
source
- the input stream over the XML datafilter
- the filter to use to prune parts of the XML treeincludeRoot
- if true the root node of the XML will form the root of
the created data. Otherwise the data is build starting with the children
of the root node.
ParserConfigurationException
- if there is a problem preparing XML parsing
SAXException
- if there is a problem with the XML
IOException
- if an IO problem occurspublic DataProviderBuilder addJSONString(String jsonString) throws JSONException
JSONException
public DataProviderBuilder addJSONFile(File jsonFile) throws IOException, JSONException
jsonFile
- the JSON file containing the data to add to the
data provider.
IOException
JSONException
public DataProviderBuilder addJavaObject(Object o)
DataProviderBuilder dpb = new DataProviderBuilder(); dpb.addJavaObject(person);the template would reference the first name directly. So the field in the template could be "getFirstName()". NOTE: if the docmosis property "docmosis.populator.lookup.java.forgiving" is set to false then lookups to Java objects where the method can't be found produces an error. By default this is set to true.
o
- the Java object to add to the root of our data
public DataProviderBuilder addJavaObject(Object o, String key)
DataProviderBuilder dpb = new DataProviderBuilder(); dpb.addJavaObject(person, "myPerson");the template would reference the first name in the context of the key "person". So the field in the template could be "myPerson.getFirstName()". NOTE: if the docmosis property "docmosis.populator.lookup.java.forgiving" is set to false then lookups to Java objects where the method can't be found produces an error. By default this is set to true and a lookup on a non-existent Java method is the same as if there is no data.
o
- the object to addkey
- the key under which the object will be referenced
public DataProviderBuilder addJavaObject(Object o, String key, boolean forgiving)
DataProviderBuilder dpb = new DataProviderBuilder(); dpb.addJavaObject(person, "myPerson");the template would reference the first name in the context of the key "person". So the field in the template could be "myPerson.getFirstName()". NOTE: if the docmosis property "docmosis.populator.lookup.java.forgiving" is set to false then lookups to Java objects where the method can't be found produces an error. By default this is set to true and a lookup on a non-existent Java method is the same as if there is no data. The forgiving parameter overrides the properties that are set.
o
- the object to addkey
- the key under which the object will be referencedforgiving
- whether attempts to reference a non-existent method on the Java Objects
are fatal or ignored. Overrides default property settings.
public DataProviderBuilder addSQL(ResultSet rs, String key) throws SQLException
ResultSet myResultSet = executeQuery("select number, rooms from floors where hotelid = 1"); dpb.addSQL(myResultSet, "floors");could be referenced in the template using fields named:
"floors[0].number" or "floors[1].rooms"NOTE: All data is read into memory from the resultset.
rs
- the result set containing the results of a database querykey
- the key under which the data will be referenced.
SQLException
- if an SQLException occurs during reading of the datapublic DataProviderBuilder addSQL(ResultSet rs, DataProviderGrouping[] grouping) throws NoSuchColumnNameException, SQLException, DataProviderGroupingException
rs
- the result set containing the results of a database querygrouping
- the grouping definition for transforming data into a hierarchy
SQLException
- if something goes wrong reading from the result set.
DataProviderGroupingException
- if there is a problem with grouping the data.
NoSuchColumnNameException
- for invalid column names in the groupings.public DataProviderBuilder add(DataProvider dp, String key)
dp
- the DataProvider to addkey
- the key under which to add the given data provider
public DataProviderBuilder addImage(String key, File imageFile)
imageFile
- the file containing the imagekey
- the key under which the image will be referenced
IllegalArgumentException
- if the key is not valid (ie starts with a number or contains an
out-of-sequence index)public DataProviderBuilder addImage(String key, InputStream imageStream)
imageStream
- the InputStream containing the imagekey
- the key under which the image will be referenced
IllegalArgumentException
- if the key is not valid (ie starts with a number or contains an
out-of-sequence index).public DataProviderBuilder add(String key, String value)
The following VALUEs are treated specially: true - data is added as a Boolean TRUE value false - data is added as a Boolean FALSE value [image:xxx.jpg] - xxx.jpg is assumed to be a file that specifies the image.Also, if any StringInterceptors have been added, they will be called and given the chance to apply special interpretations to the key and value.
key
- a for the element.value
- the value to pass in.
IllegalArgumentException
- if the key is not valid (ie starts with a
number or contains an out-of-sequence index).public DataProviderBuilder add(String[] keyValuePair)
keyValuePair
- an array of length two, where the first element
is the key and the second element is the value.
add(String, String)
public DataProviderBuilder addAll(String[][] keyValuePairs)
keyValuePairs
- an double array of width two, and any height.
For the width, each first element is the key and second element
is the value.
For the height, each element corresponds to another key value pair.
add(String, String)
public DataProviderBuilder addAll(Map map)
addJavaObject()
methods.
This method is equivalent to adding all key-value pairs from the Map
using the add(String, String) method. The same special interpretation
to the keys and values applies.
map
- the map to get the key value pairs from.
addJavaObject(Object)
,
add(String, String)
public DataProviderBuilder addFile(File keyValuePairFile) throws IOException
java.io.InputStreamReader
).
keyValuePairFile
- a file containing key value pairs, with two
values separated by the default delimiter. Each line represents another key
value pair. Within each row, the first element is the key and the
second element is the value.
UnsupportedEncodingException
- if the give charSetName is not valid
IOException
- if there's a problem reading from the file.add(String, String)
public DataProviderBuilder addFile(File keyValuePairFile, char delimiter) throws IOException
java.io.InputStreamReader
).
keyValuePairFile
- a file containing key value pairs, with two
values separated by a given delimiter. Each row represents another key
value pair. Within each line, the first element is the key and the
second element is the value.delimiter
- the delimiter to separate each key from the value.
UnsupportedEncodingException
- if the give charSetName is not valid
IOException
- if there's a problem reading from the file.add(String, String)
public DataProviderBuilder addFile(File keyValuePairFile, char delimiter, String charSetName) throws IOException
java.nio.charset.Charset
).
If the given charSetName is null, the default character encoding is used
for the platform (see
java.io.InputStreamReader
)
keyValuePairFile
- a file containing key value pairs, with two
values separated by a given delimiter. Each row represents another key
value pair. Within each line, the first element is the key and the
second element is the value.delimiter
- the delimiter to separate each key from the value.charSetName
- The name of a supported
charset
. A null value
means the platform default character encoding will be used.
UnsupportedEncodingException
- if the give charSetName is not valid
IOException
- if there's a problem reading from the file.add(String, String)
public DataProvider getDataProvider()
public String toString()
|
Docmosis API 3.0.1 | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |