Search in sources :

Example 1 with XObject

use of org.apache.xpath.objects.XObject in project robovm by robovm.

the class PredicatedNodeTest method executePredicates.

/**
   * 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("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 ========");
                }
                // 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;
}
Also used : PrefixResolver(org.apache.xml.utils.PrefixResolver) XObject(org.apache.xpath.objects.XObject)

Example 2 with XObject

use of org.apache.xpath.objects.XObject in project robovm by robovm.

the class StepPattern method getProximityPosition.

/**
   * Get the proximity position index of the current node based on this
   * node test.
   *
   *
   * @param xctxt XPath runtime context.
   * @param predPos Which predicate we're evaluating of foo[1][2][3].
   * @param findLast If true, don't terminate when the context node is found.
   *
   * @return the proximity position index of the current node based on the
   *         node test.
   */
private final int getProximityPosition(XPathContext xctxt, int predPos, boolean findLast) {
    int pos = 0;
    int context = xctxt.getCurrentNode();
    DTM dtm = xctxt.getDTM(context);
    int parent = dtm.getParent(context);
    try {
        DTMAxisTraverser traverser = dtm.getAxisTraverser(Axis.CHILD);
        for (int child = traverser.first(parent); DTM.NULL != child; child = traverser.next(parent, child)) {
            try {
                xctxt.pushCurrentNode(child);
                if (NodeTest.SCORE_NONE != super.execute(xctxt, child)) {
                    boolean pass = true;
                    try {
                        xctxt.pushSubContextList(this);
                        for (int i = 0; i < predPos; i++) {
                            xctxt.pushPredicatePos(i);
                            try {
                                XObject pred = m_predicates[i].execute(xctxt);
                                try {
                                    if (XObject.CLASS_NUMBER == pred.getType()) {
                                        if ((pos + 1) != (int) pred.numWithSideEffects()) {
                                            pass = false;
                                            break;
                                        }
                                    } else if (!pred.boolWithSideEffects()) {
                                        pass = false;
                                        break;
                                    }
                                } finally {
                                    pred.detach();
                                }
                            } finally {
                                xctxt.popPredicatePos();
                            }
                        }
                    } finally {
                        xctxt.popSubContextList();
                    }
                    if (pass)
                        pos++;
                    if (!findLast && child == context) {
                        return pos;
                    }
                }
            } finally {
                xctxt.popCurrentNode();
            }
        }
    } catch (javax.xml.transform.TransformerException se) {
        // TODO: should keep throw sax exception...
        throw new java.lang.RuntimeException(se.getMessage());
    }
    return pos;
}
Also used : DTMAxisTraverser(org.apache.xml.dtm.DTMAxisTraverser) DTM(org.apache.xml.dtm.DTM) XObject(org.apache.xpath.objects.XObject)

Example 3 with XObject

use of org.apache.xpath.objects.XObject in project robovm by robovm.

the class StepPattern method executeRelativePathPattern.

/**
   * 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 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}.
   *
   * @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;
}
Also used : DTMAxisTraverser(org.apache.xml.dtm.DTMAxisTraverser) XObject(org.apache.xpath.objects.XObject)

Example 4 with XObject

use of org.apache.xpath.objects.XObject in project robovm by robovm.

the class StepPattern method getMatchScore.

/**
   * Get the match score of the given node.
   *
   * @param xctxt The XPath runtime context.
   * @param context The node to be tested.
   *
   * @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}.
   *
   * @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;
}
Also used : XObject(org.apache.xpath.objects.XObject)

Example 5 with XObject

use of org.apache.xpath.objects.XObject in project robovm by robovm.

the class UnionPattern method execute.

/**
   * Test a node to see if it matches any of the patterns in the union.
   *
   * @param xctxt XPath runtime context.
   *
   * @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}.
   *
   * @throws javax.xml.transform.TransformerException
   */
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException {
    XObject bestScore = null;
    int n = m_patterns.length;
    for (int i = 0; i < n; i++) {
        XObject score = m_patterns[i].execute(xctxt);
        if (score != NodeTest.SCORE_NONE) {
            if (null == bestScore)
                bestScore = score;
            else if (score.num() > bestScore.num())
                bestScore = score;
        }
    }
    if (null == bestScore) {
        bestScore = NodeTest.SCORE_NONE;
    }
    return bestScore;
}
Also used : XObject(org.apache.xpath.objects.XObject)

Aggregations

XObject (org.apache.xpath.objects.XObject)107 TransformerException (javax.xml.transform.TransformerException)27 DTM (org.apache.xml.dtm.DTM)24 XPathContext (org.apache.xpath.XPathContext)24 XNodeSet (org.apache.xpath.objects.XNodeSet)15 DTMIterator (org.apache.xml.dtm.DTMIterator)12 VariableStack (org.apache.xpath.VariableStack)11 Vector (java.util.Vector)9 QName (org.apache.xml.utils.QName)9 Node (org.w3c.dom.Node)9 Expression (org.apache.xpath.Expression)8 XString (org.apache.xpath.objects.XString)7 DTMAxisTraverser (org.apache.xml.dtm.DTMAxisTraverser)6 XMLString (org.apache.xml.utils.XMLString)6 org.apache.xpath (org.apache.xpath)6 Document (org.w3c.dom.Document)6 ArrayList (java.util.ArrayList)5 NodeVector (org.apache.xml.utils.NodeVector)5 SAXException (org.xml.sax.SAXException)5 Hashtable (java.util.Hashtable)4