Commons JXPath - Extension Functions

Standard Extension Functions

创建新的对象

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())");

调用静态方法

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

调用普通方法

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')");

 

转载于:https://www.cnblogs.com/huey/p/4703383.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值