tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 Tools and Libs > JXPath > How to create Function Library

How to create Function Library 

JXPath is a java library for Object Graph Navigation using the XPath syntax. This requires the libraries commons-jxpath-1.3.jar, commons-beanutils.jar and commons-logging.jar to be in classpath. The following example shows how to create a function library in JXPath.

File Name  :  
com/bethecoder/tutorials/jxpath/common/Student.java 
   
package com.bethecoder.tutorials.jxpath.common;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Student {

  private String name;
  private int age;
  private String hobby;
  private List<String> nickNames  = new ArrayList<String>();

  public Student() {
  }

  public Student(String name, int age, String hobby) {
    super();
    this.name = name;
    this.age = age;
    this.hobby = hobby;
  }
  
  public Student(String name, int age, String hobby, String [] nickNames) {
    super();
    this.name = name;
    this.age = age;
    this.hobby = hobby;
    this.nickNames = Arrays.asList(nickNames);
  }

  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  public String getHobby() {
    return hobby;
  }
  public void setHobby(String hobby) {
    this.hobby = hobby;
  }

  public List<String> getNickNames() {
    return nickNames;
  }

  public void setNickNames(List<String> nickNames) {
    this.nickNames = nickNames;
  }
  
  public String toString() {
    return "Student[name = " + name + ", age = " + age + ", hobby = " + hobby + ", " + nickNames + "]";
  }

}
   

File Name  :  
com/bethecoder/tutorials/jxpath/functions/CaseFormatter.java 
   
package com.bethecoder.tutorials.jxpath.functions;

public class CaseFormatter {
  
  public static String upper(String str) {
    return str.toUpperCase();
  }
  
  public static String lower(String str) {
    return str.toLowerCase();
  }
}
   

File Name  :  
com/bethecoder/tutorials/jxpath/MultipleFunctionsTest.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.jxpath;

import org.apache.commons.jxpath.ClassFunctions;
import org.apache.commons.jxpath.FunctionLibrary;
import org.apache.commons.jxpath.JXPathContext;

import com.bethecoder.tutorials.jxpath.common.Student;
import com.bethecoder.tutorials.jxpath.functions.CaseFormatter;

public class MultipleFunctionsTest {

  /**
   @param args
   */
  public static void main(String[] args) {
    Student std = new Student("Sriram"2"Singing");
    JXPathContext context = JXPathContext.newContext(std);
    
    //Groups different Functions objects
    FunctionLibrary functionLibrary = new FunctionLibrary();
    
    //Register static methods in CaseFormatter as functions for namespace "case"
    functionLibrary.addFunctions(new ClassFunctions(CaseFormatter.class, "case"));
    
    //Register static methods in Math as functions for namespace "math"
    functionLibrary.addFunctions(new ClassFunctions(Math.class, "math"));
    
    context.setFunctions(functionLibrary);
    context.getVariables().declareVariable("a"2.0d);
    context.getVariables().declareVariable("b"4.0d);

    //Access functions from "case" namespace    
    System.out.println(context.getValue("case:upper('be the coder')"));
    System.out.println(context.getValue("case:upper(name)"));
    System.out.println(context.getValue("case:lower('BE THE CODER')"));
    System.out.println(context.getValue("case:lower(name)"));
    
    //Access functions from "math" namespace
    System.out.println(context.getValue("math:pow($a, $b)"));
    System.out.println(context.getValue("math:sqrt(144.0)"));
    System.out.println(context.getValue("math:log(1729)"));
    System.out.println(context.getValue("math:random()"));
  }
}
   

It gives the following output,
BE THE CODER
SRIRAM
be the coder
sriram

16.0
12.0
7.455298485683291
0.8809938129091442


                    Print

   



Books Recommendation

 
 
bl  br