首页 > 其他 > 详细

JXPath(1.3) - Extension Functions

时间:2015-08-05 00:44:14      阅读:312      评论:0      收藏:0      [点我收藏+]

Standard Extension Functions

create new objects

JXPathContext context = JXPathContext.newContext(null);
Book book = (Book) context.getValue("com.huey.jxpath.Book.new()");
Author author = (Author) context.getValue("com.huey.jxpath.Author.new(‘Eric‘, ‘Freeman‘, ‘F‘, java.util.Date.new())");

call static methods

JXPathContext context = JXPathContext.newContext(null);
Book bestBook = (Book) context.getValue("com.huey.jxpath.Book.getBestBook()");

call regular methods

the target of the method is specified as the first parameter of the function.

JXPathContext context = JXPathContext.newContext(null);
context.getVariables().declareVariable("book", book);
String title = (String) context.getValue("getTitle($book)");

 

Custom Extension Functions

定义一个格式化类:

package com.huey.jxpath;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MyFormats {
    public static String dateToStr(Date d, String pattern){
        return new SimpleDateFormat(pattern).format(d);
    }    
}

将格式化类注册到 JXPathContext:

JXPathContext context = JXPathContext.newContext(null);
context.setFunctions(new ClassFunctions(MyFormats.class, "formats"));
context.getVariables().declareVariable("today", new Date());
String today = (String) context.getValue("formats:dateToStr($today, ‘yyyy-MM-dd‘)");

 

JXPath(1.3) - Extension Functions

原文:http://www.cnblogs.com/huey/p/4703383.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!