JasperReports Ultimate Guide - Sample Reference - Schema Reference - Configuration Reference - API (Javadoc)

JasperReports - BeanShell Sample (version 4.6.0)


Shows how reports could be compiled using a customized compiler that relies on BeanShell for expressions evaluation.

Download All Sample Source Files
Browse Sample Source Files on SVN


Main Features in This Sample

Using the BeanShell Scripting Language for Report Expressions (BeanShell Report Compiler)

Secondary Features
Report Compilers


top

Using the BeanShell Scripting Language for Report Expressions (BeanShell Report Compiler)Documented by Sanda Zaharia


Description / Goal
How to use BeanShell scripting for report expressions.

Since
0.6.6

Other Samples
/demo/samples/antcompile
/demo/samples/groovy
/demo/samples/java1.5
/demo/samples/javascript


The BeanShell Report Compiler

In the beginning JasperReports relied on the JDK 1.2-compatible report compiler. Then the JDK 1.3-compatible report compiler was introduced, then the Jikes and JDT-based ones. Both JDK 1.2 and JDK 1.3-based compiler implementations were slow and problematic when compiling reports on-the-fly, especially in a web environment, because they worked only with files on disk and required temporary working directories to store generated Java source files and corresponding class files during the report-compilation process.
This is why a new implementation was added based on the BeanShell library for runtime expression evaluation. BeanShell does not produce Java bytecode and can work with in-memory scripts interpreted at runtime. Working with the JRBshCompiler simplified a lot applications deployment, but the expression evaluation process was slower than before, generating a noticeable loss in performance.
The later addition of the JDT-based report compiler made the whole process both faster and simpler to deploy, as it does not require files on disk and its performance is comparable to the Jikes-based compiler performance. Therefore, the JRBshCompiler implementation is shipped with the JasperReports library for historical reasons only.

The BeanShell Sample

The main purpose of this sample is to show how the BeanShell compiler implementation works. Useful information about various compiler implementations can be found here.
In order to compile a report, the engine needs to know which is the report expressions language and which compiler implementation to use. If a BeanShell report compilation is expected, one has to set the language attribute to bsh in the report JRXML file, and the default compiler implementation for the BeanShell language in the /src/jasperreports.properties file:

net.sf.jasperreports.compiler.bsh=net.sf.jasperreports.compilers.JRBshCompiler

In the report template are included specific BeanShell expressions.
Having two numbers, 3 and 5, the report will output first their values, and then their calculated sum. The two numbers are declared as follows:

  <parameter name="A" class="java.lang.Double">
    <defaultValueExpression><![CDATA[new Double(3)]]></defaultValueExpression>
  </parameter>
  <parameter name="B" class="java.lang.Double">
    <defaultValueExpression><![CDATA[new Double(5)]]></defaultValueExpression>
  </parameter>


The next two expressions in the report template read values from parameters declared above and store them in two text fields.

  <textFieldExpression class="java.lang.Double"><![CDATA[$P{A}]]></textFieldExpression>
  <textFieldExpression class="java.lang.Double"><![CDATA[$P{B}]]></textFieldExpression>

Next, the A + B sum is calculated:

  <textFieldExpression class="java.lang.Double"><![CDATA[new Double($P{A}.doubleValue() + $P{B}.doubleValue())]]></textFieldExpression>

Running the Sample

Running the sample requires the Apache Ant library. Make sure that ant is already installed on your system (version 1.5 or later).
In a command prompt/terminal window set the current folder to demo/samples/beanshell within the JasperReports source project and run the > ant test view command.
It will generate all supported document types containing the sample report in the demo/samples/beanshell/build/reports directory.
Then the report will open in the JasperReports internal viewer.



© 2001- Jaspersoft Corporation www.jaspersoft.com