Search in sources :

Example 56 with XObject

use of org.apache.xpath.objects.XObject in project j2objc by google.

the class MatchPatternIterator method acceptNode.

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

Example 57 with XObject

use of org.apache.xpath.objects.XObject in project j2objc by google.

the class StepPattern method executePredicates.

/**
   * Execute the predicates on this step to determine if the current node 
   * should be filtered or accepted.
   *
   * @param xctxt The XPath runtime context.
   * @param dtm The DTM of the current node.
   * @param currentNode The current node context.
   *
   * @return true if the node should be accepted, false otherwise.
   *
   * @throws javax.xml.transform.TransformerException
   */
protected final boolean executePredicates(XPathContext xctxt, DTM dtm, int currentNode) throws javax.xml.transform.TransformerException {
    boolean result = true;
    boolean positionAlreadySeen = false;
    int n = getPredicateCount();
    try {
        xctxt.pushSubContextList(this);
        for (int i = 0; i < n; i++) {
            xctxt.pushPredicatePos(i);
            try {
                XObject pred = m_predicates[i].execute(xctxt);
                try {
                    if (XObject.CLASS_NUMBER == pred.getType()) {
                        int pos = (int) pred.num();
                        if (positionAlreadySeen) {
                            result = (pos == 1);
                            break;
                        } else {
                            positionAlreadySeen = true;
                            if (!checkProximityPosition(xctxt, i, dtm, currentNode, pos)) {
                                result = false;
                                break;
                            }
                        }
                    } else if (!pred.boolWithSideEffects()) {
                        result = false;
                        break;
                    }
                } finally {
                    pred.detach();
                }
            } finally {
                xctxt.popPredicatePos();
            }
        }
    } finally {
        xctxt.popSubContextList();
    }
    return result;
}
Also used : XObject(org.apache.xpath.objects.XObject)

Example 58 with XObject

use of org.apache.xpath.objects.XObject in project j2objc by google.

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 59 with XObject

use of org.apache.xpath.objects.XObject in project j2objc by google.

the class StepPattern method execute.

/**
   * Execute an expression in the XPath runtime context, and return the
   * result of the expression.
   *
   *
   * @param xctxt The XPath runtime context.
   * @param currentNode The currentNode.
   * @param dtm The DTM of the current node.
   * @param expType The expanded type ID of the current node.
   *
   * @return The result of the expression in the form of a <code>XObject</code>.
   *
   * @throws javax.xml.transform.TransformerException if a runtime exception
   *         occurs.
   */
public XObject execute(XPathContext xctxt, int currentNode, DTM dtm, int expType) throws javax.xml.transform.TransformerException {
    if (m_whatToShow == NodeTest.SHOW_BYFUNCTION) {
        if (null != m_relativePathPattern) {
            return m_relativePathPattern.execute(xctxt);
        } else
            return NodeTest.SCORE_NONE;
    }
    XObject score;
    score = super.execute(xctxt, currentNode, dtm, expType);
    if (score == NodeTest.SCORE_NONE)
        return NodeTest.SCORE_NONE;
    if (getPredicateCount() != 0) {
        if (!executePredicates(xctxt, dtm, currentNode))
            return NodeTest.SCORE_NONE;
    }
    if (null != m_relativePathPattern)
        return m_relativePathPattern.executeRelativePathPattern(xctxt, dtm, currentNode);
    return score;
}
Also used : XObject(org.apache.xpath.objects.XObject)

Example 60 with XObject

use of org.apache.xpath.objects.XObject in project j2objc by google.

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)

Aggregations

XObject (org.apache.xpath.objects.XObject)102 TransformerException (javax.xml.transform.TransformerException)24 DTM (org.apache.xml.dtm.DTM)24 XPathContext (org.apache.xpath.XPathContext)24 XNodeSet (org.apache.xpath.objects.XNodeSet)14 DTMIterator (org.apache.xml.dtm.DTMIterator)12 VariableStack (org.apache.xpath.VariableStack)10 Vector (java.util.Vector)8 QName (org.apache.xml.utils.QName)8 Expression (org.apache.xpath.Expression)8 Node (org.w3c.dom.Node)7 DTMAxisTraverser (org.apache.xml.dtm.DTMAxisTraverser)6 XMLString (org.apache.xml.utils.XMLString)6 org.apache.xpath (org.apache.xpath)6 XString (org.apache.xpath.objects.XString)6 ArrayList (java.util.ArrayList)4 Hashtable (java.util.Hashtable)4 QName (javax.xml.namespace.QName)4 XPathFunction (javax.xml.xpath.XPathFunction)4 XPathFunctionException (javax.xml.xpath.XPathFunctionException)4