Skip to content

Commit

Permalink
Fix xml file processing
Browse files Browse the repository at this point in the history
  • Loading branch information
cherylking committed Jan 31, 2024
1 parent 3bc6e01 commit e130bc4
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* (C) Copyright IBM Corporation 2017, 2020
* (C) Copyright IBM Corporation 2017, 2024
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -47,9 +47,15 @@ public class HttpPortUtil {
private static final XPath XPATH = XPathFactory.newInstance().newXPath();

private static final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
static {
factory.setNamespaceAware(true);
}
private static boolean factoryInitialized = false;

public static void initDocumentBuilderFactory() throws ParserConfigurationException {
if (!factoryInitialized) {
factory.setNamespaceAware(true);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
}
}

public static Integer getHttpPort(File serverXML, File bootstrapProperties)
throws FileNotFoundException, IOException, ParserConfigurationException, SAXException,
Expand Down Expand Up @@ -83,6 +89,7 @@ public static Integer getHttpPort(File serverXML, File bootstrapProperties, File

protected static Integer getHttpPortForServerXML(String serverXML, Properties bootstrapProperties, String configVariableXML) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException,
ArquillianConfigurationException {
initDocumentBuilderFactory();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new ByteArrayInputStream(serverXML.getBytes()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ private DocumentBuilder getDocumentBuilder() {
docBuilderFactory.setIgnoringElementContentWhitespace(true);
docBuilderFactory.setValidating(false);
try {
docBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
docBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
docBuilder = docBuilderFactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
// fail catastrophically if we can't create a document builder
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* (C) Copyright IBM Corporation 2017, 2021.
* (C) Copyright IBM Corporation 2017, 2024.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -55,7 +55,9 @@ public void createDocument(File xmlFile) throws ParserConfigurationException, SA
builderFactory.setCoalescing(true);
builderFactory.setIgnoringElementContentWhitespace(true);
builderFactory.setValidating(false);
DocumentBuilder builder = builderFactory.newDocumentBuilder();
builderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
builderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
DocumentBuilder builder = builderFactory.newDocumentBuilder();
doc = builder.parse(xmlFile);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* (C) Copyright IBM Corporation 2019, 2023.
* (C) Copyright IBM Corporation 2019, 2024.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -3446,6 +3446,8 @@ protected Collection<File> getOmitFilesList(File looseAppFile, String srcDirecto
try {
if (looseAppFile != null && looseAppFile.exists()) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(looseAppFile);
NodeList archiveList = document.getElementsByTagName("archive");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* (C) Copyright IBM Corporation 2021, 2023.
* (C) Copyright IBM Corporation 2021, 2024.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -114,20 +114,20 @@ private Map<File, String> populateESAMap(File additionalBOM) {
*/
private void prepareFeature(String groupId, String artifactId, String version, File additionalBOM, Map<File, String> esaMap) {
try {
String repoLocation = parseRepositoryLocation(additionalBOM, groupId, artifactId, "pom", version);
String targetJsonFile = createArtifactFilePath(repoLocation, groupId, FEATURES_JSON_ARTIFACT_ID, "json",
version);
File generatedJson = generateJson(targetJsonFile, esaMap);
if (generatedJson.exists()) {
jsonFile = generatedJson;
provideJsonFileDependency(generatedJson, groupId, version);
info("The features.json has been generated at the following location: " + generatedJson);
}else {
warn("The features.json could not be generated at the following location: " + generatedJson);
}
String repoLocation = parseRepositoryLocation(additionalBOM, groupId, artifactId, "pom", version);
String targetJsonFile = createArtifactFilePath(repoLocation, groupId, FEATURES_JSON_ARTIFACT_ID, "json",
version);
File generatedJson = generateJson(targetJsonFile, esaMap);
if (generatedJson.exists()) {
jsonFile = generatedJson;
provideJsonFileDependency(generatedJson, groupId, version);
info("The features.json has been generated at the following location: " + generatedJson);
}else {
warn("The features.json could not be generated at the following location: " + generatedJson);
}
} catch (PluginExecutionException e) {
warn("Error: The features.json could not be generated.");
warn(e.getMessage());
warn("Error: The features.json could not be generated.");
warn(e.getMessage());
}
}

Expand All @@ -144,42 +144,44 @@ private Map<File, String> downloadArtifactsFromBOM(File additionalBOM) throws Pl
Map<File, String> result = new HashMap<File, String>();
ArrayList<String> missing_tags = new ArrayList<>();
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(additionalBOM);
doc.getDocumentElement().normalize();
NodeList dependencyList = doc.getElementsByTagName("dependency");
for (int itr = 0; itr < dependencyList.getLength(); itr++) {
Node node = dependencyList.item(itr);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) node;

if(eElement.getElementsByTagName("groupId").item(0) == null ) {
missing_tags.add("groupId");
}
if(eElement.getElementsByTagName("artifactId").item(0) == null ) {
missing_tags.add("artifactId ");
}
if(eElement.getElementsByTagName("type").item(0) == null ) {
missing_tags.add("type");
}
if(eElement.getElementsByTagName("version").item(0) == null ) {
missing_tags.add("version");
}

if(!missing_tags.isEmpty()) {
throw new PluginExecutionException("Error: "+ missing_tags.toString() + " tag(s) not found in features-bom file " + additionalBOM);
}

String groupId = eElement.getElementsByTagName("groupId").item(0).getTextContent();
String artifactId = eElement.getElementsByTagName("artifactId").item(0).getTextContent();
String type = eElement.getElementsByTagName("type").item(0).getTextContent();
String version = eElement.getElementsByTagName("version").item(0).getTextContent();

File artifactFile = downloadArtifact(groupId, artifactId, type, version);
result.put(artifactFile, groupId);
}
}
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(additionalBOM);
doc.getDocumentElement().normalize();
NodeList dependencyList = doc.getElementsByTagName("dependency");
for (int itr = 0; itr < dependencyList.getLength(); itr++) {
Node node = dependencyList.item(itr);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) node;

if(eElement.getElementsByTagName("groupId").item(0) == null ) {
missing_tags.add("groupId");
}
if(eElement.getElementsByTagName("artifactId").item(0) == null ) {
missing_tags.add("artifactId ");
}
if(eElement.getElementsByTagName("type").item(0) == null ) {
missing_tags.add("type");
}
if(eElement.getElementsByTagName("version").item(0) == null ) {
missing_tags.add("version");
}

if(!missing_tags.isEmpty()) {
throw new PluginExecutionException("Error: "+ missing_tags.toString() + " tag(s) not found in features-bom file " + additionalBOM);
}

String groupId = eElement.getElementsByTagName("groupId").item(0).getTextContent();
String artifactId = eElement.getElementsByTagName("artifactId").item(0).getTextContent();
String type = eElement.getElementsByTagName("type").item(0).getTextContent();
String version = eElement.getElementsByTagName("version").item(0).getTextContent();

File artifactFile = downloadArtifact(groupId, artifactId, type, version);
result.put(artifactFile, groupId);
}
}
} catch (ParserConfigurationException | SAXException | IOException e) {
throw new PluginExecutionException("Cannot read the features-bom file " + additionalBOM.getAbsolutePath() + ". " + e.getMessage());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,10 @@ public Set<String> getServerXmlFeatures(Set<String> origResult, File serverDirec
debug("The server file " + canonicalServerFile + " is empty.");
} else {
try {
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
DocumentBuilder db = dbf.newDocumentBuilder();
db.setErrorHandler(new ErrorHandler() {
@Override
public void warning(SAXParseException e) throws SAXException {
Expand Down

0 comments on commit e130bc4

Please sign in to comment.