Example usage for javax.xml.xpath XPathFactory newXPath

List of usage examples for javax.xml.xpath XPathFactory newXPath

Introduction

In this page you can find the example usage for javax.xml.xpath XPathFactory newXPath.

Prototype

public abstract XPath newXPath();

Source Link

Document

Return a new XPath using the underlying object model determined when the XPathFactory was instantiated.

Usage

From source file:br.com.insula.spring.security.janrain.JanrainService.java

private XPath createXPath() {
    XPathFactory xPathFactory = XPathFactory.newInstance();
    return xPathFactory.newXPath();
}

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.  jav a 2s  .c om
            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 };
}

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);/*from   w ww  .j  ava 2s  .  com*/
    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:org.alloy.metal.xml.merge.ImportProcessor.java

public ImportProcessor(ResourceLoader loader) {
    this.loader = loader;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {//from  w  w  w . j ava  2 s  .  c o  m
        builder = dbf.newDocumentBuilder();
        XPathFactory factory = XPathFactory.newInstance();
        xPath = factory.newXPath();
    } catch (ParserConfigurationException e) {
        LOG.error("Unable to create document builder", e);
        throw new RuntimeException(e);
    }
}

From source file:org.syncope.console.commons.XMLRolesReader.java

/**
 * Get all roles allowed for specific page and actio requested.
 *
 * @param pageId//from   w w  w . jav a2  s .  co m
 * @param actionId
 * @return roles list comma separated
 */
public String getAllAllowedRoles(final String pageId, final String actionId) {

    if (doc == null) {
        init();
    }
    if (doc == null) {
        return "";
    }

    final StringBuilder roles = new StringBuilder();
    try {
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        XPathExpression expr = xpath.compile(
                "//page[@id='" + pageId + "']/" + "action[@id='" + actionId + "']/" + "entitlement/text()");
        Object result = expr.evaluate(doc, XPathConstants.NODESET);

        NodeList nodes = (NodeList) result;

        for (int i = 0; i < nodes.getLength(); i++) {
            if (i > 0) {
                roles.append(",");
            }
            roles.append(nodes.item(i).getNodeValue());
        }
    } catch (XPathExpressionException e) {
        LOG.error("While parsing authorizations file", e);
    }

    LOG.debug("Authorizations found: {}", roles);

    return roles.toString();
}

From source file:org.jasig.portlet.calendar.service.RoleService.java

/**
 * Do the real work of reading the role list.
 *
 * @return the set of role names.//from w w  w  .  j  a  v  a  2  s .co m
 */
private Set<String> readRolesFromPortletXml() {
    try {
        URL portletXmlUrl = context.getResource(PORTLET_XML_PATH);
        InputSource is = new InputSource(portletXmlUrl.openStream());

        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
        Document doc = builder.parse(is);

        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPath xpath = xpathFactory.newXPath();
        XPathExpression xPathExpression = xpath.compile(ROLES_XPATH);

        NodeList nodeList = (NodeList) xPathExpression.evaluate(doc, XPathConstants.NODESET);

        Set<String> roles = new LinkedHashSet<String>();
        for (int i = 0; i < nodeList.getLength(); i++) {
            String role = nodeList.item(i).getNodeValue();
            roles.add(role);
        }

        return roles;

    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new RuntimeException("Error reading roles from portlet.xml", e);
    }
}

From source file:com.microsoft.samples.federation.FederationMetadataDocument.java

private String getSingleNodeText(String xpathStr) {
    XPathFactory xpf = XPathFactory.newInstance();
    XPath xpath = xpf.newXPath();

    try {/* w  w w. ja  va  2s.c o m*/
        XPathExpression xpe = xpath.compile(xpathStr);
        NodeList nodeList = (NodeList) xpe.evaluate(_fmdDoc, XPathConstants.NODESET);
        if (nodeList.getLength() > 0) {
            return nodeList.item(0).getNodeValue();
        }

    } catch (XPathExpressionException e) {
        //do something here
    }
    return null;
}

From source file:com.cognifide.aet.job.common.datafilters.removenodes.RemoveNodesDataModifier.java

@Override
public void setParameters(Map<String, String> params) throws ParametersException {
    if (params.containsKey(PARAM_XPATH)) {
        xpathString = params.get(PARAM_XPATH);
        XPathFactory xPathfactory = XPathFactory.newInstance();
        XPath xpath = xPathfactory.newXPath();
        try {//  w  w  w .  j  a  va 2  s.c  o m
            expr = xpath.compile(xpathString);
        } catch (XPathExpressionException e) {
            throw new ParametersException(e.getMessage(), e);
        }
    } else {
        throw new ParametersException("XPath must be provided");
    }

}

From source file:org.openmrs.module.dhisreport.api.dxf2.DataValueSetTest.java

protected String xpathTest(String xpathString, String xml) throws XPathExpressionException {
    InputSource source = new InputSource(new StringReader(xml));
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    xpath.setNamespaceContext(new Dxf2NamespaceResolver());

    return (String) xpath.evaluate(xpathString, source);
}

From source file:com.hotwire.test.steps.application.IosApplication.java

public String getExpectedAnalytics(String analyticsParams) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);//from  w w  w.ja v  a2  s  . c  o m
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse("vertical");
    XPathFactory xpathfactory = XPathFactory.newInstance();
    XPath xpath = xpathfactory.newXPath();

    String paramsToPath = "//" + (analyticsParams.replaceAll(":", "/")) + "/node()";
    XPathExpression expr = xpath.compile(paramsToPath);
    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < nodes.getLength(); i++) {
        sb.append((nodes.item(i).getNodeName() + "=" + nodes.item(i).getTextContent()).trim());
    }
    String expectedParams = sb.toString().replaceAll("#text=", ";");

    return expectedParams;
}