JasperReports Ultimate Guide - Samples - Schema - Configuration - Functions - FAQ - API (Javadoc)
|
|
|
|
JasperReports - Java 1.5 Sample (version 6.21.0) | |
|
|
|
Main Features in This Sample | |
| Using Java 1.5 Syntax in Report Expressions (Java 1.5 Report Compiler) |
|
|
Secondary Features | |
Report Compilers |
|
|
|||||||
top | ||||||||
|
||||||||
Using Java 1.5 Syntax in Report Expressions (Java 1.5 Report Compiler) | Documented by Sanda Zaharia | |||||||
|
||||||||
| Description / Goal |
| How to use Java 1.5 language specific features inside report expressions. | |||||
| Since |
| 1.1.1 | |||||
| Other Samples |
|
|
|||||
|
||||||||
|
Java 1.5 Scripting Example
The main purpose of this sample is to show how the Java 1.5 compiler implementation works. Useful information about various Java compiler implementations can be found here. This sample contains report expressions written using Java 1.5. The JRJdtCompiler default implementation is Java 1.5-compatible and is strongly recommended to use it when handling Java 1.5-related expressions. In order to use Java 1.5, the report language attribute can be either not set (because Java represents the default scripting language),
or declared as follows:
language="java"
In the sample's report template the language attribute is not set.
Next, one have to instruct the JDT compiler to observe Java 1.5 code compatibility. In the /src/jasperreports.properties file the following properties are set:
Having two integer 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.Integer">
Both A and B values are declared of java.lang.Integer type. But their values are let as primitive int types, because
Java 1.5 performs both autoboxing and unboxing mechanisms. When needed, primitive types are automatically converted into their wrapper class.
The Java 1.5 syntax becomes a lot simplified. Equivalent Java 1.4 expressions would be:
<defaultValueExpression><![CDATA[Integer.valueOf(3)]]></defaultValueExpression>
<defaultValueExpression><![CDATA[Integer.valueOf(5)]]></defaultValueExpression>
The next two expressions in the report template read values from parameters declared above and store them in two text fields. These expressions can be evaluated using either Java 1.5 or Java 1.4: <textFieldExpression class="java.lang.Integer"><![CDATA[$P{A}]]></textFieldExpression>
<textFieldExpression class="java.lang.Integer"><![CDATA[$P{B}]]></textFieldExpression>
Next, the A + B sum is calculated using a Java 1.4 expression: <textFieldExpression class="java.lang.Integer"><![CDATA[Integer.valueOf($P{A}.intValue() + $P{B}.intValue())]]></textFieldExpression>
A and B being Integer , their intValue() method is called in order to calculate the sum.
After that, because the sum should be
stored itself in an Integer value, an Integer object is made available for this purpose. The Java expression looks
rather complicate and one has to take care to instantiate objects with their proper types in order to avoid class
cast exceptions.
Now, the same A + B sum is calculated using a Java 1.5 expression: <textFieldExpression class="java.lang.Integer"><![CDATA[$P{A} + $P{B}]]></textFieldExpression>
Object creation, autoboxing, unboxing and type conversion are transparent processes here, the user only has to write a simple addition operation between the two report parameters (however, the specific parameter syntax still has to be respected). Finally, depending on the greeting parameter's value, a greeting formula is shown. This parameter is an enumerated Greeting type
(another specific Java 1.5 feature):
<parameter name="greeting" class="Greeting">
The Greeting type is defined in the /src/Greeting.java file:
public enum Greeting { bye, day }
When the parameter's value is Greeting.bye , the output message will be 'Bye!';
when the parameter's value is Greeting.day , the message will be 'Have a nice day!':
<staticText> <reportElement x="0" y="450" width="480" height="35"> <printWhenExpression>$P{greeting} == Greeting.bye</printWhenExpression> </reportElement> <textElement textAlignment="Center"> <font size="24"/> </textElement> <text>Bye!</text> </staticText> <staticText> <reportElement x="0" y="450" width="480" height="35"> <printWhenExpression>$P{greeting} == Greeting.day</printWhenExpression> </reportElement> <textElement textAlignment="Center"> <font size="24"/> </textElement> <text>Have a nice day!</text> </staticText>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/java1.5 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/java1.5/build/reports directory.
Then the report will open in the JasperReports internal viewer. |
|||||||
|
|
© 2001- Cloud Software Group, Inc. www.jaspersoft.com |