Search in sources :

Example 1 with XNodeSet

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

the class Variable 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 {
    org.apache.xml.utils.PrefixResolver xprefixResolver = xctxt.getNamespaceContext();
    XObject result;
    // XObject result = xctxt.getVariable(m_qname);
    if (m_fixUpWasCalled) {
        if (m_isGlobal)
            result = xctxt.getVarStack().getGlobalVariable(xctxt, m_index, destructiveOK);
        else
            result = xctxt.getVarStack().getLocalVariable(xctxt, m_index, destructiveOK);
    } else {
        result = xctxt.getVarStack().getVariableOrParam(xctxt, m_qname);
    }
    if (null == result) {
        // This should now never happen...
        warn(xctxt, XPATHErrorResources.WG_ILLEGAL_VARIABLE_REFERENCE, //"VariableReference given for variable out "+
        new Object[] { m_qname.getLocalPart() });
        //      (new RuntimeException()).printStackTrace();
        //      error(xctxt, XPATHErrorResources.ER_COULDNOT_GET_VAR_NAMED,
        //            new Object[]{ m_qname.getLocalPart() });  //"Could not get variable named "+varName);
        result = new XNodeSet(xctxt.getDTMManager());
    }
    return result;
//    }
//    else
//    {
//      // Hack city... big time.  This is needed to evaluate xpaths from extensions, 
//      // pending some bright light going off in my head.  Some sort of callback?
//      synchronized(this)
//      {
//      	org.apache.xalan.templates.ElemVariable vvar= getElemVariable();
//      	if(null != vvar)
//      	{
//          m_index = vvar.getIndex();
//          m_isGlobal = vvar.getIsTopLevel();
//          m_fixUpWasCalled = true;
//          return execute(xctxt);
//      	}
//      }
//      throw new javax.xml.transform.TransformerException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_VAR_NOT_RESOLVABLE, new Object[]{m_qname.toString()})); //"Variable not resolvable: "+m_qname);
//    }
}
Also used : XObject(org.apache.xpath.objects.XObject) XNodeSet(org.apache.xpath.objects.XNodeSet)

Example 2 with XNodeSet

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

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

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

the class KeyTable method getNodeSetDTMByKey.

/**
   * Given a valid element key, return the corresponding node list.
   * 
   * @param name The name of the key, which must match the 'name' attribute on xsl:key.
   * @param ref The value that must match the value found by the 'match' attribute on xsl:key.
   * @return a set of nodes referenced by the key named <CODE>name</CODE> and the reference <CODE>ref</CODE>. If no node is referenced by this key, an empty node set is returned.
   */
public XNodeSet getNodeSetDTMByKey(QName name, XMLString ref) {
    XNodeSet refNodes = (XNodeSet) getRefsTable().get(ref);
    // clone wiht reset the node set
    try {
        if (refNodes != null) {
            refNodes = (XNodeSet) refNodes.cloneWithReset();
        }
    } catch (CloneNotSupportedException e) {
        refNodes = null;
    }
    if (refNodes == null) {
        //  create an empty XNodeSet
        KeyIterator ki = (KeyIterator) (m_keyNodes).getContainedIter();
        XPathContext xctxt = ki.getXPathContext();
        refNodes = new XNodeSet(xctxt.getDTMManager()) {

            public void setRoot(int nodeHandle, Object environment) {
            // Root cannot be set on non-iterated node sets. Ignore it.
            }
        };
        refNodes.reset();
    }
    return refNodes;
}
Also used : XPathContext(org.apache.xpath.XPathContext) XObject(org.apache.xpath.objects.XObject) XNodeSet(org.apache.xpath.objects.XNodeSet)

Example 4 with XNodeSet

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

the class KeyTable method addValueInRefsTable.

/**
   * Add an association between a ref and a node in the m_refsTable.
   * Requires that m_refsTable != null
   * @param xctxt XPath context
   * @param ref the value of the use clause of the current key for the given node
   * @param node the node to reference
   */
private void addValueInRefsTable(XPathContext xctxt, XMLString ref, int node) {
    XNodeSet nodes = (XNodeSet) m_refsTable.get(ref);
    if (nodes == null) {
        nodes = new XNodeSet(node, xctxt.getDTMManager());
        nodes.nextNode();
        m_refsTable.put(ref, nodes);
    } else {
        // easily compare node against the current node.
        if (nodes.getCurrentNode() != node) {
            nodes.mutableNodeset().addNode(node);
            nodes.nextNode();
        }
    }
}
Also used : XNodeSet(org.apache.xpath.objects.XNodeSet)

Example 5 with XNodeSet

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

the class JAXPExtensionsProvider method extFunction.

/**
     * Execute the extension function.
     */
public Object extFunction(String ns, String funcName, Vector argVec, Object methodKey) throws javax.xml.transform.TransformerException {
    try {
        if (funcName == null) {
            String fmsg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_ARG_CANNOT_BE_NULL, new Object[] { "Function Name" });
            throw new NullPointerException(fmsg);
        }
        //Find the XPathFunction corresponding to namespace and funcName
        javax.xml.namespace.QName myQName = new QName(ns, funcName);
        // throw XPathFunctionException
        if (extensionInvocationDisabled) {
            String fmsg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_EXTENSION_FUNCTION_CANNOT_BE_INVOKED, new Object[] { myQName.toString() });
            throw new XPathFunctionException(fmsg);
        }
        // Assuming user is passing all the needed parameters ( including
        // default values )
        int arity = argVec.size();
        javax.xml.xpath.XPathFunction xpathFunction = resolver.resolveFunction(myQName, arity);
        // not using methodKey
        ArrayList argList = new ArrayList(arity);
        for (int i = 0; i < arity; i++) {
            Object argument = argVec.elementAt(i);
            // Explicitly getting NodeList by using nodelist()
            if (argument instanceof XNodeSet) {
                argList.add(i, ((XNodeSet) argument).nodelist());
            } else if (argument instanceof XObject) {
                Object passedArgument = ((XObject) argument).object();
                argList.add(i, passedArgument);
            } else {
                argList.add(i, argument);
            }
        }
        return (xpathFunction.evaluate(argList));
    } catch (XPathFunctionException xfe) {
        // further execution by throwing WrappedRuntimeException 
        throw new org.apache.xml.utils.WrappedRuntimeException(xfe);
    } catch (Exception e) {
        throw new javax.xml.transform.TransformerException(e);
    }
}
Also used : TransformerException(javax.xml.transform.TransformerException) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) TransformerException(javax.xml.transform.TransformerException) XPathFunctionException(javax.xml.xpath.XPathFunctionException) XNodeSet(org.apache.xpath.objects.XNodeSet) XPathFunctionException(javax.xml.xpath.XPathFunctionException) XObject(org.apache.xpath.objects.XObject) QName(javax.xml.namespace.QName) XPathFunction(javax.xml.xpath.XPathFunction) XObject(org.apache.xpath.objects.XObject)

Aggregations

XNodeSet (org.apache.xpath.objects.XNodeSet)35 XObject (org.apache.xpath.objects.XObject)17 DTM (org.apache.xml.dtm.DTM)9 TransformerException (javax.xml.transform.TransformerException)8 DTMIterator (org.apache.xml.dtm.DTMIterator)8 XMLString (org.apache.xml.utils.XMLString)6 NodeSetDTM (org.apache.xpath.NodeSetDTM)5 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 XPathContext (org.apache.xpath.XPathContext)4 Node (org.w3c.dom.Node)3 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