86 lines
3.6 KiB
Java
86 lines
3.6 KiB
Java
package io.gitlab.jfronny.inceptum;
|
|
|
|
import io.gitlab.jfronny.inceptum.common.model.maven.Pom;
|
|
import org.w3c.dom.Document;
|
|
import org.w3c.dom.Node;
|
|
import org.xml.sax.SAXException;
|
|
|
|
import javax.xml.parsers.*;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
|
|
public class Main {
|
|
private static final DocumentBuilder FACTORY;
|
|
|
|
static {
|
|
try {
|
|
FACTORY = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
|
} catch (ParserConfigurationException e) {
|
|
throw new RuntimeException("Could not create document builder", e);
|
|
}
|
|
}
|
|
|
|
public static void main(String[] args) throws IOException, SAXException {
|
|
Pom result;
|
|
try (InputStream is = Main.class.getResourceAsStream("/ExamplePom.xml")) {
|
|
Document doc = FACTORY.parse(is);
|
|
doc.documentElement.normalize();
|
|
result = new Pom();
|
|
if (!"project".equals(doc.documentElement.nodeName)) throw new IOException("Illegal document name");
|
|
boolean hasModelVersion = false;
|
|
boolean hasGroupId = false;
|
|
boolean hasArtifactId = false;
|
|
boolean hasVersion = false;
|
|
for (Node node : doc.documentElement.childNodes) {
|
|
switch (node.nodeName) {
|
|
case "modelVersion" -> {
|
|
hasModelVersion = true;
|
|
result.modelVersion = node.textContent;
|
|
}
|
|
case "parent" -> {
|
|
// Dirty hack to get slf4j working: simply assume the groupId and version of the parent is also the groupId of this
|
|
if (!hasGroupId) {
|
|
for (Node child : node.childNodes) {
|
|
switch (child.nodeName) {
|
|
case "groupId" -> {
|
|
if (!hasGroupId) {
|
|
hasGroupId = true;
|
|
result.groupId = node.textContent;
|
|
}
|
|
}
|
|
case "version" -> {
|
|
if (!hasVersion) {
|
|
hasVersion = true;
|
|
result.version = node.textContent;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
case "groupId" -> {
|
|
hasGroupId = true;
|
|
result.groupId = node.textContent;
|
|
}
|
|
case "artifactId" -> {
|
|
hasArtifactId = true;
|
|
result.artifactId = node.textContent;
|
|
}
|
|
case "version" -> {
|
|
hasVersion = true;
|
|
result.version = node.textContent;
|
|
}
|
|
case "packaging" -> result.packaging = node.textContent;
|
|
case "classifier" -> result.classifier = node.textContent;
|
|
default -> {}
|
|
}
|
|
}
|
|
if (!hasModelVersion) throw new IOException("Pom lacks modelVersion");
|
|
if (!hasGroupId) throw new IOException("Pom lacks groupId");
|
|
if (!hasArtifactId) throw new IOException("Pom lacks artifactId");
|
|
if (!hasVersion) throw new IOException("Pom lacks version");
|
|
}
|
|
System.out.println("Parsed: $result");
|
|
}
|
|
}
|