Search in sources :

Example 1 with WalkingIterator

use of org.apache.xpath.axes.WalkingIterator in project robovm by robovm.

the class RedundentExprEliminator method countSteps.

/**
   * Count the steps in a given location path.
   * 
   * @param lpi The location path iterator that owns the steps.
   * @return The number of steps in the given location path.
   */
protected int countSteps(LocPathIterator lpi) {
    if (lpi instanceof WalkingIterator) {
        WalkingIterator wi = (WalkingIterator) lpi;
        AxesWalker aw = wi.getFirstWalker();
        int count = 0;
        while (null != aw) {
            count++;
            aw = aw.getNextWalker();
        }
        return count;
    } else
        return 1;
}
Also used : AxesWalker(org.apache.xpath.axes.AxesWalker) WalkingIterator(org.apache.xpath.axes.WalkingIterator)

Example 2 with WalkingIterator

use of org.apache.xpath.axes.WalkingIterator in project robovm by robovm.

the class RedundentExprEliminator method createIteratorFromSteps.

/**
   * Create a new WalkingIterator from the steps in another WalkingIterator.
   * 
   * @param wi The iterator from where the steps will be taken.
   * @param numSteps The number of steps from the first to copy into the new 
   *                 iterator.
   * @return The new iterator.
   */
protected WalkingIterator createIteratorFromSteps(final WalkingIterator wi, int numSteps) {
    WalkingIterator newIter = new WalkingIterator(wi.getPrefixResolver());
    try {
        AxesWalker walker = (AxesWalker) wi.getFirstWalker().clone();
        newIter.setFirstWalker(walker);
        walker.setLocPathIterator(newIter);
        for (int i = 1; i < numSteps; i++) {
            AxesWalker next = (AxesWalker) walker.getNextWalker().clone();
            walker.setNextWalker(next);
            next.setLocPathIterator(newIter);
            walker = next;
        }
        walker.setNextWalker(null);
    } catch (CloneNotSupportedException cnse) {
        throw new WrappedRuntimeException(cnse);
    }
    return newIter;
}
Also used : AxesWalker(org.apache.xpath.axes.AxesWalker) WalkingIterator(org.apache.xpath.axes.WalkingIterator) WrappedRuntimeException(org.apache.xml.utils.WrappedRuntimeException)

Example 3 with WalkingIterator

use of org.apache.xpath.axes.WalkingIterator in project robovm by robovm.

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)

Example 4 with WalkingIterator

use of org.apache.xpath.axes.WalkingIterator in project j2objc by google.

the class RedundentExprEliminator method createIteratorFromSteps.

/**
 * Create a new WalkingIterator from the steps in another WalkingIterator.
 *
 * @param wi The iterator from where the steps will be taken.
 * @param numSteps The number of steps from the first to copy into the new
 *                 iterator.
 * @return The new iterator.
 */
protected WalkingIterator createIteratorFromSteps(final WalkingIterator wi, int numSteps) {
    WalkingIterator newIter = new WalkingIterator(wi.getPrefixResolver());
    try {
        AxesWalker walker = (AxesWalker) wi.getFirstWalker().clone();
        newIter.setFirstWalker(walker);
        walker.setLocPathIterator(newIter);
        for (int i = 1; i < numSteps; i++) {
            AxesWalker next = (AxesWalker) walker.getNextWalker().clone();
            walker.setNextWalker(next);
            next.setLocPathIterator(newIter);
            walker = next;
        }
        walker.setNextWalker(null);
    } catch (CloneNotSupportedException cnse) {
        throw new WrappedRuntimeException(cnse);
    }
    return newIter;
}
Also used : AxesWalker(org.apache.xpath.axes.AxesWalker) WalkingIterator(org.apache.xpath.axes.WalkingIterator) WrappedRuntimeException(org.apache.xml.utils.WrappedRuntimeException)

Example 5 with WalkingIterator

use of org.apache.xpath.axes.WalkingIterator 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

WalkingIterator (org.apache.xpath.axes.WalkingIterator)6 AxesWalker (org.apache.xpath.axes.AxesWalker)4 WrappedRuntimeException (org.apache.xml.utils.WrappedRuntimeException)2 ExpressionOwner (org.apache.xpath.ExpressionOwner)2 LocPathIterator (org.apache.xpath.axes.LocPathIterator)2