Search in sources :

Example 56 with XPath

use of javax.xml.xpath.XPath in project flyway by flyway.

the class MavenTestCase method getPomVersion.

/**
     * Retrieves the version embedded in the project pom. Useful for running these tests in IntelliJ.
     *
     * @return The POM version.
     */
private String getPomVersion() {
    try {
        File pom = new File("pom.xml");
        if (!pom.exists()) {
            return "unknown";
        }
        XPath xPath = XPathFactory.newInstance().newXPath();
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(false);
        Document document = documentBuilderFactory.newDocumentBuilder().parse(pom);
        return xPath.evaluate("/project/version", document);
    } catch (Exception e) {
        throw new IllegalStateException("Unable to read POM version", e);
    }
}
Also used : XPath(javax.xml.xpath.XPath) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Document(org.w3c.dom.Document) File(java.io.File)

Example 57 with XPath

use of javax.xml.xpath.XPath in project flyway by flyway.

the class AntLargeTest method getPomVersion.

/**
     * Retrieves the version embedded in the project pom. Useful for running these tests in IntelliJ.
     *
     * @return The POM version.
     */
private String getPomVersion() {
    try {
        File pom = new File("pom.xml");
        if (!pom.exists()) {
            return "unknown";
        }
        XPath xPath = XPathFactory.newInstance().newXPath();
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(false);
        Document document = documentBuilderFactory.newDocumentBuilder().parse(pom);
        return xPath.evaluate("/project/version", document);
    } catch (Exception e) {
        throw new IllegalStateException("Unable to read POM version", e);
    }
}
Also used : XPath(javax.xml.xpath.XPath) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Document(org.w3c.dom.Document) File(java.io.File)

Example 58 with XPath

use of javax.xml.xpath.XPath in project flyway by flyway.

the class CommandLineLargeTest method getPomVersion.

/**
     * Retrieves the version embedded in the project pom. Useful for running these tests in IntelliJ.
     *
     * @return The POM version.
     */
protected String getPomVersion() {
    try {
        File pom = new File("pom.xml");
        if (!pom.exists()) {
            return "unknown";
        }
        XPath xPath = XPathFactory.newInstance().newXPath();
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(false);
        Document document = documentBuilderFactory.newDocumentBuilder().parse(pom);
        return xPath.evaluate("/project/version", document);
    } catch (Exception e) {
        throw new IllegalStateException("Unable to read POM version", e);
    }
}
Also used : XPath(javax.xml.xpath.XPath) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Document(org.w3c.dom.Document) File(java.io.File)

Example 59 with XPath

use of javax.xml.xpath.XPath in project camel by apache.

the class BomGeneratorMojo method overwriteDependencyManagement.

private void overwriteDependencyManagement(Document pom, List<Dependency> dependencies) throws Exception {
    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xpath.compile("/project/dependencyManagement/dependencies");
    NodeList nodes = (NodeList) expr.evaluate(pom, XPathConstants.NODESET);
    if (nodes.getLength() == 0) {
        throw new IllegalStateException("No dependencies found in the dependencyManagement section of the current pom");
    }
    Node dependenciesSection = nodes.item(0);
    // cleanup the dependency management section
    while (dependenciesSection.hasChildNodes()) {
        Node child = dependenciesSection.getFirstChild();
        dependenciesSection.removeChild(child);
    }
    for (Dependency dep : dependencies) {
        Element dependencyEl = pom.createElement("dependency");
        Element groupIdEl = pom.createElement("groupId");
        groupIdEl.setTextContent(dep.getGroupId());
        dependencyEl.appendChild(groupIdEl);
        Element artifactIdEl = pom.createElement("artifactId");
        artifactIdEl.setTextContent(dep.getArtifactId());
        dependencyEl.appendChild(artifactIdEl);
        Element versionEl = pom.createElement("version");
        versionEl.setTextContent(dep.getVersion());
        dependencyEl.appendChild(versionEl);
        if (!"jar".equals(dep.getType())) {
            Element typeEl = pom.createElement("type");
            typeEl.setTextContent(dep.getType());
            dependencyEl.appendChild(typeEl);
        }
        if (dep.getClassifier() != null) {
            Element classifierEl = pom.createElement("classifier");
            classifierEl.setTextContent(dep.getClassifier());
            dependencyEl.appendChild(classifierEl);
        }
        if (dep.getScope() != null && !"compile".equals(dep.getScope())) {
            Element scopeEl = pom.createElement("scope");
            scopeEl.setTextContent(dep.getScope());
            dependencyEl.appendChild(scopeEl);
        }
        if (dep.getExclusions() != null && !dep.getExclusions().isEmpty()) {
            Element exclsEl = pom.createElement("exclusions");
            for (Exclusion e : dep.getExclusions()) {
                Element exclEl = pom.createElement("exclusion");
                Element groupIdExEl = pom.createElement("groupId");
                groupIdExEl.setTextContent(e.getGroupId());
                exclEl.appendChild(groupIdExEl);
                Element artifactIdExEl = pom.createElement("artifactId");
                artifactIdExEl.setTextContent(e.getArtifactId());
                exclEl.appendChild(artifactIdExEl);
                exclsEl.appendChild(exclEl);
            }
            dependencyEl.appendChild(exclsEl);
        }
        dependenciesSection.appendChild(dependencyEl);
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Exclusion(org.apache.maven.model.Exclusion) Dependency(org.apache.maven.model.Dependency)

Example 60 with XPath

use of javax.xml.xpath.XPath in project camel by apache.

the class BomGeneratorMojo method loadBasePom.

private Document loadBasePom() throws Exception {
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document pom = builder.parse(sourcePom);
    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xpath.compile("/project/parent/version");
    Node node = (Node) expr.evaluate(pom, XPathConstants.NODE);
    if (node != null && node.getTextContent() != null && node.getTextContent().trim().equals("${project.version}")) {
        node.setTextContent(project.getVersion());
    }
    return pom;
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document)

Aggregations

XPath (javax.xml.xpath.XPath)526 Document (org.w3c.dom.Document)254 NodeList (org.w3c.dom.NodeList)230 XPathFactory (javax.xml.xpath.XPathFactory)215 Node (org.w3c.dom.Node)171 XPathExpressionException (javax.xml.xpath.XPathExpressionException)159 XPathExpression (javax.xml.xpath.XPathExpression)142 DocumentBuilder (javax.xml.parsers.DocumentBuilder)125 Element (org.w3c.dom.Element)118 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)97 IOException (java.io.IOException)89 Test (org.junit.Test)80 InputSource (org.xml.sax.InputSource)59 File (java.io.File)57 SAXException (org.xml.sax.SAXException)57 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)51 ByteArrayInputStream (java.io.ByteArrayInputStream)44 ArrayList (java.util.ArrayList)44 InputStream (java.io.InputStream)39 DSNamespaceContext (org.apache.xml.security.test.dom.DSNamespaceContext)37