Search in sources :

Example 31 with XNodeSet

use of org.apache.xpath.objects.XNodeSet in project robovm by robovm.

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 32 with XNodeSet

use of org.apache.xpath.objects.XNodeSet in project robovm by robovm.

the class VariableSafeAbsRef method execute.

/**
   * Dereference the variable, and return the reference value.  Note that lazy 
   * evaluation will occur.  If a variable within scope is not found, a warning 
   * will be sent to the error listener, and an empty nodeset will be returned.
   *
   *
   * @param xctxt The runtime execution context.
   *
   * @return The evaluated variable, or an empty nodeset if not found.
   *
   * @throws javax.xml.transform.TransformerException
   */
public XObject execute(XPathContext xctxt, boolean destructiveOK) throws javax.xml.transform.TransformerException {
    XNodeSet xns = (XNodeSet) super.execute(xctxt, destructiveOK);
    DTMManager dtmMgr = xctxt.getDTMManager();
    int context = xctxt.getContextNode();
    if (dtmMgr.getDTM(xns.getRoot()).getDocument() != dtmMgr.getDTM(context).getDocument()) {
        Expression expr = (Expression) xns.getContainedIter();
        xns = (XNodeSet) expr.asIterator(xctxt, context);
    }
    return xns;
}
Also used : DTMManager(org.apache.xml.dtm.DTMManager) Expression(org.apache.xpath.Expression) XNodeSet(org.apache.xpath.objects.XNodeSet)

Aggregations

XNodeSet (org.apache.xpath.objects.XNodeSet)32 XObject (org.apache.xpath.objects.XObject)16 DTM (org.apache.xml.dtm.DTM)8 DTMIterator (org.apache.xml.dtm.DTMIterator)8 TransformerException (javax.xml.transform.TransformerException)6 XMLString (org.apache.xml.utils.XMLString)6 ArrayList (java.util.ArrayList)4 Hashtable (java.util.Hashtable)4 Vector (java.util.Vector)4 QName (javax.xml.namespace.QName)4 XPathFunction (javax.xml.xpath.XPathFunction)4 XPathFunctionException (javax.xml.xpath.XPathFunctionException)4 Expression (org.apache.xpath.Expression)4 NodeSetDTM (org.apache.xpath.NodeSetDTM)4 XPathContext (org.apache.xpath.XPathContext)4 ElemTemplateElement (org.apache.xalan.templates.ElemTemplateElement)2 KeyDeclaration (org.apache.xalan.templates.KeyDeclaration)2 KeyManager (org.apache.xalan.transformer.KeyManager)2 TransformerImpl (org.apache.xalan.transformer.TransformerImpl)2 DTMManager (org.apache.xml.dtm.DTMManager)2