04.06.2015 Views

iReport Ultimate Guide - Docs.nimsoft.com

iReport Ultimate Guide - Docs.nimsoft.com

iReport Ultimate Guide - Docs.nimsoft.com

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

We will describe the nature of fields, variables, and parameters in the next chapter. For now we just have to keep in mind that<br />

they always represent objects (that is, they can have a null value) and that you specify their type when you declare them within<br />

a report. Version 0.6.2 of JasperReports introduced a new syntax: $R{resource_key}. This is used to localize strings. I will<br />

discuss this at greater lengths in Chapter Chapter 17, “Internationalization,” on page 323.<br />

In spite of the possible <strong>com</strong>plexity 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 <strong>com</strong>plex 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 <strong>com</strong>plicated) 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 />

$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<br />

Result<br />

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

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

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

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

“tes”<br />

false<br />

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 <strong>com</strong>plexity 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 />

40

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

Saved successfully!

Ooh no, something went wrong!