Class AbstractXmlDataSource<T extends AbstractXmlDataSource<?>>
- java.lang.Object
-
- net.sf.jasperreports.engine.data.JRAbstractTextDataSource
-
- net.sf.jasperreports.engine.data.AbstractXmlDataSource<T>
-
- All Implemented Interfaces:
HierarchicalDataSource<T>,RandomAccessDataSource,JRDataSource,JRRewindableDataSource
- Direct Known Subclasses:
JaxenXmlDataSource,JRXmlDataSource
public abstract class AbstractXmlDataSource<T extends AbstractXmlDataSource<?>> extends JRAbstractTextDataSource implements RandomAccessDataSource, HierarchicalDataSource<T>
Abstract XML data source implementation that allows to access the data from a xml document using XPath expressions.The data source is constructed around a node set (record set) selected by an XPath expression from the xml document.
Each field can provide an additional XPath expression that will be used to select its value. This expression must be specified using the
PROPERTY_FIELD_EXPRESSIONcustom property at field level. The use of thefield descriptionto specify the XPath expression is still supported, but is now discouraged, the above mentioned custom property taking precedence over the field description. In case no XPath expression is specified, the name of the field will be used for the selection of the value. The expression is evaluated in the context of the current node thus the expression should be relative to the current node.To support subreports, sub data sources can be created. There are two different methods for creating sub data sources. The first one allows to create a sub data source rooted at the current node. The current node can be seen as a new document around which the sub data source is created. The second method allows to create a sub data source that is rooted at the same document that is used by the data source but uses a different XPath select expression.
Example:
<A> <B id="0"> <C> <C> </B> <B id="1"> <C> <C> </B> <D id="3"> <E> <E> </D> </A>Data source creation
- new JRXmlDataSource(document, "/A/B") - creates a data source with two nodes of type /A/B
- new JRXmlDataSource(document, "/A/D") - creates a data source with two nodes of type /A/D
- @id - will select the "id" attribute from the current node
- C - will select the value of the first node of type C under the current node.
- "((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("/B/C") - in the context of the node B, creates a data source with elements of type /B/C
- "((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).dataSource("/A/D") - creates a data source with elements of type /A/D
Generally the full power of XPath expression is available. As an example, "/A/B[@id > 0"] will select all the nodes of type /A/B having the id greater than 0. You'll find a short XPath tutorial here.
Note on performance. Due to the fact that all the XPath expression are interpreted the data source performance is not great. For the cases where more speed is required, consider implementing a custom data source that directly accesses the Document through the DOM API.
- Author:
- Narcis Marcu (narcism@users.sourceforge.net)
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringPROPERTY_FIELD_EXPRESSIONProperty specifying the XPath expression for the dataset field.-
Fields inherited from class net.sf.jasperreports.engine.data.JRAbstractTextDataSource
EXCEPTION_MESSAGE_KEY_CANNOT_CONVERT_FIELD_TYPE, EXCEPTION_MESSAGE_KEY_CANNOT_MODIFY_PROPERTIES_AFTER_START, EXCEPTION_MESSAGE_KEY_NODE_NOT_AVAILABLE, EXCEPTION_MESSAGE_KEY_NULL_DOCUMENT, EXCEPTION_MESSAGE_KEY_NULL_SELECT_EXPRESSION, EXCEPTION_MESSAGE_KEY_UNKNOWN_COLUMN_NAME, EXCEPTION_MESSAGE_KEY_UNKNOWN_NUMBER_TYPE
-
-
Constructor Summary
Constructors Constructor Description AbstractXmlDataSource()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description TdataSource()Creates a sub data source using as root document the document used by "this" data source.abstract TdataSource(java.lang.String selectExpr)Creates a sub data source using as root document the document used by "this" data source.abstract org.w3c.dom.NodegetCurrentNode()org.w3c.dom.DocumentgetDocument()Access the document that this data source is based on.protected java.lang.StringgetFieldExpression(JRField field)java.lang.ObjectgetFieldValue(JRField jrField)Gets the field value for the current position.abstract java.lang.ObjectgetSelectObject(org.w3c.dom.Node currentNode, java.lang.String expression)java.lang.StringgetText(org.w3c.dom.Node node)Return the text that a node contains.TsubDataSource()Creates a sub data source using the current node (record) as the root of the document.abstract TsubDataSource(java.lang.String selectExpr)Creates a sub data source using the current node (record) as the root of the document.abstract org.w3c.dom.DocumentsubDocument()Creates a document using the current node as root.-
Methods inherited from class net.sf.jasperreports.engine.data.JRAbstractTextDataSource
convertNumber, convertStringValue, getConvertBean, getDatePattern, getLocale, getNumberPattern, getTextAttributes, getTimeZone, setDatePattern, setLocale, setLocale, setNumberPattern, setTextAttributes, setTextAttributes, setTimeZone, setTimeZone
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface net.sf.jasperreports.engine.JRDataSource
next
-
Methods inherited from interface net.sf.jasperreports.engine.JRRewindableDataSource
moveFirst
-
Methods inherited from interface net.sf.jasperreports.engine.data.RandomAccessDataSource
currentIndex, moveToRecord, recordCount
-
-
-
-
Field Detail
-
PROPERTY_FIELD_EXPRESSION
public static final java.lang.String PROPERTY_FIELD_EXPRESSION
Property specifying the XPath expression for the dataset field.- See Also:
- Constant Field Values
-
-
Method Detail
-
getDocument
public org.w3c.dom.Document getDocument()
Access the document that this data source is based on.- Returns:
- the document used by this data source
-
getCurrentNode
public abstract org.w3c.dom.Node getCurrentNode()
-
getSelectObject
public abstract java.lang.Object getSelectObject(org.w3c.dom.Node currentNode, java.lang.String expression) throws JRException- Throws:
JRException
-
getFieldValue
public java.lang.Object getFieldValue(JRField jrField) throws JRException
Description copied from interface:JRDataSourceGets the field value for the current position.- Specified by:
getFieldValuein interfaceJRDataSource- Returns:
- an object containing the field value. The object type must be the field object type.
- Throws:
JRException
-
subDataSource
public abstract T subDataSource(java.lang.String selectExpr) throws JRException
Creates a sub data source using the current node (record) as the root of the document. An additional XPath expression specifies the select criteria applied to this new document and that produces the nodes (records) for the data source.- Specified by:
subDataSourcein interfaceHierarchicalDataSource<T extends AbstractXmlDataSource<?>>- Parameters:
selectExpr- the XPath select expression- Returns:
- the xml sub data source
- Throws:
JRException- if the sub data source couldn't be created- See Also:
JRXmlDataSource(Document, String)
-
subDataSource
public T subDataSource() throws JRException
Creates a sub data source using the current node (record) as the root of the document. The data source will contain exactly one record consisting of the document node itself.- Specified by:
subDataSourcein interfaceHierarchicalDataSource<T extends AbstractXmlDataSource<?>>- Returns:
- the xml sub data source
- Throws:
JRException- if the data source cannot be created- See Also:
JRXmlDataSource.subDataSource(String),JRXmlDataSource(Document)
-
subDocument
public abstract org.w3c.dom.Document subDocument() throws JRExceptionCreates a document using the current node as root.- Returns:
- a document having the current node as root
- Throws:
JRException
-
dataSource
public abstract T dataSource(java.lang.String selectExpr) throws JRException
Creates a sub data source using as root document the document used by "this" data source. An additional XPath expression specifies the select criteria applied to this document and that produces the nodes (records) for the data source.- Parameters:
selectExpr- the XPath select expression- Returns:
- the xml sub data source
- Throws:
JRException- if the sub data source couldn't be created- See Also:
JRXmlDataSource(Document, String)
-
dataSource
public T dataSource() throws JRException
Creates a sub data source using as root document the document used by "this" data source. The data source will contain exactly one record consisting of the document node itself.- Returns:
- the xml sub data source
- Throws:
JRException- if the data source cannot be created- See Also:
JRXmlDataSource.dataSource(String),JRXmlDataSource(Document)
-
getText
public java.lang.String getText(org.w3c.dom.Node node)
Return the text that a node contains. This routine:- Ignores comments and processing instructions.
- Concatenates TEXT nodes, CDATA nodes, and the results of recursively processing EntityRef nodes.
- Ignores any element nodes in the sublist. (Other possible options are to recurse into element sublists or throw an exception.)
- Parameters:
node- a DOM node- Returns:
- a String representing node contents or null
-
getFieldExpression
protected java.lang.String getFieldExpression(JRField field)
-
-