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

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

Introduction

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

Prototype

public Variables getVariables() 

Source Link

Document

Returns the variable pool associated with the context.

Usage

From source file:org.xchain.namespaces.core.TestJXPathContext.java

private boolean isDeclaredInCurrentOrParent(JXPathContext pathContext, String variableName) {
    while (pathContext != null) {
        // Check if the variable is declared in the current context.
        if (!pathContext.getVariables().isDeclaredVariable(variableName)) {
            // Variable not declared in this context.  Move to the parent.
            pathContext = pathContext.getParentContext();
        } else {/*from  w w w  . j  a  v  a2s  . c om*/
            // Variable is declared in this context.
            return true;
        }
    }

    // Variable not found in any context.
    return false;
}

From source file:org.xchain.namespaces.core.TestVariableCommand.java

@Test
public void testModify() throws Exception {
    // Declare the variable at the 'request' level.
    JXPathContext requestContext = context;
    // Dig up to the 'request' context.
    while (requestContext.getParentContext() != null)
        requestContext = requestContext.getParentContext();

    // Declare the variable.
    requestContext.getVariables().declareVariable(RESULT, "string1");

    // Get the command.
    Command command = catalog.getCommand(MODIFY_COMMAND);

    // execute the command.
    command.execute(context);//from w  w  w.  jav  a  2s .co  m

    // Get the result.
    String resultString = (String) context.getValue("$" + RESULT, String.class);

    assertEquals("The modification of a declared variable failed.", "string2", resultString);
}

From source file:org.xchain.namespaces.core.TestVariableCommand.java

@Test
public void testRequestModify() throws Exception {
    // Declare the variable at the 'request' level.
    JXPathContext requestContext = context;
    // Dig up to the 'request' context.
    while (requestContext.getParentContext() != null)
        requestContext = requestContext.getParentContext();

    // Declare the variable.
    requestContext.getVariables().declareVariable(RESULT, "string1");

    // Get the command.
    Command command = catalog.getCommand(REQUEST_MODIFY_COMMAND);

    // execute the command.
    command.execute(context);/*from   w  ww  . ja v a  2 s .c  o  m*/

    // Get the result.
    String resultString = (String) context.getValue("$" + RESULT, String.class);

    assertEquals("The modification of a declared variable failed.", "string2", resultString);
}

From source file:org.xchain.namespaces.core.VariableCommand.java

public boolean execute(JXPathContext context) throws Exception {
    QName variableName = getName(context);
    Object variableValue = null;//from   w  ww  .  j  a v a2s.c  om

    if (hasSelect()) {
        variableValue = getSelect(context);
    } else if (hasSelectNodes()) {
        variableValue = getSelectNodes(context);
    } else if (hasSelectSingleNode()) {
        variableValue = getSelectSingleNode(context);
    } else {
        throw new Exception("Variable '" + variableName
                + "' must have a select attribute (select, select-nodes, or select-single-node)");
    }

    // get the scope.
    Scope scope = getScope(context);

    if (log.isDebugEnabled()) {
        log.debug("Setting variable name '" + variableName + "' to value '" + variableValue + "' in scope '"
                + scope + "'.");
    }

    // declare the variable.
    ((ScopedQNameVariables) context.getVariables()).declareVariable(variableName, variableValue, scope);

    // return false and allow other chains to execute.
    return false;
}

From source file:org.xchain.namespaces.csv.CsvXChainTestCase.java

protected void declareTestVariables(JXPathContext context) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    context.getVariables().declareVariable("output", outputStream);
    context.getVariables().declareVariable("double-quote", "\"");
    context.getVariables().declareVariable("single-quote", "'");
}

From source file:org.xchain.namespaces.csv.CsvXChainTestCase.java

protected String getOutputString(JXPathContext context) {
    return ((ByteArrayOutputStream) context.getVariables().getVariable("output")).toString();
}

From source file:org.xchain.namespaces.hibernate.AbstractQueryResultCommand.java

protected void storeValue(JXPathContext context, Object value) throws Exception {
    if (hasResult()) {
        // TODO This should check that the path exists first.  If it doesn't exist, create it.
        context.setValue(getResult(context), value);
    } else if (hasVariable()) {
        ((ScopedQNameVariables) context.getVariables()).declareVariable(getVariable(context), value,
                getScope(context));/*from ww  w. ja  va2s .c  o  m*/
    } else {
        throw new Exception("Result or Variable must be given.");
    }
}

From source file:org.xchain.namespaces.hibernate.QueryCommand.java

public boolean execute(JXPathContext context) throws Exception {
    Session session = HibernateLifecycle.getCurrentSession(getName(context));
    Query query = null;//from w  w  w  .  j  ava2 s.c  om
    QName variable = null;

    if (!hasQuery())
        throw new Exception("A query command requires a query.");

    if (session == null)
        throw new Exception("Query requires a session.");

    variable = getResult(context);

    query = session.createQuery(getQuery(context));

    // put the query into the context.
    ((ScopedQNameVariables) context.getVariables()).declareVariable(variable, query, Scope.chain);

    // execute the chain 
    return super.execute(context);

}

From source file:org.xchain.namespaces.hibernate.test.command.TestSession1.java

public boolean execute(JXPathContext context) throws Exception {
    Session session = HibernateLifecycle.getCurrentSession(getName(context));
    boolean result = false;

    // test validity of session
    if (session != null && session.isOpen()) {
        // assert that the command succeeded.
        context.getVariables().declareVariable("test-success", "true");
    }/*from  w  w  w.j  av a  2  s  . c om*/

    // execute the chain 
    return super.execute(context);
}

From source file:org.xchain.namespaces.hibernate.test.command.TestTransaction1.java

public boolean execute(JXPathContext context) throws Exception {
    Session session = HibernateLifecycle.getCurrentSession(getName(context));
    Transaction transaction = session.getTransaction();
    boolean result = false;

    // test validity of transaction
    if (transaction != null && transaction.isActive()) {
        // assert that the command succeeded.
        context.getVariables().declareVariable("test-success-1", transaction);
    }/*w ww.  j  a  va2s .c  o  m*/

    // execute the chain 
    return super.execute(context);
}