Search in sources :

Example 51 with DTM

use of org.apache.xml.dtm.DTM in project j2objc by google.

the class XPath method getMatchScore.

/**
   * Get the match score of the given node.
   *
   * @param xctxt XPath runtime context.
   * @param context The current source tree context node.
   * 
   * @return score, one of {@link #MATCH_SCORE_NODETEST},
   * {@link #MATCH_SCORE_NONE}, {@link #MATCH_SCORE_OTHER}, 
   * or {@link #MATCH_SCORE_QNAME}.
   *
   * @throws javax.xml.transform.TransformerException
   */
public double getMatchScore(XPathContext xctxt, int context) throws javax.xml.transform.TransformerException {
    xctxt.pushCurrentNode(context);
    xctxt.pushCurrentExpressionNode(context);
    try {
        XObject score = m_mainExp.execute(xctxt);
        if (DEBUG_MATCHES) {
            DTM dtm = xctxt.getDTM(context);
            System.out.println("score: " + score.num() + " for " + dtm.getNodeName(context) + " for xpath " + this.getPatternString());
        }
        return score.num();
    } finally {
        xctxt.popCurrentNode();
        xctxt.popCurrentExpressionNode();
    }
// return XPath.MATCH_SCORE_NONE;
}
Also used : DTM(org.apache.xml.dtm.DTM) XObject(org.apache.xpath.objects.XObject)

Example 52 with DTM

use of org.apache.xml.dtm.DTM in project j2objc by google.

the class FuncKey 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 {
    // TransformerImpl transformer = (TransformerImpl)xctxt;
    TransformerImpl transformer = (TransformerImpl) xctxt.getOwnerObject();
    XNodeSet nodes = null;
    int context = xctxt.getCurrentNode();
    DTM dtm = xctxt.getDTM(context);
    int docContext = dtm.getDocumentRoot(context);
    if (DTM.NULL == docContext) {
    // path.error(context, XPATHErrorResources.ER_CONTEXT_HAS_NO_OWNERDOC); //"context does not have an owner document!");
    }
    String xkeyname = getArg0().execute(xctxt).str();
    QName keyname = new QName(xkeyname, xctxt.getNamespaceContext());
    XObject arg = getArg1().execute(xctxt);
    boolean argIsNodeSetDTM = (XObject.CLASS_NODESET == arg.getType());
    KeyManager kmgr = transformer.getKeyManager();
    // Don't bother with nodeset logic if the thing is only one node.
    if (argIsNodeSetDTM) {
        XNodeSet ns = (XNodeSet) arg;
        ns.setShouldCacheNodes(true);
        int len = ns.getLength();
        if (len <= 1)
            argIsNodeSetDTM = false;
    }
    if (argIsNodeSetDTM) {
        Hashtable usedrefs = null;
        DTMIterator ni = arg.iter();
        int pos;
        UnionPathIterator upi = new UnionPathIterator();
        upi.exprSetParent(this);
        while (DTM.NULL != (pos = ni.nextNode())) {
            dtm = xctxt.getDTM(pos);
            XMLString ref = dtm.getStringValue(pos);
            if (null == ref)
                continue;
            if (null == usedrefs)
                usedrefs = new Hashtable();
            if (usedrefs.get(ref) != null) {
                // We already have 'em.
                continue;
            } else {
                // ISTRUE being used as a dummy value.
                usedrefs.put(ref, ISTRUE);
            }
            XNodeSet nl = kmgr.getNodeSetDTMByKey(xctxt, docContext, keyname, ref, xctxt.getNamespaceContext());
            nl.setRoot(xctxt.getCurrentNode(), xctxt);
            //        try
            //        {
            upi.addIterator(nl);
        //        }
        //        catch(CloneNotSupportedException cnse)
        //        {
        //          // will never happen.
        //        }
        //mnodeset.addNodesInDocOrder(nl, xctxt); needed??
        }
        int current = xctxt.getCurrentNode();
        upi.setRoot(current, xctxt);
        nodes = new XNodeSet(upi);
    } else {
        XMLString ref = arg.xstr();
        nodes = kmgr.getNodeSetDTMByKey(xctxt, docContext, keyname, ref, xctxt.getNamespaceContext());
        nodes.setRoot(xctxt.getCurrentNode(), xctxt);
    }
    return nodes;
}
Also used : TransformerImpl(org.apache.xalan.transformer.TransformerImpl) UnionPathIterator(org.apache.xpath.axes.UnionPathIterator) QName(org.apache.xml.utils.QName) Hashtable(java.util.Hashtable) XMLString(org.apache.xml.utils.XMLString) XNodeSet(org.apache.xpath.objects.XNodeSet) DTMIterator(org.apache.xml.dtm.DTMIterator) DTM(org.apache.xml.dtm.DTM) XMLString(org.apache.xml.utils.XMLString) KeyManager(org.apache.xalan.transformer.KeyManager) XObject(org.apache.xpath.objects.XObject)

Example 53 with DTM

use of org.apache.xml.dtm.DTM in project j2objc by google.

the class ElemNumber method getPreviousNode.

/**
   * Get the previous node to be counted.
   *
   * @param xctxt The XPath runtime state for this.
   * @param pos The current node
   *
   * @return the previous node to be counted.
   *
   * @throws TransformerException
   */
public int getPreviousNode(XPathContext xctxt, int pos) throws TransformerException {
    XPath countMatchPattern = getCountMatchPattern(xctxt, pos);
    DTM dtm = xctxt.getDTM(pos);
    if (Constants.NUMBERLEVEL_ANY == m_level) {
        XPath fromMatchPattern = m_fromMatchPattern;
        // or the top of the tree is found.
        while (DTM.NULL != pos) {
            // Get the previous sibling, if there is no previous sibling, 
            // then count the parent, but if there is a previous sibling, 
            // dive down to the lowest right-hand (last) child of that sibling.
            int next = dtm.getPreviousSibling(pos);
            if (DTM.NULL == next) {
                next = dtm.getParent(pos);
                if ((DTM.NULL != next) && ((((null != fromMatchPattern) && (fromMatchPattern.getMatchScore(xctxt, next) != XPath.MATCH_SCORE_NONE))) || (dtm.getNodeType(next) == DTM.DOCUMENT_NODE))) {
                    // return null from function.
                    pos = DTM.NULL;
                    // from while loop
                    break;
                }
            } else {
                // dive down to the lowest right child.
                int child = next;
                while (DTM.NULL != child) {
                    child = dtm.getLastChild(next);
                    if (DTM.NULL != child)
                        next = child;
                }
            }
            pos = next;
            if ((DTM.NULL != pos) && ((null == countMatchPattern) || (countMatchPattern.getMatchScore(xctxt, pos) != XPath.MATCH_SCORE_NONE))) {
                break;
            }
        }
    } else // NUMBERLEVEL_MULTI or NUMBERLEVEL_SINGLE
    {
        while (DTM.NULL != pos) {
            pos = dtm.getPreviousSibling(pos);
            if ((DTM.NULL != pos) && ((null == countMatchPattern) || (countMatchPattern.getMatchScore(xctxt, pos) != XPath.MATCH_SCORE_NONE))) {
                break;
            }
        }
    }
    return pos;
}
Also used : XPath(org.apache.xpath.XPath) NodeSetDTM(org.apache.xpath.NodeSetDTM) DTM(org.apache.xml.dtm.DTM)

Example 54 with DTM

use of org.apache.xml.dtm.DTM in project j2objc by google.

the class KeyTable method getRefsTable.

/**
   * @return lazy initialized refs table associating evaluation of key function
   *         with a XNodeSet
   */
private Hashtable getRefsTable() {
    if (m_refsTable == null) {
        // initial capacity set to a prime number to improve hash performance
        m_refsTable = new Hashtable(89);
        KeyIterator ki = (KeyIterator) (m_keyNodes).getContainedIter();
        XPathContext xctxt = ki.getXPathContext();
        Vector keyDecls = getKeyDeclarations();
        int nKeyDecls = keyDecls.size();
        int currentNode;
        m_keyNodes.reset();
        while (DTM.NULL != (currentNode = m_keyNodes.nextNode())) {
            try {
                for (int keyDeclIdx = 0; keyDeclIdx < nKeyDecls; keyDeclIdx++) {
                    KeyDeclaration keyDeclaration = (KeyDeclaration) keyDecls.elementAt(keyDeclIdx);
                    XObject xuse = keyDeclaration.getUse().execute(xctxt, currentNode, ki.getPrefixResolver());
                    if (xuse.getType() != xuse.CLASS_NODESET) {
                        XMLString exprResult = xuse.xstr();
                        addValueInRefsTable(xctxt, exprResult, currentNode);
                    } else {
                        DTMIterator i = ((XNodeSet) xuse).iterRaw();
                        int currentNodeInUseClause;
                        while (DTM.NULL != (currentNodeInUseClause = i.nextNode())) {
                            DTM dtm = xctxt.getDTM(currentNodeInUseClause);
                            XMLString exprResult = dtm.getStringValue(currentNodeInUseClause);
                            addValueInRefsTable(xctxt, exprResult, currentNode);
                        }
                    }
                }
            } catch (TransformerException te) {
                throw new WrappedRuntimeException(te);
            }
        }
    }
    return m_refsTable;
}
Also used : Hashtable(java.util.Hashtable) DTMIterator(org.apache.xml.dtm.DTMIterator) XNodeSet(org.apache.xpath.objects.XNodeSet) KeyDeclaration(org.apache.xalan.templates.KeyDeclaration) XPathContext(org.apache.xpath.XPathContext) XMLString(org.apache.xml.utils.XMLString) DTM(org.apache.xml.dtm.DTM) Vector(java.util.Vector) XObject(org.apache.xpath.objects.XObject) TransformerException(javax.xml.transform.TransformerException) WrappedRuntimeException(org.apache.xml.utils.WrappedRuntimeException)

Example 55 with DTM

use of org.apache.xml.dtm.DTM in project j2objc by google.

the class SerializerUtils method outputResultTreeFragment.

/**
     * Given a result tree fragment, walk the tree and
     * output it to the SerializationHandler.
     *
     * @param obj Result tree fragment object
     * @param support XPath context for the result tree fragment
     *
     * @throws org.xml.sax.SAXException
     */
public static void outputResultTreeFragment(SerializationHandler handler, XObject obj, XPathContext support) throws org.xml.sax.SAXException {
    int doc = obj.rtf();
    DTM dtm = support.getDTM(doc);
    if (null != dtm) {
        for (int n = dtm.getFirstChild(doc); DTM.NULL != n; n = dtm.getNextSibling(n)) {
            handler.flushPending();
            // to flush prefixes, will that cause problems ???
            if (dtm.getNodeType(n) == DTM.ELEMENT_NODE && dtm.getNamespaceURI(n) == null)
                handler.startPrefixMapping("", "");
            dtm.dispatchToEvents(n, handler);
        }
    }
}
Also used : DTM(org.apache.xml.dtm.DTM)

Aggregations

DTM (org.apache.xml.dtm.DTM)100 XObject (org.apache.xpath.objects.XObject)24 DTMIterator (org.apache.xml.dtm.DTMIterator)23 NodeSetDTM (org.apache.xpath.NodeSetDTM)20 TransformerException (javax.xml.transform.TransformerException)17 XPathContext (org.apache.xpath.XPathContext)16 XMLString (org.apache.xml.utils.XMLString)12 XString (org.apache.xpath.objects.XString)10 SAXException (org.xml.sax.SAXException)10 XNodeSet (org.apache.xpath.objects.XNodeSet)8 XPath (org.apache.xpath.XPath)7 IOException (java.io.IOException)6 Vector (java.util.Vector)6 TransformerImpl (org.apache.xalan.transformer.TransformerImpl)6 DTMAxisTraverser (org.apache.xml.dtm.DTMAxisTraverser)6 SerializationHandler (org.apache.xml.serializer.SerializationHandler)6 QName (org.apache.xml.utils.QName)6 DOMSource (javax.xml.transform.dom.DOMSource)5 Hashtable (java.util.Hashtable)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4