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

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

Introduction

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

Prototype

public abstract Object getValue(String xpath);

Source Link

Document

Evaluates the xpath and returns the resulting object.

Usage

From source file:org.openvpms.component.business.service.archetype.ArchetypeServiceFunctionsTestCase.java

/**
 * Verifies that a lookup expression evaluates to the expected result.
 *
 * @param expected the expected result//w w  w . j  a  va  2 s  .co m
 * @param node     the exoression node
 * @param context  the context
 */
private void checkLookup(String expected, String node, JXPathContext context) {
    String expression = "openvpms:lookup(., '" + node + "')";
    assertEquals(expected, context.getValue(expression));
}

From source file:org.openvpms.component.business.service.archetype.helper.IMObjectBean.java

/**
 * Evaluates a JXPath expression.//  w  w  w .  j  av a  2  s  .c  o m
 *
 * @param expression the expression
 * @return the result of the expression
 */
private Object evaluate(String expression) {
    Object result;
    JXPathContext context = JXPathHelper.newContext(object);
    result = context.getValue(expression);
    return result;
}

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

/**
 * Test that packaged functions work as expects
 *//*from w  ww  .j  a  va 2  s  .  co  m*/
@Test
public void testPackagedFunctions() {
    JXPathContext ctx = JXPathHelper.newContext(this);
    assertTrue(ctx.getValue("java.util.Date.new()") instanceof Date);
    assertTrue(ctx.getValue("'jimbo'").equals("jimbo"));
}

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

/**
 * Test the JXPath expressions into collections
 *//*w  w  w  .  j av a  2 s  .  co  m*/
@Test
public void testJXPathCollectionExpressions() {
    List<Party> list = new ArrayList<Party>();
    list.add(createPerson("MR", "Jim", "Alateras"));
    list.add(createPerson("MS", "Bernadette", "Feeney"));
    list.add(createPerson("MS", "Grace", "Alateras"));

    JXPathContext ctx = JXPathHelper.newContext(list);
    // NOTE: Index starts at 1 not 0.
    assertTrue(ctx.getValue(".[1]/details/firstName").equals("Jim"));
    assertTrue(ctx.getValue(".[2]/details/lastName").equals("Feeney"));
    assertTrue(ctx.getValue(".[3]/details/title").equals("MS"));
}

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 av  a  2s .c o 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
 *//* www  .j a va 2  s.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//www . j  a va 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

/**
 * Test that the extension functions are called through JXPathHelper
 *//*from w w w .ja  v a2 s  . c  o  m*/
@Test
public void testJXPathHelperExtensionFunctions() {
    // prepare the helper
    Map<String, Object> props = new HashMap<String, Object>();
    props.put("tf", TestFunctions.class.getName());
    new JXPathHelper(props);

    Party person = new Party();
    person.setName("Mr Jim Alateras");

    JXPathContext context = JXPathHelper.newContext(person);
    Boolean bool = (Boolean) context.getValue("tf:testName(.)");
    assertTrue(bool);

    person.setName(null);
    bool = (Boolean) context.getValue("tf:testName(.)");
    assertFalse(bool);
}

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

/**
 * Test for bug OVPMD-210//  w w  w.  j a  va 2 s.  c o m
 */
@Test
public void testOVPMS210() {
    BigDecimalValues values = new BigDecimalValues(new BigDecimal(100), new BigDecimal(200));
    JXPathContext ctx = JXPathHelper.newContext(values);
    Object obj = ctx.getValue("/low + /high");
    assertTrue(obj instanceof BigDecimal);
    assertTrue(obj.equals(new BigDecimal(300)));
}

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

/**
 * Test normal maths operations//from w  ww  .j  a  v  a2  s  .c  o m
 */
@Test
public void testJXPathMaths() {
    JXPathContext ctx = JXPathHelper.newContext(new Object());
    ctx.getValue("2 + 2");
    ctx.getValue("2 - 2");
    ctx.getValue("2 div 2");
    ctx.getValue("2 * 2");
    ctx.getValue("-2");
    ctx.getValue("2 mod 2");
}