com.metaeffekt.artifact.analysis.bom.validation.ValidationResultGenerator Maven / Gradle / Ivy
The newest version!
package com.metaeffekt.artifact.analysis.bom.validation;
import com.metaeffekt.artifact.analysis.bom.spdx.facade.SpdxApiFacade;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.cyclonedx.parsers.JsonParser;
import org.cyclonedx.parsers.XmlParser;
import org.spdx.library.model.SpdxDocument;
import java.io.*;
import java.util.*;
import java.util.stream.Collectors;
@Slf4j
public class ValidationResultGenerator {
// FIXME: strange test setup; usage of static methods in test is awkward
public static void generateCycloneDXValidationResults(List bomFiles, String outputDirectory, File template) throws IOException {
for (File bomFile : bomFiles) {
Map placeholderData = new HashMap<>();
List validationResults = new ArrayList<>();
placeholderData.put("documentName", bomFile.getName());
placeholderData.put("creationDate", Calendar.getInstance().getTime().toString());
placeholderData.put("manualAdjustments", "No manual adjustments were made.");
placeholderData.put("additionalRemarks", "No additional remarks.");
placeholderData.put("specVersion", "1.6");
if (FilenameUtils.getExtension(bomFile.getName()).equals("xml")) {
validationResults.addAll(new XmlParser().validate(bomFile).stream().map(Throwable::getMessage).collect(Collectors.toList()));
} else if (FilenameUtils.getExtension(bomFile.getName()).equals("json")) {
validationResults.addAll(new JsonParser().validate(bomFile).stream().map(Throwable::getMessage).collect(Collectors.toList()));
} else {
log.error("Unsupported file type {}", bomFile.getName());
validationResults.add("Unsupported file type " + bomFile.getName() + ". Can't provide validation " +
"results.");
}
writeValidationResultsToFile(validationResults, outputDirectory, bomFile, placeholderData, template);
}
}
public static void generateSpdxValidationResults(List spdxDocumentFiles, String outputDirectory, File template) {
for (File spdxDocumentFile : spdxDocumentFiles) {
Map placeholderData = new HashMap<>();
List validationResults = new ArrayList<>();
placeholderData.put("documentName", spdxDocumentFile.getName());
placeholderData.put("creationDate", Calendar.getInstance().getTime().toString());
placeholderData.put("manualAdjustments", "No manual adjustments were made.");
placeholderData.put("additionalRemarks", "No additional remarks.");
SpdxDocument spdxDocument = SpdxApiFacade.generateFromFile(spdxDocumentFile);
if (spdxDocument != null) {
validationResults.addAll(spdxDocument.verify("2.3"));
placeholderData.put("specVersion", "2.3");
} else {
placeholderData.put("specVersion", "Could not be automatically derived");
placeholderData.put("validationResults", "Validation failed because the document is not a valid " +
"document as described by the spdx specification.");
}
writeValidationResultsToFile(validationResults, outputDirectory, spdxDocumentFile, placeholderData, template);
}
}
private static void writeValidationResultsToFile(List validationResults, String outputDirectory,
File documentFile, Map placeholderData, File template) {
File validationFile = new File(outputDirectory + FilenameUtils.removeExtension(documentFile.getName()) +
".validation.md");
if (validationFile.exists()) {
validationFile.delete();
}
if (validationResults.isEmpty() && !placeholderData.containsKey("validationResults")) {
placeholderData.put("validationResults", "Document validation returned no errors or warnings.");
} else if (!placeholderData.containsKey("validationResults")) {
placeholderData.put("validationResults", filteredValidationResults(validationResults));
}
try (BufferedReader reader = new BufferedReader(new FileReader(template.toString()))) {
StringBuilder content = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
content.append(line).append("\n");
}
String result = content.toString();
for (Map.Entry entry : placeholderData.entrySet()) {
String placeholder = "{{" + entry.getKey() + "}}";
result = result.replace(placeholder, entry.getValue());
}
try (BufferedWriter writer = new BufferedWriter(new FileWriter(validationFile.toString()))) {
writer.write(result);
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static String filteredValidationResults(List validationResults) {
return validationResults.stream().distinct().collect(Collectors.joining("\n"));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy