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:com.technofovea.hl2parse.JxPathUtil.java

/**
 * Adds custom functions onto a context which correspond to
 * static methods on this class, such as {@link #startswith(java.lang.String, java.lang.String)}
 * @param context The context to alter./*  ww w  . ja  va  2  s . c  o  m*/
 */
public static void addFunctions(JXPathContext context) {
    context.setFunctions(new ClassFunctions(JxPathUtil.class, "custom"));
}

From source file:de.tudarmstadt.ukp.dkpro.core.tokit.TokenMergerTest.java

@SuppressWarnings("unchecked")
public static List<Object> pick(Collection<?> aContext, String aPath) {
    List<Object> result = new ArrayList<Object>();
    for (Object a : aContext) {
        JXPathContext ctx = JXPathContext.newContext(a);
        ctx.setFunctions(new ClassFunctions(JXPathCasFunctions.class, "cas"));
        result.addAll(ctx.selectNodes(aPath));
    }//from w ww  . j a v  a  2s.c om
    return result;
}

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

/**
 * Actually add global functions and packages as well as those
 * listed in the configuration object./*from   w  w w  .  j a  va2s  . c  o m*/
 *
 * @param context a <code>JXPathContext</code> value
 * @param conf a <code>Configuration</code> value holding local
 * packages and functions.
 */
private static void setup(JXPathHelperConfiguration setup, JXPathContext context, Configuration conf)
        throws ConfigurationException {

    // Create local config (if necessary)
    JXPathHelperConfiguration local = conf == null ? setup : new JXPathHelperConfiguration(setup, conf);

    // Setup context with local config
    context.setLenient(setup.isLenient());
    context.setFunctions(local.getLibrary());
    if (local.getNamespaces() != null) {
        for (Iterator i = local.getNamespaces().entrySet().iterator(); i.hasNext();) {
            final Map.Entry entry = (Map.Entry) i.next();
            context.registerNamespace((String) entry.getKey(), (String) entry.getValue());
        }
    }
}

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

/**
 * Performs the <code>setvalue</code> action.
 *
 * @return <code>true</code> if setvalue executed successfully, otherwise
 * <code>false</code>./* ww  w. j  a  v  a  2s  .  c  o  m*/
 * @throws XFormsException if an error occurred during <code>setvalue</code>
 * processing.
 */
public boolean perform() throws XFormsException {
    // get location path and instance id
    String locationPath = getLocationPath();
    String instanceId = getInstanceId(locationPath);

    // get instance
    Instance instance = getModel().getInstance(instanceId);

    if (instance.existsNode(locationPath)) {
        if (this.valueAttribute != null) {
            // evaluate value expression
            JXPathContext context = instance.getInstanceContext();
            Pointer instancePointer = context.getPointer(locationPath);
            JXPathContext valueContext = context.getRelativeContext(instancePointer);
            valueContext.setFunctions(context.getFunctions());
            Object value = valueContext.getValue(this.valueAttribute);
            String newValue = value != null ? value.toString() : "null";

            if (getLogger().isDebugEnabled()) {
                getLogger().debug(this + " perform: setting evaluated value '" + newValue + "'");
            }

            // set node value
            instance.setNodeValue(locationPath, newValue);
        } else {
            if (getLogger().isDebugEnabled()) {
                getLogger().debug(this + " perform: setting literal value '" + this.nodeValue + "'");
            }

            // set node value
            instance.setNodeValue(locationPath, this.nodeValue);
        }

        // update behaviour
        setDeferredRecalculate(this.model.getId(), true);
        setDeferredRevalidate(this.model.getId(), true);
        setDeferredRefresh(this.model.getId(), true);

        // indicate success
        return true;
    }
    // todo: error handling ?
    getLogger().warn(this + " perform: nodeset " + getLocationPath() + " does not exist");

    // indicate failure
    return false;
}

From source file:org.chiba.xml.xforms.constraints.MainDependencyGraph.java

/**
 * builds the dependency graph for a single Bind. Processes all instancenodes that are associated with
 * the bind and creates one Vertex-object for every Modelitem Property found. That means that if there are
 * two instancenodes in the evaluated nodeset, two Vertices for every property (readonly, required, relevant,
 * constraint, calculate) will be created.
 * <p/>/*from w ww.j  a  v a  2s.co m*/
 * Note: only dynamic Modelitem Properties will be processed.
 */
public void buildBindGraph(Bind bind, Model model) {
    Instance instance;
    String locationPath = bind.getLocationPath();

    instance = model.getInstance(PathUtil.getInstanceId(model, locationPath));

    if (instance == null) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("ignoring " + bind);
        }

        // no instance - no dependencies ;-)
        return;
    }

    JXPathContext instanceContext = instance.getInstanceContext();
    Iterator iterator = instance.getPointerIterator(locationPath);

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("processing " + bind + " nodeset='" + bind.getBindingExpression() + "'");
        LOGGER.debug("locationPath=" + locationPath);
    }

    while (iterator.hasNext()) {
        Pointer instancePointer = (Pointer) iterator.next();
        JXPathContext relativeContext = instanceContext.getRelativeContext(instancePointer);
        relativeContext.setFunctions(instanceContext.getFunctions());

        String s = instancePointer.asPath();
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("processing path:" + s);
        }
        ModelItem modelItem = instance.getModelItem(s);
        NodeImpl node = (NodeImpl) modelItem.getNode();

        if (bind.hasCalculate()) {
            modelItem.setCalculate(bind.getCalculate());
            this.addReferredNodesToGraph(relativeContext, node, bind.getCalculate(), Vertex.CALCULATE_VERTEX);
        }

        if (bind.hasRelevant()) {
            modelItem.setRelevant(bind.getRelevant());
            this.addReferredNodesToGraph(relativeContext, node, bind.getRelevant(), Vertex.RELEVANT_VERTEX);
        }

        if (bind.hasReadonly()) {
            modelItem.setReadonly(bind.getReadonly());
            this.addReferredNodesToGraph(relativeContext, node, bind.getReadonly(), Vertex.READONLY_VERTEX);
        }

        if (bind.hasRequired()) {
            modelItem.setRequired(bind.getRequired());
            this.addReferredNodesToGraph(relativeContext, node, bind.getRequired(), Vertex.REQUIRED_VERTEX);
        }

        if (bind.hasConstraint()) {
            modelItem.setConstraint(bind.getConstraint());
            this.addReferredNodesToGraph(relativeContext, node, bind.getConstraint(), Vertex.CONSTRAINT_VERTEX);
        }

        if (bind.hasDatatype()) {
            modelItem.setDatatype(bind.getDatatype());
        }

        if (bind.hasP3PType()) {
            modelItem.setP3PType(bind.getP3PType());
        }
    }
}

From source file:org.chiba.xml.xforms.xpath.test.ExtensionFunctionsTest.java

public void testInstance() throws Exception {
    Document inDocument = getXmlResource("instance-test.xml");

    ChibaBean chibaBean = new ChibaBean();
    chibaBean.setXMLContainer(inDocument);
    chibaBean.init();//  w  w w .ja va  2 s. c om

    //        XPathExtensionFunctions functions = new XPathExtensionFunctions(chibaBean.getContainer().getDefaultModel());
    XPathExtensionFunctions functions = new XPathExtensionFunctions();
    functions.setNamespaceContext(chibaBean.getContainer().getDefaultModel().getDefaultInstance().getElement());

    JXPathContext context = chibaBean.getContainer().getDefaultModel().getDefaultInstance()
            .getInstanceContext();
    context.setFunctions(functions);

    Object o = context.getValue("instance('first')/some-dummy");
    assertTrue(o.equals("some dummy value"));

    o = context.getValue("xforms:instance('first')/some-dummy");
    assertTrue(o.equals("some dummy value"));

    o = context.getValue("instance('second')/.");
    assertTrue(o.equals("another dummy value"));

    Pointer pointer = context.getPointer("instance('second')/.");
    assertEquals("another dummy value", pointer.getValue());
    assertEquals("/another-dummy[1]", pointer.asPath());

    o = context.getValue("xforms:instance('second')/.");
    assertTrue(o.equals("another dummy value"));
}

From source file:org.commonjava.maven.galley.maven.parse.JXPathUtils.java

public static JXPathContext newContext(final Node node) {
    final JXPathContext ctx = JXPathContext.newContext(node);
    ctx.setLenient(true);/*www . ja va  2s.  c o  m*/
    ctx.setFunctions(new ClassFunctions(ResolveFunctions.class, "ext"));

    return ctx;
}

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 .j  ava2  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//ww  w.  j  a v  a 2  s  .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
 *//*from ww w  .j a va2 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);
}