Search in sources :

Example 1 with AxesWalker

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

the class RedundentExprEliminator method changePartToRef.

/**
   * Change a given number of steps to a single variable reference.
   * 
   * @param uniquePseudoVarName The name of the variable reference.
   * @param wi The walking iterator that is to be changed.
   * @param numSteps The number of steps to be changed.
   * @param isGlobal true if this will be a global reference.
   */
protected LocPathIterator changePartToRef(final QName uniquePseudoVarName, WalkingIterator wi, final int numSteps, final boolean isGlobal) {
    Variable var = new Variable();
    var.setQName(uniquePseudoVarName);
    var.setIsGlobal(isGlobal);
    if (isGlobal) {
        ElemTemplateElement elem = getElemFromExpression(wi);
        StylesheetRoot root = elem.getStylesheetRoot();
        Vector vars = root.getVariablesAndParamsComposed();
        var.setIndex(vars.size() - 1);
    }
    // Walk to the first walker after the one's we are replacing.
    AxesWalker walker = wi.getFirstWalker();
    for (int i = 0; i < numSteps; i++) {
        assertion(null != walker, "Walker should not be null!");
        walker = walker.getNextWalker();
    }
    if (null != walker) {
        FilterExprWalker few = new FilterExprWalker(wi);
        few.setInnerExpression(var);
        few.exprSetParent(wi);
        few.setNextWalker(walker);
        walker.setPrevWalker(few);
        wi.setFirstWalker(few);
        return wi;
    } else {
        FilterExprIteratorSimple feis = new FilterExprIteratorSimple(var);
        feis.exprSetParent(wi.exprGetParent());
        return feis;
    }
}
Also used : Variable(org.apache.xpath.operations.Variable) FilterExprWalker(org.apache.xpath.axes.FilterExprWalker) AxesWalker(org.apache.xpath.axes.AxesWalker) FilterExprIteratorSimple(org.apache.xpath.axes.FilterExprIteratorSimple) Vector(java.util.Vector)

Example 2 with AxesWalker

use of org.apache.xpath.axes.AxesWalker 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 3 with AxesWalker

use of org.apache.xpath.axes.AxesWalker 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 4 with AxesWalker

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

the class RedundentExprEliminator method changePartToRef.

/**
 * Change a given number of steps to a single variable reference.
 *
 * @param uniquePseudoVarName The name of the variable reference.
 * @param wi The walking iterator that is to be changed.
 * @param numSteps The number of steps to be changed.
 * @param isGlobal true if this will be a global reference.
 */
protected LocPathIterator changePartToRef(final QName uniquePseudoVarName, WalkingIterator wi, final int numSteps, final boolean isGlobal) {
    Variable var = new Variable();
    var.setQName(uniquePseudoVarName);
    var.setIsGlobal(isGlobal);
    if (isGlobal) {
        ElemTemplateElement elem = getElemFromExpression(wi);
        StylesheetRoot root = elem.getStylesheetRoot();
        Vector vars = root.getVariablesAndParamsComposed();
        var.setIndex(vars.size() - 1);
    }
    // Walk to the first walker after the one's we are replacing.
    AxesWalker walker = wi.getFirstWalker();
    for (int i = 0; i < numSteps; i++) {
        assertion(null != walker, "Walker should not be null!");
        walker = walker.getNextWalker();
    }
    if (null != walker) {
        FilterExprWalker few = new FilterExprWalker(wi);
        few.setInnerExpression(var);
        few.exprSetParent(wi);
        few.setNextWalker(walker);
        walker.setPrevWalker(few);
        wi.setFirstWalker(few);
        return wi;
    } else {
        FilterExprIteratorSimple feis = new FilterExprIteratorSimple(var);
        feis.exprSetParent(wi.exprGetParent());
        return feis;
    }
}
Also used : Variable(org.apache.xpath.operations.Variable) FilterExprWalker(org.apache.xpath.axes.FilterExprWalker) AxesWalker(org.apache.xpath.axes.AxesWalker) FilterExprIteratorSimple(org.apache.xpath.axes.FilterExprIteratorSimple) Vector(java.util.Vector)

Example 5 with AxesWalker

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

the class RedundentExprEliminator method stepsEqual.

/**
 * Compare a given number of steps between two iterators, to see if they are equal.
 *
 * @param iter1 The first iterator to compare.
 * @param iter2 The second iterator to compare.
 * @param numSteps The number of steps to compare.
 * @return true If the given number of steps are equal.
 */
protected boolean stepsEqual(WalkingIterator iter1, WalkingIterator iter2, int numSteps) {
    AxesWalker aw1 = iter1.getFirstWalker();
    AxesWalker aw2 = iter2.getFirstWalker();
    for (int i = 0; (i < numSteps); i++) {
        if ((null == aw1) || (null == aw2))
            return false;
        if (!aw1.deepEquals(aw2))
            return false;
        aw1 = aw1.getNextWalker();
        aw2 = aw2.getNextWalker();
    }
    assertion((null != aw1) || (null != aw2), "Total match is incorrect!");
    return true;
}
Also used : AxesWalker(org.apache.xpath.axes.AxesWalker)

Aggregations

AxesWalker (org.apache.xpath.axes.AxesWalker)8 WalkingIterator (org.apache.xpath.axes.WalkingIterator)4 Vector (java.util.Vector)2 WrappedRuntimeException (org.apache.xml.utils.WrappedRuntimeException)2 FilterExprIteratorSimple (org.apache.xpath.axes.FilterExprIteratorSimple)2 FilterExprWalker (org.apache.xpath.axes.FilterExprWalker)2 Variable (org.apache.xpath.operations.Variable)2