@Override public LocalBinding getBinding() { return (LocalBinding)super.getBinding(); }
@Override public LocalBinding getBinding() { return (LocalBinding)super.getBinding(); }
/** * Replace all references to a particular variable within a subtree * * @param exp the expression at the root of the subtree * @param binding the variable binding whose references are sought * @param replacement the expression to be used in place of the variable reference * @param mustCopy true if the replacement expression must be copied before use */ public static void replaceVariableReferences(Expression exp, final Binding binding, Expression replacement, boolean mustCopy) { ExpressionSelector selector = child -> child instanceof VariableReference && ((VariableReference) child).getBinding() == binding; replaceSelectedSubexpressions(exp, selector, replacement, mustCopy); }
/** * Replace all references to a particular variable within a subtree * * @param exp the expression at the root of the subtree * @param binding the variable binding whose references are sought * @param replacement the expression to be used in place of the variable reference * @param mustCopy true if the replacement expression must be copied before use */ public static void replaceVariableReferences(Expression exp, final Binding binding, Expression replacement, boolean mustCopy) { ExpressionSelector selector = child -> child instanceof VariableReference && ((VariableReference) child).getBinding() == binding; replaceSelectedSubexpressions(exp, selector, replacement, mustCopy); }
/** * Determine whether an expression depends on any one of a set of variables * * @param exp the expression being tested * @param bindingList the set of variables being tested * @return true if the expression depends on one of the given variables */ public static boolean dependsOnVariable(Expression exp, final Binding[] bindingList) { return !(bindingList == null || bindingList.length == 0) && contains(exp, false, e -> { if (e instanceof VariableReference) { for (Binding binding : bindingList) { if (((VariableReference) e).getBinding() == binding) { return true; } } } return false; }); }
/** * Add a representation of this expression to a PathMap. The PathMap captures a map of the nodes visited * by an expression in a source tree. * <p>The default implementation of this method assumes that an expression does no navigation other than * the navigation done by evaluating its subexpressions, and that the subexpressions are evaluated in the * same context as the containing expression. The method must be overridden for any expression * where these assumptions do not hold. For example, implementations exist for AxisExpression, ParentExpression, * and RootExpression (because they perform navigation), and for the doc(), document(), and collection() * functions because they create a new navigation root. Implementations also exist for PathExpression and * FilterExpression because they have subexpressions that are evaluated in a different context from the * calling expression.</p> * * @param pathMap the PathMap to which the expression should be added * @param pathMapNodeSet the PathMapNodeSet to which the paths embodied in this expression should be added * @return the pathMapNodeSet representing the points in the source document that are both reachable by this * expression, and that represent possible results of this expression. For an expression that does * navigation, it represents the end of the arc in the path map that describes the navigation route. For other * expressions, it is the same as the input pathMapNode. */ public PathMap.PathMapNodeSet addToPathMap(PathMap pathMap, PathMap.PathMapNodeSet pathMapNodeSet) { return pathMap.getPathForVariable(getBinding()); }
/** * Determine whether an expression depends on any one of a set of variables * * @param exp the expression being tested * @param bindingList the set of variables being tested * @return true if the expression depends on one of the given variables */ public static boolean dependsOnVariable(Expression exp, final Binding[] bindingList) { return !(bindingList == null || bindingList.length == 0) && contains(exp, false, e -> { if (e instanceof VariableReference) { for (Binding binding : bindingList) { if (((VariableReference) e).getBinding() == binding) { return true; } } } return false; }); }
/** * Get a list of all references to a particular variable within a subtree * @param exp the expression at the root of the subtree * @param binding the variable binding whose references are sought * @param list a list to be populated with the references to this variable */ public static void gatherVariableReferences(Expression exp, Binding binding, List list) { if (exp instanceof VariableReference && ((VariableReference)exp).getBinding() == binding) { list.add(exp); } else { for (Iterator iter = exp.iterateSubExpressions(); iter.hasNext(); ) { gatherVariableReferences((Expression)iter.next(), binding, list); } } }
/** * Get a list of all references to a particular variable within a subtree * @param exp the expression at the root of the subtree * @param binding the variable binding whose references are sought * @param list a list to be populated with the references to this variable */ public static void gatherVariableReferences(Expression exp, Binding binding, List list) { if (exp instanceof VariableReference && ((VariableReference)exp).getBinding() == binding) { list.add(exp); } else { for (Iterator iter = exp.iterateSubExpressions(); iter.hasNext(); ) { gatherVariableReferences((Expression)iter.next(), binding, list); } } }
/** * Gather a list of all the variable bindings on which a given expression depends * @param e the expression being tested * @param list a list to which the bindings are to be added. The items in this list must * implement {@link Binding} */ public static void gatherReferencedVariables(Expression e, List list) { if (e instanceof VariableReference) { Binding binding = ((VariableReference)e).getBinding(); if (!list.contains(binding)) { list.add(binding); } } else { for (Iterator children = e.iterateSubExpressions(); children.hasNext();) { Expression child = (Expression)children.next(); gatherReferencedVariables(child, list); } } }
/** * Gather a list of all the variable bindings on which a given expression depends * @param e the expression being tested * @param list a list to which the bindings are to be added. The items in this list must * implement {@link Binding} */ public static void gatherReferencedVariables(Expression e, List list) { if (e instanceof VariableReference) { Binding binding = ((VariableReference)e).getBinding(); if (!list.contains(binding)) { list.add(binding); } } else { for (Iterator children = e.iterateSubExpressions(); children.hasNext();) { Expression child = (Expression)children.next(); gatherReferencedVariables(child, list); } } }
/** * Rebind all variable references to a binding * @param exp the expression whose contained variable references are to be rebound * @param oldBinding the old binding for the variable references * @param newBinding the new binding to which the variables should be rebound */ public static void rebindVariableReferences( Expression exp, Binding oldBinding, Binding newBinding) { if (exp instanceof VariableReference) { if (((VariableReference)exp).getBinding() == oldBinding) { ((VariableReference)exp).fixup(newBinding); } } else { Iterator iter = exp.iterateSubExpressions(); while (iter.hasNext()) { Expression e = (Expression)iter.next(); rebindVariableReferences(e, oldBinding, newBinding); } } }
/** * Rebind all variable references to a binding * @param exp the expression whose contained variable references are to be rebound * @param oldBinding the old binding for the variable references * @param newBinding the new binding to which the variables should be rebound */ public static void rebindVariableReferences( Expression exp, Binding oldBinding, Binding newBinding) { if (exp instanceof VariableReference) { if (((VariableReference)exp).getBinding() == oldBinding) { ((VariableReference)exp).fixup(newBinding); } } else { Iterator iter = exp.iterateSubExpressions(); while (iter.hasNext()) { Expression e = (Expression)iter.next(); rebindVariableReferences(e, oldBinding, newBinding); } } } }
/** * Get a list of all references to a particular variable within a subtree * * @param exp the expression at the root of the subtree * @param binding the variable binding whose references are sought * @param list a list to be populated with the references to this variable */ public static void gatherVariableReferences(Expression exp, Binding binding, List<VariableReference> list) { if (exp instanceof VariableReference && ((VariableReference) exp).getBinding() == binding) { list.add((VariableReference) exp); } else { for (Operand o : exp.operands()) { gatherVariableReferences(o.getChildExpression(), binding, list); } } }
/** * Get a list of all references to a particular variable within a subtree * * @param exp the expression at the root of the subtree * @param binding the variable binding whose references are sought * @param list a list to be populated with the references to this variable */ public static void gatherVariableReferences(Expression exp, Binding binding, List<VariableReference> list) { if (exp instanceof VariableReference && ((VariableReference) exp).getBinding() == binding) { list.add((VariableReference) exp); } else { for (Operand o : exp.operands()) { gatherVariableReferences(o.getChildExpression(), binding, list); } } }
public AbstractExpression exprFor (VariableReference var) { if (var.getBinding() != null) { StructuredQName varName= var.getBinding().getVariableQName(); return new Variable (qnameFor (varName)); } else { // total HACK, but Saxon provides no other public method to retrieve the constant value try { Object o = var.optimize(null, null); if (o instanceof Literal) { return exprFor ((Literal) o); } } catch (XPathException e) { throw new LuxException ("Unsupported variable reference: " + var); } throw new LuxException ("Unsupported variable reference: " + var); } }
/** * Replace all references to the variable bound by this let expression, * that occur within the action expression, with the given expression * * * @param seq the expression * @return true if the variable was successfully inlined. (Returns false, for example, * if a variable reference occurs inside a try/catch, which inhibits inlining). */ public boolean replaceVariable(Expression seq) { boolean done = ExpressionTool.inlineVariableReferences(getAction(), this, seq); if (done && isIndexedVariable() && seq instanceof VariableReference) { Binding newBinding = ((VariableReference) seq).getBinding(); if (newBinding instanceof Assignation) { ((Assignation) newBinding).setIndexedVariable(); } } return done; }
/** * Rebind all variable references to a binding * * @param exp the expression whose contained variable references are to be rebound * @param oldBinding the old binding for the variable references * @param newBinding the new binding to which the variables should be rebound */ public static void rebindVariableReferences( Expression exp, Binding oldBinding, Binding newBinding) { if (exp instanceof VariableReference) { if (((VariableReference) exp).getBinding() == oldBinding) { ((VariableReference) exp).fixup(newBinding); } } else { for (Operand o : exp.operands()) { rebindVariableReferences(o.getChildExpression(), oldBinding, newBinding); } } }
/** * Rebind all variable references to a binding * * @param exp the expression whose contained variable references are to be rebound * @param oldBinding the old binding for the variable references * @param newBinding the new binding to which the variables should be rebound */ public static void rebindVariableReferences( Expression exp, Binding oldBinding, Binding newBinding) { if (exp instanceof VariableReference) { if (((VariableReference) exp).getBinding() == oldBinding) { ((VariableReference) exp).fixup(newBinding); } } else { for (Operand o : exp.operands()) { rebindVariableReferences(o.getChildExpression(), oldBinding, newBinding); } } }
/** * Replace all references to the variable bound by this let expression, * that occur within the action expression, with the given expression * * * @param seq the expression * @return true if the variable was successfully inlined. (Returns false, for example, * if a variable reference occurs inside a try/catch, which inhibits inlining). */ public boolean replaceVariable(Expression seq) { boolean done = ExpressionTool.inlineVariableReferences(getAction(), this, seq); if (done && isIndexedVariable() && seq instanceof VariableReference) { Binding newBinding = ((VariableReference) seq).getBinding(); if (newBinding instanceof Assignation) { ((Assignation) newBinding).setIndexedVariable(); } } return done; }