Search in sources :

Example 11 with ExpressionOwner

use of org.apache.xpath.ExpressionOwner in project j2objc by google.

the class RedundentExprEliminator method findAndEliminateRedundant.

/**
 * Look through the vector from start point, looking for redundant occurances.
 * When one or more are found, create a psuedo variable declaration, insert
 * it into the stylesheet, and replace the occurance with a reference to
 * the psuedo variable.  When a redundent variable is found, it's slot in
 * the vector will be replaced by null.
 *
 * @param start The position to start looking in the vector.
 * @param firstOccuranceIndex The position of firstOccuranceOwner.
 * @param firstOccuranceOwner The owner of the expression we are looking for.
 * @param psuedoVarRecipient Where to put the psuedo variables.
 *
 * @return The number of expression occurances that were modified.
 */
protected int findAndEliminateRedundant(int start, int firstOccuranceIndex, ExpressionOwner firstOccuranceOwner, ElemTemplateElement psuedoVarRecipient, Vector paths) throws org.w3c.dom.DOMException {
    MultistepExprHolder head = null;
    MultistepExprHolder tail = null;
    int numPathsFound = 0;
    int n = paths.size();
    Expression expr1 = firstOccuranceOwner.getExpression();
    if (DEBUG)
        assertIsLocPathIterator(expr1, firstOccuranceOwner);
    boolean isGlobal = (paths == m_absPaths);
    LocPathIterator lpi = (LocPathIterator) expr1;
    int stepCount = countSteps(lpi);
    for (int j = start; j < n; j++) {
        ExpressionOwner owner2 = (ExpressionOwner) paths.elementAt(j);
        if (null != owner2) {
            Expression expr2 = owner2.getExpression();
            boolean isEqual = expr2.deepEquals(lpi);
            if (isEqual) {
                LocPathIterator lpi2 = (LocPathIterator) expr2;
                if (null == head) {
                    head = new MultistepExprHolder(firstOccuranceOwner, stepCount, null);
                    tail = head;
                    numPathsFound++;
                }
                tail.m_next = new MultistepExprHolder(owner2, stepCount, null);
                tail = tail.m_next;
                // Null out the occurance, so we don't have to test it again.
                paths.setElementAt(null, j);
                // foundFirst = true;
                numPathsFound++;
            }
        }
    }
    // Change all globals in xsl:templates, etc, to global vars no matter what.
    if ((0 == numPathsFound) && isGlobal) {
        head = new MultistepExprHolder(firstOccuranceOwner, stepCount, null);
        numPathsFound++;
    }
    if (null != head) {
        ElemTemplateElement root = isGlobal ? psuedoVarRecipient : findCommonAncestor(head);
        LocPathIterator sharedIter = (LocPathIterator) head.m_exprOwner.getExpression();
        ElemVariable var = createPseudoVarDecl(root, sharedIter, isGlobal);
        if (DIAGNOSE_MULTISTEPLIST)
            System.err.println("Created var: " + var.getName() + (isGlobal ? "(Global)" : ""));
        QName uniquePseudoVarName = var.getName();
        while (null != head) {
            ExpressionOwner owner = head.m_exprOwner;
            if (DIAGNOSE_MULTISTEPLIST)
                diagnoseLineNumber(owner.getExpression());
            changeToVarRef(uniquePseudoVarName, owner, paths, root);
            head = head.m_next;
        }
        // Replace the first occurance with the variable's XPath, so
        // that further reduction may take place if needed.
        paths.setElementAt(var.getSelect(), firstOccuranceIndex);
    }
    return numPathsFound;
}
Also used : Expression(org.apache.xpath.Expression) QName(org.apache.xml.utils.QName) LocPathIterator(org.apache.xpath.axes.LocPathIterator) ExpressionOwner(org.apache.xpath.ExpressionOwner)

Example 12 with ExpressionOwner

use of org.apache.xpath.ExpressionOwner in project j2objc by google.

the class RedundentExprEliminator method matchAndEliminatePartialPaths.

/**
 * For a given path, see if there are any partitial matches in the list,
 * and, if there are, replace those partial paths with psuedo variable refs,
 * and create the psuedo variable decl.
 *
 * @return The head of the list, which may have changed.
 */
protected MultistepExprHolder matchAndEliminatePartialPaths(MultistepExprHolder testee, MultistepExprHolder head, boolean isGlobal, int lengthToTest, ElemTemplateElement varScope) {
    if (null == testee.m_exprOwner)
        return head;
    // Start with the longest possible match, and move down.
    WalkingIterator iter1 = (WalkingIterator) testee.m_exprOwner.getExpression();
    if (partialIsVariable(testee, lengthToTest))
        return head;
    MultistepExprHolder matchedPaths = null;
    MultistepExprHolder matchedPathsTail = null;
    MultistepExprHolder meh = head;
    while (null != meh) {
        if ((meh != testee) && (null != meh.m_exprOwner)) {
            WalkingIterator iter2 = (WalkingIterator) meh.m_exprOwner.getExpression();
            if (stepsEqual(iter1, iter2, lengthToTest)) {
                if (null == matchedPaths) {
                    try {
                        matchedPaths = (MultistepExprHolder) testee.clone();
                        // So it won't be processed again.
                        testee.m_exprOwner = null;
                    } catch (CloneNotSupportedException cnse) {
                    }
                    matchedPathsTail = matchedPaths;
                    matchedPathsTail.m_next = null;
                }
                try {
                    matchedPathsTail.m_next = (MultistepExprHolder) meh.clone();
                    // So it won't be processed again.
                    meh.m_exprOwner = null;
                } catch (CloneNotSupportedException cnse) {
                }
                matchedPathsTail = matchedPathsTail.m_next;
                matchedPathsTail.m_next = null;
            }
        }
        meh = meh.m_next;
    }
    int matchCount = 0;
    if (null != matchedPaths) {
        ElemTemplateElement root = isGlobal ? varScope : findCommonAncestor(matchedPaths);
        WalkingIterator sharedIter = (WalkingIterator) matchedPaths.m_exprOwner.getExpression();
        WalkingIterator newIter = createIteratorFromSteps(sharedIter, lengthToTest);
        ElemVariable var = createPseudoVarDecl(root, newIter, isGlobal);
        if (DIAGNOSE_MULTISTEPLIST)
            System.err.println("Created var: " + var.getName() + (isGlobal ? "(Global)" : ""));
        while (null != matchedPaths) {
            ExpressionOwner owner = matchedPaths.m_exprOwner;
            WalkingIterator iter = (WalkingIterator) owner.getExpression();
            if (DIAGNOSE_MULTISTEPLIST)
                diagnoseLineNumber(iter);
            LocPathIterator newIter2 = changePartToRef(var.getName(), iter, lengthToTest, isGlobal);
            owner.setExpression(newIter2);
            matchedPaths = matchedPaths.m_next;
        }
    }
    if (DIAGNOSE_MULTISTEPLIST)
        diagnoseMultistepList(matchCount, lengthToTest, isGlobal);
    return head;
}
Also used : WalkingIterator(org.apache.xpath.axes.WalkingIterator) LocPathIterator(org.apache.xpath.axes.LocPathIterator) ExpressionOwner(org.apache.xpath.ExpressionOwner)

Aggregations

ExpressionOwner (org.apache.xpath.ExpressionOwner)12 LocPathIterator (org.apache.xpath.axes.LocPathIterator)8 QName (org.apache.xml.utils.QName)4 Expression (org.apache.xpath.Expression)4 WalkingIterator (org.apache.xpath.axes.WalkingIterator)2