11.02.2013 Views

iReport Ultimate Guide

iReport Ultimate Guide

iReport Ultimate Guide

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>iReport</strong> <strong>Ultimate</strong> <strong>Guide</strong><br />

In spite of the possible complexity of an expression, usually it is a simple operation that returns a value. It is not a snippet of<br />

code, or a set of many instructions, and you cannot use complex constructs or flow control keywords, such as switches, loops,<br />

for and while cycles, if and else.<br />

Be that as it may, there is a simple if-else expression construct that is very useful in many situations. An expression is just an<br />

arbitrary operation (however complicated) that returns a value. You can use all the mathematical operators or call object<br />

methods, but at any stage the expression must represent a value. In Java, all these operators can be applied only to primitive<br />

values, except for the sum operator (+). The sum operator can be applied to a String expression with the special meaning of<br />

“concatenate”. So, for example:<br />

40<br />

$F{city} + “, ” + $F{state}<br />

will result in a string like this:<br />

San Francisco, California<br />

All the objects in an expression may include methods. A method can accept zero or more arguments, and it can return or not a<br />

value; in an expression you can use only methods that return a value (otherwise you would have nothing to return from your<br />

expression). The syntax of a method call is:<br />

Object.method(argument1, argument2, and so on.)<br />

Some examples:<br />

Expression Result<br />

“test”.length() 4<br />

“test”.substring(0, 3) “tes”<br />

“test”.startsWith(“A”) false<br />

“test”.substring(1, 2).startsWith(“e”) true<br />

All the methods of each object are usually explained in a set of documents called “Javadocs;” they are freely available on the<br />

Internet.<br />

You can use parentheses to isolate expressions and make the overall expression more readable.<br />

3.5.3 Using an If-Else Construct in an Expression<br />

A way to create an if-else-like expression is by using the special question mark operator. Here is a sample:<br />

(($F{name}.length() > 50) ? $F{name}.substring(0,50) : $F{name})<br />

The syntax is () ? : . It is extremely useful, and the good news is<br />

that it can be recursive, meaning that the value on true and false can be represented by another expression which can be a<br />

new condition:<br />

(($F{name}.length() > 50) ?<br />

(($F{name}.startsWidth(“A”)) ? “AAAA” : “BBB”)<br />

:<br />

$F{name})<br />

This expression returns the String “AAAA” when the value of the field name is longer than 50 characters and starts with A,<br />

returns BBB if it is longer than 50 characters but does not start with A, and, finally, returns the original field value if neither of<br />

these conditions is true.<br />

Despite the possible complexity of an expression (having multiple if-else instructions and so on), it can be insufficient to<br />

define a needed value. For example, if you want to print a number in Roman numerals or give back the name of the weekday<br />

of a date, it is possible to transfer the elaborations to an external Java class method, which must be declared as static, as shown<br />

in the following:<br />

MyFormatter.toRomanNumber( $F{MyInteger}.intValue() )

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!