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, Class requiredType);

Source Link

Document

Evaluates the xpath, converts the result to the specified class and returns the resulting object.

Usage

From source file:org.chiba.xml.xforms.action.SetIndexAction.java

/**
 * Performs the <code>setindex</code> action.
 *
 * @return always <code>true</code>.
 * @throws XFormsException if an error occurred during <code>setindex</code>
 * processing./*from w  w  w .ja v  a 2s. c  o  m*/
 */
public boolean perform() throws XFormsException {
    // lookup repeat element
    Repeat repeat = (Repeat) this.container.lookup(this.repeatAttribute);

    // todo: check wether this interpretation is correct/useful and write a test for it
    String locationPath;
    if (isRepeated()) {
        // use the enclosing repeat as context
        RepeatItem repeatItem = (RepeatItem) getContainerObject().lookup(getRepeatItemId());
        locationPath = repeatItem.getLocationPath() + "[" + this.indexAttribute + "]";
    } else {
        // use the target repeat as context
        locationPath = repeat.getLocationPath() + "[" + this.indexAttribute + "]";
    }

    // get instance
    Instance instance = repeat.getModel().getInstance(repeat.getInstanceId());
    JXPathContext context = instance.getInstanceContext();

    int index;
    if (instance.existsNode(locationPath)) {
        // compute index from position
        String instancePath = context.getPointer(locationPath).asPath();
        index = PathUtil.stepIndex(PathUtil.lastStep(instancePath));
    } else {
        // compute index w/o context
        index = ((Integer) context.getValue(this.indexAttribute, java.lang.Integer.class)).intValue();
    }

    // check boundaries
    if (index < 1) {
        repeat.setIndex(1);
        this.container.dispatch(repeat.getTarget(), EventFactory.SCROLL_FIRST, null);
    } else if (index > repeat.getContextSize()) {
        index = repeat.getContextSize();
        repeat.setIndex(index);
        this.container.dispatch(repeat.getTarget(), EventFactory.SCROLL_LAST, null);
    } else {
        // set repeat index
        repeat.setIndex(index);
    }

    // update behaviour (defined by chiba ;-)
    setDeferredRefresh(repeat.getModel().getId(), true);

    // always indicate success
    return true;
}

From source file:org.chiba.xml.xforms.ui.test.RepeatTest.java

/**
 * Tests for correct prototype handling.
 *
 * @throws Exception if any error occurred during the test.
 *//*  ww w .j  a  va 2s.c  o m*/
public void testRepeatPrototype() throws Exception {
    JXPathContext context = JXPathContext.newContext(this.repeat.getElement());

    /*
    <chiba:data ... >
    <xforms:group chiba:transient="true" chiba:position="0">
        <xforms:input id="input" xforms:ref=".">
            <xforms:alert id="..."/>
        </xforms:input>
        <xforms:output id="output" xforms:ref="."/>
    </xforms:group>
    </chiba:data>
    */
    assertEquals("1", context.getValue("count(chiba:data/*)", Integer.class).toString());

    assertEquals("group", context.getValue("local-name(chiba:data/*[1])").toString());
    assertEquals(NamespaceCtx.XFORMS_NS, context.getValue("namespace-uri(chiba:data/*[1])").toString());
    assertEquals("true", context.getValue("chiba:data/xforms:group/@chiba:transient").toString());
    assertEquals("0", context.getValue("chiba:data/xforms:group/@chiba:position").toString());
    assertEquals("2", context.getValue("count(chiba:data/xforms:group/*)", Integer.class).toString());

    assertEquals("input", context.getValue("local-name(chiba:data/xforms:group/*[1])").toString());
    assertEquals(NamespaceCtx.XFORMS_NS,
            context.getValue("namespace-uri(chiba:data/xforms:group/*[1])").toString());
    assertEquals("input", context.getValue("chiba:data/xforms:group/xforms:input/@id").toString());
    assertEquals(".", context.getValue("chiba:data/xforms:group/xforms:input/@xforms:ref").toString());
    assertEquals("1",
            context.getValue("count(chiba:data/xforms:group/xforms:input/*)", Integer.class).toString());

    assertEquals("alert", context.getValue("local-name(chiba:data/xforms:group/xforms:input/*[1])").toString());
    assertEquals(NamespaceCtx.XFORMS_NS,
            context.getValue("namespace-uri(chiba:data/xforms:group/xforms:input/*[1])").toString());
    assertEquals(true, context.getValue("chiba:data/xforms:group/xforms:output/@id").toString().length() > 0);
    assertEquals("0",
            context.getValue("count(chiba:data/xforms:group/xforms:output/*)", Integer.class).toString());

    assertEquals("output", context.getValue("local-name(chiba:data/xforms:group/*[2])").toString());
    assertEquals(NamespaceCtx.XFORMS_NS,
            context.getValue("namespace-uri(chiba:data/xforms:group/*[2])").toString());
    assertEquals("output", context.getValue("chiba:data/xforms:group/xforms:output/@id").toString());
    assertEquals(".", context.getValue("chiba:data/xforms:group/xforms:output/@xforms:ref").toString());
    assertEquals("0",
            context.getValue("count(chiba:data/xforms:group/xforms:output/*)", Integer.class).toString());
}

From source file:org.chiba.xml.xforms.xpath.XFormsExtensionFunctions.java

/**
 * The avg() Function [7.7.1]./*from www.j  a va 2 s.com*/
 * <p/>
 * Function avg returns the arithmetic average of the result of
 * converting the string-values of each node in the argument node-set
 * to a number. The sum is computed with sum(), and divided with div
 * by the value computed with count().
 *
 * @param context the expression context.
 * @param nodeset the node-set.
 * @return the computed node-set average.
 */
public static double avg(ExpressionContext context, List nodeset) {
    if ((nodeset == null) || (nodeset.size() == 0)) {
        return Double.NaN;
    }

    JXPathContext rootContext = context.getJXPathContext();
    rootContext.getVariables().declareVariable("nodeset", nodeset);

    Double value = (Double) rootContext.getValue("sum($nodeset) div count($nodeset)", Double.class);

    return value.doubleValue();
}

From source file:org.chiba.xml.xforms.xpath.XFormsExtensionFunctions.java

/**
 * The max() Function [7.7.3]./*from   w w w . j a  v a 2  s.c  o m*/
 * <p/>
 * Function max returns the maximum value of the result of converting
 * the string-values of each node in argument node-set to a number.
 * "Maximum" is determined with the &gt; operator. If the parameter is
 * an empty node-set, the return value is NaN.
 *
 * @param context the expression context.
 * @param nodeset the node-set.
 * @return the computed node-set maximum.
 */
public static double max(ExpressionContext context, List nodeset) {
    if ((nodeset == null) || (nodeset.size() == 0)) {
        return Double.NaN;
    }

    JXPathContext rootContext = context.getJXPathContext();
    Object max = nodeset.get(0);

    for (int index = 1; index < nodeset.size(); index++) {
        Object current = nodeset.get(index);
        rootContext.getVariables().declareVariable("max", max);
        rootContext.getVariables().declareVariable("current", current);

        boolean more = ((Boolean) rootContext.getValue("number($current) > number($max)", Boolean.class))
                .booleanValue();

        if (more) {
            max = current;
        }
    }

    return (new Double(max.toString())).doubleValue();
}

From source file:org.chiba.xml.xforms.xpath.XFormsExtensionFunctions.java

/**
 * The min() Function [7.7.2]./*ww w  . j  av  a  2  s .c  o m*/
 * <p/>
 * Function min returns the minimum value of the result of converting
 * the string-values of each node in argument node-set to a number.
 * "Minimum" is determined with the &lt; operator. If the parameter is
 * an empty node-set, the return value is NaN.
 *
 * @param context the expression context.
 * @param nodeset the node-set.
 * @return the computed node-set minimum.
 */
public static double min(ExpressionContext context, List nodeset) {
    if ((nodeset == null) || (nodeset.size() == 0)) {
        return Double.NaN;
    }

    JXPathContext rootContext = context.getJXPathContext();
    Object min = nodeset.get(0);

    for (int index = 1; index < nodeset.size(); index++) {
        Object current = nodeset.get(index);
        rootContext.getVariables().declareVariable("min", min);
        rootContext.getVariables().declareVariable("current", current);

        boolean less = ((Boolean) rootContext.getValue("number($current) < number($min)", Boolean.class))
                .booleanValue();

        if (less) {
            min = current;
        }
    }

    return (new Double(min.toString())).doubleValue();
}

From source file:org.firesoa.common.jxpath.JXPathTestCase.java

protected void assertXPathValue(JXPathContext ctx, String xpath, Object expected, Class resultType) {
    ctx.setLenient(false);//w ww .j  a  v a  2  s.c  om
    Object actual = ctx.getValue(xpath, resultType);
    assertEquals("Evaluating <" + xpath + ">", expected, actual);
}

From source file:org.lilyproject.runtime.conf.test.InheritanceTest.java

public void testInheritance1() throws Exception {
    ConfImpl parent = loadConf("inherit1_parent.xml");

    // Child 1/*  w  ww.j ava 2 s.c  o  m*/
    {
        ConfImpl child = loadConf("inherit1_child1.xml");
        child.inherit(parent);

        JXPathContext context = JXPathContext.newContext(child);
        assertEquals(1, context.getValue("count(properties)", Integer.class));
        assertEquals(3, context.getValue("count(properties/property)", Integer.class));

        assertEquals("value1", context.getValue("properties/property[@key='key1']"));
        assertEquals("value2 - altered", context.getValue("properties/property[@key='key2']"));
        assertEquals("value3", context.getValue("properties/property[@key='key3']"));
    }

    // Child 2
    {
        ConfImpl child = loadConf("inherit1_child2.xml");
        child.inherit(parent);

        JXPathContext context = JXPathContext.newContext(child);
        assertEquals(1, context.getValue("count(properties)", Integer.class));
        assertEquals(4, context.getValue("count(properties/property)", Integer.class));

        assertEquals("value1", context.getValue("properties/property[@key='key1']"));
        assertEquals("value2 - altered", context.getValue("properties/property[@key='key2'][1]"));
        assertEquals("value2", context.getValue("properties/property[@key='key2'][2]"));
        assertEquals("value3", context.getValue("properties/property[@key='key3']"));
    }

    // Child 3
    {
        ConfImpl child = loadConf("inherit1_child3.xml");
        child.inherit(parent);

        JXPathContext context = JXPathContext.newContext(child);
        assertEquals(1, context.getValue("count(properties)", Integer.class));
        assertEquals(1, context.getValue("count(properties/property)", Integer.class));

        assertEquals("value3", context.getValue("properties/property[@key='key3']"));
    }

    // Child 4
    {
        ConfImpl child = loadConf("inherit1_child4.xml");
        child.inherit(parent);

        JXPathContext context = JXPathContext.newContext(child);
        assertEquals(0, context.getValue("count(*)", Integer.class));
    }
}

From source file:org.lilyproject.runtime.conf.test.InheritanceTest.java

public void testInheritance2() throws Exception {
    ConfImpl parent = loadConf("inherit2_parent.xml");

    // Child 1//from  w w w. jav  a  2  s.co  m
    {
        ConfImpl child = loadConf("inherit2_child1.xml");
        child.inherit(parent);

        JXPathContext context = JXPathContext.newContext(child);
        assertEquals(3, context.getValue("count(*)", Integer.class));

        assertEquals("val1", context.getValue("@attr1"));
        assertEquals("val2 - altered", context.getValue("@attr2"));

        assertEquals("ee", context.getValue("c/e"));

        context.setLenient(true);
        assertEquals(null, context.getValue("c/d"));
    }
}

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

/**
 * Determines if a node value is valid according to an associated
 * expression./*  w  w w  .  j  a va 2 s . c  o m*/
 *
 * @param context the assertion context
 * @return <tt>true</tt> if the expression is valid,
 *         otherwise <tt>false</tt>
 */
public static boolean validate(ActionContext context) {
    NamedProperty property = context.getProperty("expression");
    String expression = (String) property.getValue();
    ArchetypeDescriptor archetype = getArchetypeDescriptor(context.getNode());
    if (archetype != null) {
        expression = getExpression(archetype, expression);
    }
    JXPathContext pathContext = JXPathHelper.newContext(context.getParent());
    Object result = pathContext.getValue(expression, Boolean.class);
    return (result != null) && (Boolean) result;
}

From source file:org.openvpms.web.echo.style.StylePropertyEvaluator.java

/**
 * Evaluates an expression.//w w  w. j a va2s  .c  om
 *
 * @param context    the jxpath context
 * @param expression the xpath expression
 * @return the value of the expression
 */
private String evaluate(String expression, JXPathContext context) {
    String result = null;
    try {
        Object value = context.getValue(expression, Object.class);
        if (value instanceof Number) {
            result = String.valueOf(round((Number) value));
        } else if (value != null) {
            result = value.toString();
        }
    } catch (Throwable exception) {
        throw new StyleSheetException(StyleSheetException.ErrorCode.InvalidExpression, exception, expression);
    }
    return result;
}