Java Code Examples for org.apache.xpath.XPath#MATCH_SCORE_NONE

The following examples show how to use org.apache.xpath.XPath#MATCH_SCORE_NONE . 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: TemplateList.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Given a match pattern and template association, return the 
 * score of that match.  This score or priority can always be 
 * statically calculated.
 *
 * @param matchPat The match pattern to template association.
 *
 * @return {@link org.apache.xpath.patterns.NodeTest#SCORE_NODETEST}, 
 *         {@link org.apache.xpath.patterns.NodeTest#SCORE_NONE}, 
 *         {@link org.apache.xpath.patterns.NodeTest#SCORE_NSWILD}, 
 *         {@link org.apache.xpath.patterns.NodeTest#SCORE_QNAME}, or
 *         {@link org.apache.xpath.patterns.NodeTest#SCORE_OTHER}, or 
 *         the value defined by the priority attribute of the template.
 *
 */
private double getPriorityOrScore(TemplateSubPatternAssociation matchPat)
{

  double priority = matchPat.getTemplate().getPriority();

  if (priority == XPath.MATCH_SCORE_NONE)
  {
    Expression ex = matchPat.getStepPattern();

    if (ex instanceof NodeTest)
    {
      return ((NodeTest) ex).getDefaultScore();
    }
  }

  return priority;
}
 
Example 2
Source File: ElemNumber.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Given a 'from' pattern (ala xsl:number), a match pattern
 * and a context, find the first ancestor that matches the
 * pattern (including the context handed in).
 *
 * @param xctxt The XPath runtime state for this.
 * @param fromMatchPattern The ancestor must match this pattern.
 * @param countMatchPattern The ancestor must also match this pattern.
 * @param context The node that "." expresses.
 * @param namespaceContext The context in which namespaces in the
 * queries are supposed to be expanded.
 *
 * @return the first ancestor that matches the given pattern
 *
 * @throws javax.xml.transform.TransformerException
 */
int findAncestor(
        XPathContext xctxt, XPath fromMatchPattern, XPath countMatchPattern, 
        int context, ElemNumber namespaceContext)
          throws javax.xml.transform.TransformerException
{
  DTM dtm = xctxt.getDTM(context);
  while (DTM.NULL != context)
  {
    if (null != fromMatchPattern)
    {
      if (fromMatchPattern.getMatchScore(xctxt, context)
              != XPath.MATCH_SCORE_NONE)
      {

        //context = null;
        break;
      }
    }

    if (null != countMatchPattern)
    {
      if (countMatchPattern.getMatchScore(xctxt, context)
              != XPath.MATCH_SCORE_NONE)
      {
        break;
      }
    }

    context = dtm.getParent(context);
  }

  return context;
}
 
Example 3
Source File: ElemNumber.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Given a 'from' pattern (ala xsl:number), a match pattern
 * and a context, find the first ancestor that matches the
 * pattern (including the context handed in).
 * @param xctxt The XPath runtime state for this.
 * @param fromMatchPattern The ancestor must match this pattern.
 * @param countMatchPattern The ancestor must also match this pattern.
 * @param context The node that "." expresses.
 * @param namespaceContext The context in which namespaces in the
 * queries are supposed to be expanded.
 *
 * @return the first preceding, ancestor or self node that 
 * matches the given pattern
 *
 * @throws javax.xml.transform.TransformerException
 */
private int findPrecedingOrAncestorOrSelf(
        XPathContext xctxt, XPath fromMatchPattern, XPath countMatchPattern, 
        int context, ElemNumber namespaceContext)
          throws javax.xml.transform.TransformerException
{
  DTM dtm = xctxt.getDTM(context);
  while (DTM.NULL != context)
  {
    if (null != fromMatchPattern)
    {
      if (fromMatchPattern.getMatchScore(xctxt, context)
              != XPath.MATCH_SCORE_NONE)
      {
        context = DTM.NULL;

        break;
      }
    }

    if (null != countMatchPattern)
    {
      if (countMatchPattern.getMatchScore(xctxt, context)
              != XPath.MATCH_SCORE_NONE)
      {
        break;
      }
    }

    int prevSibling = dtm.getPreviousSibling(context);

    if (DTM.NULL == prevSibling)
    {
      context = dtm.getParent(context);
    }
    else
    {

      // Now go down the chain of children of this sibling 
      context = dtm.getLastChild(prevSibling);

      if (context == DTM.NULL)
        context = prevSibling;
    }
  }

  return context;
}
 
Example 4
Source File: ElemNumber.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Get the previous node to be counted.
 *
 * @param xctxt The XPath runtime state for this.
 * @param pos The current node
 *
 * @return the previous node to be counted.
 *
 * @throws TransformerException
 */
public int getPreviousNode(XPathContext xctxt, int pos)
        throws TransformerException
{

  XPath countMatchPattern = getCountMatchPattern(xctxt, pos);
  DTM dtm = xctxt.getDTM(pos);

  if (Constants.NUMBERLEVEL_ANY == m_level)
  {
    XPath fromMatchPattern = m_fromMatchPattern;

    // Do a backwards document-order walk 'till a node is found that matches 
    // the 'from' pattern, or a node is found that matches the 'count' pattern, 
    // or the top of the tree is found.
    while (DTM.NULL != pos)
    {

      // Get the previous sibling, if there is no previous sibling, 
      // then count the parent, but if there is a previous sibling, 
      // dive down to the lowest right-hand (last) child of that sibling.
      int next = dtm.getPreviousSibling(pos);

      if (DTM.NULL == next)
      {
        next = dtm.getParent(pos);

        if ((DTM.NULL != next) && ((((null != fromMatchPattern) && (fromMatchPattern.getMatchScore(
                xctxt, next) != XPath.MATCH_SCORE_NONE))) 
            || (dtm.getNodeType(next) == DTM.DOCUMENT_NODE)))
        {
          pos = DTM.NULL;  // return null from function.

          break;  // from while loop
        }
      }
      else
      {

        // dive down to the lowest right child.
        int child = next;

        while (DTM.NULL != child)
        {
          child = dtm.getLastChild(next);

          if (DTM.NULL != child)
            next = child;
        }
      }

      pos = next;

      if ((DTM.NULL != pos)
              && ((null == countMatchPattern)
                  || (countMatchPattern.getMatchScore(xctxt, pos)
                      != XPath.MATCH_SCORE_NONE)))
      {
        break;
      }
    }
  }
  else  // NUMBERLEVEL_MULTI or NUMBERLEVEL_SINGLE
  {
    while (DTM.NULL != pos)
    {
      pos = dtm.getPreviousSibling(pos);

      if ((DTM.NULL != pos)
              && ((null == countMatchPattern)
                  || (countMatchPattern.getMatchScore(xctxt, pos)
                      != XPath.MATCH_SCORE_NONE)))
      {
        break;
      }
    }
  }

  return pos;
}
 
Example 5
Source File: ElemNumber.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Get the ancestors, up to the root, that match the
 * pattern.
 * 
 * @param xctxt The XPath runtime state for this.
 * @param node Count this node and it's ancestors.
 * @param stopAtFirstFound Flag indicating to stop after the
 * first node is found (difference between level = single
 * or multiple)
 * @return The number of ancestors that match the pattern.
 *
 * @throws javax.xml.transform.TransformerException
 */
NodeVector getMatchingAncestors(
        XPathContext xctxt, int node, boolean stopAtFirstFound)
          throws javax.xml.transform.TransformerException
{

  NodeSetDTM ancestors = new NodeSetDTM(xctxt.getDTMManager());
  XPath countMatchPattern = getCountMatchPattern(xctxt, node);
  DTM dtm = xctxt.getDTM(node);

  while (DTM.NULL != node)
  {
    if ((null != m_fromMatchPattern)
            && (m_fromMatchPattern.getMatchScore(xctxt, node)
                != XPath.MATCH_SCORE_NONE))
    {

      // The following if statement gives level="single" different 
      // behavior from level="multiple", which seems incorrect according 
      // to the XSLT spec.  For now we are leaving this in to replicate 
      // the same behavior in XT, but, for all intents and purposes we 
      // think this is a bug, or there is something about level="single" 
      // that we still don't understand.
      if (!stopAtFirstFound)
        break;
    }

    if (null == countMatchPattern)
      System.out.println(
        "Programmers error! countMatchPattern should never be null!");

    if (countMatchPattern.getMatchScore(xctxt, node)
            != XPath.MATCH_SCORE_NONE)
    {
      ancestors.addElement(node);

      if (stopAtFirstFound)
        break;
    }

    node = dtm.getParent(node);
  }

  return ancestors;
}
 
Example 6
Source File: TemplateSubPatternAssociation.java    From j2objc with Apache License 2.0 3 votes vote down vote up
/**
 * Return the mode associated with the template.
 *
 *
 * @param xctxt XPath context to use with this template
 * @param targetNode Target node
 * @param mode reference, which may be null, to the <a href="http://www.w3.org/TR/xslt#modes">current mode</a>.
 * @return The mode associated with the template.
 *
 * @throws TransformerException
 */
public boolean matches(XPathContext xctxt, int targetNode, QName mode)
        throws TransformerException
{

  double score = m_stepPattern.getMatchScore(xctxt, targetNode);

  return (XPath.MATCH_SCORE_NONE != score)
         && matchModes(mode, m_template.getMode());
}