Example usage for javax.xml.xpath XPath compile

List of usage examples for javax.xml.xpath XPath compile

Introduction

In this page you can find the example usage for javax.xml.xpath XPath compile.

Prototype

public XPathExpression compile(String expression) throws XPathExpressionException;

Source Link

Document

Compile an XPath expression for later evaluation.

Usage

From source file:eu.europa.esig.dss.DSSXMLUtils.java

/**
 * @param xpathString XPath query string
 * @return/*from  w w w. ja  va2s  .c om*/
 */
private static XPathExpression createXPathExpression(final String xpathString) {

    /* XPath */
    final XPath xpath = factory.newXPath();
    xpath.setNamespaceContext(namespacePrefixMapper);
    try {
        final XPathExpression expr = xpath.compile(xpathString);
        return expr;
    } catch (XPathExpressionException ex) {
        throw new DSSException(ex);
    }
}

From source file:org.jboss.windup.decorator.archive.PomDescriptionDecorator.java

@Override
public void afterPropertiesSet() throws Exception {
    XPath xpath = factory.newXPath();
    xPath = xpath.compile(DESCRIPTION);
}

From source file:org.jboss.windup.decorator.archive.PomLinkDecorator.java

@Override
public void afterPropertiesSet() throws Exception {
    XPath xpath = factory.newXPath();
    linkXPath = xpath.compile(LINK);
}

From source file:com.alliander.osgp.platform.cucumber.RunXpathResult.java

public XpathResult runXPathExpression(final String response, final String path)
        throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {

    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    final DocumentBuilder builder = factory.newDocumentBuilder();
    final InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(response));
    final Document doc = builder.parse(is);
    final XPathFactory xPathfactory = XPathFactory.newInstance();
    final XPath xpath = xPathfactory.newXPath();

    return new XpathResult(xpath.compile(path), doc);
}

From source file:org.apache.solr.kelvin.responseanalyzers.LegacyResponseAnalyzer.java

public void configure(JsonNode config) throws Exception {
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    expr = xpath.compile("/response/federator/result/doc");
}

From source file:org.apache.solr.kelvin.responseanalyzers.XmlDoclistExtractorResponseAnalyzer.java

public void configure(JsonNode config) throws Exception {
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    expr = xpath.compile("/response/result/doc");

}

From source file:com.dianping.phoenix.dev.core.tools.scanner.ServiceMetaScanner.java

@Override
protected List<ServicePortEntry> doScan(Document doc) throws Exception {
    List<ServicePortEntry> resList = new ArrayList<ServicePortEntry>();
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    XPathExpression expr = xpath.compile("//service");

    Object xmlRes = expr.evaluate(doc, XPathConstants.NODESET);

    NodeList nodes = (NodeList) xmlRes;

    for (int i = 0; i < nodes.getLength(); i++) {
        String serviceName = nodes.item(i).getAttributes().getNamedItem("name").getNodeValue();
        XPathExpression portExpr = xpath.compile("//service[@name='" + serviceName + "']/port/text()");
        Object portRes = portExpr.evaluate(doc, XPathConstants.NODESET);
        NodeList ports = (NodeList) portRes;
        int projectPort = -1;
        if (ports.getLength() >= 1) {
            projectPort = Integer.parseInt(ports.item(0).getNodeValue());
        }//ww  w. j  a  v  a  2 s  .co  m

        if (projectPort > 0 && StringUtils.isNotBlank(serviceName)) {
            resList.add(new ServicePortEntry(serviceName, projectPort));
        }
    }

    return resList;
}

From source file:com.dianping.maven.plugin.tools.misc.scanner.ProjectMetaScanner.java

@Override
protected List<ProjectPortEntry> doScan(Document doc) throws Exception {
    List<ProjectPortEntry> resList = new ArrayList<ProjectPortEntry>();
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    XPathExpression expr = xpath.compile("//project");

    Object xmlRes = expr.evaluate(doc, XPathConstants.NODESET);

    NodeList nodes = (NodeList) xmlRes;

    for (int i = 0; i < nodes.getLength(); i++) {
        String projectName = nodes.item(i).getAttributes().getNamedItem("name").getNodeValue();
        XPathExpression portExpr = xpath.compile("//project[@name='" + projectName + "']/port/text()");
        Object portRes = portExpr.evaluate(doc, XPathConstants.NODESET);
        NodeList ports = (NodeList) portRes;
        int projectPort = -1;
        if (ports.getLength() >= 1) {
            projectPort = Integer.parseInt(ports.item(0).getNodeValue());
        }/* ww w  .java 2 s. co m*/

        if (projectPort > 0 && StringUtils.isNotBlank(projectName)) {
            resList.add(new ProjectPortEntry(projectName, projectPort));
        }
    }

    return resList;
}

From source file:org.ambraproject.search.service.DummySOLRMessageSender.java

private Node XPathSingleNodeQuery(Document dom, String statement) throws XPathExpressionException {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    XPathExpression expr = xpath.compile(statement);

    return (Node) expr.evaluate(dom, XPathConstants.NODE);
}

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);// www .ja va 2 s .  c  o 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));

}