From d233b1cc9c4bf3373c21a3270a9654d5c5a4d007 Mon Sep 17 00:00:00 2001 From: Alex Geller Date: Fri, 1 Apr 2022 09:18:31 +0200 Subject: [PATCH] Fixed issue that specifying an attachment was mandatory --- .../org/mustangproject/commandline/Main.java | 50 ++++++++++++------- 1 file changed, 33 insertions(+), 17 deletions(-) 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 972c6c5a..f7624a2f 100755 --- a/Mustang-CLI/src/main/java/org/mustangproject/commandline/Main.java +++ b/Mustang-CLI/src/main/java/org/mustangproject/commandline/Main.java @@ -72,11 +72,12 @@ public class Main { + " [--source=]: set input PDF file\n" + " [--source-xml=]: set input XML file\n" + " [--out=]: set output PDF file\n" - + " [--format ]: set ZUGFeRD or FacturX\n" + + " [--format ]: set Factur-X, ZUGFeRD or Order-X\n" + " [--version <1|2>]: set ZUGFeRD version\n" + " [--profile <...>]: set ZUGFeRD profile\n" + " For ZUGFeRD v1: ASIC, OMFORT or EXENDED\n" - + " For ZUGFeRD v2: INIMUM, BASIC L, ASIC, IUS, N16931, Rechnung, EXENDED \n" + + " For ZUGFeRD v2: INIMUM, BASIC L, ASIC, IUS, N16931, Rechnung, EXENDED\n" + + " [--attachments=]: list of file attachments (passing a single empty file name prevents prompting)\n" + " --action=ubl convert UN/CEFACT 2016b CII XML to UBL XML\n" + " [--source ]: set input XML file\n" + " [--out ]: set output XML file\n" @@ -161,7 +162,7 @@ public class Main { * Prompts the user for a input or output filename * * @param prompt the text the user is asked - * @param defaultFilename a default Filename + * @param defaultFilename a default Filename. Passing an empty string indicates that specifying a file is optional * @param expectedExtension will warn if filename does not match expected file extension, "or" possible with e.g. pdf|xml * @param ensureFileExists will warn if file does NOT exist (for input files) * @param ensureFileNotExists will warn if file DOES exist (for output files) @@ -174,7 +175,7 @@ public class Main { do { // for a more sophisticated dialogue maybe https://github.com/mabe02/lanterna/ // could be taken into account - System.out.print(prompt + " (default: " + defaultFilename + "):"); + System.out.print(prompt + (defaultFilename.isEmpty() ? ":" : " (default: " + defaultFilename + "):")); BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in)); try { selectedName = buffer.readLine(); @@ -185,6 +186,9 @@ public class Main { if (selectedName.isEmpty()) { // pressed return without entering anything + if(defaultFilename.isEmpty()) { + return ""; //no default -> this is an optional filename + } selectedName = defaultFilename; } @@ -342,9 +346,10 @@ public class Main { options.addOption(new Option("f", "format",true, "which format to output")); options.addOption(new Option("", "version",true, "which version of the standard to use")); options.addOption(new Option("", "profile",true, "which profile of the standard to use")); - Option attachmentOpt=new Option("", "attachment", true, "File attachments"); + Option attachmentOpt=new Option("", "attachments", true, "File attachments"); attachmentOpt.setValueSeparator(','); attachmentOpt.setArgs(Option.UNLIMITED_VALUES); + options.addOption(attachmentOpt); options.addOption(new Option("", "source",true, "which source file to use")); options.addOption(new Option("", "source-xml",true, "which source file to use")); options.addOption(new Option("", "out",true, "which output file to write to")); @@ -374,6 +379,8 @@ public class Main { String zugferdVersion = cmd.getOptionValue("version"); String zugferdProfile = cmd.getOptionValue("profile"); + String[] attachmentFilenames = cmd.hasOption("attachments") ? cmd.getOptionValues("attachments") : null; + ArrayList attachments=new ArrayList <>(); boolean optionsRecognized=false; @@ -387,7 +394,7 @@ public class Main { performMetrics(directoryName, filesFromStdIn, ignoreFileExt); optionsRecognized=true; } else if ((action!=null)&&(action.equals("combine"))) { - performCombine(sourceName, sourceXMLName, outName, format, zugferdVersion, zugferdProfile, ignoreFileExt, attachments); + performCombine(sourceName, sourceXMLName, outName, format, zugferdVersion, zugferdProfile, ignoreFileExt, attachmentFilenames, attachments); optionsRecognized=true; } else if ((action!=null)&&(action.equals("extract"))) { performExtract(sourceName, outName); @@ -522,7 +529,7 @@ public class Main { } private static void performCombine(String pdfName, String xmlName, String outName, String format, String zfVersion, - String zfProfile, Boolean ignoreInputErrors, ArrayList attachments) throws Exception { + String zfProfile, Boolean ignoreInputErrors, String[] attachmentFilenames, ArrayList attachments) throws Exception { /* * ZUGFeRDExporter ze= new ZUGFeRDExporterFromA1Factory() * .setProducer("toecount") .setCreator(System.getProperty("user.name")) @@ -550,16 +557,25 @@ public class Main { System.out.println("Output PDF set to " + outName); } - byte attachmentContents[]=null; - String attachmentFilename, attachmentMime, attachmentDescription; - attachmentFilename = getFilenameFromUser("Attachment filename (empty for none)", "", "pdf", true, false); - if (attachmentFilename.length()!=0) { - attachmentContents=Files.readAllBytes(Paths.get(attachmentFilename)); - attachmentMime= Files.probeContentType(Paths.get(attachmentFilename)); - attachments.add(new FileAttachment(attachmentFilename, attachmentMime, "Data", attachmentContents)); - } - - + if (attachmentFilenames == null) { + byte attachmentContents[]=null; + String attachmentFilename, attachmentMime, attachmentDescription; + attachmentFilename = getFilenameFromUser("Attachment filename (empty for none)", "", "pdf", true, false); + if (attachmentFilename.length()!=0) { + attachmentContents=Files.readAllBytes(Paths.get(attachmentFilename)); + attachmentMime= Files.probeContentType(Paths.get(attachmentFilename)); + attachments.add(new FileAttachment(attachmentFilename, attachmentMime, "Data", attachmentContents)); + } + } else { + for (int i = 0; i < attachmentFilenames.length; i++) { + String attachmentFilename = attachmentFilenames[i]; + if (!attachmentFilename.isEmpty()) { + byte[] attachmentContents = Files.readAllBytes(Paths.get(attachmentFilename)); + String attachmentMime = Files.probeContentType(Paths.get(attachmentFilename)); + attachments.add(new FileAttachment(attachmentFilename, attachmentMime, "Data", attachmentContents)); + } + } + } if (format == null) {