码迷,mamicode.com
首页 > 其他好文 > 详细

JXPath(1.3) - Extension Functions

时间:2015-08-05 00:45:11      阅读:152      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!