示例#1
0
  /**
   * Return the XPathContext to be used for evaluating expressions.
   *
   * <p>If the child is nested withing a forEach tag its iteration context is used. Otherwise, a new
   * context is created based on an empty Document.
   *
   * @param child the tag whose context should be returned
   * @param pageContext the current page context
   * @return the XPath evaluation context
   */
  public static XPathContext getContext(Tag child, PageContext pageContext) {
    // if within a forEach tag, use its context
    ForEachTag forEachTag = (ForEachTag) TagSupport.findAncestorWithClass(child, ForEachTag.class);
    if (forEachTag != null) {
      return forEachTag.getContext();
    }

    // otherwise, create a new context referring to an empty document
    XPathContext context = new XPathContext(false);
    VariableStack variableStack = new JSTLVariableStack(pageContext);
    context.setVarStack(variableStack);
    int dtm = context.getDTMHandleFromNode(newEmptyDocument());
    context.pushCurrentNodeAndExpression(dtm, dtm);
    return context;
  }