Example usage for org.apache.commons.jxpath FunctionLibrary addFunctions

List of usage examples for org.apache.commons.jxpath FunctionLibrary addFunctions

Introduction

In this page you can find the example usage for org.apache.commons.jxpath FunctionLibrary addFunctions.

Prototype

public void addFunctions(Functions functions) 

Source Link

Document

Add functions to the library

Usage

From source file:org.openvpms.archetype.function.factory.ArchetypeFunctionsFactory.java

/**
 * Creates a new {@code FunctionLibrary} containing functions that use specified {@link IArchetypeService}.
 *
 * @param service the archetype service/* w  w w  .  j  a va  2s  .  c o  m*/
 * @return the functions
 */
public FunctionLibrary create(IArchetypeService service) {
    ILookupService lookups = getLookupService();
    PatientAgeFormatter formatter = getPatientAgeFormatter();

    PracticeRules rules = new PracticeRules(service, getCurrencies());
    PatientRules patientRules = new PatientRules(rules, service, lookups, formatter);
    CustomerRules customerRules = new CustomerRules(service, lookups);
    ReminderRules reminderRules = new ReminderRules(service, patientRules);
    FunctionLibrary library = new FunctionLibrary();
    library.addFunctions(create("date", DateFunctions.class));
    library.addFunctions(new ExpressionFunctions("expr"));
    library.addFunctions(create("history", new HistoryFunctions(service)));
    library.addFunctions(create("list", new ListFunctions(service, lookups)));
    library.addFunctions(create("lookup", LookupFunctions.class));
    library.addFunctions(create("math", new MathFunctions()));
    library.addFunctions(create("openvpms", new ArchetypeServiceFunctions(service, lookups)));
    library.addFunctions(create("party", new PartyFunctions(service, lookups, patientRules)));
    library.addFunctions(create("reminder", new ReminderFunctions(service, reminderRules, customerRules)));
    library.addFunctions(create("word", WordUtils.class));
    return library;
}

From source file:org.openvpms.archetype.function.history.HistoryFunctionsTestCase.java

/**
 * Creates a new {@code JXPathContext} with the history functions registered.
 *
 * @param object the context object//from   w ww. ja va  2  s .c om
 * @return a new JXPath context
 */
private JXPathContext createContext(IMObject object) {
    HistoryFunctions history = new HistoryFunctions(getArchetypeService());
    ArchetypeServiceFunctions functions = new ArchetypeServiceFunctions(getArchetypeService(),
            getLookupService());
    FunctionLibrary library = new FunctionLibrary();
    library.addFunctions(new ObjectFunctions(history, "history"));
    library.addFunctions(new ObjectFunctions(functions, "openvpms"));
    return JXPathHelper.newContext(object, library);
}

From source file:org.openvpms.archetype.function.list.ListFunctionsTestCase.java

/**
 * Creates a new JXPathContext./*w  w  w.j a v a 2 s .c om*/
 *
 * @param object the context object
 * @return a new JXPathContext
 */
private JXPathContext createContext(Object object) {
    ListFunctions listFunctions = new ListFunctions(getArchetypeService(), getLookupService());
    FunctionLibrary library = new FunctionLibrary();
    library.addFunctions(new ObjectFunctions(listFunctions, "list"));
    library.addFunctions(new ExpressionFunctions("expr"));
    return JXPathHelper.newContext(object, library);
}

From source file:org.openvpms.archetype.function.party.PartyFunctionsTestCase.java

/**
 * Creates a new JXPathContext, with the party functions registered.
 *
 * @param object the context object/*from   ww  w  .j a va2  s . com*/
 * @return a new JXPathContext
 */
private JXPathContext createContext(IMObject object) {
    IArchetypeService service = getArchetypeService();
    ILookupService lookups = getLookupService();
    ArchetypeServiceFunctions functions = new ArchetypeServiceFunctions(service, lookups);
    PartyFunctions partyFunctions = new PartyFunctions(service, lookups,
            new PatientRules(null, service, lookups));
    FunctionLibrary library = new FunctionLibrary();
    library.addFunctions(new ObjectFunctions(functions, "openvpms"));
    library.addFunctions(new ObjectFunctions(partyFunctions, "party"));
    return JXPathHelper.newContext(object, library);
}

From source file:org.openvpms.component.system.common.jxpath.JXPathHelper.java

/**
 * Create a new context for the specified object that has access to the supplied functions.
 *
 * @param object    the context bean//from   w w w. jav  a 2  s .co m
 * @param functions the functions
 * @return JXPathContext the context object
 */
public static JXPathContext newContext(Object object, Functions functions) {
    JXPathContext context = JXPathContext.newContext(object);
    FunctionLibrary lib = new FunctionLibrary();
    lib.addFunctions(context.getFunctions());
    lib.addFunctions(functions);
    context.setFunctions(lib);
    context.setLenient(true);

    return context;
}

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

/**
 * Test the JXPath expressions for retrieving an object with an id
 * from a collection/*from  w w w .j ava  2 s  . co  m*/
 */
@Test
public void testJXPathSearchCollectionForMatchingUid() {
    List<Party> list = new ArrayList<Party>();
    Party person = createPerson("MR", "Jim", "Alateras");
    person.setId(1);
    list.add(person);

    person = createPerson("MS", "Bernadette", "Feeney");
    person.setId(2);
    list.add(person);

    person = createPerson("MS", "Grace", "Alateras");
    person.setId(3);
    list.add(person);

    JXPathContext ctx = JXPathHelper.newContext(list);
    // NOTE: Using a extension function to do the work.
    assertTrue(ctx.getValue(
            "org.openvpms.component.system.service.jxpath.TestFunctions.findObjectWithUid(., 1)") != null);
    assertTrue(ctx.getValue(
            "org.openvpms.component.system.service.jxpath.TestFunctions.findObjectWithUid(., 3)") != null);
    assertTrue(ctx.getValue(
            "org.openvpms.component.system.service.jxpath.TestFunctions.findObjectWithUid(., 4)") == null);
    assertTrue(ctx.getValue(
            "org.openvpms.component.system.service.jxpath.TestFunctions.findObjectWithUid(., 0)") == null);

    // execute the same test using function namespaces
    FunctionLibrary lib = new FunctionLibrary();
    lib.addFunctions(new ClassFunctions(TestFunctions.class, "collfunc"));
    ctx.setFunctions(lib);
    assertTrue(ctx.getValue("collfunc:findObjectWithUid(., 1)") != null);
    assertTrue(ctx.getValue("collfunc:findObjectWithUid(., 3)") != null);
    assertTrue(ctx.getValue("collfunc:findObjectWithUid(., 4)") == null);
    assertTrue(ctx.getValue("collfunc:findObjectWithUid(., 0)") == null);
    assertTrue(ctx.getValue("collfunc:findObjectWithUid(., 1)/name") != null);
}

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

/**
 * Test that we can sum correctly over a list of objects
 *//*from  w w w .  j av  a 2 s  .com*/
@Test
public void testSumOverBigDecimal() {
    FunctionLibrary lib = new FunctionLibrary();
    lib.addFunctions(new ClassFunctions(TestFunctions.class, "ns"));

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

    JXPathContext ctx = JXPathHelper.newContext(values);
    ctx.setFunctions(lib);

    Object sum = ctx.getValue("ns:sum(child::high)");
    assertTrue(sum.getClass() == BigDecimal.class);
    sum = ctx.getValue("ns:sum(child::low)");
    assertTrue(sum.getClass() == BigDecimal.class);
}

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  . ja v  a2  s .  c om*/
 */
@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   w  ww. j  av  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.openvpms.macro.impl.MacroFunctionsTestCase.java

/**
 * Sets up the test case./*from w  ww  .ja  va2s .co  m*/
 */
@Before
public void setUp() {
    MacroTestHelper.createMacro("displayName", "openvpms:get(., 'displayName')");
    MacroTestHelper.createMacro("numbertest", "concat('input number: ', $number)");

    final IArchetypeService service = getArchetypeService();
    functions = new DefaultArchetypeFunctionsFactory(service, getLookupService(), null, null) {
        @Override
        public FunctionLibrary create(IArchetypeService service) {
            FunctionLibrary library = super.create(service);
            library.addFunctions(create("macro", new MacroFunctions(macros)));
            return library;
        }
    };

    ReportFactory factory = new ReportFactory(service, getLookupService(), new DocumentHandlers(), functions);
    macros = new LookupMacros(getLookupService(), service, factory);
}