Search in sources :

Example 31 with XPath

use of org.apache.xpath.XPath 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)

Example 32 with XPath

use of org.apache.xpath.XPath in project intellij-community by JetBrains.

the class XalanStyleFrame method eval.

public Value eval(String expr) throws Debugger.EvaluationException {
    assert isValid();
    try {
        final DTMIterator context = myTransformer.getContextNodeList();
        final int ctx;
        final DTM dtm = context.getDTM(myCurrentNode);
        if (dtm.getDocumentRoot(myCurrentNode) == myCurrentNode) {
            ctx = dtm.getFirstChild(myCurrentNode);
        } else {
            ctx = myCurrentNode;
        }
        final DTMNodeProxy c = new DTMNodeProxy(dtm, ctx);
        final PrefixResolver prefixResolver = new PrefixResolverDefault(c) {

            public String getNamespaceForPrefix(String prefix, Node context) {
                if (context instanceof DTMNodeProxy) {
                    final DTMNodeProxy proxy = (DTMNodeProxy) context;
                    final DTM dtm = proxy.getDTM();
                    int p = proxy.getDTMNodeNumber();
                    while (p != DTM.NULL) {
                        int nsNode = dtm.getFirstNamespaceNode(p, true);
                        while (nsNode != DTM.NULL) {
                            final String s = dtm.getLocalName(nsNode);
                            if (s.equals(prefix)) {
                                return dtm.getNodeValue(nsNode);
                            }
                            nsNode = dtm.getNextNamespaceNode(p, nsNode, true);
                        }
                        p = dtm.getParent(p);
                    }
                }
                return super.getNamespaceForPrefix(prefix, context);
            }
        };
        final XPath xPath = new XPath(expr, myCurrentElement, prefixResolver, XPath.SELECT, myTransformer.getErrorListener());
        return new XObjectValue(xPath.execute(myContext, myCurrentNode, myCurrentElement));
    } catch (Exception e) {
        debug(e);
        final String message = e.getMessage();
        throw new Debugger.EvaluationException(message != null ? message : e.getClass().getSimpleName());
    }
}
Also used : XPath(org.apache.xpath.XPath) Debugger(org.intellij.plugins.xsltDebugger.rt.engine.Debugger) Node(org.w3c.dom.Node) PrefixResolver(org.apache.xml.utils.PrefixResolver) TransformerException(javax.xml.transform.TransformerException) DTMIterator(org.apache.xml.dtm.DTMIterator) DTM(org.apache.xml.dtm.DTM) DTMNodeProxy(org.apache.xml.dtm.ref.DTMNodeProxy) PrefixResolverDefault(org.apache.xml.utils.PrefixResolverDefault)

Aggregations

XPath (org.apache.xpath.XPath)32 TransformerException (javax.xml.transform.TransformerException)7 DTM (org.apache.xml.dtm.DTM)7 Vector (java.util.Vector)6 NodeSetDTM (org.apache.xpath.NodeSetDTM)6 ErrorListener (javax.xml.transform.ErrorListener)4 ExpressionVisitor (org.apache.xalan.extensions.ExpressionVisitor)4 StringTokenizer (java.util.StringTokenizer)2 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)2 ElemExtensionCall (org.apache.xalan.templates.ElemExtensionCall)2 ElemLiteralResult (org.apache.xalan.templates.ElemLiteralResult)2 ElemTemplate (org.apache.xalan.templates.ElemTemplate)2 ElemTemplateElement (org.apache.xalan.templates.ElemTemplateElement)2 KeyDeclaration (org.apache.xalan.templates.KeyDeclaration)2 Stylesheet (org.apache.xalan.templates.Stylesheet)2 QName (org.apache.xml.utils.QName)2 SAXSourceLocator (org.apache.xml.utils.SAXSourceLocator)2 StringVector (org.apache.xml.utils.StringVector)2 Expression (org.apache.xpath.Expression)2 StepPattern (org.apache.xpath.patterns.StepPattern)2