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.hibernate.test.command.TestTransaction2.java

public boolean execute(JXPathContext context) throws Exception {
    boolean result = false;
    Transaction transaction = (Transaction) context.getValue("$test-success-1");

    // test commit of transaction
    if (transaction != null && transaction.wasCommitted()) {
        // assert that the command succeeded.
        context.getVariables().declareVariable("test-success-2", "true");
    }//from   ww  w.  java2s .  c  om

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

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

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

    Person bean = new Person();
    bean.setId(new Integer(1234));
    bean.setName("Test Person");
    context.getVariables().declareVariable("test-validate-bean", bean);

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

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

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

    Person bean = new Person();
    context.getVariables().declareVariable("test-validate-bean", bean);

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

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

public boolean execute(JXPathContext context) throws Exception {
    Session session = HibernateLifecycle.getCurrentSession(getName(context));
    Map contextHolder = new HashMap();
    List beans = new LinkedList();

    Person bean1 = new Person();
    beans.add(bean1);//from w w  w.j a  v  a2  s.  co m

    Person bean2 = new Person();
    beans.add(bean2);

    Person bean3 = new Person();
    bean3.setId(new Integer(1234));
    bean3.setName("Test Person");
    beans.add(bean3);

    contextHolder.put("beans", beans);
    context.getVariables().declareVariable("test-validate-beans", contextHolder);

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

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

public boolean execute(JXPathContext context) throws Exception {
    boolean valid = true;
    List<InvalidValue> invalidValues = new ArrayList<InvalidValue>();

    // validate using hibernate select attribute
    if (hasSelect()) {
        Object bean = getSelect(context);
        if (!valid(bean, invalidValues)) {
            valid = false;/*from  www  . j a  va2s  . c om*/
        }
    }

    // validate using hibernate select-nodes attribute
    if (hasSelectNodes()) {
        List beans = getSelectNodes(context);
        for (Iterator it = beans.iterator(); it.hasNext();) {
            Object bean = it.next();
            if (!valid(bean, invalidValues)) {
                valid = false;
            }
        }
    }

    // validate using hibernate select-single-node attribute
    if (hasSelectSingleNode()) {
        Object bean = getSelectSingleNode(context);
        if (!valid(bean, invalidValues)) {
            valid = false;
        }
    }

    if (!valid) {
        ((ScopedQNameVariables) context.getVariables()).declareVariable(getMessagesQName(context),
                invalidValues, Scope.chain);
    }

    // iterate over the children clauses looking for a either the valid / invalid clause
    // if a match is found, then execute the associated chain.
    Iterator<Command> childIterator = getCommandList().iterator();
    while (childIterator.hasNext()) {
        Command clause = childIterator.next();

        if (valid && clause instanceof ValidClause) {
            return clause.execute(context);
        } else if (!valid && clause instanceof InvalidClause) {
            return clause.execute(context);
        }
    }

    // if we got this far, then just return null.
    return false;
}

From source file:org.xchain.namespaces.javascript.MergeJavaScriptTest.java

@Test
public void testJavaScriptMerge() throws Exception {
    JXPathContext context = org.xchain.framework.jxpath.JXPathContextFactoryImpl.newInstance().newContext(null,
            new Object());
    HttpServletResponse response = createHttpResponseMockery();
    ((ScopedQNameVariables) context.getVariables())
            .declareVariable(new QName(org.xchain.namespaces.servlet.Constants.URI,
                    org.xchain.namespaces.servlet.Constants.RESPONSE), response);

    Command command = CatalogFactory.getInstance().getCatalog(CATALOG_SYSTEM_ID)
            .getCommand(COMPRESS_CHAIN_NAME);
    command.execute(context);// w w  w  .  j  a  v a  2s .c o  m
    String result = ((ServletByteArrayOutputStream) response.getOutputStream()).getByteArrayOutputStream()
            .toString();
}

From source file:org.xchain.namespaces.jta.TransactionCommand.java

public boolean execute(JXPathContext context) throws Exception {
    boolean managingTransaction = false;
    boolean result = false;
    UserTransaction transaction = ContainerContext.getJtaLookupStrategy().getUserTransaction();

    if (transaction != null) {
        try {/*from   w ww  .  j  ava2 s  . c o m*/
            // if there is not a transaction running, then start one.
            if (Status.STATUS_ACTIVE != transaction.getStatus()) {
                managingTransaction = true;
                transaction.setTransactionTimeout(this.getTimeout(context));
                transaction.begin();
            }

            // put the transaction into the context.
            ((ScopedQNameVariables) context.getVariables()).declareVariable(getResult(context), transaction,
                    Scope.execution);

            // execute the chain 
            result = super.execute(context);

            // roll back the transaction.
            if (managingTransaction) {
                transaction.commit();
            }
        } catch (Exception e) {
            if (managingTransaction) {
                if (transaction.getStatus() != Status.STATUS_NO_TRANSACTION) {
                    transaction.rollback();
                }
            }
            throw e;
        } finally {
            // TODO: If we defined the transaction variable, then we should remove it.
        }
    } else {
        // TODO: Should this throw an IllegalStateException?  Returning true seems like the wrong behavior.
        result = true;
    }
    return result;
}

From source file:org.xchain.namespaces.servlet.GetCookie.java

public boolean execute(JXPathContext context) throws Exception {
    String name = getName(context);
    Cookie[] cookies = getRequest(context).getCookies();
    for (int i = 0; cookies != null && i < cookies.length; i++) {
        Cookie cookie = cookies[i];//from ww  w. j ava2 s .  c  om
        if (name.equals(cookie.getName())) {
            ((ScopedQNameVariables) context.getVariables()).declareVariable(getVariable(context),
                    cookie.getValue(), Scope.execution);
        }
    }
    return false;
}

From source file:org.xchain.namespaces.test.AnnotationDefaultCommand.java

public boolean execute(JXPathContext context) throws Exception {
    QName variableName = getName(context);
    Object variableValue = getValue(context);

    // set the value of the expression to the variable name.
    ((ScopedQNameVariables) context.getVariables()).declareVariable(variableName, variableValue, Scope.request);

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

From source file:org.xchain.namespaces.test.AssertExceptionTypeCommand.java

public boolean execute(JXPathContext context) throws Exception {
    // get the type.
    QName variableName = getName(context);
    Boolean result = Boolean.FALSE;

    // call the children and get the exception.
    try {/*from   www  . j av  a2s.c  o m*/
        super.execute(context);
    } catch (Exception e) {
        ((ScopedQNameVariables) context.getVariables()).declareVariable(variableName, e, Scope.request);
    }

    return false;
}