Java Code Examples for com.sun.org.apache.xpath.internal.XPathContext#popCurrentNode()

The following examples show how to use com.sun.org.apache.xpath.internal.XPathContext#popCurrentNode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StepPattern.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Get the match score of the given node.
 *
 * @param xctxt The XPath runtime context.
 * @param context The node to be tested.
 *
 * @return {@link com.sun.org.apache.xpath.internal.patterns.NodeTest#SCORE_NODETEST},
 *         {@link com.sun.org.apache.xpath.internal.patterns.NodeTest#SCORE_NONE},
 *         {@link com.sun.org.apache.xpath.internal.patterns.NodeTest#SCORE_NSWILD},
 *         {@link com.sun.org.apache.xpath.internal.patterns.NodeTest#SCORE_QNAME}, or
 *         {@link com.sun.org.apache.xpath.internal.patterns.NodeTest#SCORE_OTHER}.
 *
 * @throws javax.xml.transform.TransformerException
 */
public double getMatchScore(XPathContext xctxt, int context)
        throws javax.xml.transform.TransformerException
{

  xctxt.pushCurrentNode(context);
  xctxt.pushCurrentExpressionNode(context);

  try
  {
    XObject score = execute(xctxt);

    return score.num();
  }
  finally
  {
    xctxt.popCurrentNode();
    xctxt.popCurrentExpressionNode();
  }

  // return XPath.MATCH_SCORE_NONE;
}
 
Example 2
Source File: StepPattern.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
/**
 * Get the match score of the given node.
 *
 * @param xctxt The XPath runtime context.
 * @param context The node to be tested.
 *
 * @return {@link com.sun.org.apache.xpath.internal.patterns.NodeTest#SCORE_NODETEST},
 *         {@link com.sun.org.apache.xpath.internal.patterns.NodeTest#SCORE_NONE},
 *         {@link com.sun.org.apache.xpath.internal.patterns.NodeTest#SCORE_NSWILD},
 *         {@link com.sun.org.apache.xpath.internal.patterns.NodeTest#SCORE_QNAME}, or
 *         {@link com.sun.org.apache.xpath.internal.patterns.NodeTest#SCORE_OTHER}.
 *
 * @throws javax.xml.transform.TransformerException
 */
public double getMatchScore(XPathContext xctxt, int context)
        throws javax.xml.transform.TransformerException
{

  xctxt.pushCurrentNode(context);
  xctxt.pushCurrentExpressionNode(context);

  try
  {
    XObject score = execute(xctxt);

    return score.num();
  }
  finally
  {
    xctxt.popCurrentNode();
    xctxt.popCurrentExpressionNode();
  }

  // return XPath.MATCH_SCORE_NONE;
}
 
Example 3
Source File: StepPattern.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Execute the match pattern step relative to another step.
 *
 *
 * @param xctxt The XPath runtime context.
 * @param dtm The DTM of the current node.
 * @param currentNode The current node context.
 *
 * @return {@link com.sun.org.apache.xpath.internal.patterns.NodeTest#SCORE_NODETEST},
 *         {@link com.sun.org.apache.xpath.internal.patterns.NodeTest#SCORE_NONE},
 *         {@link com.sun.org.apache.xpath.internal.patterns.NodeTest#SCORE_NSWILD},
 *         {@link com.sun.org.apache.xpath.internal.patterns.NodeTest#SCORE_QNAME}, or
 *         {@link com.sun.org.apache.xpath.internal.patterns.NodeTest#SCORE_OTHER}.
 *
 * @throws javax.xml.transform.TransformerException
 */
protected final XObject executeRelativePathPattern(
        XPathContext xctxt, DTM dtm, int currentNode)
          throws javax.xml.transform.TransformerException
{

  XObject score = NodeTest.SCORE_NONE;
  int context = currentNode;
  DTMAxisTraverser traverser;

  traverser = dtm.getAxisTraverser(m_axis);

  for (int relative = traverser.first(context); DTM.NULL != relative;
          relative = traverser.next(context, relative))
  {
    try
    {
      xctxt.pushCurrentNode(relative);

      score = execute(xctxt);

      if (score != NodeTest.SCORE_NONE)
        break;
    }
    finally
    {
      xctxt.popCurrentNode();
    }
  }

  return score;
}
 
Example 4
Source File: UnionChildIterator.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test whether a specified node is visible in the logical view of a
 * TreeWalker or NodeIterator. This function will be called by the
 * implementation of TreeWalker and NodeIterator; it is not intended to
 * be called directly from user code.
 * @param n  The node to check to see if it passes the filter or not.
 * @return  a constant to determine whether the node is accepted,
 *   rejected, or skipped, as defined  above .
 */
public short acceptNode(int n)
{
  XPathContext xctxt = getXPathContext();
  try
  {
    xctxt.pushCurrentNode(n);
    for (int i = 0; i < m_nodeTests.length; i++)
    {
      PredicatedNodeTest pnt = m_nodeTests[i];
      XObject score = pnt.execute(xctxt, n);
      if (score != NodeTest.SCORE_NONE)
      {
        // Note that we are assuming there are no positional predicates!
        if (pnt.getPredicateCount() > 0)
        {
          if (pnt.executePredicates(n, xctxt))
            return DTMIterator.FILTER_ACCEPT;
        }
        else
          return DTMIterator.FILTER_ACCEPT;

      }
    }
  }
  catch (javax.xml.transform.TransformerException se)
  {

    // TODO: Fix this.
    throw new RuntimeException(se.getMessage());
  }
  finally
  {
    xctxt.popCurrentNode();
  }
  return DTMIterator.FILTER_SKIP;
}
 
Example 5
Source File: UnionChildIterator.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test whether a specified node is visible in the logical view of a
 * TreeWalker or NodeIterator. This function will be called by the
 * implementation of TreeWalker and NodeIterator; it is not intended to
 * be called directly from user code.
 * @param n  The node to check to see if it passes the filter or not.
 * @return  a constant to determine whether the node is accepted,
 *   rejected, or skipped, as defined  above .
 */
public short acceptNode(int n)
{
  XPathContext xctxt = getXPathContext();
  try
  {
    xctxt.pushCurrentNode(n);
    for (int i = 0; i < m_nodeTests.length; i++)
    {
      PredicatedNodeTest pnt = m_nodeTests[i];
      XObject score = pnt.execute(xctxt, n);
      if (score != NodeTest.SCORE_NONE)
      {
        // Note that we are assuming there are no positional predicates!
        if (pnt.getPredicateCount() > 0)
        {
          if (pnt.executePredicates(n, xctxt))
            return DTMIterator.FILTER_ACCEPT;
        }
        else
          return DTMIterator.FILTER_ACCEPT;

      }
    }
  }
  catch (javax.xml.transform.TransformerException se)
  {

    // TODO: Fix this.
    throw new RuntimeException(se.getMessage());
  }
  finally
  {
    xctxt.popCurrentNode();
  }
  return DTMIterator.FILTER_SKIP;
}
 
Example 6
Source File: StepPattern.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Execute the match pattern step relative to another step.
 *
 *
 * @param xctxt The XPath runtime context.
 * @param dtm The DTM of the current node.
 * @param currentNode The current node context.
 *
 * @return {@link com.sun.org.apache.xpath.internal.patterns.NodeTest#SCORE_NODETEST},
 *         {@link com.sun.org.apache.xpath.internal.patterns.NodeTest#SCORE_NONE},
 *         {@link com.sun.org.apache.xpath.internal.patterns.NodeTest#SCORE_NSWILD},
 *         {@link com.sun.org.apache.xpath.internal.patterns.NodeTest#SCORE_QNAME}, or
 *         {@link com.sun.org.apache.xpath.internal.patterns.NodeTest#SCORE_OTHER}.
 *
 * @throws javax.xml.transform.TransformerException
 */
protected final XObject executeRelativePathPattern(
        XPathContext xctxt, DTM dtm, int currentNode)
          throws javax.xml.transform.TransformerException
{

  XObject score = NodeTest.SCORE_NONE;
  int context = currentNode;
  DTMAxisTraverser traverser;

  traverser = dtm.getAxisTraverser(m_axis);

  for (int relative = traverser.first(context); DTM.NULL != relative;
          relative = traverser.next(context, relative))
  {
    try
    {
      xctxt.pushCurrentNode(relative);

      score = execute(xctxt);

      if (score != NodeTest.SCORE_NONE)
        break;
    }
    finally
    {
      xctxt.popCurrentNode();
    }
  }

  return score;
}
 
Example 7
Source File: StepPattern.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Execute the match pattern step relative to another step.
 *
 *
 * @param xctxt The XPath runtime context.
 * @param dtm The DTM of the current node.
 * @param currentNode The current node context.
 *
 * @return {@link com.sun.org.apache.xpath.internal.patterns.NodeTest#SCORE_NODETEST},
 *         {@link com.sun.org.apache.xpath.internal.patterns.NodeTest#SCORE_NONE},
 *         {@link com.sun.org.apache.xpath.internal.patterns.NodeTest#SCORE_NSWILD},
 *         {@link com.sun.org.apache.xpath.internal.patterns.NodeTest#SCORE_QNAME}, or
 *         {@link com.sun.org.apache.xpath.internal.patterns.NodeTest#SCORE_OTHER}.
 *
 * @throws javax.xml.transform.TransformerException
 */
protected final XObject executeRelativePathPattern(
        XPathContext xctxt, DTM dtm, int currentNode)
          throws javax.xml.transform.TransformerException
{

  XObject score = NodeTest.SCORE_NONE;
  int context = currentNode;
  DTMAxisTraverser traverser;

  traverser = dtm.getAxisTraverser(m_axis);

  for (int relative = traverser.first(context); DTM.NULL != relative;
          relative = traverser.next(context, relative))
  {
    try
    {
      xctxt.pushCurrentNode(relative);

      score = execute(xctxt);

      if (score != NodeTest.SCORE_NONE)
        break;
    }
    finally
    {
      xctxt.popCurrentNode();
    }
  }

  return score;
}
 
Example 8
Source File: PredicatedNodeTest.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 *  Test whether a specified node is visible in the logical view of a
 * TreeWalker or NodeIterator. This function will be called by the
 * implementation of TreeWalker and NodeIterator; it is not intended to
 * be called directly from user code.
 * @param n  The node to check to see if it passes the filter or not.
 * @return  a constant to determine whether the node is accepted,
 *   rejected, or skipped, as defined  above .
 */
public short acceptNode(int n)
{

  XPathContext xctxt = m_lpi.getXPathContext();

  try
  {
    xctxt.pushCurrentNode(n);

    XObject score = execute(xctxt, n);

    // System.out.println("\n::acceptNode - score: "+score.num()+"::");
    if (score != NodeTest.SCORE_NONE)
    {
      if (getPredicateCount() > 0)
      {
        countProximityPosition(0);

        if (!executePredicates(n, xctxt))
          return DTMIterator.FILTER_SKIP;
      }

      return DTMIterator.FILTER_ACCEPT;
    }
  }
  catch (javax.xml.transform.TransformerException se)
  {

    // TODO: Fix this.
    throw new RuntimeException(se.getMessage());
  }
  finally
  {
    xctxt.popCurrentNode();
  }

  return DTMIterator.FILTER_SKIP;
}
 
Example 9
Source File: PredicatedNodeTest.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 *  Test whether a specified node is visible in the logical view of a
 * TreeWalker or NodeIterator. This function will be called by the
 * implementation of TreeWalker and NodeIterator; it is not intended to
 * be called directly from user code.
 * @param n  The node to check to see if it passes the filter or not.
 * @return  a constant to determine whether the node is accepted,
 *   rejected, or skipped, as defined  above .
 */
public short acceptNode(int n)
{

  XPathContext xctxt = m_lpi.getXPathContext();

  try
  {
    xctxt.pushCurrentNode(n);

    XObject score = execute(xctxt, n);

    // System.out.println("\n::acceptNode - score: "+score.num()+"::");
    if (score != NodeTest.SCORE_NONE)
    {
      if (getPredicateCount() > 0)
      {
        countProximityPosition(0);

        if (!executePredicates(n, xctxt))
          return DTMIterator.FILTER_SKIP;
      }

      return DTMIterator.FILTER_ACCEPT;
    }
  }
  catch (javax.xml.transform.TransformerException se)
  {

    // TODO: Fix this.
    throw new RuntimeException(se.getMessage());
  }
  finally
  {
    xctxt.popCurrentNode();
  }

  return DTMIterator.FILTER_SKIP;
}
 
Example 10
Source File: PredicatedNodeTest.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Process the predicates.
 *
 * @param context The current context node.
 * @param xctxt The XPath runtime context.
 *
 * @return the result of executing the predicate expressions.
 *
 * @throws javax.xml.transform.TransformerException
 */
boolean executePredicates(int context, XPathContext xctxt)
        throws javax.xml.transform.TransformerException
{

  int nPredicates = getPredicateCount();
  // System.out.println("nPredicates: "+nPredicates);
  if (nPredicates == 0)
    return true;

  PrefixResolver savedResolver = xctxt.getNamespaceContext();

  try
  {
    m_predicateIndex = 0;
    xctxt.pushSubContextList(this);
    xctxt.pushNamespaceContext(m_lpi.getPrefixResolver());
    xctxt.pushCurrentNode(context);

    for (int i = 0; i < nPredicates; i++)
    {
      // System.out.println("Executing predicate expression - waiting count: "+m_lpi.getWaitingCount());
      XObject pred = m_predicates[i].execute(xctxt);
      // System.out.println("\nBack from executing predicate expression - waiting count: "+m_lpi.getWaitingCount());
      // System.out.println("pred.getType(): "+pred.getType());
      if (XObject.CLASS_NUMBER == pred.getType())
      {
        if (DEBUG_PREDICATECOUNTING)
        {
          System.out.flush();
          System.out.println("\n===== start predicate count ========");
          System.out.println("m_predicateIndex: " + m_predicateIndex);
          // System.out.println("getProximityPosition(m_predicateIndex): "
          //                   + getProximityPosition(m_predicateIndex));
          System.out.println("pred.num(): " + pred.num());
        }

        int proxPos = this.getProximityPosition(m_predicateIndex);
        int predIndex = (int) pred.num();
        if (proxPos != predIndex)
        {
          if (DEBUG_PREDICATECOUNTING)
          {
            System.out.println("\nnode context: "+nodeToString(context));
            System.out.println("index predicate is false: "+proxPos);
            System.out.println("\n===== end predicate count ========");
          }
          return false;
        }
        else if (DEBUG_PREDICATECOUNTING)
        {
          System.out.println("\nnode context: "+nodeToString(context));
          System.out.println("index predicate is true: "+proxPos);
          System.out.println("\n===== end predicate count ========");
        }

        // If there is a proximity index that will not change during the
        // course of itteration, then we know there can be no more true
        // occurances of this predicate, so flag that we're done after
        // this.
        //
        // bugzilla 14365
        // We can't set m_foundLast = true unless we're sure that -all-
        // remaining parameters are stable, or else last() fails. Fixed so
        // only sets m_foundLast if on the last predicate
        if(m_predicates[i].isStableNumber() && i == nPredicates - 1)
        {
          m_foundLast = true;
        }
      }
      else if (!pred.bool())
        return false;

      countProximityPosition(++m_predicateIndex);
    }
  }
  finally
  {
    xctxt.popCurrentNode();
    xctxt.popNamespaceContext();
    xctxt.popSubContextList();
    m_predicateIndex = -1;
  }

  return true;
}
 
Example 11
Source File: MatchPatternIterator.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 *  Test whether a specified node is visible in the logical view of a
 * TreeWalker or NodeIterator. This function will be called by the
 * implementation of TreeWalker and NodeIterator; it is not intended to
 * be called directly from user code.
 * @param n  The node to check to see if it passes the filter or not.
 * @return  a constant to determine whether the node is accepted,
 *   rejected, or skipped, as defined  above .
 */
public short acceptNode(int n, XPathContext xctxt)
{

  try
  {
    xctxt.pushCurrentNode(n);
    xctxt.pushIteratorRoot(m_context);
    if(DEBUG)
    {
      System.out.println("traverser: "+m_traverser);
      System.out.print("node: "+n);
      System.out.println(", "+m_cdtm.getNodeName(n));
      // if(m_cdtm.getNodeName(n).equals("near-east"))
      System.out.println("pattern: "+m_pattern.toString());
      NodeTest.debugWhatToShow(m_pattern.getWhatToShow());
    }

    XObject score = m_pattern.execute(xctxt);

    if(DEBUG)
    {
      // System.out.println("analysis: "+Integer.toBinaryString(m_analysis));
      System.out.println("score: "+score);
      System.out.println("skip: "+(score == NodeTest.SCORE_NONE));
    }

    // System.out.println("\n::acceptNode - score: "+score.num()+"::");
    return (score == NodeTest.SCORE_NONE) ? DTMIterator.FILTER_SKIP
                  : DTMIterator.FILTER_ACCEPT;
  }
  catch (javax.xml.transform.TransformerException se)
  {

    // TODO: Fix this.
    throw new RuntimeException(se.getMessage());
  }
  finally
  {
    xctxt.popCurrentNode();
    xctxt.popIteratorRoot();
  }

}
 
Example 12
Source File: OneStepIterator.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Get the current sub-context position.  In order to do the
 * reverse axes count, for the moment this re-searches the axes
 * up to the predicate.  An optimization on this is to cache
 * the nodes searched, but, for the moment, this case is probably
 * rare enough that the added complexity isn't worth it.
 *
 * @param predicateIndex The predicate index of the proximity position.
 *
 * @return The pridicate index, or -1.
 */
protected int getProximityPosition(int predicateIndex)
{
  if(!isReverseAxes())
    return super.getProximityPosition(predicateIndex);

  // A negative predicate index seems to occur with
  // (preceding-sibling::*|following-sibling::*)/ancestor::*[position()]/*[position()]
  // -sb
  if(predicateIndex < 0)
    return -1;

  if (m_proximityPositions[predicateIndex] <= 0)
  {
    XPathContext xctxt = getXPathContext();
    try
    {
      OneStepIterator clone = (OneStepIterator) this.clone();

      int root = getRoot();
      xctxt.pushCurrentNode(root);
      clone.setRoot(root, xctxt);

      // clone.setPredicateCount(predicateIndex);
      clone.m_predCount = predicateIndex;

      // Count 'em all
      int count = 1;
      int next;

      while (DTM.NULL != (next = clone.nextNode()))
      {
        count++;
      }

      m_proximityPositions[predicateIndex] += count;
    }
    catch (CloneNotSupportedException cnse)
    {

      // can't happen
    }
    finally
    {
      xctxt.popCurrentNode();
    }
  }

  return m_proximityPositions[predicateIndex];
}
 
Example 13
Source File: ExsltDynamic.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * The dyn:min function calculates the minimum value for the nodes passed as the
 * first argument, where the value of each node is calculated dynamically using
 * an XPath expression passed as a string as the second argument.
 * <p>
 * The expressions are evaluated relative to the nodes passed as the first argument.
 * In other words, the value for each node is calculated by evaluating the XPath
 * expression with all context information being the same as that for the call to
 * the dyn:min function itself, except for the following:
 * <p>
 * <ul>
 *  <li>the context node is the node whose value is being calculated.</li>
 *  <li>the context position is the position of the node within the node set passed
 *    as the first argument to the dyn:min function, arranged in document order.</li>
 *  <li>the context size is the number of nodes passed as the first argument to the
 *    dyn:min function.</li>
 * </ul>
 * <p>
 * The dyn:min function returns the minimum of these values, calculated in exactly
 * the same way as for math:min.
 * <p>
 * If the expression string passed as the second argument is an invalid XPath expression
 * (including an empty string), this function returns NaN.
 * <p>
 * This function must take a second argument. To calculate the minimum of a set of
 * nodes based on their string values, you should use the math:min function.
 *
 * @param myContext The ExpressionContext passed by the extension processor
 * @param nl The node set
 * @param expr The expression string
 *
 * @return The minimum evaluation value
 */
public static double min(ExpressionContext myContext, NodeList nl, String expr)
  throws SAXNotSupportedException
{

  XPathContext xctxt = null;
  if (myContext instanceof XPathContext.XPathExpressionContext)
    xctxt = ((XPathContext.XPathExpressionContext) myContext).getXPathContext();
  else
    throw new SAXNotSupportedException(XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_CONTEXT_PASSED, new Object[]{myContext }));

  if (expr == null || expr.length() == 0)
    return Double.NaN;

  NodeSetDTM contextNodes = new NodeSetDTM(nl, xctxt);
  xctxt.pushContextNodeList(contextNodes);

  double minValue = Double.MAX_VALUE;
  for (int i = 0; i < nl.getLength(); i++)
  {
    int contextNode = contextNodes.item(i);
    xctxt.pushCurrentNode(contextNode);

    double result = 0;
    try
    {
      XPath dynamicXPath = new XPath(expr, xctxt.getSAXLocator(),
                                     xctxt.getNamespaceContext(),
                                     XPath.SELECT);
      result = dynamicXPath.execute(xctxt, contextNode, xctxt.getNamespaceContext()).num();
    }
    catch (TransformerException e)
    {
      xctxt.popCurrentNode();
      xctxt.popContextNodeList();
      return Double.NaN;
    }

    xctxt.popCurrentNode();

    if (result < minValue)
        minValue = result;
  }

  xctxt.popContextNodeList();
  return minValue;

}
 
Example 14
Source File: ExsltDynamic.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * The dyn:min function calculates the minimum value for the nodes passed as the
 * first argument, where the value of each node is calculated dynamically using
 * an XPath expression passed as a string as the second argument.
 * <p>
 * The expressions are evaluated relative to the nodes passed as the first argument.
 * In other words, the value for each node is calculated by evaluating the XPath
 * expression with all context information being the same as that for the call to
 * the dyn:min function itself, except for the following:
 * <p>
 * <ul>
 *  <li>the context node is the node whose value is being calculated.</li>
 *  <li>the context position is the position of the node within the node set passed
 *    as the first argument to the dyn:min function, arranged in document order.</li>
 *  <li>the context size is the number of nodes passed as the first argument to the
 *    dyn:min function.</li>
 * </ul>
 * <p>
 * The dyn:min function returns the minimum of these values, calculated in exactly
 * the same way as for math:min.
 * <p>
 * If the expression string passed as the second argument is an invalid XPath expression
 * (including an empty string), this function returns NaN.
 * <p>
 * This function must take a second argument. To calculate the minimum of a set of
 * nodes based on their string values, you should use the math:min function.
 *
 * @param myContext The ExpressionContext passed by the extension processor
 * @param nl The node set
 * @param expr The expression string
 *
 * @return The minimum evaluation value
 */
public static double min(ExpressionContext myContext, NodeList nl, String expr)
  throws SAXNotSupportedException
{

  XPathContext xctxt = null;
  if (myContext instanceof XPathContext.XPathExpressionContext)
    xctxt = ((XPathContext.XPathExpressionContext) myContext).getXPathContext();
  else
    throw new SAXNotSupportedException(XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_CONTEXT_PASSED, new Object[]{myContext }));

  if (expr == null || expr.length() == 0)
    return Double.NaN;

  NodeSetDTM contextNodes = new NodeSetDTM(nl, xctxt);
  xctxt.pushContextNodeList(contextNodes);

  double minValue = Double.MAX_VALUE;
  for (int i = 0; i < nl.getLength(); i++)
  {
    int contextNode = contextNodes.item(i);
    xctxt.pushCurrentNode(contextNode);

    double result = 0;
    try
    {
      XPath dynamicXPath = new XPath(expr, xctxt.getSAXLocator(),
                                     xctxt.getNamespaceContext(),
                                     XPath.SELECT);
      result = dynamicXPath.execute(xctxt, contextNode, xctxt.getNamespaceContext()).num();
    }
    catch (TransformerException e)
    {
      xctxt.popCurrentNode();
      xctxt.popContextNodeList();
      return Double.NaN;
    }

    xctxt.popCurrentNode();

    if (result < minValue)
        minValue = result;
  }

  xctxt.popContextNodeList();
  return minValue;

}
 
Example 15
Source File: ExsltDynamic.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
/**
 * The dyn:min function calculates the minimum value for the nodes passed as the
 * first argument, where the value of each node is calculated dynamically using
 * an XPath expression passed as a string as the second argument.
 * <p>
 * The expressions are evaluated relative to the nodes passed as the first argument.
 * In other words, the value for each node is calculated by evaluating the XPath
 * expression with all context information being the same as that for the call to
 * the dyn:min function itself, except for the following:
 * <p>
 * <ul>
 *  <li>the context node is the node whose value is being calculated.</li>
 *  <li>the context position is the position of the node within the node set passed
 *    as the first argument to the dyn:min function, arranged in document order.</li>
 *  <li>the context size is the number of nodes passed as the first argument to the
 *    dyn:min function.</li>
 * </ul>
 * <p>
 * The dyn:min function returns the minimum of these values, calculated in exactly
 * the same way as for math:min.
 * <p>
 * If the expression string passed as the second argument is an invalid XPath expression
 * (including an empty string), this function returns NaN.
 * <p>
 * This function must take a second argument. To calculate the minimum of a set of
 * nodes based on their string values, you should use the math:min function.
 *
 * @param myContext The ExpressionContext passed by the extension processor
 * @param nl The node set
 * @param expr The expression string
 *
 * @return The minimum evaluation value
 */
public static double min(ExpressionContext myContext, NodeList nl, String expr)
  throws SAXNotSupportedException
{

  XPathContext xctxt = null;
  if (myContext instanceof XPathContext.XPathExpressionContext)
    xctxt = ((XPathContext.XPathExpressionContext) myContext).getXPathContext();
  else
    throw new SAXNotSupportedException(XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_CONTEXT_PASSED, new Object[]{myContext }));

  if (expr == null || expr.length() == 0)
    return Double.NaN;

  NodeSetDTM contextNodes = new NodeSetDTM(nl, xctxt);
  xctxt.pushContextNodeList(contextNodes);

  double minValue = Double.MAX_VALUE;
  for (int i = 0; i < nl.getLength(); i++)
  {
    int contextNode = contextNodes.item(i);
    xctxt.pushCurrentNode(contextNode);

    double result = 0;
    try
    {
      XPath dynamicXPath = new XPath(expr, xctxt.getSAXLocator(),
                                     xctxt.getNamespaceContext(),
                                     XPath.SELECT);
      result = dynamicXPath.execute(xctxt, contextNode, xctxt.getNamespaceContext()).num();
    }
    catch (TransformerException e)
    {
      xctxt.popCurrentNode();
      xctxt.popContextNodeList();
      return Double.NaN;
    }

    xctxt.popCurrentNode();

    if (result < minValue)
        minValue = result;
  }

  xctxt.popContextNodeList();
  return minValue;

}
 
Example 16
Source File: FilterExprIteratorSimple.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
/**
 * Execute the expression.  Meant for reuse by other FilterExpr iterators
 * that are not derived from this object.
 */
public static XNodeSet executeFilterExpr(int context, XPathContext xctxt,
                                                                                              PrefixResolver prefixResolver,
                                                                                              boolean isTopLevel,
                                                                                              int stackFrame,
                                                                                              Expression expr )
  throws com.sun.org.apache.xml.internal.utils.WrappedRuntimeException
{
  PrefixResolver savedResolver = xctxt.getNamespaceContext();
  XNodeSet result = null;

  try
  {
    xctxt.pushCurrentNode(context);
    xctxt.setNamespaceContext(prefixResolver);

    // The setRoot operation can take place with a reset operation,
    // and so we may not be in the context of LocPathIterator#nextNode,
    // so we have to set up the variable context, execute the expression,
    // and then restore the variable context.

    if (isTopLevel)
    {
      // System.out.println("calling m_expr.execute(getXPathContext())");
      VariableStack vars = xctxt.getVarStack();

      // These three statements need to be combined into one operation.
      int savedStart = vars.getStackFrame();
      vars.setStackFrame(stackFrame);

      result = (com.sun.org.apache.xpath.internal.objects.XNodeSet) expr.execute(xctxt);
      result.setShouldCacheNodes(true);

      // These two statements need to be combined into one operation.
      vars.setStackFrame(savedStart);
    }
    else
        result = (com.sun.org.apache.xpath.internal.objects.XNodeSet) expr.execute(xctxt);

  }
  catch (javax.xml.transform.TransformerException se)
  {

    // TODO: Fix...
    throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(se);
  }
  finally
  {
    xctxt.popCurrentNode();
    xctxt.setNamespaceContext(savedResolver);
  }
  return result;
}
 
Example 17
Source File: FilterExprIteratorSimple.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Execute the expression.  Meant for reuse by other FilterExpr iterators
 * that are not derived from this object.
 */
public static XNodeSet executeFilterExpr(int context, XPathContext xctxt,
                                                                                              PrefixResolver prefixResolver,
                                                                                              boolean isTopLevel,
                                                                                              int stackFrame,
                                                                                              Expression expr )
  throws com.sun.org.apache.xml.internal.utils.WrappedRuntimeException
{
  PrefixResolver savedResolver = xctxt.getNamespaceContext();
  XNodeSet result = null;

  try
  {
    xctxt.pushCurrentNode(context);
    xctxt.setNamespaceContext(prefixResolver);

    // The setRoot operation can take place with a reset operation,
    // and so we may not be in the context of LocPathIterator#nextNode,
    // so we have to set up the variable context, execute the expression,
    // and then restore the variable context.

    if (isTopLevel)
    {
      // System.out.println("calling m_expr.execute(getXPathContext())");
      VariableStack vars = xctxt.getVarStack();

      // These three statements need to be combined into one operation.
      int savedStart = vars.getStackFrame();
      vars.setStackFrame(stackFrame);

      result = (com.sun.org.apache.xpath.internal.objects.XNodeSet) expr.execute(xctxt);
      result.setShouldCacheNodes(true);

      // These two statements need to be combined into one operation.
      vars.setStackFrame(savedStart);
    }
    else
        result = (com.sun.org.apache.xpath.internal.objects.XNodeSet) expr.execute(xctxt);

  }
  catch (javax.xml.transform.TransformerException se)
  {

    // TODO: Fix...
    throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(se);
  }
  finally
  {
    xctxt.popCurrentNode();
    xctxt.setNamespaceContext(savedResolver);
  }
  return result;
}
 
Example 18
Source File: FilterExprIteratorSimple.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Execute the expression.  Meant for reuse by other FilterExpr iterators
 * that are not derived from this object.
 */
public static XNodeSet executeFilterExpr(int context, XPathContext xctxt,
                                                                                              PrefixResolver prefixResolver,
                                                                                              boolean isTopLevel,
                                                                                              int stackFrame,
                                                                                              Expression expr )
  throws com.sun.org.apache.xml.internal.utils.WrappedRuntimeException
{
  PrefixResolver savedResolver = xctxt.getNamespaceContext();
  XNodeSet result = null;

  try
  {
    xctxt.pushCurrentNode(context);
    xctxt.setNamespaceContext(prefixResolver);

    // The setRoot operation can take place with a reset operation,
    // and so we may not be in the context of LocPathIterator#nextNode,
    // so we have to set up the variable context, execute the expression,
    // and then restore the variable context.

    if (isTopLevel)
    {
      // System.out.println("calling m_expr.execute(getXPathContext())");
      VariableStack vars = xctxt.getVarStack();

      // These three statements need to be combined into one operation.
      int savedStart = vars.getStackFrame();
      vars.setStackFrame(stackFrame);

      result = (com.sun.org.apache.xpath.internal.objects.XNodeSet) expr.execute(xctxt);
      result.setShouldCacheNodes(true);

      // These two statements need to be combined into one operation.
      vars.setStackFrame(savedStart);
    }
    else
        result = (com.sun.org.apache.xpath.internal.objects.XNodeSet) expr.execute(xctxt);

  }
  catch (javax.xml.transform.TransformerException se)
  {

    // TODO: Fix...
    throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(se);
  }
  finally
  {
    xctxt.popCurrentNode();
    xctxt.setNamespaceContext(savedResolver);
  }
  return result;
}
 
Example 19
Source File: PredicatedNodeTest.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Process the predicates.
 *
 * @param context The current context node.
 * @param xctxt The XPath runtime context.
 *
 * @return the result of executing the predicate expressions.
 *
 * @throws javax.xml.transform.TransformerException
 */
boolean executePredicates(int context, XPathContext xctxt)
        throws javax.xml.transform.TransformerException
{

  int nPredicates = getPredicateCount();
  // System.out.println("nPredicates: "+nPredicates);
  if (nPredicates == 0)
    return true;

  PrefixResolver savedResolver = xctxt.getNamespaceContext();

  try
  {
    m_predicateIndex = 0;
    xctxt.pushSubContextList(this);
    xctxt.pushNamespaceContext(m_lpi.getPrefixResolver());
    xctxt.pushCurrentNode(context);

    for (int i = 0; i < nPredicates; i++)
    {
      // System.out.println("Executing predicate expression - waiting count: "+m_lpi.getWaitingCount());
      XObject pred = m_predicates[i].execute(xctxt);
      // System.out.println("\nBack from executing predicate expression - waiting count: "+m_lpi.getWaitingCount());
      // System.out.println("pred.getType(): "+pred.getType());
      if (XObject.CLASS_NUMBER == pred.getType())
      {
        if (DEBUG_PREDICATECOUNTING)
        {
          System.out.flush();
          System.out.println("\n===== start predicate count ========");
          System.out.println("m_predicateIndex: " + m_predicateIndex);
          // System.out.println("getProximityPosition(m_predicateIndex): "
          //                   + getProximityPosition(m_predicateIndex));
          System.out.println("pred.num(): " + pred.num());
        }

        int proxPos = this.getProximityPosition(m_predicateIndex);
        int predIndex = (int) pred.num();
        if (proxPos != predIndex)
        {
          if (DEBUG_PREDICATECOUNTING)
          {
            System.out.println("\nnode context: "+nodeToString(context));
            System.out.println("index predicate is false: "+proxPos);
            System.out.println("\n===== end predicate count ========");
          }
          return false;
        }
        else if (DEBUG_PREDICATECOUNTING)
        {
          System.out.println("\nnode context: "+nodeToString(context));
          System.out.println("index predicate is true: "+proxPos);
          System.out.println("\n===== end predicate count ========");
        }

        // If there is a proximity index that will not change during the
        // course of itteration, then we know there can be no more true
        // occurances of this predicate, so flag that we're done after
        // this.
        //
        // bugzilla 14365
        // We can't set m_foundLast = true unless we're sure that -all-
        // remaining parameters are stable, or else last() fails. Fixed so
        // only sets m_foundLast if on the last predicate
        if(m_predicates[i].isStableNumber() && i == nPredicates - 1)
        {
          m_foundLast = true;
        }
      }
      else if (!pred.bool())
        return false;

      countProximityPosition(++m_predicateIndex);
    }
  }
  finally
  {
    xctxt.popCurrentNode();
    xctxt.popNamespaceContext();
    xctxt.popSubContextList();
    m_predicateIndex = -1;
  }

  return true;
}
 
Example 20
Source File: PredicatedNodeTest.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
/**
 * Process the predicates.
 *
 * @param context The current context node.
 * @param xctxt The XPath runtime context.
 *
 * @return the result of executing the predicate expressions.
 *
 * @throws javax.xml.transform.TransformerException
 */
boolean executePredicates(int context, XPathContext xctxt)
        throws javax.xml.transform.TransformerException
{

  int nPredicates = getPredicateCount();
  // System.out.println("nPredicates: "+nPredicates);
  if (nPredicates == 0)
    return true;

  PrefixResolver savedResolver = xctxt.getNamespaceContext();

  try
  {
    m_predicateIndex = 0;
    xctxt.pushSubContextList(this);
    xctxt.pushNamespaceContext(m_lpi.getPrefixResolver());
    xctxt.pushCurrentNode(context);

    for (int i = 0; i < nPredicates; i++)
    {
      // System.out.println("Executing predicate expression - waiting count: "+m_lpi.getWaitingCount());
      XObject pred = m_predicates[i].execute(xctxt);
      // System.out.println("\nBack from executing predicate expression - waiting count: "+m_lpi.getWaitingCount());
      // System.out.println("pred.getType(): "+pred.getType());
      if (XObject.CLASS_NUMBER == pred.getType())
      {
        if (DEBUG_PREDICATECOUNTING)
        {
          System.out.flush();
          System.out.println("\n===== start predicate count ========");
          System.out.println("m_predicateIndex: " + m_predicateIndex);
          // System.out.println("getProximityPosition(m_predicateIndex): "
          //                   + getProximityPosition(m_predicateIndex));
          System.out.println("pred.num(): " + pred.num());
        }

        int proxPos = this.getProximityPosition(m_predicateIndex);
        int predIndex = (int) pred.num();
        if (proxPos != predIndex)
        {
          if (DEBUG_PREDICATECOUNTING)
          {
            System.out.println("\nnode context: "+nodeToString(context));
            System.out.println("index predicate is false: "+proxPos);
            System.out.println("\n===== end predicate count ========");
          }
          return false;
        }
        else if (DEBUG_PREDICATECOUNTING)
        {
          System.out.println("\nnode context: "+nodeToString(context));
          System.out.println("index predicate is true: "+proxPos);
          System.out.println("\n===== end predicate count ========");
        }

        // If there is a proximity index that will not change during the
        // course of itteration, then we know there can be no more true
        // occurances of this predicate, so flag that we're done after
        // this.
        //
        // bugzilla 14365
        // We can't set m_foundLast = true unless we're sure that -all-
        // remaining parameters are stable, or else last() fails. Fixed so
        // only sets m_foundLast if on the last predicate
        if(m_predicates[i].isStableNumber() && i == nPredicates - 1)
        {
          m_foundLast = true;
        }
      }
      else if (!pred.bool())
        return false;

      countProximityPosition(++m_predicateIndex);
    }
  }
  finally
  {
    xctxt.popCurrentNode();
    xctxt.popNamespaceContext();
    xctxt.popSubContextList();
    m_predicateIndex = -1;
  }

  return true;
}