Example usage for org.apache.commons.jxpath JXPathContext setFunctions

List of usage examples for org.apache.commons.jxpath JXPathContext setFunctions

Introduction

In this page you can find the example usage for org.apache.commons.jxpath JXPathContext setFunctions.

Prototype

public void setFunctions(Functions functions) 

Source Link

Document

Install a library of extension functions.

Usage

From source file:org.openvpms.component.system.service.jxpath.JXPathTestCase.java

/**
 * Test that we can still sum using a conversion function in the
 * expression//from   w w w  .  j av a 2s  .c o m
 */
@Test
public void testSumOverDouble() {
    FunctionLibrary lib = new FunctionLibrary();
    lib.addFunctions(new ClassFunctions(TestFunctions.class, "ns"));

    List<DoubleValues> values = new ArrayList<DoubleValues>();
    values.add(new DoubleValues(12, 12));
    values.add(new DoubleValues(13, 13));
    values.add(new DoubleValues(15, 15));

    Map<String, Object> map = new HashMap<String, Object>();
    map.put("values", values);
    JXPathContext ctx = JXPathHelper.newContext(map);
    ctx.setFunctions(lib);

    //Object sum = ctx.getValue("sum(values/child::high[self::high < 0])");
    Object sum = ctx.getValue("ns:sum(ns:toBigDecimalValues(values)/child::high)");
    assertTrue(sum.getClass() == BigDecimal.class);
    //sum = ctx.getValue("ns:sum(ns:toBigDecimal(child::low))");
    //assertTrue(sum.getClass() == BigDecimal.class);
}

From source file:org.openvpms.component.system.service.jxpath.JXPathTestCase.java

/**
 * Verifies that {@link ObjectFunctions} can be added to the function
 * library and their instance and static methods invoked.
 *//*from  ww w. j a  v a  2  s  .  c o  m*/
@Test
public void testObjectFunctions() {
    List<String> list = new ArrayList<String>();
    list.add("a");
    list.add("b");
    list.add("c");
    JXPathContext ctx = JXPathHelper.newContext(list);
    FunctionLibrary lib = new FunctionLibrary();
    lib.addFunctions(new ObjectFunctions(new TestFunctions(), "test"));
    ctx.setFunctions(lib);

    // test instance methods
    assertEquals("a", ctx.getValue("test:getValue(., 0)"));
    assertEquals("c", ctx.getValue("test:getValue(., 2)"));
    assertEquals("foo", ctx.getValue("test:getValue()"));

    // test static methods
    assertEquals("Jimmy", ctx.getValue("test:getContacts()"));
}

From source file:org.paxml.core.Context.java

private void setXpathFunctions(JXPathContext xpathContext) {
    Functions existing = xpathContext.getFunctions();
    final FunctionLibrary funcLib;
    if (existing == null) {
        funcLib = new FunctionLibrary();
    } else if (existing instanceof FunctionLibrary) {
        funcLib = (FunctionLibrary) existing;
    } else {// ww w.ja  v  a2  s .  com
        funcLib = new FunctionLibrary();
        funcLib.addFunctions(existing);
    }

    for (ITagLibrary lib : getPaxml().getParser().getTagLibraries()) {
        for (String name : lib.getUtilFunctionsFactoryNames()) {
            Class<? extends IUtilFunctionsFactory> clazz = lib.getUtilFunctionsFactory(name);
            Class<?> xpathFunClass = ReflectUtils.createObject(clazz).getXpathUtilFunctions(this);
            if (xpathFunClass == null) {
                // skip this one
                continue;
            }
            Util util = ReflectUtils.getAnnotation(clazz, Util.class);
            if (util == null) {
                throw new PaxmlRuntimeException(
                        "Internal error: util function factory is not annotated: " + clazz.getName());
            }
            funcLib.addFunctions(new ClassFunctions(xpathFunClass, util.value()));
        }
    }

    xpathContext.setFunctions(funcLib);
}

From source file:org.xsystem.bpm2machineservice.impl.ScriptingService.java

public JXPathContext makeJXPathContext(Map<String, Object> data) {
    JXPathContext context = JXPathContext.newContext(data);
    FunctionLibrary lib = new FunctionLibrary();
    lib.addFunctions(new ClassFunctions(CalendarHelper.class, "calendarhelper"));
    lib.addFunctions(new ClassFunctions(Calendar.class, "calendar"));

    context.setFunctions(lib);

    return context;
}

From source file:org.xsystem.sql2.http.PageServlet2.java

static Map<String, Object> getContext(Map<String, String> evals, Map<String, Object> reqContext) {
    JXPathContext context = JXPathContext.newContext(reqContext);
    context.setFunctions(new ClassFunctions(Base64Decode.class, "BASE64"));
    context.setLenient(true);//  w  w  w  .j av a2s.  co  m

    Map ret = new HashMap();
    evals.entrySet().forEach(entry -> {
        String key = entry.getKey();
        String eval = entry.getValue();
        Object value = context.getValue(eval);
        ret.put(key, value);
    });

    return ret;
}