Search in sources :

Example 21 with XPath

use of javax.xml.xpath.XPath in project spring-boot by spring-projects.

the class Versions method evaluateExpression.

private static String evaluateExpression(String expression) {
    try {
        XPathFactory xPathFactory = XPathFactory.newInstance();
        XPath xpath = xPathFactory.newXPath();
        XPathExpression expr = xpath.compile(expression);
        String version = expr.evaluate(new InputSource(new FileReader("target/dependencies-pom.xml")));
        return version;
    } catch (Exception ex) {
        throw new IllegalStateException("Failed to evaluate expression", ex);
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) XPathFactory(javax.xml.xpath.XPathFactory) InputSource(org.xml.sax.InputSource) FileReader(java.io.FileReader)

Example 22 with XPath

use of javax.xml.xpath.XPath in project spring-framework by spring-projects.

the class XpathExpectationsHelper method compileXpathExpression.

private XPathExpression compileXpathExpression(String expression, Map<String, String> namespaces) throws XPathExpressionException {
    SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext();
    namespaceContext.setBindings((namespaces != null) ? namespaces : Collections.<String, String>emptyMap());
    XPath xpath = XPathFactory.newInstance().newXPath();
    xpath.setNamespaceContext(namespaceContext);
    return xpath.compile(expression);
}
Also used : XPath(javax.xml.xpath.XPath) SimpleNamespaceContext(org.springframework.util.xml.SimpleNamespaceContext)

Example 23 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 24 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)

Example 25 with XPath

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

the class EipDocumentationEnricherMojo method runPlugin.

private void runPlugin() throws Exception {
    Document document = XmlHelper.buildNamespaceAwareDocument(inputCamelSchemaFile);
    XPath xPath = XmlHelper.buildXPath(new CamelSpringNamespace());
    DomFinder domFinder = new DomFinder(document, xPath);
    DocumentationEnricher documentationEnricher = new DocumentationEnricher(document);
    // include schema files from camel-core, camel-corem-xml and from camel-spring
    File rootDir = new File(camelCoreDir, pathToModelDir);
    Map<String, File> jsonFiles = PackageHelper.findJsonFiles(rootDir);
    File rootDir2 = new File(camelCoreXmlDir, pathToCoreXmlModelDir);
    Map<String, File> jsonFiles2 = PackageHelper.findJsonFiles(rootDir2);
    File rootDir3 = new File(camelSpringDir, pathToSpringModelDir);
    Map<String, File> jsonFiles3 = PackageHelper.findJsonFiles(rootDir3);
    // merge the json files together
    jsonFiles.putAll(jsonFiles2);
    jsonFiles.putAll(jsonFiles3);
    NodeList elementsAndTypes = domFinder.findElementsAndTypes();
    documentationEnricher.enrichTopLevelElementsDocumentation(elementsAndTypes, jsonFiles);
    Map<String, String> typeToNameMap = buildTypeToNameMap(elementsAndTypes);
    Set<String> injectedTypes = new LinkedHashSet<String>();
    getLog().info("Found " + typeToNameMap.size() + " models to use when enriching the XSD schema");
    for (Map.Entry<String, String> entry : typeToNameMap.entrySet()) {
        String elementType = entry.getKey();
        String elementName = entry.getValue();
        if (jsonFileExistsForElement(jsonFiles, elementName)) {
            getLog().debug("Enriching " + elementName);
            File file = jsonFiles.get(elementName);
            injectAttributesDocumentation(domFinder, documentationEnricher, file, elementType, injectedTypes);
        }
    }
    saveToFile(document, outputCamelSchemaFile, XmlHelper.buildTransformer());
}
Also used : XPath(javax.xml.xpath.XPath) LinkedHashSet(java.util.LinkedHashSet) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) File(java.io.File) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

XPath (javax.xml.xpath.XPath)197 Document (org.w3c.dom.Document)107 NodeList (org.w3c.dom.NodeList)99 XPathExpressionException (javax.xml.xpath.XPathExpressionException)70 Node (org.w3c.dom.Node)70 XPathExpression (javax.xml.xpath.XPathExpression)69 XPathFactory (javax.xml.xpath.XPathFactory)59 DocumentBuilder (javax.xml.parsers.DocumentBuilder)45 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)37 IOException (java.io.IOException)32 Element (org.w3c.dom.Element)32 Test (org.junit.Test)25 InputSource (org.xml.sax.InputSource)23 SAXException (org.xml.sax.SAXException)21 File (java.io.File)19 ArrayList (java.util.ArrayList)19 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)18 HashMap (java.util.HashMap)16 InputStream (java.io.InputStream)12 PBXNativeTarget (com.facebook.buck.apple.xcode.xcodeproj.PBXNativeTarget)11