Example usage for javax.xml.xpath XPathExpression evaluate

List of usage examples for javax.xml.xpath XPathExpression evaluate

Introduction

In this page you can find the example usage for javax.xml.xpath XPathExpression evaluate.

Prototype

public String evaluate(InputSource source) throws XPathExpressionException;

Source Link

Document

Evaluate the compiled XPath expression in the context of the specified InputSource and return the result as a String .

Usage

From source file:gov.nih.nci.cacis.transform.XSLTv2TransformerTest.java

@Test
public void transformStream() throws XMLStreamException, TransformerException, URISyntaxException, IOException,
        SAXException, ParserConfigurationException, XPathExpressionException {
    final ByteArrayOutputStream os = new ByteArrayOutputStream();

    final Map<String, String> params = new HashMap<String, String>();
    params.put("BaseURI", "http://yadda.com/someUUID");

    transform.transform(params, sampleMessageIS, os);
    assertNotNull(os);/*w w w. j a  v  a  2s  .  co m*/
    assertTrue(os.size() > 0);

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true); // never forget this!
    DocumentBuilder builder = factory.newDocumentBuilder();

    Document doc = builder.parse(new ByteArrayInputStream(os.toByteArray()));
    assertNotNull(doc);

    XPathFactory xPathFactory = XPathFactory.newInstance();
    XPath xpath = xPathFactory.newXPath();
    XPathExpression expr = xpath.compile("/world/country[1]/city[1]");

    assertEquals("Tokyo", expr.evaluate(doc));

}

From source file:com.greglturnquist.springagram.fileservice.s3.FileService.java

/**
 * Parse the {@link AmazonS3Exception} error result to capture the endpoint for
 * redirection./*ww  w  .  j  a  va2  s  . c  o  m*/
 *
 * @param e
 */
private void updateEndpoint(AmazonS3Exception e) {

    try {
        Document errorResponseDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                .parse(new StringInputStream(e.getErrorResponseXml()));

        XPathExpression endpointXpathExtr = XPathFactory.newInstance().newXPath().compile("/Error/Endpoint");

        this.s3Client.setEndpoint(endpointXpathExtr.evaluate(errorResponseDoc));
    } catch (Exception ex) {
        throw new RuntimeException(e);
    }
}

From source file:org.ow2.chameleon.fuchsia.push.base.subscriber.tool.HubDiscovery.java

public String hasHub(Document doc) {
    String hub = null;/* ww  w.j  a v  a2 s . c  om*/

    XPathFactory factory = XPathFactory.newInstance();
    XPath xPath = factory.newXPath();
    XPathExpression xPathExpression;

    try {
        xPathExpression = xPath.compile("/feed/link[@rel='hub']/@href");
        hub = xPathExpression.evaluate(doc);
        if ((hub == null) || (hub.length() == 0)) {
            xPathExpression = xPath.compile("//link[@rel='hub']/@href");
            hub = xPathExpression.evaluate(doc);
        }

        if (hub.length() == 0) {
            return null;
        }

        return hub;

    } catch (XPathExpressionException e) {
        LOGGER.error("XPathExpression invalid", e);
        return null;
    }
}

From source file:org.ow2.chameleon.fuchsia.push.base.subscriber.tool.HubDiscovery.java

public String hasTopic(Document doc) {
    String topic;//from  w  w  w  .ja v  a  2  s . com

    XPathFactory factory = XPathFactory.newInstance();
    XPath xPath = factory.newXPath();
    XPathExpression xPathExpression;

    try {
        xPathExpression = xPath.compile("/feed/link[@rel='self']/@href");
        topic = xPathExpression.evaluate(doc);
        if ((topic == null) || (topic.length() == 0)) {
            xPathExpression = xPath.compile("//link[@rel='self']/@href");
            topic = xPathExpression.evaluate(doc);
        }

        if (topic.length() == 0) {
            return null;
        }
        return topic;

    } catch (XPathExpressionException e) {
        LOGGER.error("Invalid XpathExpression", e);
        return null;
    }

}

From source file:io.github.microcks.web.SoapController.java

private String getDispatchCriteriaFromXPathEval(Operation operation, String body) {
    try {/*ww  w .j a v  a 2 s . c o  m*/
        // Evaluating request regarding XPath build with operation dispatcher rules.
        XPathExpression xpath = SoapUIXPathBuilder.buildXPathMatcherFromRules(operation.getDispatcherRules());
        return xpath.evaluate(new InputSource(new StringReader(body)));
    } catch (Exception e) {
        log.error("Error during Xpath evaluation", e);
    }
    return null;
}

From source file:it.acubelab.batframework.systemPlugins.TagmeAnnotator.java

private String getConfigValue(String setting, String name, Document doc) throws XPathExpressionException {
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression userExpr = xpath
            .compile("tagme/setting[@name=\"" + setting + "\"]/param[@name=\"" + name + "\"]/@value");
    return userExpr.evaluate(doc);
}

From source file:org.apache.manifoldcf.agents.output.opensearchserver.OpenSearchServerConnection.java

protected String checkXPath(String xPathQuery) throws ManifoldCFException {
    try {//from   ww  w.  j a va 2 s  .c  om
        readXmlResponse();
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        XPathExpression xPathExpr = xpath.compile(xPathQuery);
        return xPathExpr.evaluate(xmlResponse);
    } catch (XPathExpressionException e) {
        throw new ManifoldCFException(e);
    }
}

From source file:it.acubelab.batframework.systemPlugins.AIDAAnnotator.java

private String getConfigValue(String setting, String name, Document doc) throws XPathExpressionException {
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression userExpr = xpath
            .compile("aida/setting[@name=\"" + setting + "\"]/param[@name=\"" + name + "\"]/@value");
    return userExpr.evaluate(doc);
}

From source file:edu.wisc.hrs.dao.BaseHrsSoapDao.java

protected String evaluateXPath(Node node, String expression) {
    final XPathFactory xpathFactory = XPathFactory.newInstance();
    final XPath xPath = xpathFactory.newXPath();
    final XPathExpression xpathExpression;
    try {/*from   ww  w. j  a  v a 2  s .  c  o m*/
        xpathExpression = xPath.compile(expression);
    } catch (XPathExpressionException e) {
        return null;
    }

    try {
        return xpathExpression.evaluate(node);
    } catch (XPathExpressionException e) {
        return null;
    }
}

From source file:org.objenesis.tck.OsgiTest.java

protected String[] getTestBundlesNames() {
    String version = getImplementationVersion(Objenesis.class);
    // Null means we are an IDE, not in Maven. So we have an IDE project dependency instead
    // of a Maven dependency with the jar. Which explains why the version is null (no Manifest
    // since there's no jar. In that case we get the version from the pom.xml and hope the Maven
    // jar is up-to-date in the local repository
    if (version == null) {
        try {/*  www.ja v  a 2s.c o m*/
            XPathFactory xPathFactory = XPathFactory.newInstance();
            final XPath xPath = xPathFactory.newXPath();
            XPathExpression xPathExpression;
            try {
                xPathExpression = xPath.compile("/project/parent/version");
            } catch (final XPathExpressionException e) {
                throw new RuntimeException(e);
            }

            final DocumentBuilderFactory xmlFact = DocumentBuilderFactory.newInstance();
            xmlFact.setNamespaceAware(false);
            final DocumentBuilder builder = xmlFact.newDocumentBuilder();
            final Document doc = builder.parse(new File("pom.xml"));
            version = xPathExpression.evaluate(doc);
        } catch (final Exception e) {
            throw new RuntimeException(e);
        }
    }
    return new String[] { "org.objenesis, objenesis, " + version };
}