Search in sources :

Example 1 with LocPathIterator

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

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 2 with LocPathIterator

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

the class RedundentExprEliminator method createMultistepExprList.

/**
   * For the reduction of location path parts, create a list of all 
   * the multistep paths with more than one step, sorted by the 
   * number of steps, with the most steps occuring earlier in the list.
   * If the list is only one member, don't bother returning it.
   * 
   * @param paths Vector of ExpressionOwner objects, which may contain null entries. 
   *              The ExpressionOwner objects must own LocPathIterator objects.
   * @return null if no multipart paths are found or the list is only of length 1, 
   * otherwise the first MultistepExprHolder in a linked list of these objects.
   */
protected MultistepExprHolder createMultistepExprList(Vector paths) {
    MultistepExprHolder first = null;
    int n = paths.size();
    for (int i = 0; i < n; i++) {
        ExpressionOwner eo = (ExpressionOwner) paths.elementAt(i);
        if (null == eo)
            continue;
        // Assuming location path iterators should be OK.
        LocPathIterator lpi = (LocPathIterator) eo.getExpression();
        int numPaths = countSteps(lpi);
        if (numPaths > 1) {
            if (null == first)
                first = new MultistepExprHolder(eo, numPaths, null);
            else
                first = first.addInSortedOrder(eo, numPaths);
        }
    }
    if ((null == first) || (first.getLength() <= 1))
        return null;
    else
        return first;
}
Also used : LocPathIterator(org.apache.xpath.axes.LocPathIterator) ExpressionOwner(org.apache.xpath.ExpressionOwner)

Example 3 with LocPathIterator

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

the class FuncCurrent method execute.

/**
   * Execute the function.  The function must return
   * a valid object.
   * @param xctxt The current execution context.
   * @return A valid XObject.
   *
   * @throws javax.xml.transform.TransformerException
   */
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException {
    SubContextList subContextList = xctxt.getCurrentNodeList();
    int currentNode = DTM.NULL;
    if (null != subContextList) {
        if (subContextList instanceof PredicatedNodeTest) {
            LocPathIterator iter = ((PredicatedNodeTest) subContextList).getLocPathIterator();
            currentNode = iter.getCurrentContextNode();
        } else if (subContextList instanceof StepPattern) {
            throw new RuntimeException(XSLMessages.createMessage(XSLTErrorResources.ER_PROCESSOR_ERROR, null));
        }
    } else {
        // not predicate => ContextNode == CurrentNode
        currentNode = xctxt.getContextNode();
    }
    return new XNodeSet(currentNode, xctxt.getDTMManager());
}
Also used : PredicatedNodeTest(org.apache.xpath.axes.PredicatedNodeTest) StepPattern(org.apache.xpath.patterns.StepPattern) LocPathIterator(org.apache.xpath.axes.LocPathIterator) SubContextList(org.apache.xpath.axes.SubContextList) XNodeSet(org.apache.xpath.objects.XNodeSet)

Example 4 with LocPathIterator

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

the class RedundentExprEliminator method oldFindAndEliminateRedundant.

/**
 * To be removed.
 */
protected int oldFindAndEliminateRedundant(int start, int firstOccuranceIndex, ExpressionOwner firstOccuranceOwner, ElemTemplateElement psuedoVarRecipient, Vector paths) throws org.w3c.dom.DOMException {
    QName uniquePseudoVarName = null;
    boolean foundFirst = false;
    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;
    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 (!foundFirst) {
                    foundFirst = true;
                    // Insert variable decl into psuedoVarRecipient
                    // We want to insert this into the first legitimate
                    // position for a variable.
                    ElemVariable var = createPseudoVarDecl(psuedoVarRecipient, lpi, isGlobal);
                    if (null == var)
                        return 0;
                    uniquePseudoVarName = var.getName();
                    changeToVarRef(uniquePseudoVarName, firstOccuranceOwner, paths, psuedoVarRecipient);
                    // Replace the first occurance with the variable's XPath, so
                    // that further reduction may take place if needed.
                    paths.setElementAt(var.getSelect(), firstOccuranceIndex);
                    numPathsFound++;
                }
                changeToVarRef(uniquePseudoVarName, owner2, paths, psuedoVarRecipient);
                // 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) && (paths == m_absPaths)) {
        ElemVariable var = createPseudoVarDecl(psuedoVarRecipient, lpi, true);
        if (null == var)
            return 0;
        uniquePseudoVarName = var.getName();
        changeToVarRef(uniquePseudoVarName, firstOccuranceOwner, paths, psuedoVarRecipient);
        paths.setElementAt(var.getSelect(), firstOccuranceIndex);
        numPathsFound++;
    }
    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 5 with LocPathIterator

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

the class RedundentExprEliminator method oldFindAndEliminateRedundant.

/**
   * To be removed.
   */
protected int oldFindAndEliminateRedundant(int start, int firstOccuranceIndex, ExpressionOwner firstOccuranceOwner, ElemTemplateElement psuedoVarRecipient, Vector paths) throws org.w3c.dom.DOMException {
    QName uniquePseudoVarName = null;
    boolean foundFirst = false;
    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;
    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 (!foundFirst) {
                    foundFirst = true;
                    // Insert variable decl into psuedoVarRecipient
                    // We want to insert this into the first legitimate 
                    // position for a variable.
                    ElemVariable var = createPseudoVarDecl(psuedoVarRecipient, lpi, isGlobal);
                    if (null == var)
                        return 0;
                    uniquePseudoVarName = var.getName();
                    changeToVarRef(uniquePseudoVarName, firstOccuranceOwner, paths, psuedoVarRecipient);
                    // Replace the first occurance with the variable's XPath, so  
                    // that further reduction may take place if needed.
                    paths.setElementAt(var.getSelect(), firstOccuranceIndex);
                    numPathsFound++;
                }
                changeToVarRef(uniquePseudoVarName, owner2, paths, psuedoVarRecipient);
                // 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) && (paths == m_absPaths)) {
        ElemVariable var = createPseudoVarDecl(psuedoVarRecipient, lpi, true);
        if (null == var)
            return 0;
        uniquePseudoVarName = var.getName();
        changeToVarRef(uniquePseudoVarName, firstOccuranceOwner, paths, psuedoVarRecipient);
        paths.setElementAt(var.getSelect(), firstOccuranceIndex);
        numPathsFound++;
    }
    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)

Aggregations

LocPathIterator (org.apache.xpath.axes.LocPathIterator)10 ExpressionOwner (org.apache.xpath.ExpressionOwner)8 QName (org.apache.xml.utils.QName)4 Expression (org.apache.xpath.Expression)4 PredicatedNodeTest (org.apache.xpath.axes.PredicatedNodeTest)2 SubContextList (org.apache.xpath.axes.SubContextList)2 WalkingIterator (org.apache.xpath.axes.WalkingIterator)2 XNodeSet (org.apache.xpath.objects.XNodeSet)2 StepPattern (org.apache.xpath.patterns.StepPattern)2