Posts

Showing posts from July, 2011

OGNL Part 8: Lambda without closures

The part 7 is a tutorial on Selection. Here I explain the very easy example from the OGNL language guide . This is factorial function as shown in the following example. package au.com.ojitha.ongl; import ognl.Ognl; import ognl.OgnlException; public class Lambda { /** * @param args * @throws OgnlException */ public static void main(String[] args) throws OgnlException { System.out.println(Ognl.getValue( "#fact = :[#this <=1 ? 1 : #this*#fact(#this-1)], #fact(4)", null)); } }   The output is 4*3*2*1=24.

OGNL Part 7: Tutorial on OGNL Selection

We’ve already discussed the  Selection in  the part 6 . Here the example of selecting first element or last element in the filtered array or list. ^ for the first element (lines 19-20) $ for the last element (lines 23-24) package au.com.ojitha.ongl; import ognl.Ognl; import ognl.OgnlException; public class SelectionTutorial { /** * @param args * @throws OgnlException */ public static void main(String[] args) throws OgnlException { Root root = new Root(); // selection System.out.println("slect integer which are greater than 2: " + Ognl.getValue("array.{? #this > 2}", root)); // return the first element System.out.println("select the first element in the array: " + Ognl.getValue("array.{^ #this > 2}", root)); //return the last element System.out.println("select the last element in the array: " + Ognl.getValue("array.{$ #this > 2}", root)); } } class Root { pr

OGNL Part 6: Selection

In the part 5 of this series, we’ve discussed the Projection concept. In this part, see the example for the selection which is very similar to concept of SQL selection. package au.com.ojitha.ongl; import java.util.List; import ognl.Ognl; import ognl.OgnlException; public class ProjectingExmaple { /** * @param args * @throws OgnlException */ public static void main(String[] args) throws OgnlException { List<Integer> classes =(List<Integer>)Ognl.getValue("array.{? #this > 2}", new MyRoot()); for (Integer class1 : classes) { System.out.println(class1); } System.out.println("size of the output "+classes.size()); } } class MyRoot { private int[] array = {1,2,3,4}; public int[] getArray() { return array; } public void setArray(int[] array) { this.array = array; } } Here the output 3 4 size of the output 2 Reference OGNL language guide for more information.

OGNL Part 5: Projection

This is very important concept in the OGNL very similar to the database projection concept. In the part 4 , we’ve already discussed the Arrays. Following code is based on the array. package au.com.ojitha.ongl; import java.util.List; import ognl.Ognl; import ognl.OgnlException; public class ProjectingExmaple { /** * @param args * @throws OgnlException */ public static void main(String[] args) throws OgnlException { List classes =(List )Ognl.getValue("array.{class}", new MyRoot()); for (Class class1 : classes) { System.out.println(class1); } System.out.println("size of the output "+classes.size()); } } class MyRoot { private int[] array = {1,2,3,4}; public int[] getArray() { return array; } public void setArray(int[] array) { this.array = array; } } Here the output class java.lang.Integer class java.lang.Integer class java.lang.Integer size of the output 4

OGNL Part 4: Lists and Arrays

In the part 3 , we’ve discussed about Maps. In this blog, let’s look at how to create array and lists. The most common thing is both are accessible via index. package au.com.ojitha.ongl; import java.util.List; import ognl.Ognl; import ognl.OgnlException; public class BasicListExample { /** * @param args * @throws OgnlException */ public static void main(String[] args) throws OgnlException { // List List<String> list = (List) Ognl.getValue("{'a','b','c'}", null); System.out.println(list + " is size of " + Ognl.getValue("size", list)); System.out .println("The third element is " + Ognl.getValue("[2]", list)); // array Integer[] intValues = (Integer[]) Ognl.getValue("new Integer[]{1,2,3}", null); System.out.println("Size of the array is " + Ognl.getValue("length", intValues)); System.out.println("The third element is " + Ogn

OGNL Part 3: How to read Map

In this example, explains how the OGNL access the Map.  Refer to the part 2 for basic understanding of navigation chain. import java.util.HashMap; import java.util.Map; import ognl.Ognl; import ognl.OgnlException; public class SecondExample { /** * @param args * @throws OgnlException */ public static void main(String[] args) throws OgnlException { Map<String, String> mapValues = new HashMap<String, String>(); mapValues.put("lname", "Kumanayaka"); mapValues.put("fname", "Ojitha"); System.out.println(Ognl.getValue("fname+' '+ lname", mapValues)); } } OGNL always treat different kind of objects differently. Instead of accessing Map as in line 16 and 17, you can use OGNL to initialize the map as shown in the line 19 in the following code. package au.com.ojitha.ongl; import java.util.Map; import ognl.Ognl; import ognl.OgnlException; public class SecondExample { /** * @param args

OGNL Part 2: Navigation Chain

In the part 1 of this series explained how to setup the Eclipse project for testing. The navigation chain is the fundamental thing in the OGNL . See the following example. package au.com.ojitha.ongl; import ognl.Ognl; import ognl.OgnlException; public class FirstEcample { /** * @param args * @throws OgnlException */ public static void main(String[] args) throws OgnlException { Person p = new Person(); p.setName(new Name()); //set the value Ognl.setValue("name.firstName", p, "Ojitha"); Ognl.setValue("name.lastName", p, "Kumanayaka"); //retrieve the value System.out.println(Ognl.getValue("name.firstName+' '+name.lastName", p)); } } class Person { private Name name; public Name getName() { return name; } public void setName(Name name) { this.name = name; } } class Name { private String firstName; private String lastName; public String getFirstName() { return firstName; }

OGNL Part 1: Getting started

In this series, I am going to explain the OGNL Language Guide with code example. In this first part, let’ se how to setup the Maven project in Eclipse 3.6 and run the first example. Here the Maven POM file <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>au.com.ojitha</groupId> <artifactId>OgnalExample-1</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>ognl</groupId> <artifactId>ognl</artifactId> <version>3.0.2</version> </dependency> </dependencies> </project> Here the simplest example. package au.com.ojitha.ongl; import ognl.Ognl; import ognl.OgnlException; public