public void testComplexOperationWithVariables() { JXPathContext context = JXPathContext.newContext(null); context.getVariables().declareVariable("a", Integer.valueOf(0)); context.getVariables().declareVariable("b", Integer.valueOf(0)); context.getVariables().declareVariable("c", Integer.valueOf(1)); assertXPathValue(context, "$a + $b <= $c", Boolean.TRUE); }
public Object evaluate(NodeDefinition context, NodeDefinition thisNode) { if (!(Schema.class.isAssignableFrom(context.getClass()) || NodeDefinition.class.isAssignableFrom(context.getClass()))) { throw new IllegalArgumentException( "Unable to evaluate expression with context class " + context.getClass().getName()); } JXPathContext jxPathContext = JXPathContext.newContext(CONTEXT, context); Variables variables = jxPathContext.getVariables(); variables.declareVariable(AbstractExpression.THIS_VARIABLE_NAME, thisNode); String expr = Path.getNormalizedPath(expression); Object result = jxPathContext.getValue(expr); return result; }
public boolean execute(JXPathContext context) throws Exception { QName variableName = getName(context); Object variableValue = null; 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; }