Search in sources :

Example 6 with NodeSetDTM

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

the class NodeSequence method addNodeInDocOrder.

/**
   * Add the node into a vector of nodes where it should occur in
   * document order.
   * @param node The node to be added.
   * @return insertIndex.
   * @throws RuntimeException thrown if this NodeSetDTM is not of 
   * a mutable type.
   */
protected int addNodeInDocOrder(int node) {
    assertion(hasCache(), "addNodeInDocOrder must be done on a mutable sequence!");
    int insertIndex = -1;
    NodeVector vec = getVector();
    // This needs to do a binary search, but a binary search 
    // is somewhat tough because the sequence test involves 
    // two nodes.
    int size = vec.size(), i;
    for (i = size - 1; i >= 0; i--) {
        int child = vec.elementAt(i);
        if (child == node) {
            // Duplicate, suppress insert
            i = -2;
            break;
        }
        DTM dtm = m_dtmMgr.getDTM(node);
        if (!dtm.isNodeAfter(node, child)) {
            break;
        }
    }
    if (i != -2) {
        insertIndex = i + 1;
        vec.insertElementAt(node, insertIndex);
    }
    // checkDups();
    return insertIndex;
}
Also used : NodeVector(org.apache.xml.utils.NodeVector) NodeSetDTM(org.apache.xpath.NodeSetDTM) DTM(org.apache.xml.dtm.DTM)

Example 7 with NodeSetDTM

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

the class ElemNumber method getMatchingAncestors.

/**
   * Get the ancestors, up to the root, that match the
   * pattern.
   * 
   * @param xctxt The XPath runtime state for this.
   * @param node Count this node and it's ancestors.
   * @param stopAtFirstFound Flag indicating to stop after the
   * first node is found (difference between level = single
   * or multiple)
   * @return The number of ancestors that match the pattern.
   *
   * @throws javax.xml.transform.TransformerException
   */
NodeVector getMatchingAncestors(XPathContext xctxt, int node, boolean stopAtFirstFound) throws javax.xml.transform.TransformerException {
    NodeSetDTM ancestors = new NodeSetDTM(xctxt.getDTMManager());
    XPath countMatchPattern = getCountMatchPattern(xctxt, node);
    DTM dtm = xctxt.getDTM(node);
    while (DTM.NULL != node) {
        if ((null != m_fromMatchPattern) && (m_fromMatchPattern.getMatchScore(xctxt, node) != XPath.MATCH_SCORE_NONE)) {
            // that we still don't understand.
            if (!stopAtFirstFound)
                break;
        }
        if (null == countMatchPattern)
            System.out.println("Programmers error! countMatchPattern should never be null!");
        if (countMatchPattern.getMatchScore(xctxt, node) != XPath.MATCH_SCORE_NONE) {
            ancestors.addElement(node);
            if (stopAtFirstFound)
                break;
        }
        node = dtm.getParent(node);
    }
    return ancestors;
}
Also used : XPath(org.apache.xpath.XPath) NodeSetDTM(org.apache.xpath.NodeSetDTM) NodeSetDTM(org.apache.xpath.NodeSetDTM) DTM(org.apache.xml.dtm.DTM)

Example 8 with NodeSetDTM

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

the class FuncDocument 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 {
    int context = xctxt.getCurrentNode();
    DTM dtm = xctxt.getDTM(context);
    int docContext = dtm.getDocumentRoot(context);
    XObject arg = (XObject) this.getArg0().execute(xctxt);
    String base = "";
    Expression arg1Expr = this.getArg1();
    if (null != arg1Expr) {
        // The URI reference may be relative. The base URI (see [3.2 Base URI]) 
        // of the node in the second argument node-set that is first in document 
        // order is used as the base URI for resolving the 
        // relative URI into an absolute URI. 
        XObject arg2 = arg1Expr.execute(xctxt);
        if (XObject.CLASS_NODESET == arg2.getType()) {
            int baseNode = arg2.iter().nextNode();
            if (baseNode == DTM.NULL) {
                // See http://www.w3.org/1999/11/REC-xslt-19991116-errata#E14.
                // If the second argument is an empty nodeset, this is an error.
                // The processor can recover by returning an empty nodeset.
                warn(xctxt, XSLTErrorResources.WG_EMPTY_SECOND_ARG, null);
                XNodeSet nodes = new XNodeSet(xctxt.getDTMManager());
                return nodes;
            } else {
                DTM baseDTM = xctxt.getDTM(baseNode);
                base = baseDTM.getDocumentBaseURI();
            }
        // %REVIEW% This doesn't seem to be a problem with the conformance
        // suite, but maybe it's just not doing a good test?
        //        int baseDoc = baseDTM.getDocument();
        //
        //        if (baseDoc == DTM.NULL /* || baseDoc instanceof Stylesheet  -->What to do?? */)
        //        {
        //
        //          // base = ((Stylesheet)baseDoc).getBaseIdentifier();
        //          base = xctxt.getNamespaceContext().getBaseIdentifier();
        //        }
        //        else
        //          base = xctxt.getSourceTreeManager().findURIFromDoc(baseDoc);
        } else {
            //Can not convert other type to a node-set!;
            arg2.iter();
        }
    } else {
        // If the second argument is omitted, then it defaults to 
        // the node in the stylesheet that contains the expression that 
        // includes the call to the document function. Note that a 
        // zero-length URI reference is a reference to the document 
        // relative to which the URI reference is being resolved; thus 
        // document("") refers to the root node of the stylesheet; 
        // the tree representation of the stylesheet is exactly 
        // the same as if the XML document containing the stylesheet 
        // was the initial source document.
        assertion(null != xctxt.getNamespaceContext(), "Namespace context can not be null!");
        base = xctxt.getNamespaceContext().getBaseIdentifier();
    }
    XNodeSet nodes = new XNodeSet(xctxt.getDTMManager());
    NodeSetDTM mnl = nodes.mutableNodeset();
    DTMIterator iterator = (XObject.CLASS_NODESET == arg.getType()) ? arg.iter() : null;
    int pos = DTM.NULL;
    while ((null == iterator) || (DTM.NULL != (pos = iterator.nextNode()))) {
        XMLString ref = (null != iterator) ? xctxt.getDTM(pos).getStringValue(pos) : arg.xstr();
        // member.
        if (null == arg1Expr && DTM.NULL != pos) {
            DTM baseDTM = xctxt.getDTM(pos);
            base = baseDTM.getDocumentBaseURI();
        }
        if (null == ref)
            continue;
        if (DTM.NULL == docContext) {
            //"context does not have an owner document!");
            error(xctxt, XSLTErrorResources.ER_NO_CONTEXT_OWNERDOC, null);
        }
        // From http://www.ics.uci.edu/pub/ietf/uri/rfc1630.txt
        // A partial form can be distinguished from an absolute form in that the
        // latter must have a colon and that colon must occur before any slash
        // characters. Systems not requiring partial forms should not use any
        // unencoded slashes in their naming schemes.  If they do, absolute URIs
        // will still work, but confusion may result.
        int indexOfColon = ref.indexOf(':');
        int indexOfSlash = ref.indexOf('/');
        if ((indexOfColon != -1) && (indexOfSlash != -1) && (indexOfColon < indexOfSlash)) {
            // The url (or filename, for that matter) is absolute.
            base = null;
        }
        int newDoc = getDoc(xctxt, context, ref.toString(), base);
        // nodes.mutableNodeset().addNode(newDoc);  
        if (DTM.NULL != newDoc) {
            // TODO: mnl.addNodeInDocOrder(newDoc, true, xctxt); ??
            if (!mnl.contains(newDoc)) {
                mnl.addElement(newDoc);
            }
        }
        if (null == iterator || newDoc == DTM.NULL)
            break;
    }
    return nodes;
}
Also used : NodeSetDTM(org.apache.xpath.NodeSetDTM) Expression(org.apache.xpath.Expression) XMLString(org.apache.xml.utils.XMLString) NodeSetDTM(org.apache.xpath.NodeSetDTM) DTM(org.apache.xml.dtm.DTM) XMLString(org.apache.xml.utils.XMLString) XObject(org.apache.xpath.objects.XObject) XNodeSet(org.apache.xpath.objects.XNodeSet) DTMIterator(org.apache.xml.dtm.DTMIterator)

Example 9 with NodeSetDTM

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

the class CountersTable method countNode.

/**
   * Count forward until the given node is found, or until
   * we have looked to the given amount.
   *
   * @param support The XPath context to use  
   * @param numberElem The given xsl:number element.
   * @param node The node to count.
   * 
   * @return The node count, or 0 if not found.
   *
   * @throws TransformerException
   */
public int countNode(XPathContext support, ElemNumber numberElem, int node) throws TransformerException {
    int count = 0;
    Vector counters = getCounters(numberElem);
    int nCounters = counters.size();
    // XPath countMatchPattern = numberElem.getCountMatchPattern(support, node);
    // XPath fromMatchPattern = numberElem.m_fromMatchPattern;
    int target = numberElem.getTargetNode(support, node);
    if (DTM.NULL != target) {
        for (int i = 0; i < nCounters; i++) {
            Counter counter = (Counter) counters.elementAt(i);
            count = counter.getPreviouslyCounted(support, target);
            if (count > 0)
                return count;
        }
        // In the loop below, we collect the nodes in backwards doc order, so 
        // we don't have to do inserts, but then we store the nodes in forwards 
        // document order, so we don't have to insert nodes into that list, 
        // so that's what the appendBtoFList stuff is all about.  In cases 
        // of forward counting by one, this will mean a single node copy from 
        // the backwards list (m_newFound) to the forwards list (counter.m_countNodes).
        count = 0;
        if (m_newFound == null)
            m_newFound = new NodeSetDTM(support.getDTMManager());
        for (; DTM.NULL != target; target = numberElem.getPreviousNode(support, target)) {
            // block above.
            if (0 != count) {
                for (int i = 0; i < nCounters; i++) {
                    Counter counter = (Counter) counters.elementAt(i);
                    int cacheLen = counter.m_countNodes.size();
                    if ((cacheLen > 0) && (counter.m_countNodes.elementAt(cacheLen - 1) == target)) {
                        count += (cacheLen + counter.m_countNodesStartCount);
                        if (cacheLen > 0)
                            appendBtoFList(counter.m_countNodes, m_newFound);
                        m_newFound.removeAllElements();
                        return count;
                    }
                }
            }
            m_newFound.addElement(target);
            count++;
        }
        // If we got to this point, then we didn't find a counter, so make 
        // one and add it to the list.
        Counter counter = new Counter(numberElem, new NodeSetDTM(support.getDTMManager()));
        // for diagnostics
        m_countersMade++;
        appendBtoFList(counter.m_countNodes, m_newFound);
        m_newFound.removeAllElements();
        counters.addElement(counter);
    }
    return count;
}
Also used : NodeSetDTM(org.apache.xpath.NodeSetDTM) Vector(java.util.Vector)

Example 10 with NodeSetDTM

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

the class FuncId method getNodesByID.

/**
   * Fill in a list with nodes that match a space delimited list if ID 
   * ID references.
   *
   * @param xctxt The runtime XPath context.
   * @param docContext The document where the nodes are being looked for.
   * @param refval A space delimited list of ID references.
   * @param usedrefs List of references for which nodes were found.
   * @param nodeSet Node set where the nodes will be added to.
   * @param mayBeMore true if there is another set of nodes to be looked for.
   *
   * @return The usedrefs value.
   */
private StringVector getNodesByID(XPathContext xctxt, int docContext, String refval, StringVector usedrefs, NodeSetDTM nodeSet, boolean mayBeMore) {
    if (null != refval) {
        String ref = null;
        //      DOMHelper dh = xctxt.getDOMHelper();
        StringTokenizer tokenizer = new StringTokenizer(refval);
        boolean hasMore = tokenizer.hasMoreTokens();
        DTM dtm = xctxt.getDTM(docContext);
        while (hasMore) {
            ref = tokenizer.nextToken();
            hasMore = tokenizer.hasMoreTokens();
            if ((null != usedrefs) && usedrefs.contains(ref)) {
                ref = null;
                continue;
            }
            int node = dtm.getElementById(ref);
            if (DTM.NULL != node)
                nodeSet.addNodeInDocOrder(node, xctxt);
            if ((null != ref) && (hasMore || mayBeMore)) {
                if (null == usedrefs)
                    usedrefs = new StringVector();
                usedrefs.addElement(ref);
            }
        }
    }
    return usedrefs;
}
Also used : StringTokenizer(java.util.StringTokenizer) StringVector(org.apache.xml.utils.StringVector) NodeSetDTM(org.apache.xpath.NodeSetDTM) DTM(org.apache.xml.dtm.DTM)

Aggregations

NodeSetDTM (org.apache.xpath.NodeSetDTM)16 DTM (org.apache.xml.dtm.DTM)10 DTMIterator (org.apache.xml.dtm.DTMIterator)4 NodeVector (org.apache.xml.utils.NodeVector)4 StringVector (org.apache.xml.utils.StringVector)4 XNodeSet (org.apache.xpath.objects.XNodeSet)4 XObject (org.apache.xpath.objects.XObject)4 StringTokenizer (java.util.StringTokenizer)2 Vector (java.util.Vector)2 XMLString (org.apache.xml.utils.XMLString)2 Expression (org.apache.xpath.Expression)2 XPath (org.apache.xpath.XPath)2