Search in sources :

Example 1 with XObject

use of com.sun.org.apache.xpath.internal.objects.XObject in project Payara by payara.

the class VerifierTest method getXPathValueForNonRuntime.

/*
     *getXPathValueForNonRuntime(String xpath)
     *   return String - is the value of the element specified in the xpath.
     */
public String getXPathValueForNonRuntime(String xpath) {
    try {
        String value = null;
        Document d = getVerifierContext().getDocument();
        if (d == null)
            return null;
        XObject result = XPathAPI.eval(d, xpath, (PrefixResolver) new XpathPrefixResolver(d));
        NodeList nl = result.nodelist();
        for (int i = 0; i < nl.getLength(); i++) {
            Node n = ((Node) nl.item(i)).getFirstChild();
            if (n == null)
                return null;
            value = n.getNodeValue();
        }
        return value;
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}
Also used : XpathPrefixResolver(com.sun.enterprise.tools.verifier.XpathPrefixResolver) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) XObject(com.sun.org.apache.xpath.internal.objects.XObject)

Example 2 with XObject

use of com.sun.org.apache.xpath.internal.objects.XObject in project gocd by gocd.

the class XmlXpathContext method tryGetNodeSet.

private IRubyObject tryGetNodeSet(ThreadContext thread_context, String expr) throws XPathExpressionException {
    XObject xobj = null;
    Node contextNode = context.node;
    try {
        com.sun.org.apache.xpath.internal.XPath xpathInternal = new com.sun.org.apache.xpath.internal.XPath(expr, null, prefixResolver, com.sun.org.apache.xpath.internal.XPath.SELECT);
        if (contextNode == null)
            xobj = xpathInternal.execute(xpathSupport, DTM.NULL, prefixResolver);
        else
            xobj = xpathInternal.execute(xpathSupport, contextNode, prefixResolver);
        switch(xobj.getType()) {
            case XObject.CLASS_BOOLEAN:
                return thread_context.getRuntime().newBoolean(xobj.bool());
            case XObject.CLASS_NUMBER:
                return thread_context.getRuntime().newFloat(xobj.num());
            case XObject.CLASS_NODESET:
                NodeList nodeList = xobj.nodelist();
                XmlNodeSet xmlNodeSet = (XmlNodeSet) NokogiriService.XML_NODESET_ALLOCATOR.allocate(getRuntime(), getNokogiriClass(getRuntime(), "Nokogiri::XML::NodeSet"));
                xmlNodeSet.setNodeList(nodeList);
                xmlNodeSet.initialize(thread_context.getRuntime(), context);
                return xmlNodeSet;
            default:
                return thread_context.getRuntime().newString(xobj.str());
        }
    } catch (TransformerException ex) {
        throw new XPathExpressionException(expr);
    }
}
Also used : XPathExpressionException(javax.xml.xpath.XPathExpressionException) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) XObject(com.sun.org.apache.xpath.internal.objects.XObject) TransformerException(javax.xml.transform.TransformerException)

Example 3 with XObject

use of com.sun.org.apache.xpath.internal.objects.XObject in project Payara by payara.

the class VerifierTest method getNonRuntimeCountNodeSet.

public int getNonRuntimeCountNodeSet(String xpath) {
    try {
        // int value = -1;
        Document d = getVerifierContext().getDocument();
        if (d == null)
            return -1;
        XObject result = XPathAPI.eval(d, xpath, (PrefixResolver) new XpathPrefixResolver(d));
        NodeList nl = result.nodelist();
        NodeSet ns = new NodeSet(nl);
        return ns.getLength();
    } catch (Exception ex) {
        ex.printStackTrace();
        return -1;
    }
}
Also used : NodeSet(com.sun.org.apache.xpath.internal.NodeSet) XpathPrefixResolver(com.sun.enterprise.tools.verifier.XpathPrefixResolver) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) XObject(com.sun.org.apache.xpath.internal.objects.XObject)

Example 4 with XObject

use of com.sun.org.apache.xpath.internal.objects.XObject in project tmdm-studio-se by Talend.

the class Util method getTextNodes.

public static String[] getTextNodes(Node contextNode, String xPath, Node namespaceNode) throws XtentisException {
    String[] results = null;
    // test for hard-coded values
    if (xPath.startsWith("\"") && xPath.endsWith("\"")) {
        // $NON-NLS-1$ //$NON-NLS-2$
        return new String[] { xPath.substring(1, xPath.length() - 1) };
    }
    // test for incomplete path (elements missing /text())
    if (!xPath.matches(".*@[^/\\]]+")) {
        // $NON-NLS-1$
        if (!xPath.endsWith(")")) {
            // $NON-NLS-1$
            // $NON-NLS-1$
            xPath += "/text()";
        }
    }
    try {
        XObject xo = XPathAPI.eval(contextNode, xPath, namespaceNode);
        if (xo.getType() == XObject.CLASS_NODESET) {
            NodeList l = xo.nodelist();
            int len = l.getLength();
            results = new String[len];
            for (int i = 0; i < len; i++) {
                Node n = l.item(i);
                results[i] = n.getNodeValue();
            }
        } else {
            results = new String[] { xo.toString() };
        }
    } catch (Exception e) {
        String err = Messages.Util_18 + xPath + Messages.Util_19 + e.getClass().getName() + Messages.Util_20 + e.getLocalizedMessage();
        throw new XtentisException(err);
    }
    return results;
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) XObject(com.sun.org.apache.xpath.internal.objects.XObject) ConnectException(java.net.ConnectException) IOException(java.io.IOException) WebServiceException(javax.xml.ws.WebServiceException) MissingJarsException(com.amalto.workbench.service.MissingJarsException) MalformedURLException(java.net.MalformedURLException) SAXParseException(org.xml.sax.SAXParseException) InaccessibleWSDLException(com.sun.xml.internal.ws.wsdl.parser.InaccessibleWSDLException)

Example 5 with XObject

use of com.sun.org.apache.xpath.internal.objects.XObject in project freemarker by apache.

the class SunInternalXalanXPathSupport method executeQuery.

public synchronized TemplateModel executeQuery(Object context, String xpathQuery) throws TemplateModelException {
    if (!(context instanceof Node)) {
        if (context != null) {
            if (isNodeList(context)) {
                int cnt = ((List) context).size();
                if (cnt != 0) {
                    throw new TemplateModelException("Cannot perform an XPath query against a node set of " + cnt + " nodes. Expecting a single node." + ERRMSG_RECOMMEND_JAXEN);
                } else {
                    throw new TemplateModelException(ERRMSG_EMPTY_NODE_SET);
                }
            } else {
                throw new TemplateModelException("Cannot perform an XPath query against a " + context.getClass().getName() + ". Expecting a single org.w3c.dom.Node.");
            }
        } else {
            throw new TemplateModelException(ERRMSG_EMPTY_NODE_SET);
        }
    }
    Node node = (Node) context;
    try {
        XPath xpath = new XPath(xpathQuery, null, customPrefixResolver, XPath.SELECT, null);
        int ctxtNode = xpathContext.getDTMHandleFromNode(node);
        XObject xresult = xpath.execute(xpathContext, ctxtNode, customPrefixResolver);
        if (xresult instanceof XNodeSet) {
            NodeListModel result = new NodeListModel(node);
            result.xpathSupport = this;
            NodeIterator nodeIterator = xresult.nodeset();
            Node n;
            do {
                n = nodeIterator.nextNode();
                if (n != null) {
                    result.add(n);
                }
            } while (n != null);
            return result.size() == 1 ? result.get(0) : result;
        }
        if (xresult instanceof XBoolean) {
            return ((XBoolean) xresult).bool() ? TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE;
        }
        if (xresult instanceof XNull) {
            return null;
        }
        if (xresult instanceof XString) {
            return new SimpleScalar(xresult.toString());
        }
        if (xresult instanceof XNumber) {
            return new SimpleNumber(Double.valueOf(((XNumber) xresult).num()));
        }
        throw new TemplateModelException("Cannot deal with type: " + xresult.getClass().getName());
    } catch (TransformerException te) {
        throw new TemplateModelException(te);
    }
}
Also used : XPath(com.sun.org.apache.xpath.internal.XPath) NodeIterator(org.w3c.dom.traversal.NodeIterator) TemplateModelException(freemarker.template.TemplateModelException) XNull(com.sun.org.apache.xpath.internal.objects.XNull) XNumber(com.sun.org.apache.xpath.internal.objects.XNumber) Node(org.w3c.dom.Node) XBoolean(com.sun.org.apache.xpath.internal.objects.XBoolean) SimpleScalar(freemarker.template.SimpleScalar) XNodeSet(com.sun.org.apache.xpath.internal.objects.XNodeSet) SimpleNumber(freemarker.template.SimpleNumber) XString(com.sun.org.apache.xpath.internal.objects.XString) List(java.util.List) XObject(com.sun.org.apache.xpath.internal.objects.XObject) TransformerException(javax.xml.transform.TransformerException)

Aggregations

XObject (com.sun.org.apache.xpath.internal.objects.XObject)5 Node (org.w3c.dom.Node)4 NodeList (org.w3c.dom.NodeList)4 XpathPrefixResolver (com.sun.enterprise.tools.verifier.XpathPrefixResolver)2 TransformerException (javax.xml.transform.TransformerException)2 Document (org.w3c.dom.Document)2 MissingJarsException (com.amalto.workbench.service.MissingJarsException)1 NodeSet (com.sun.org.apache.xpath.internal.NodeSet)1 XPath (com.sun.org.apache.xpath.internal.XPath)1 XBoolean (com.sun.org.apache.xpath.internal.objects.XBoolean)1 XNodeSet (com.sun.org.apache.xpath.internal.objects.XNodeSet)1 XNull (com.sun.org.apache.xpath.internal.objects.XNull)1 XNumber (com.sun.org.apache.xpath.internal.objects.XNumber)1 XString (com.sun.org.apache.xpath.internal.objects.XString)1 InaccessibleWSDLException (com.sun.xml.internal.ws.wsdl.parser.InaccessibleWSDLException)1 SimpleNumber (freemarker.template.SimpleNumber)1 SimpleScalar (freemarker.template.SimpleScalar)1 TemplateModelException (freemarker.template.TemplateModelException)1 IOException (java.io.IOException)1 ConnectException (java.net.ConnectException)1