Search in sources :

Example 1 with XPathSelector

use of net.sf.saxon.s9api.XPathSelector in project sirix by sirixdb.

the class TestNodeWrapperS9ApiXPath method testB.

@Test
public void testB() throws Exception {
    final XPathSelector selector = new XPathEvaluator.Builder("//b", mHolder.getSession()).build().call();
    final StringBuilder strBuilder = new StringBuilder();
    strBuilder.append("<result>");
    for (final XdmItem item : selector) {
        strBuilder.append(item.toString());
    }
    strBuilder.append("</result>");
    assertXMLEqual("expected pieces to be similar", "<result><b xmlns:p=\"ns\">foo<c xmlns:p=\"ns\"/></b><b xmlns:p=\"ns\" p:x=\"y\">" + "<c xmlns:p=\"ns\"/>bar</b></result>", strBuilder.toString());
}
Also used : XPathSelector(net.sf.saxon.s9api.XPathSelector) XdmItem(net.sf.saxon.s9api.XdmItem) Test(org.junit.Test)

Example 2 with XPathSelector

use of net.sf.saxon.s9api.XPathSelector in project sirix by sirixdb.

the class TestNodeWrapperS9ApiXPath method testCountB.

@Test
public void testCountB() throws Exception {
    final XPathSelector selector = new XPathEvaluator.Builder("count(//b)", mHolder.getSession()).build().call();
    final StringBuilder sb = new StringBuilder();
    for (final XdmItem item : selector) {
        sb.append(item.getStringValue());
    }
    assertEquals("2", sb.toString());
}
Also used : XPathSelector(net.sf.saxon.s9api.XPathSelector) XdmItem(net.sf.saxon.s9api.XdmItem) Test(org.junit.Test)

Example 3 with XPathSelector

use of net.sf.saxon.s9api.XPathSelector in project sirix by sirixdb.

the class TestNodeWrapperS9ApiXPath method testB2.

@Test
public void testB2() throws Exception {
    final XPathSelector selector = new XPathEvaluator.Builder("//b[2]", mHolder.getSession()).build().call();
    final StringBuilder strBuilder = new StringBuilder();
    for (final XdmItem item : selector) {
        strBuilder.append(item.toString());
    }
    assertXMLEqual("expected pieces to be similar", "<b xmlns:p=\"ns\" p:x=\"y\"><c xmlns:p=\"ns\"/>bar</b>", strBuilder.toString());
}
Also used : XPathSelector(net.sf.saxon.s9api.XPathSelector) XdmItem(net.sf.saxon.s9api.XdmItem) Test(org.junit.Test)

Example 4 with XPathSelector

use of net.sf.saxon.s9api.XPathSelector in project sirix by sirixdb.

the class XPathEvaluator method call.

@Override
public XPathSelector call() throws Exception {
    final Processor proc = new Processor(false);
    final Configuration config = proc.getUnderlyingConfiguration();
    final NodeInfo doc = new DocumentWrapper(mSession, mRevision, config);
    final XPathCompiler xpath = proc.newXPathCompiler();
    final DocumentBuilder builder = proc.newDocumentBuilder();
    final XdmItem item = builder.wrap(doc);
    final XPathSelector selector = xpath.compile(mExpression).load();
    selector.setContextItem(item);
    return selector;
}
Also used : DocumentWrapper(org.sirix.saxon.wrapper.DocumentWrapper) Processor(net.sf.saxon.s9api.Processor) Configuration(net.sf.saxon.Configuration) XPathCompiler(net.sf.saxon.s9api.XPathCompiler) DocumentBuilder(net.sf.saxon.s9api.DocumentBuilder) NodeInfo(net.sf.saxon.om.NodeInfo) XPathSelector(net.sf.saxon.s9api.XPathSelector) XdmItem(net.sf.saxon.s9api.XdmItem)

Example 5 with XPathSelector

use of net.sf.saxon.s9api.XPathSelector in project jmeter by apache.

the class XPathUtil method computeAssertionResultUsingSaxon.

/**
 * @param result The result of xpath2 assertion
 * @param xmlFile XML data
 * @param xPathQuery XPath Query
 * @param namespaces Space separated set of prefix=namespace
 * @param isNegated invert result
 * @throws SaxonApiException when the parser has problems with the given xml or xpath query
 * @throws FactoryConfigurationError when the parser can not be instantiated
 */
public static void computeAssertionResultUsingSaxon(AssertionResult result, String xmlFile, String xPathQuery, String namespaces, Boolean isNegated) throws SaxonApiException, FactoryConfigurationError {
    // generating the cache key
    final ImmutablePair<String, String> key = ImmutablePair.of(xPathQuery, namespaces);
    // check the cache
    XPathExecutable xPathExecutable;
    if (StringUtils.isNotEmpty(xPathQuery)) {
        xPathExecutable = XPATH_CACHE.get(key);
    } else {
        log.warn("Error : {}", JMeterUtils.getResString("xpath2_extractor_empty_query"));
        return;
    }
    try (StringReader reader = new StringReader(xmlFile)) {
        // We could instantiate it once but might trigger issues in the future
        // Sharing of a DocumentBuilder across multiple threads is not recommended.
        // However, in the current implementation sharing a DocumentBuilder (once
        // initialized)
        // will only cause problems if a SchemaValidator is used.
        net.sf.saxon.s9api.DocumentBuilder builder = PROCESSOR.newDocumentBuilder();
        XdmNode xdmNode = builder.build(new SAXSource(new InputSource(reader)));
        if (xPathExecutable != null) {
            XPathSelector selector = null;
            try {
                Document doc;
                doc = XPathUtil.makeDocumentBuilder(false, false, false, false).newDocument();
                XObject xObject = XPathAPI.eval(doc, xPathQuery, getPrefixResolverForXPath2(doc, namespaces));
                selector = xPathExecutable.load();
                selector.setContextItem(xdmNode);
                XdmValue nodes = selector.evaluate();
                boolean resultOfEval = true;
                int length = nodes.size();
                // In case we need to extract everything
                if (length == 0) {
                    resultOfEval = false;
                } else if (xObject.getType() == XObject.CLASS_BOOLEAN) {
                    resultOfEval = Boolean.parseBoolean(nodes.itemAt(0).getStringValue());
                }
                result.setFailure(isNegated ? resultOfEval : !resultOfEval);
                result.setFailureMessage(isNegated ? "Nodes Matched for " + xPathQuery : "No Nodes Matched for " + xPathQuery);
            } catch (ParserConfigurationException | TransformerException e) {
                // NOSONAR Exception handled by return
                result.setError(true);
                result.setFailureMessage("Exception: " + e.getMessage() + " for:" + xPathQuery);
            } finally {
                if (selector != null) {
                    try {
                        selector.getUnderlyingXPathContext().setContextItem(null);
                    } catch (Exception e) {
                        // NOSONAR Ignored on purpose
                        result.setError(true);
                        result.setFailureMessage("Exception: " + e.getMessage() + " for:" + xPathQuery);
                    }
                }
            }
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) Document(org.w3c.dom.Document) XdmNode(net.sf.saxon.s9api.XdmNode) XMLStreamException(javax.xml.stream.XMLStreamException) SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException) SaxonApiException(net.sf.saxon.s9api.SaxonApiException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XdmValue(net.sf.saxon.s9api.XdmValue) SAXSource(javax.xml.transform.sax.SAXSource) XPathSelector(net.sf.saxon.s9api.XPathSelector) StringReader(java.io.StringReader) XPathExecutable(net.sf.saxon.s9api.XPathExecutable) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XObject(org.apache.xpath.objects.XObject) TransformerException(javax.xml.transform.TransformerException)

Aggregations

XPathSelector (net.sf.saxon.s9api.XPathSelector)10 XdmItem (net.sf.saxon.s9api.XdmItem)9 Test (org.junit.Test)6 StringReader (java.io.StringReader)3 XPathExecutable (net.sf.saxon.s9api.XPathExecutable)3 XdmValue (net.sf.saxon.s9api.XdmValue)3 IOException (java.io.IOException)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 TransformerException (javax.xml.transform.TransformerException)2 SAXSource (javax.xml.transform.sax.SAXSource)2 Processor (net.sf.saxon.s9api.Processor)2 SaxonApiException (net.sf.saxon.s9api.SaxonApiException)2 XPathCompiler (net.sf.saxon.s9api.XPathCompiler)2 XdmNode (net.sf.saxon.s9api.XdmNode)2 InputSource (org.xml.sax.InputSource)2 SAXException (org.xml.sax.SAXException)2 SAXParseException (org.xml.sax.SAXParseException)2 StreamSource (javax.xml.transform.stream.StreamSource)1 Configuration (net.sf.saxon.Configuration)1