tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Tools and Libs > JXPath > How to use Extension Predicate

How to use Extension Predicate 

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 accessing extension function as predicate in JXPath.

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

import java.util.List;

public class ComplexBean {

  private List<String> stringList;
  private String [] stringArray;
  
  public List<String> getStringList() {
    return stringList;
  }
  public void setStringList(List<String> stringList) {
    this.stringList = stringList;
  }
  public String[] getStringArray() {
    return stringArray;
  }
  public void setStringArray(String[] stringArray) {
    this.stringArray = stringArray;
  }

}
   

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

import org.apache.commons.jxpath.ExpressionContext;

public class IndexFunction {
  
  public static boolean odd(ExpressionContext expressionContext) {
    //System.out.println(expressionContext.getPosition());
    //System.out.println(expressionContext.getContextNodePointer());
    //System.out.println(expressionContext.getContextNodePointer().asPath());
    
    return expressionContext.getPosition() == 1;
  }
  
  public static boolean even(ExpressionContext expressionContext) {
    return expressionContext.getPosition() == 0;
  }
}
   

File Name  :  
com/bethecoder/tutorials/jxpath/ExtensionPredicateTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.jxpath;

import java.util.Arrays;
import java.util.Iterator;

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

import com.bethecoder.tutorials.jxpath.common.ComplexBean;
import com.bethecoder.tutorials.jxpath.functions.IndexFunction;

public class ExtensionPredicateTest {

  /**
   @param args
   */
  public static void main(String[] args) {
    ComplexBean bean = new ComplexBean();
    bean.setStringList(Arrays.asList("Maths""Physics""Chemistry""Hindi""English"));

    //Create JXPathContext with ComplexBean instance as ROOT node
      JXPathContext context = JXPathContext.newContext(bean);
      
      //Register a class function in the name space 'index'
      context.setFunctions(new ClassFunctions(IndexFunction.class, "index"));
      
      //Get elements at ODD index
      Iterator<?> oddVals = context.iterate("/stringList[index:odd()]");
      while (oddVals.hasNext()) {
        System.out.println(oddVals.next());
      }
      
      //Get elements at EVEN index      
      Iterator<?> evenVals = context.iterate("/stringList[index:even()]");
      while (evenVals.hasNext()) {
        System.out.println(evenVals.next());
      }
  }

}
   

It gives the following output,
Maths
Chemistry
English

Physics
Hindi



 
  


  
bl  br