Jaspersoft provides two example custom data sources:
| • | Custom Bean Data Source |
The examples are found in the <js-install>/samples/customDataSource directory. Once you have deployed JasperReports Server to your application server, you can use Apache Ant to build and deploy the examples.
The custom data source examples have been verified with 4.7.1.
Installing the Custom Data Source Examples
Java Development Kit
Because you must recompile the Java source files, you need the Java Development Kit (JDK). Ensure that the JAVA_HOME environment variable points to a full JDK installation.
About Apache Ant
If you used an installer to install JasperReports Server, you have Ant installed already. Run Ant using the following command:
<js-install>apache-antbinant <ant-arguments>
If you installed JasperReports Server manually with a WAR file, you must download Ant from http://ant.apache.org. Ant 1.8.1 was used for testing, but earlier versions may also work.
Installation
Each sample directory includes:
| • | build.xml: The Ant build file. |
| • | src: Java source directory. |
| • | webapp: A directory containing other files required by the examples, such as JSPs and Spring configuration files, which are copied directly to the JasperReports Server web application directory. |
| • | reports: A directory containing example JRXML files that use the sample custom data sources. |
To install the samples in your JasperReports Server web application:
| 1. | At the command line, change directories to the custom data source sample directory (<js-install/samples/customDataSource). |
| 2. | Edit build.xml and set the webAppDir property to the root of the JasperReports Server web application. |
| 3. | Run the Ant command (as described in ) with no arguments; this executes the default target, which is named deploy. The deploy target initiates these actions: |
| • | Compiles the Java source under the src directory. |
| • | Deploys the compiled Java class files to the web application. |
| • | Deploys files under the webapp directory to the web application. |
| 4. | Restart the application server. |
| These steps only make the example custom data sources themselves available in JasperReports Server. To test the data sources, you must also create instances of the custom data sources in JasperReports Server, then upload the reports that accompany the samples. |
Custom Bean Data Source
The custom bean data source implementation creates a data source from a collection of Java beans declared in the source code. Its Spring bean definition file is in <js-install>/samples/customDataSource/webapp/WEB-INF/applicationContext‑sampleCDS.xml. Jaspersoft provides an example report that uses this data source; it is called simpleCDS.jrxml and is located in the <js-install>/samples/customDataSource/reports directory.
Webscraper Custom Data Source
The webscraper custom data source implementation fetches a web page, decodes its HTML, and extracts selected data that is turned into field values in the data source. Its Spring bean definition file is located in <js-install>/samples/
customDataSource/webapp/WEB-INF/applicationContext-webscraperDS.xml.
The example reports for this data source read a web page from http://www.craigslist.org and extract a list of items for sale.
The webscraper data source configuration includes these elements:
| • | URL: An HTTP URL that refers to the HTML page containing the desired content. |
| • | DOM path: An XPath expression that locates HTML elements to be turned into rows in the data source. |
| • | Field paths: XPath expressions for each field defined in the JRXML. JasperReports Server uses these paths to locate the field value in each row selected by the DOM path. |
The implementation creates a data source by:
| • | Using the URL to issue a GET request for an HTML page. |
| • | Using the DOM path to select XML elements from the converted response. |
| • | Creating a new data source row for each selected element. |
| • | Determining the context for each field based on its field path. |
| • | The data source takes two parameters: the URL of the web page and the XPath that determines how elements in the HTML page become rows in the data source. The parameters can either be specified by a data source definition in the repository or by a query string in the JRXML. JasperReports Server includes sample reports that each show one of these approaches: |
| • | The <js-install>/samples/reports/webscrapertest.jrxml report has no query. Instead, it relies on an instance of the custom data source that you must create in the repository. Typical parameters to use with this data source are: |
URL:: http://sfbay.craigslist.org/search/cta/eby?query=&srchType=T&minAsk=&maxAsk=&nh=66
DOM Path: /html/body/blockquote[2]/p
| • | The <js-install>/samples/reports/webscraperQEtest.jrxml example contains a queryString element that specifies the URL and the DOM path. It should be used without defining a data source instance, because JasperReports Server doesn’t run the query executer for this particular implementation if a data source is defined for the report unit. |
| The URL above is an example of the type of query parameter that might work with an external web source. Note that web sites are frequently redesigned and the URLs used by any website are subject to change. |
In order to use the webscraper data source, you must first register the webscraper query executer factory. One way to do this is as follows:
| 1. | Open the file <js-install>/WEB-INF/classes/jasperreports.properties for editing |
| 2. | Add the following at the end of the file: |
# registering query executer for webscraperQEtest.jrxml examplenet.sf.jasperreports.query.executer.factory.webscraper=example.cds.WebScraperQueryExecuterFactory[/code] |
| 4. | Restart your application server. |
For more information about registering query executors, see the Report Query section in the JasperReports Library Ultimate Guide.
Recommended Comments
There are no comments to display.