From f1adde1abadb55e0134ccccbc72edad2a4dcefff Mon Sep 17 00:00:00 2001 From: jstaerk Date: Fri, 18 Apr 2025 16:09:26 +0200 Subject: [PATCH] closes #818 --- History.md | 1 + .../org/mustangproject/commandline/Main.java | 14 ++- .../commandline/ValidatorFileWalker.java | 119 +++++++++--------- 3 files changed, 73 insertions(+), 61 deletions(-) diff --git a/History.md b/History.md index 8d09ce9f..03373820 100644 --- a/History.md +++ b/History.md @@ -13,6 +13,7 @@ - #802 - #809 - #812 +- #818 2.16.3 diff --git a/Mustang-CLI/src/main/java/org/mustangproject/commandline/Main.java b/Mustang-CLI/src/main/java/org/mustangproject/commandline/Main.java index 5ab757fe..5d7806ab 100755 --- a/Mustang-CLI/src/main/java/org/mustangproject/commandline/Main.java +++ b/Mustang-CLI/src/main/java/org/mustangproject/commandline/Main.java @@ -347,8 +347,11 @@ public class Main { Option attachmentOpt = new Option("attachments", "attachments", true, "File attachments"); attachmentOpt.setValueSeparator(','); attachmentOpt.setArgs(Option.UNLIMITED_VALUES); - options.addOption(attachmentOpt); + Option excludeOpt = new Option("exclude", "exclude", true, "Files to exclude from recursive directory traversal"); + excludeOpt.setValueSeparator(','); + excludeOpt.setArgs(Option.UNLIMITED_VALUES); + options.addOption(excludeOpt); options.addOption(new Option("source", "source", true, "which source file to use")); options.addOption(new Option("source-xml", "source-xml", true, "which source file to use")); options.addOption(new Option("language", "language", true, "output language (en, de or fr)")); @@ -389,6 +392,7 @@ public class Main { String zugferdProfile = cmd.getOptionValue("profile"); String[] attachmentFilenames = cmd.hasOption("attachments") ? cmd.getOptionValues("attachments") : null; + String[] excludedFilenames = cmd.hasOption("exclude") ? cmd.getOptionValues("exclude") : null; ArrayList attachments = new ArrayList<>(); @@ -433,9 +437,9 @@ public class Main { } else if ((action != null) && (action.equals("validate"))) { optionsRecognized = performValidate(sourceName, noNotices, cmd.getOptionValue("logAppend"), LogAsPDF); } else if ((action != null) && (action.equals("validateExpectValid"))) { - optionsRecognized = performValidateExpect(true, directoryName); + optionsRecognized = performValidateExpect(true, directoryName, excludedFilenames); } else if ((action != null) && (action.equals("validateExpectInvalid"))) { - optionsRecognized = performValidateExpect(false, directoryName); + optionsRecognized = performValidateExpect(false, directoryName, excludedFilenames); } } catch (UnrecognizedOptionException ex) { @@ -487,8 +491,8 @@ public class Main { return optionsRecognized; } - private static boolean performValidateExpect(boolean valid, String dirName) { - ValidatorFileWalker zfWalk = new ValidatorFileWalker(valid); + private static boolean performValidateExpect(boolean valid, String dirName, String[] excludedFiles) { + ValidatorFileWalker zfWalk = new ValidatorFileWalker(valid, excludedFiles); Path startingDir = Paths.get(dirName); try { Files.walkFileTree(startingDir, zfWalk); diff --git a/Mustang-CLI/src/main/java/org/mustangproject/commandline/ValidatorFileWalker.java b/Mustang-CLI/src/main/java/org/mustangproject/commandline/ValidatorFileWalker.java index 5b5d8340..4fa12361 100644 --- a/Mustang-CLI/src/main/java/org/mustangproject/commandline/ValidatorFileWalker.java +++ b/Mustang-CLI/src/main/java/org/mustangproject/commandline/ValidatorFileWalker.java @@ -1,7 +1,6 @@ package org.mustangproject.commandline; - import java.io.IOException; import java.nio.file.FileSystems; import java.nio.file.FileVisitResult; @@ -11,25 +10,29 @@ import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; import java.text.DateFormat; import java.text.SimpleDateFormat; +import java.util.Arrays; import java.util.Date; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.mustangproject.validator.ZUGFeRDValidator; import static org.xmlunit.assertj.XmlAssert.assertThat; -public class ValidatorFileWalker - extends SimpleFileVisitor { +public class ValidatorFileWalker + extends SimpleFileVisitor { private static final Logger LOGGER = LoggerFactory.getLogger(ValidatorFileWalker.class.getCanonicalName()); // log protected PathMatcher matcher; protected ZUGFeRDValidator zul; - protected int fileCount=1; - protected boolean expectValid=true; - protected boolean allValid=true; + protected int fileCount = 1; + protected boolean expectValid = true; + protected boolean allValid = true; + protected String[] excludedFiles = {}; - public ValidatorFileWalker(boolean expectValid) { + public ValidatorFileWalker(boolean expectValid, String[] excludedFiles) { this.zul = new ZUGFeRDValidator(); - this.expectValid=expectValid; + this.expectValid = expectValid; + this.excludedFiles = excludedFiles; matcher = FileSystems.getDefault().getPathMatcher("glob:*.{pdf,xml}"); } @@ -37,54 +40,58 @@ public class ValidatorFileWalker public boolean getResult() { return allValid; } - // Print information about - // each type of file. - @Override - public FileVisitResult visitFile(Path file, - BasicFileAttributes attr) { - DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - //get current date time with Date() - Date date = new Date(); - String expectedString="valid"; - if (!expectValid) { - expectedString="invalid"; - } - if ((attr!=null)&&(attr.isRegularFile())) { - if (matcher.matches(file.getFileName())) { - String thisResultString=" valid"; - try { - assertThat(zul.validate(file.toAbsolutePath().toString())).valueByXPath("/validation/summary/@status") - .asString() - .isEqualTo(expectedString); - - } catch (AssertionError ae) { - thisResultString="invalid"; - allValid=false; - } - LOGGER.info(String.format("\n@%s Testing file %d: %s (%s)", dateFormat.format(date), fileCount++, thisResultString, file)); - - } - } - return FileVisitResult.CONTINUE; - } - // Print each directory visited. - @Override - public FileVisitResult postVisitDirectory(Path dir, - IOException exc) { - LOGGER.info("\nDirectory: %s%n", dir); - return FileVisitResult.CONTINUE; - } + // Print information about + // each type of file. + @Override + public FileVisitResult visitFile(Path file, + BasicFileAttributes attr) { + DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + //get current date time with Date() + Date date = new Date(); + String expectedString = "valid"; + if (!expectValid) { + expectedString = "invalid"; + } + if ((attr != null) && (attr.isRegularFile())) { + if (matcher.matches(file.getFileName())) { + // I could have extended the path matcher but an exclusion list is quite simple + if ((excludedFiles == null) || (!Arrays.asList(excludedFiles).contains(file.getFileName().toString()))) { - // If there is some error accessing - // the file, let the user know. - // If you don't override this method - // and an error occurs, an IOException - // is thrown. - @Override - public FileVisitResult visitFileFailed(Path file, - IOException exc) { - LOGGER.error(exc.getMessage(),exc); - return FileVisitResult.CONTINUE; - } + String thisResultString = " valid"; + try { + assertThat(zul.validate(file.toAbsolutePath().toString())).valueByXPath("/validation/summary/@status") + .asString() + .isEqualTo(expectedString); + + } catch (AssertionError ae) { + thisResultString = "invalid"; + allValid = false; + } + LOGGER.info(String.format("\n@%s Testing file %d: %s (%s) ", dateFormat.format(date), fileCount++, thisResultString, file)); + } + } + } + return FileVisitResult.CONTINUE; + } + + // Print each directory visited. + @Override + public FileVisitResult postVisitDirectory(Path dir, + IOException exc) { + LOGGER.info("\nDirectory: %s%n", dir); + return FileVisitResult.CONTINUE; + } + + // If there is some error accessing + // the file, let the user know. + // If you don't override this method + // and an error occurs, an IOException + // is thrown. + @Override + public FileVisitResult visitFileFailed(Path file, + IOException exc) { + LOGGER.error(exc.getMessage(), exc); + return FileVisitResult.CONTINUE; + } }