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

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

Introduction

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

Prototype

FunctionLibrary

Source Link

Usage

From source file:org.apache.cocoon.components.modules.input.JXPathHelperConfiguration.java

/**
 * Create root jxpath configuration//www.j  av a 2 s . c o m
 */
public JXPathHelperConfiguration(Configuration config) throws ConfigurationException {
    this.lenient = config.getChild("lenient").getValueAsBoolean(true);
    this.library = new FunctionLibrary();
    setup(config);

    // the following is necessary to be able to use methods on objects without
    // explicitely registering extension functions (see PackageFunctions javadoc)
    this.library.addFunctions(new PackageFunctions("", null));
}

From source file:org.apache.cocoon.components.modules.input.JXPathHelperConfiguration.java

/**
 * Create child jxpath configuration/*ww  w  .j av  a 2  s . c o m*/
 */
public JXPathHelperConfiguration(JXPathHelperConfiguration global, Configuration config)
        throws ConfigurationException {
    this.lenient = global.lenient;
    this.library = new FunctionLibrary();
    this.library.addFunctions(global.getLibrary());
    if (global.getNamespaces() != null) {
        this.namespaces = new HashMap(global.getNamespaces());
    }
    setup(config);
}

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 ww  . java2  s  . 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 ww  w. ja  v a2s . com*/
 * @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 ww.  j av  a2s . co m
 *
 * @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   www . j  a v  a2s  . co m
 * @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//w  w w.  j  a v a2  s.  c om
 * @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// w w  w . j ava 2s.com
 */
@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
 *///w w  w  . j ava2s .  c  o m
@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/*w  w  w. j a  va 2 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);
}