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);
}
}
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);
}
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);
}
}
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;
}
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());
}
Aggregations