This commit is contained in:
jstaerk
2025-04-18 16:09:26 +02:00
parent 31c5d0461e
commit f1adde1aba
3 changed files with 73 additions and 61 deletions

View File

@@ -13,6 +13,7 @@
- #802 - #802
- #809 - #809
- #812 - #812
- #818
2.16.3 2.16.3

View File

@@ -347,8 +347,11 @@ public class Main {
Option attachmentOpt = new Option("attachments", "attachments", true, "File attachments"); Option attachmentOpt = new Option("attachments", "attachments", true, "File attachments");
attachmentOpt.setValueSeparator(','); attachmentOpt.setValueSeparator(',');
attachmentOpt.setArgs(Option.UNLIMITED_VALUES); attachmentOpt.setArgs(Option.UNLIMITED_VALUES);
options.addOption(attachmentOpt); 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", "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("source-xml", "source-xml", true, "which source file to use"));
options.addOption(new Option("language", "language", true, "output language (en, de or fr)")); 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 zugferdProfile = cmd.getOptionValue("profile");
String[] attachmentFilenames = cmd.hasOption("attachments") ? cmd.getOptionValues("attachments") : null; String[] attachmentFilenames = cmd.hasOption("attachments") ? cmd.getOptionValues("attachments") : null;
String[] excludedFilenames = cmd.hasOption("exclude") ? cmd.getOptionValues("exclude") : null;
ArrayList<FileAttachment> attachments = new ArrayList<>(); ArrayList<FileAttachment> attachments = new ArrayList<>();
@@ -433,9 +437,9 @@ public class Main {
} else if ((action != null) && (action.equals("validate"))) { } else if ((action != null) && (action.equals("validate"))) {
optionsRecognized = performValidate(sourceName, noNotices, cmd.getOptionValue("logAppend"), LogAsPDF); optionsRecognized = performValidate(sourceName, noNotices, cmd.getOptionValue("logAppend"), LogAsPDF);
} else if ((action != null) && (action.equals("validateExpectValid"))) { } else if ((action != null) && (action.equals("validateExpectValid"))) {
optionsRecognized = performValidateExpect(true, directoryName); optionsRecognized = performValidateExpect(true, directoryName, excludedFilenames);
} else if ((action != null) && (action.equals("validateExpectInvalid"))) { } else if ((action != null) && (action.equals("validateExpectInvalid"))) {
optionsRecognized = performValidateExpect(false, directoryName); optionsRecognized = performValidateExpect(false, directoryName, excludedFilenames);
} }
} catch (UnrecognizedOptionException ex) { } catch (UnrecognizedOptionException ex) {
@@ -487,8 +491,8 @@ public class Main {
return optionsRecognized; return optionsRecognized;
} }
private static boolean performValidateExpect(boolean valid, String dirName) { private static boolean performValidateExpect(boolean valid, String dirName, String[] excludedFiles) {
ValidatorFileWalker zfWalk = new ValidatorFileWalker(valid); ValidatorFileWalker zfWalk = new ValidatorFileWalker(valid, excludedFiles);
Path startingDir = Paths.get(dirName); Path startingDir = Paths.get(dirName);
try { try {
Files.walkFileTree(startingDir, zfWalk); Files.walkFileTree(startingDir, zfWalk);

View File

@@ -1,7 +1,6 @@
package org.mustangproject.commandline; package org.mustangproject.commandline;
import java.io.IOException; import java.io.IOException;
import java.nio.file.FileSystems; import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult; import java.nio.file.FileVisitResult;
@@ -11,7 +10,9 @@ import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date; import java.util.Date;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.mustangproject.validator.ZUGFeRDValidator; import org.mustangproject.validator.ZUGFeRDValidator;
@@ -23,13 +24,15 @@ public class ValidatorFileWalker
private static final Logger LOGGER = LoggerFactory.getLogger(ValidatorFileWalker.class.getCanonicalName()); // log private static final Logger LOGGER = LoggerFactory.getLogger(ValidatorFileWalker.class.getCanonicalName()); // log
protected PathMatcher matcher; protected PathMatcher matcher;
protected ZUGFeRDValidator zul; protected ZUGFeRDValidator zul;
protected int fileCount=1; protected int fileCount = 1;
protected boolean expectValid=true; protected boolean expectValid = true;
protected boolean allValid=true; protected boolean allValid = true;
protected String[] excludedFiles = {};
public ValidatorFileWalker(boolean expectValid) { public ValidatorFileWalker(boolean expectValid, String[] excludedFiles) {
this.zul = new ZUGFeRDValidator(); this.zul = new ZUGFeRDValidator();
this.expectValid=expectValid; this.expectValid = expectValid;
this.excludedFiles = excludedFiles;
matcher = FileSystems.getDefault().getPathMatcher("glob:*.{pdf,xml}"); matcher = FileSystems.getDefault().getPathMatcher("glob:*.{pdf,xml}");
} }
@@ -37,6 +40,7 @@ public class ValidatorFileWalker
public boolean getResult() { public boolean getResult() {
return allValid; return allValid;
} }
// Print information about // Print information about
// each type of file. // each type of file.
@Override @Override
@@ -45,24 +49,27 @@ public class ValidatorFileWalker
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//get current date time with Date() //get current date time with Date()
Date date = new Date(); Date date = new Date();
String expectedString="valid"; String expectedString = "valid";
if (!expectValid) { if (!expectValid) {
expectedString="invalid"; expectedString = "invalid";
} }
if ((attr!=null)&&(attr.isRegularFile())) { if ((attr != null) && (attr.isRegularFile())) {
if (matcher.matches(file.getFileName())) { if (matcher.matches(file.getFileName())) {
String thisResultString=" valid"; // I could have extended the path matcher but an exclusion list is quite simple
if ((excludedFiles == null) || (!Arrays.asList(excludedFiles).contains(file.getFileName().toString()))) {
String thisResultString = " valid";
try { try {
assertThat(zul.validate(file.toAbsolutePath().toString())).valueByXPath("/validation/summary/@status") assertThat(zul.validate(file.toAbsolutePath().toString())).valueByXPath("/validation/summary/@status")
.asString() .asString()
.isEqualTo(expectedString); .isEqualTo(expectedString);
} catch (AssertionError ae) { } catch (AssertionError ae) {
thisResultString="invalid"; thisResultString = "invalid";
allValid=false; allValid = false;
}
LOGGER.info(String.format("\n@%s Testing file %d: %s (%s) ", dateFormat.format(date), fileCount++, thisResultString, file));
} }
LOGGER.info(String.format("\n@%s Testing file %d: %s (%s)", dateFormat.format(date), fileCount++, thisResultString, file));
} }
} }
return FileVisitResult.CONTINUE; return FileVisitResult.CONTINUE;
@@ -84,7 +91,7 @@ public class ValidatorFileWalker
@Override @Override
public FileVisitResult visitFileFailed(Path file, public FileVisitResult visitFileFailed(Path file,
IOException exc) { IOException exc) {
LOGGER.error(exc.getMessage(),exc); LOGGER.error(exc.getMessage(), exc);
return FileVisitResult.CONTINUE; return FileVisitResult.CONTINUE;
} }
} }