Search in sources :

Example 1 with JAXPPrefixResolver

use of org.apache.xpath.jaxp.JAXPPrefixResolver in project nokogiri by sparklemotion.

the class XmlXpathContext method tryGetNodeSet.

private IRubyObject tryGetNodeSet(ThreadContext context, String expr, NokogiriXPathFunctionResolver fnResolver) throws TransformerException {
    final Node contextNode = this.context.node;
    final JAXPPrefixResolver prefixResolver = new JAXPPrefixResolver(nsContext);
    XPath xpathInternal = new XPath(expr, null, prefixResolver, XPath.SELECT);
    // We always need to have a ContextNode with Xalan XPath implementation
    // To allow simple expression evaluation like 1+1 we are setting
    // dummy Document as Context Node
    final XObject xobj;
    if (contextNode == null)
        xobj = xpathInternal.execute(getXPathContext(fnResolver), DTM.NULL, prefixResolver);
    else
        xobj = xpathInternal.execute(getXPathContext(fnResolver), contextNode, prefixResolver);
    switch(xobj.getType()) {
        case XObject.CLASS_BOOLEAN:
            return context.getRuntime().newBoolean(xobj.bool());
        case XObject.CLASS_NUMBER:
            return context.getRuntime().newFloat(xobj.num());
        case XObject.CLASS_NODESET:
            XmlNodeSet xmlNodeSet = XmlNodeSet.create(context.getRuntime());
            xmlNodeSet.setNodeList(xobj.nodelist());
            xmlNodeSet.initialize(context.getRuntime(), this.context);
            return xmlNodeSet;
        default:
            return context.getRuntime().newString(xobj.str());
    }
}
Also used : XPath(org.apache.xpath.XPath) JAXPPrefixResolver(org.apache.xpath.jaxp.JAXPPrefixResolver) Node(org.w3c.dom.Node) XObject(org.apache.xpath.objects.XObject)

Aggregations

XPath (org.apache.xpath.XPath)1 JAXPPrefixResolver (org.apache.xpath.jaxp.JAXPPrefixResolver)1 XObject (org.apache.xpath.objects.XObject)1 Node (org.w3c.dom.Node)1