13.07.2015 Views

iReport Ultimate Guide - Nimsoft Library

iReport Ultimate Guide - Nimsoft Library

iReport Ultimate Guide - Nimsoft Library

SHOW MORE
SHOW LESS
  • No tags were found...

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>We will describe the nature of fields, variables, and parameters in the next chapter. For now we just have to keep in mind thatthey always represent objects (that is, they can have a null value) and that you specify their type when you declare them withina report. Version 0.6.2 of JasperReports introduced a new syntax: $R{resource_key}. This is used to localize strings. I willdiscuss this at greater lengths in Chapter Chapter 17, “Internationalization,” on page 323.In spite of the possible complexity of an expression, usually it is a simple operation that returns a value. It is not a snippet ofcode, or a set of many instructions, and you cannot use complex constructs or flow control keywords, such as switches, loops,for and while cycles, if and else.Be that as it may, there is a simple if-else expression construct that is very useful in many situations. An expression is just anarbitrary operation (however complicated) that returns a value. You can use all the mathematical operators or call objectmethods, but at any stage the expression must represent a value. In Java, all these operators can be applied only to primitivevalues, except for the sum operator (+). The sum operator can be applied to a String expression with the special meaning of“concatenate”. So, for example:$F{city} + “, ” + $F{state}will result in a string like this:San Francisco, CaliforniaAll the objects in an expression may include methods. A method can accept zero or more arguments, and it can return or not avalue; in an expression you can use only methods that return a value (otherwise you would have nothing to return from yourexpression). The syntax of a method call is:Object.method(argument1, argument2, and so on.)Some examples:ExpressionResult“test”.length() 4“test”.substring(0, 3)“test”.startsWith(“A”)“test”.substring(1, 2).startsWith(“e”)“tes”falsetrueAll the methods of each object are usually explained in a set of documents called “Javadocs;” they are freely available on theInternet.You can use parentheses to isolate expressions and make the overall expression more readable.3.5.3 Using an If-Else Construct in an ExpressionA way to create an if-else-like expression is by using the special question mark operator. Here is a sample:(($F{name}.length() > 50) ? $F{name}.substring(0,50) : $F{name})The syntax is () ? : . It is extremely useful, and the good news isthat it can be recursive, meaning that the value on true and false can be represented by another expression which can be anew condition:(($F{name}.length() > 50) ?(($F{name}.startsWidth(“A”)) ? “AAAA” : “BBB”):$F{name})This expression returns the String “AAAA” when the value of the field name is longer than 50 characters and starts with A,returns BBB if it is longer than 50 characters but does not start with A, and, finally, returns the original field value if neither ofthese conditions is true.Despite the possible complexity of an expression (having multiple if-else instructions and so on), it can be insufficient todefine a needed value. For example, if you want to print a number in Roman numerals or give back the name of the weekday40

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

Saved successfully!

Ooh no, something went wrong!