From 0fc7e1b16a12bd9a232623bba4f3325684176285 Mon Sep 17 00:00:00 2001 From: Guillaume Date: Tue, 16 Oct 2018 16:59:08 +0200 Subject: [PATCH 1/2] Fix indentation and move each option into sub functions --- .../org/mustangproject/toecount/Toecount.java | 474 +++++++++--------- 1 file changed, 248 insertions(+), 226 deletions(-) diff --git a/src/main/java/org/mustangproject/toecount/Toecount.java b/src/main/java/org/mustangproject/toecount/Toecount.java index 0469f4b6..4ccf68d6 100755 --- a/src/main/java/org/mustangproject/toecount/Toecount.java +++ b/src/main/java/org/mustangproject/toecount/Toecount.java @@ -188,242 +188,264 @@ public class Toecount { } while (!fileExistenceOK); - return selectedName; + return selectedName; + } + + // /opt/local/bin/mvn clean compile assembly:single + public static void main(String[] args) { + CmdLineParser parser = new CmdLineParser(); + Option dirnameOption = parser.addStringOption('d', "directory"); + Option filesFromStdInOption = parser.addBooleanOption('l', "listfromstdin"); + Option ignoreFileExtOption = parser.addBooleanOption('i', "ignorefileextension"); + Option combineOption = parser.addBooleanOption('c', "combine"); + Option extractOption = parser.addBooleanOption('e', "extract"); + Option helpOption = parser.addBooleanOption('h', "help"); + Option upgradeOption = parser.addBooleanOption('u', "upgrade"); + Option a3onlyOption = parser.addBooleanOption('a', "a3only"); + + try { + parser.parse(args); + } catch (CmdLineParser.OptionException e) { + System.err.println(e.getMessage()); + printUsage(); + System.exit(2); } - // /opt/local/bin/mvn clean compile assembly:single - public static void main(String[] args) { - CmdLineParser parser = new CmdLineParser(); - Option dirnameOption = parser.addStringOption('d', "directory"); - Option filesFromStdInOption = parser.addBooleanOption('l', "listfromstdin"); - Option ignoreFileExtOption = parser.addBooleanOption('i', "ignorefileextension"); - Option combineOption = parser.addBooleanOption('c', "combine"); - Option extractOption = parser.addBooleanOption('e', "extract"); - Option helpOption = parser.addBooleanOption('h', "help"); - Option upgradeOption = parser.addBooleanOption('u', "upgrade"); - Option a3onlyOption = parser.addBooleanOption('a', "a3only"); + String directoryName = parser.getOptionValue(dirnameOption); - try { - parser.parse(args); - } catch (CmdLineParser.OptionException e) { - System.err.println(e.getMessage()); - printUsage(); - System.exit(2); - } + Boolean filesFromStdIn = parser.getOptionValue(filesFromStdInOption, Boolean.FALSE); - String directoryName = parser.getOptionValue(dirnameOption); + Boolean combineRequested = parser.getOptionValue(combineOption, Boolean.FALSE); - Boolean filesFromStdIn = parser.getOptionValue(filesFromStdInOption, Boolean.FALSE); + Boolean extractRequested = parser.getOptionValue(extractOption, Boolean.FALSE); - Boolean combineRequested = parser.getOptionValue(combineOption, Boolean.FALSE); + Boolean helpRequested = parser.getOptionValue(helpOption, Boolean.FALSE); - Boolean extractRequested = parser.getOptionValue(extractOption, Boolean.FALSE); + Boolean upgradeRequested = parser.getOptionValue(upgradeOption, Boolean.FALSE); - Boolean helpRequested = parser.getOptionValue(helpOption, Boolean.FALSE); + Boolean ignoreFileExt = parser.getOptionValue(ignoreFileExtOption, Boolean.FALSE); - Boolean upgradeRequested = parser.getOptionValue(upgradeOption, Boolean.FALSE); + Boolean a3only = parser.getOptionValue(a3onlyOption, Boolean.FALSE); - Boolean ignoreFileExt = parser.getOptionValue(ignoreFileExtOption, Boolean.FALSE); + if (helpRequested) { + printHelp(); + } - Boolean a3only = parser.getOptionValue(a3onlyOption, Boolean.FALSE); - - if (helpRequested) { - printHelp(); - } - - else if (((directoryName != null) && (directoryName.length() > 0)) || filesFromStdIn.booleanValue()) { - - StatRun sr = new StatRun(); - if (ignoreFileExt) { - sr.ignoreFileExtension(); - } - if (directoryName != null) { - Path startingDir = Paths.get(directoryName); - - if (Files.isRegularFile(startingDir)) { - String filename = startingDir.toString(); - FileChecker fc = new FileChecker(filename, sr); - - fc.checkForZUGFeRD(); - System.out.print(fc.getOutputLine()); - - } else if (Files.isDirectory(startingDir)) { - FileTraverser pf = new FileTraverser(sr); - try { - Files.walkFileTree(startingDir, pf); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - } - - } - - if (filesFromStdIn) { - BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); - String s; - try { - while ((s = in.readLine()) != null && s.length() != 0) { - FileChecker fc = new FileChecker(s, sr); - - fc.checkForZUGFeRD(); - System.out.print(fc.getOutputLine()); - - } - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - } - System.out.println(sr.getSummaryLine()); - } else if (combineRequested) { - /* - * ZUGFeRDExporter ze= new ZUGFeRDExporterFromA1Factory() - * .setProducer("toecount") .setCreator(System.getProperty("user.name")) - * .loadFromPDFA1("invoice.pdf"); - */ - String pdfName = ""; - String xmlName = ""; - String outName = ""; - - try { - - pdfName = getFilenameFromUser("Source PDF", "invoice.pdf", "pdf", true, false); - xmlName = getFilenameFromUser("ZUGFeRD XML", "ZUGFeRD-invoice.xml", "xml", true, false); - outName = getFilenameFromUser("Ouput PDF", "invoice.ZUGFeRD.pdf", "pdf", false, true); - String versionInput = ""; - try { - versionInput = getStringFromUser("ZUGFeRD version (1 or 2)", "1", "1|2"); - - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - String profileInput = ""; - int zfIntVersion=Integer.valueOf(versionInput); - - ZUGFeRDConformanceLevel profile=ZUGFeRDConformanceLevel.EXTENDED; - if (zfIntVersion==1) { - try { - profileInput = getStringFromUser("ZUGFeRD profile b)asic, c)omfort or e)xtended", "e", "B|b|C|c|E|e").toLowerCase(); - - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - if (profileInput.equals("b")) { - profile=ZUGFeRDConformanceLevel.BASIC; - } else if (profileInput.equals("c")) { - profile=ZUGFeRDConformanceLevel.COMFORT; - } else if (profileInput.equals("e")) { - profile=ZUGFeRDConformanceLevel.EXTENDED; - - } - - } else if (zfIntVersion==2) { - try { - profileInput = getStringFromUser("ZUGFeRD profile [M]INIMUM, BASIC [W]L, [B]ASIC,\n" + - "[C]IUS, [E]N16931, E[X]TENDED", "E", "M|m|W|w|B|b|C|c|E|e|X|x|").toLowerCase(); - - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - if (profileInput.equals("m")) { - profile=ZUGFeRDConformanceLevel.MINIMUM; - } else if (profileInput.equals("w")) { - profile=ZUGFeRDConformanceLevel.BASICWL; - } else if (profileInput.equals("b")) { - profile=ZUGFeRDConformanceLevel.BASIC; - } else if (profileInput.equals("c")) { - profile=ZUGFeRDConformanceLevel.CIUS; - } else if (profileInput.equals("e")) { - profile=ZUGFeRDConformanceLevel.EN16931; - } else if (profileInput.equals("x")) { - profile=ZUGFeRDConformanceLevel.EXTENDED; - } - - } - - ZUGFeRDExporter ze = new ZUGFeRDExporterFromA1Factory().setProducer("Toecount") - .setCreator(System.getProperty("user.name")).setZUGFeRDConformanceLevel(profile).load(pdfName); - ze.setZUGFeRDVersion(zfIntVersion); - - ze.setZUGFeRDXMLData(Files.readAllBytes(Paths.get(xmlName))); - - ze.export(outName); - } catch (IOException e) { - e.printStackTrace(); - // } catch (JAXBException e) { - // e.printStackTrace(); - } - System.out.println("Written to " + outName); - } else if (extractRequested) { - ZUGFeRDImporter zi = new ZUGFeRDImporter(); - String pdfName = getFilenameFromUser("Source PDF", "invoice.pdf", "pdf", true, false); - String xmlName = getFilenameFromUser("ZUGFeRD XML", "ZUGFeRD-invoice.xml", "xml", false, true); - - zi.extract(pdfName); - try { - byte[] XMLContent=zi.getRawXML(); - if (XMLContent==null) { - System.err.println("No ZUGFeRD XML found in PDF file"); - - } else { - Files.write(Paths.get(xmlName), XMLContent); - System.out.println("Written to " + xmlName); - } - } catch (IOException e) { - e.printStackTrace(); - } - - } else if (a3only) { - /* - * ZUGFeRDExporter ze= new ZUGFeRDExporterFromA1Factory() - * .setProducer("toecount") .setCreator(System.getProperty("user.name")) - * .loadFromPDFA1("invoice.pdf"); - */ - String pdfName = ""; - String outName = ""; - try { - pdfName = getFilenameFromUser("Source PDF", "invoice.pdf", "pdf", true, false); - outName = getFilenameFromUser("Target PDF", "invoice.a3.pdf", "pdf", false, true); - - ZUGFeRDExporter ze = new ZUGFeRDExporterFromA1Factory().setAttachZUGFeRDHeaders(false).load(pdfName); - - ze.export(outName); - } catch (IOException e) { - e.printStackTrace(); - } - System.out.println("Written to " + outName); - } else if (upgradeRequested) { - try { - String xmlName = ""; - String outName = ""; - - xmlName = getFilenameFromUser("ZUGFeRD 1.0 XML source", "ZUGFeRD-invoice.xml", "xml", true, false); - outName = getFilenameFromUser("ZUGFeRD 2.0 XML target", "factur-x.xml", "xml", false, true); - - ZUGFeRDMigrator zmi = new ZUGFeRDMigrator(); - String xml = null; - xml = zmi.migrateFromV1ToV2(xmlName); - Files.write(Paths.get(outName), xml.getBytes()); - System.out.println("Written to " + outName); - } catch (FileNotFoundException ex) { - Logger.getLogger(Toecount.class.getName()).log(Level.SEVERE, null, ex); - } catch (TransformerException | UnsupportedEncodingException ex) { - Logger.getLogger(Toecount.class.getName()).log(Level.SEVERE, null, ex); - } catch (IOException ex) { - Logger.getLogger(Toecount.class.getName()).log(Level.SEVERE, null, ex); - } - } else { - // no argument or argument unknown - printUsage(); - System.exit(2); - - } + else if (((directoryName != null) && (directoryName.length() > 0)) || filesFromStdIn.booleanValue()) { + performMetrics(directoryName, filesFromStdIn, ignoreFileExt); + } else if (combineRequested) { + performCombine(); + } else if (extractRequested) { + performExtract(); + } else if (a3only) { + performConvert(); + } else if (upgradeRequested) { + performUpgrade(); + } else { + // no argument or argument unknown + printUsage(); + System.exit(2); } } + + private static void performUpgrade() { + try { + String xmlName = ""; + String outName = ""; + + xmlName = getFilenameFromUser("ZUGFeRD 1.0 XML source", "ZUGFeRD-invoice.xml", "xml", true, false); + outName = getFilenameFromUser("ZUGFeRD 2.0 XML target", "factur-x.xml", "xml", false, true); + + ZUGFeRDMigrator zmi = new ZUGFeRDMigrator(); + String xml = null; + xml = zmi.migrateFromV1ToV2(xmlName); + Files.write(Paths.get(outName), xml.getBytes()); + System.out.println("Written to " + outName); + } catch (FileNotFoundException ex) { + Logger.getLogger(Toecount.class.getName()).log(Level.SEVERE, null, ex); + } catch (TransformerException | UnsupportedEncodingException ex) { + Logger.getLogger(Toecount.class.getName()).log(Level.SEVERE, null, ex); + } catch (IOException ex) { + Logger.getLogger(Toecount.class.getName()).log(Level.SEVERE, null, ex); + } + } + + private static void performConvert() { + /* + * ZUGFeRDExporter ze= new ZUGFeRDExporterFromA1Factory() + * .setProducer("toecount") .setCreator(System.getProperty("user.name")) + * .loadFromPDFA1("invoice.pdf"); + */ + String pdfName = ""; + String outName = ""; + try { + pdfName = getFilenameFromUser("Source PDF", "invoice.pdf", "pdf", true, false); + outName = getFilenameFromUser("Target PDF", "invoice.a3.pdf", "pdf", false, true); + + ZUGFeRDExporter ze = new ZUGFeRDExporterFromA1Factory().setAttachZUGFeRDHeaders(false).load(pdfName); + + ze.export(outName); + } catch (IOException e) { + e.printStackTrace(); + } + System.out.println("Written to " + outName); + } + + private static void performExtract() { + + ZUGFeRDImporter zi = new ZUGFeRDImporter(); + String pdfName = getFilenameFromUser("Source PDF", "invoice.pdf", "pdf", true, false); + String xmlName = getFilenameFromUser("ZUGFeRD XML", "ZUGFeRD-invoice.xml", "xml", false, true); + + zi.extract(pdfName); + try { + byte[] XMLContent=zi.getRawXML(); + if (XMLContent==null) { + System.err.println("No ZUGFeRD XML found in PDF file"); + + } else { + Files.write(Paths.get(xmlName), XMLContent); + System.out.println("Written to " + xmlName); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + private static void performCombine() { + /* + * ZUGFeRDExporter ze= new ZUGFeRDExporterFromA1Factory() + * .setProducer("toecount") .setCreator(System.getProperty("user.name")) + * .loadFromPDFA1("invoice.pdf"); + */ + String pdfName = ""; + String xmlName = ""; + String outName = ""; + + try { + + pdfName = getFilenameFromUser("Source PDF", "invoice.pdf", "pdf", true, false); + xmlName = getFilenameFromUser("ZUGFeRD XML", "ZUGFeRD-invoice.xml", "xml", true, false); + outName = getFilenameFromUser("Ouput PDF", "invoice.ZUGFeRD.pdf", "pdf", false, true); + String versionInput = ""; + try { + versionInput = getStringFromUser("ZUGFeRD version (1 or 2)", "1", "1|2"); + + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + String profileInput = ""; + int zfIntVersion=Integer.valueOf(versionInput); + + ZUGFeRDConformanceLevel profile=ZUGFeRDConformanceLevel.EXTENDED; + if (zfIntVersion==1) { + try { + profileInput = getStringFromUser("ZUGFeRD profile b)asic, c)omfort or e)xtended", "e", "B|b|C|c|E|e").toLowerCase(); + + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + if (profileInput.equals("b")) { + profile=ZUGFeRDConformanceLevel.BASIC; + } else if (profileInput.equals("c")) { + profile=ZUGFeRDConformanceLevel.COMFORT; + } else if (profileInput.equals("e")) { + profile=ZUGFeRDConformanceLevel.EXTENDED; + + } + + } else if (zfIntVersion==2) { + try { + profileInput = getStringFromUser("ZUGFeRD profile [M]INIMUM, BASIC [W]L, [B]ASIC,\n" + + "[C]IUS, [E]N16931, E[X]TENDED", "E", "M|m|W|w|B|b|C|c|E|e|X|x|").toLowerCase(); + + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + if (profileInput.equals("m")) { + profile=ZUGFeRDConformanceLevel.MINIMUM; + } else if (profileInput.equals("w")) { + profile=ZUGFeRDConformanceLevel.BASICWL; + } else if (profileInput.equals("b")) { + profile=ZUGFeRDConformanceLevel.BASIC; + } else if (profileInput.equals("c")) { + profile=ZUGFeRDConformanceLevel.CIUS; + } else if (profileInput.equals("e")) { + profile=ZUGFeRDConformanceLevel.EN16931; + } else if (profileInput.equals("x")) { + profile=ZUGFeRDConformanceLevel.EXTENDED; + } + + } + + ZUGFeRDExporter ze = new ZUGFeRDExporterFromA1Factory().setProducer("Toecount") + .setCreator(System.getProperty("user.name")).setZUGFeRDConformanceLevel(profile).load(pdfName); + ze.setZUGFeRDVersion(zfIntVersion); + + ze.setZUGFeRDXMLData(Files.readAllBytes(Paths.get(xmlName))); + + ze.export(outName); + } catch (IOException e) { + e.printStackTrace(); + // } catch (JAXBException e) { + // e.printStackTrace(); + } + + System.out.println("Written to " + outName); + } + + private static void performMetrics(String directoryName, Boolean filesFromStdIn, Boolean ignoreFileExt) { + + StatRun sr = new StatRun(); + if (ignoreFileExt) { + sr.ignoreFileExtension(); + } + if (directoryName != null) { + Path startingDir = Paths.get(directoryName); + + if (Files.isRegularFile(startingDir)) { + String filename = startingDir.toString(); + FileChecker fc = new FileChecker(filename, sr); + + fc.checkForZUGFeRD(); + System.out.print(fc.getOutputLine()); + + } else if (Files.isDirectory(startingDir)) { + FileTraverser pf = new FileTraverser(sr); + try { + Files.walkFileTree(startingDir, pf); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + } + + if (filesFromStdIn) { + BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); + String s; + try { + while ((s = in.readLine()) != null && s.length() != 0) { + FileChecker fc = new FileChecker(s, sr); + + fc.checkForZUGFeRD(); + System.out.print(fc.getOutputLine()); + + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + System.out.println(sr.getSummaryLine()); + } + + +} From 329bfd489d9446dc58a4899acb3850acd95f7c1d Mon Sep 17 00:00:00 2001 From: Guillaume Date: Tue, 16 Oct 2018 18:18:34 +0200 Subject: [PATCH 2/2] Commandline: ability to specify params through input args --- .../org/mustangproject/toecount/Toecount.java | 360 ++++++++++++------ 1 file changed, 251 insertions(+), 109 deletions(-) diff --git a/src/main/java/org/mustangproject/toecount/Toecount.java b/src/main/java/org/mustangproject/toecount/Toecount.java index 4ccf68d6..80ac0e71 100755 --- a/src/main/java/org/mustangproject/toecount/Toecount.java +++ b/src/main/java/org/mustangproject/toecount/Toecount.java @@ -56,19 +56,43 @@ public class Toecount { private static void printHelp() { System.out.println("Mustangproject.org " + org.mustangproject.ZUGFeRD.Version.VERSION + " \r\n" - + "A Apache Public License library and command line tool for statistics on PDF invoices with\r\n" - + "ZUGFeRD Metadata (http://www.zugferd.org)\r\n" + "\r\n" + getUsage() + "Count operations" - + "\t--directory= count ZUGFeRD files in directory to be scanned\r\n" + + "A Apache Public License library and command line tool for statistics on PDF invoices with\r\n" + + "ZUGFeRD Metadata (http://www.zugferd.org)\r\n" + + "\r\n" + + getUsage() + + "\r\n" + + "* Count operations\r\n" + + "\t-d, --directory\tcount ZUGFeRD files in directory to be scanned\r\n" + "\t\tIf it is a directory, it will recurse.\r\n" - + "\t--listfromstdin=count ZUGFeRD files from a list of linefeed separated files on runtime.\r\n" + + "\t-l, --listfromstdin\tcount ZUGFeRD files from a list of linefeed separated files on runtime.\r\n" + "\t\tIt will start once a blank line has been entered.\r\n" - + "\t--ignorefileextension=if PDF files are counted check *.* instead of *.pdf files" - + "Merge operations" + "\t--combine= combine XML and PDF file to ZUGFeRD PDF file\r\n" - + "\t--extract= extract ZUGFeRD PDF to XML file\r\n" - + "\t--upgrade= upgrade ZUGFeRD XML to ZUGFeRD 2 XML\r\n" - + "\t--a3only= upgrade from PDF/A1 to A3 only (no ZUGFeRD data attached) \r\n" - - ); + + "\r\n" + + "\tAdditional parameter for both count operations\r\n" + + "\t[-i, --ignorefileextension]\tCheck for all files (*.*) instead of PDF files only (*.pdf)\r\n" + + "\r\n" + + "* Merge operations\r\n" + + "\t-e, --extract\textract ZUGFeRD PDF to XML file\r\n" + + "\t\tAdditional parameters (optional - user will be prompted if not defined)\r\n" + + "\t\t[--source ]: set input PDF file\r\n" + + "\t\t[--out ]: set output XML file\r\n" + + "\t-u, --upgrade\tupgrade ZUGFeRD XML to ZUGFeRD 2 XML\r\n" + + "\t\tAdditional parameters (optional - user will be prompted if not defined)\r\n" + + "\t\t[--source ]: set input XML ZUGFeRD 1 file\r\n" + + "\t\t[--out ]: set output XML ZUGFeRD 2 file\r\n" + + "\t-a, --a3only\tupgrade from PDF/A1 to A3 only (no ZUGFeRD data attached) \r\n" + + "\t\tAdditional parameters (optional - user will be prompted if not defined)\r\n" + + "\t\t[--source ]: set input PDF file\r\n" + + "\t\t[--out ]: set output PDF file\r\n" + + "\t-c, --combine\tcombine XML and PDF file to ZUGFeRD PDF file\r\n" + + "\t\tAdditional parameters (optional - user will be prompted if not defined)\r\n" + + "\t\t[--source ]: set input PDF file\r\n" + + "\t\t[--source-xml ]: set input XML file\r\n" + + "\t\t[--out ]: set output PDF file\r\n" + + "\t\t[--zugferd-version <1|2>]: set ZUGFeRD version\r\n" + + "\t\t[--zugferd-profile <...>]: set ZUGFeRD profile\r\n" + + "\t\t\tFor ZUGFeRD v1: ASIC, OMFORT or XTENDED\r\n" + + "\t\t\tFor ZUGFeRD v2: INIMUM, BASIC L, ASIC, IUS, N16931, ETENDED\r\n" + ); } /*** @@ -161,8 +185,7 @@ public class Toecount { } } else if (ensureFileExists) { - File f = new File(selectedName); - if (f.exists()) { + if (fileExists(selectedName)) { fileExistenceOK = true; } else { System.out.println("File does not exist, try again or CTRL+C to cancel"); @@ -173,12 +196,10 @@ public class Toecount { fileExistenceOK=true; if (ensureFileNotExists) { - File f = new File(selectedName); - if (f.exists()) { + if (fileExists(selectedName)) { fileExistenceOK = false; System.out.println("Output file already exists, try again or CTRL+C to cancel"); // discard the input, a scanner.reset is not sufficient - } } else { fileExistenceOK=true; @@ -194,14 +215,55 @@ public class Toecount { // /opt/local/bin/mvn clean compile assembly:single public static void main(String[] args) { CmdLineParser parser = new CmdLineParser(); - Option dirnameOption = parser.addStringOption('d', "directory"); - Option filesFromStdInOption = parser.addBooleanOption('l', "listfromstdin"); - Option ignoreFileExtOption = parser.addBooleanOption('i', "ignorefileextension"); - Option combineOption = parser.addBooleanOption('c', "combine"); - Option extractOption = parser.addBooleanOption('e', "extract"); + + // Option: Help Option helpOption = parser.addBooleanOption('h', "help"); + + // Generic options available for multiple command + // --source: input file + Option sourceOption = parser.addStringOption("source"); + // --out: output file + Option outOption = parser.addStringOption("out"); + + // Command: Extract XML + // --extract + // (--source: input PDF file) + // (--out: output XML file) + Option extractOption = parser.addBooleanOption('e', "extract"); + + // Command: Migrating ZUGFeRD 1 to 2 + // --upgrade + // (--source: input XML ZUGFeRD 1 file) + // (--out: output XML ZUGFeRD 2 file) Option upgradeOption = parser.addBooleanOption('u', "upgrade"); + + // Command: Convert PDF/A-1 to PDF/A-3 + // --a3only + // (--source: input PDF file) + // (--out: output PDF file) Option a3onlyOption = parser.addBooleanOption('a', "a3only"); + + // Command: Combining PDF and XML + // --combine + // (--source: input PDF file) + // (--source-xml: input XML file) + // (--out: output PDF file) + // (--zugferd-version: ZUGFeRD version) + // (--zugferd-profile: ZUGFeRD profile) + Option combineOption = parser.addBooleanOption('c', "combine"); + Option sourceXmlOption = parser.addStringOption("source-xml"); + Option zugferdVersionOption = parser.addStringOption("zugferd-version"); + Option zugferdProfileOption = parser.addStringOption("zugferd-profile"); + + // Command: Show metrics in dir + // --directory + // (--ignorefileextension) + Option dirnameOption = parser.addStringOption('d', "directory"); + Option ignoreFileExtOption = parser.addBooleanOption('i', "ignorefileextension"); + + // Command: Show metrics from list from stdin + // --listfromstdin + Option filesFromStdInOption = parser.addBooleanOption('l', "listfromstdin"); try { parser.parse(args); @@ -211,21 +273,20 @@ public class Toecount { System.exit(2); } + // Retrieve all options String directoryName = parser.getOptionValue(dirnameOption); - Boolean filesFromStdIn = parser.getOptionValue(filesFromStdInOption, Boolean.FALSE); - Boolean combineRequested = parser.getOptionValue(combineOption, Boolean.FALSE); - Boolean extractRequested = parser.getOptionValue(extractOption, Boolean.FALSE); - Boolean helpRequested = parser.getOptionValue(helpOption, Boolean.FALSE); - Boolean upgradeRequested = parser.getOptionValue(upgradeOption, Boolean.FALSE); - Boolean ignoreFileExt = parser.getOptionValue(ignoreFileExtOption, Boolean.FALSE); - Boolean a3only = parser.getOptionValue(a3onlyOption, Boolean.FALSE); + String sourceName = parser.getOptionValue(sourceOption); + String sourceXMLName = parser.getOptionValue(sourceXmlOption); + String outName = parser.getOptionValue(outOption); + String zugferdVersion = parser.getOptionValue(zugferdVersionOption); + String zugferdProfile = parser.getOptionValue(zugferdProfileOption); if (helpRequested) { printHelp(); @@ -234,13 +295,13 @@ public class Toecount { else if (((directoryName != null) && (directoryName.length() > 0)) || filesFromStdIn.booleanValue()) { performMetrics(directoryName, filesFromStdIn, ignoreFileExt); } else if (combineRequested) { - performCombine(); + performCombine(sourceName, sourceXMLName, outName, zugferdVersion, zugferdProfile); } else if (extractRequested) { - performExtract(); + performExtract(sourceName, outName); } else if (a3only) { - performConvert(); + performConvert(sourceName, outName); } else if (upgradeRequested) { - performUpgrade(); + performUpgrade(sourceName, outName); } else { // no argument or argument unknown printUsage(); @@ -248,14 +309,25 @@ public class Toecount { } } - private static void performUpgrade() { + private static void performUpgrade(String xmlName, String outName) { try { - String xmlName = ""; - String outName = ""; + // Get params from user if not already defined + if (xmlName == null) { + xmlName = getFilenameFromUser("ZUGFeRD 1.0 XML source", "ZUGFeRD-invoice.xml", "xml", true, false); + } else { + System.out.println("ZUGFeRD 1.0 XML source set to " + xmlName); + } + if (outName == null) { + outName = getFilenameFromUser("ZUGFeRD 2.0 XML target", "factur-x.xml", "xml", false, true); + } else { + System.out.println("ZUGFeRD 1.0 XML source set to " + outName); + } - xmlName = getFilenameFromUser("ZUGFeRD 1.0 XML source", "ZUGFeRD-invoice.xml", "xml", true, false); - outName = getFilenameFromUser("ZUGFeRD 2.0 XML target", "factur-x.xml", "xml", false, true); + // Verify params + ensureFileExists(xmlName); + ensureFileNotExists(outName); + // All params are good! continue... ZUGFeRDMigrator zmi = new ZUGFeRDMigrator(); String xml = null; xml = zmi.migrateFromV1ToV2(xmlName); @@ -270,18 +342,30 @@ public class Toecount { } } - private static void performConvert() { + private static void performConvert(String pdfName, String outName) { /* * ZUGFeRDExporter ze= new ZUGFeRDExporterFromA1Factory() * .setProducer("toecount") .setCreator(System.getProperty("user.name")) * .loadFromPDFA1("invoice.pdf"); */ - String pdfName = ""; - String outName = ""; try { - pdfName = getFilenameFromUser("Source PDF", "invoice.pdf", "pdf", true, false); - outName = getFilenameFromUser("Target PDF", "invoice.a3.pdf", "pdf", false, true); + // Get params from user if not already defined + if (pdfName == null) { + pdfName = getFilenameFromUser("Source PDF", "invoice.pdf", "pdf", true, false); + } else { + System.out.println("Source PDF set to " + pdfName); + } + if (outName == null) { + outName = getFilenameFromUser("Target PDF", "invoice.a3.pdf", "pdf", false, true); + } else { + System.out.println("Target PDF set to " + outName); + } + // Verify params + ensureFileExists(pdfName); + ensureFileNotExists(outName); + + // All params are good! continue... ZUGFeRDExporter ze = new ZUGFeRDExporterFromA1Factory().setAttachZUGFeRDHeaders(false).load(pdfName); ze.export(outName); @@ -291,14 +375,27 @@ public class Toecount { System.out.println("Written to " + outName); } - private static void performExtract() { - - ZUGFeRDImporter zi = new ZUGFeRDImporter(); - String pdfName = getFilenameFromUser("Source PDF", "invoice.pdf", "pdf", true, false); - String xmlName = getFilenameFromUser("ZUGFeRD XML", "ZUGFeRD-invoice.xml", "xml", false, true); - - zi.extract(pdfName); + private static void performExtract(String pdfName, String xmlName) { try { + // Get params from user if not already defined + if (pdfName == null) { + pdfName = getFilenameFromUser("Source PDF", "invoice.pdf", "pdf", true, false); + } else { + System.out.println("Source PDF set to " + pdfName); + } + if (xmlName == null) { + xmlName = getFilenameFromUser("ZUGFeRD XML", "ZUGFeRD-invoice.xml", "xml", false, true); + } else { + System.out.println("ZUGFeRD XML set to " + pdfName); + } + + // Verify params + ensureFileExists(pdfName); + ensureFileNotExists(xmlName); + + // All params are good! continue... + ZUGFeRDImporter zi = new ZUGFeRDImporter(); + zi.extract(pdfName); byte[] XMLContent=zi.getRawXML(); if (XMLContent==null) { System.err.println("No ZUGFeRD XML found in PDF file"); @@ -312,90 +409,118 @@ public class Toecount { } } - private static void performCombine() { + private static void performCombine(String pdfName, String xmlName, String outName, String zfVersion, String zfProfile) { /* * ZUGFeRDExporter ze= new ZUGFeRDExporterFromA1Factory() * .setProducer("toecount") .setCreator(System.getProperty("user.name")) * .loadFromPDFA1("invoice.pdf"); */ - String pdfName = ""; - String xmlName = ""; - String outName = ""; - try { + int zfIntVersion = 1; + ZUGFeRDConformanceLevel zfConformanceLevelProfile = ZUGFeRDConformanceLevel.EXTENDED; - pdfName = getFilenameFromUser("Source PDF", "invoice.pdf", "pdf", true, false); - xmlName = getFilenameFromUser("ZUGFeRD XML", "ZUGFeRD-invoice.xml", "xml", true, false); - outName = getFilenameFromUser("Ouput PDF", "invoice.ZUGFeRD.pdf", "pdf", false, true); - String versionInput = ""; - try { - versionInput = getStringFromUser("ZUGFeRD version (1 or 2)", "1", "1|2"); - - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); + if (pdfName == null) { + pdfName = getFilenameFromUser("Source PDF", "invoice.pdf", "pdf", true, false); + } else { + System.out.println("Source PDF set to " + pdfName); } - String profileInput = ""; - int zfIntVersion=Integer.valueOf(versionInput); + if (xmlName == null) { + xmlName = getFilenameFromUser("ZUGFeRD XML", "ZUGFeRD-invoice.xml", "xml", true, false); + } else { + System.out.println("ZUGFeRD XML set to " + xmlName); + } + + if (outName == null) { + outName = getFilenameFromUser("Ouput PDF", "invoice.ZUGFeRD.pdf", "pdf", false, true); + } else { + System.out.println("Ouput PDF set to " + outName); + } + + if (zfVersion == null) { + try { + zfVersion = getStringFromUser("ZUGFeRD version (1 or 2)", "1", "1|2"); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } else { + System.out.println("ZUGFeRD version set to " + zfVersion); + } + + zfIntVersion=Integer.valueOf(zfVersion); + + if (zfProfile == null) { + try { + if (zfIntVersion == 1) { + zfProfile = getStringFromUser("ZUGFeRD profile b)asic, c)omfort or e)xtended", "e", "B|b|C|c|E|e"); + } else { + zfProfile = getStringFromUser("ZUGFeRD profile [M]INIMUM, BASIC [W]L, [B]ASIC,\n" + + "[C]IUS, [E]N16931, E[X]TENDED", "E", "M|m|W|w|B|b|C|c|E|e|X|x|"); + } + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } else { + System.out.println("ZUGFeRD profile set to " + zfProfile); + } + zfProfile = zfProfile.toLowerCase(); + + // Verify params + ensureFileExists(pdfName); + ensureFileExists(xmlName); + ensureFileNotExists(outName); - ZUGFeRDConformanceLevel profile=ZUGFeRDConformanceLevel.EXTENDED; if (zfIntVersion==1) { - try { - profileInput = getStringFromUser("ZUGFeRD profile b)asic, c)omfort or e)xtended", "e", "B|b|C|c|E|e").toLowerCase(); - - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); + if (zfProfile.equals("b")) { + zfConformanceLevelProfile = ZUGFeRDConformanceLevel.BASIC; + } else if (zfProfile.equals("c")) { + zfConformanceLevelProfile = ZUGFeRDConformanceLevel.COMFORT; + } else if (zfProfile.equals("e")) { + zfConformanceLevelProfile = ZUGFeRDConformanceLevel.EXTENDED; + } else { + throw new Exception(String.format("Unknown ZUGFeRD profile '%s'", zfProfile)); } - if (profileInput.equals("b")) { - profile=ZUGFeRDConformanceLevel.BASIC; - } else if (profileInput.equals("c")) { - profile=ZUGFeRDConformanceLevel.COMFORT; - } else if (profileInput.equals("e")) { - profile=ZUGFeRDConformanceLevel.EXTENDED; - - } - } else if (zfIntVersion==2) { - try { - profileInput = getStringFromUser("ZUGFeRD profile [M]INIMUM, BASIC [W]L, [B]ASIC,\n" + - "[C]IUS, [E]N16931, E[X]TENDED", "E", "M|m|W|w|B|b|C|c|E|e|X|x|").toLowerCase(); - - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); + if (zfProfile.equals("m")) { + zfConformanceLevelProfile = ZUGFeRDConformanceLevel.MINIMUM; + } else if (zfProfile.equals("w")) { + zfConformanceLevelProfile = ZUGFeRDConformanceLevel.BASICWL; + } else if (zfProfile.equals("b")) { + zfConformanceLevelProfile = ZUGFeRDConformanceLevel.BASIC; + } else if (zfProfile.equals("c")) { + zfConformanceLevelProfile = ZUGFeRDConformanceLevel.CIUS; + } else if (zfProfile.equals("e")) { + zfConformanceLevelProfile = ZUGFeRDConformanceLevel.EN16931; + } else if (zfProfile.equals("x")) { + zfConformanceLevelProfile = ZUGFeRDConformanceLevel.EXTENDED; + } else { + throw new Exception(String.format("Unknown ZUGFeRD profile '%s'", zfProfile)); } - if (profileInput.equals("m")) { - profile=ZUGFeRDConformanceLevel.MINIMUM; - } else if (profileInput.equals("w")) { - profile=ZUGFeRDConformanceLevel.BASICWL; - } else if (profileInput.equals("b")) { - profile=ZUGFeRDConformanceLevel.BASIC; - } else if (profileInput.equals("c")) { - profile=ZUGFeRDConformanceLevel.CIUS; - } else if (profileInput.equals("e")) { - profile=ZUGFeRDConformanceLevel.EN16931; - } else if (profileInput.equals("x")) { - profile=ZUGFeRDConformanceLevel.EXTENDED; - } - + } else { + throw new Exception(String.format("Unknown ZUGFeRD version '%i'", zfIntVersion)); } + // All params are good! continue... ZUGFeRDExporter ze = new ZUGFeRDExporterFromA1Factory().setProducer("Toecount") - .setCreator(System.getProperty("user.name")).setZUGFeRDConformanceLevel(profile).load(pdfName); + .setCreator(System.getProperty("user.name")).setZUGFeRDConformanceLevel(zfConformanceLevelProfile).load(pdfName); + ze.setZUGFeRDVersion(zfIntVersion); ze.setZUGFeRDXMLData(Files.readAllBytes(Paths.get(xmlName))); ze.export(outName); + + System.out.println("Written to " + outName); + } catch (IOException e) { e.printStackTrace(); // } catch (JAXBException e) { // e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); } - - System.out.println("Written to " + outName); } private static void performMetrics(String directoryName, Boolean filesFromStdIn, Boolean ignoreFileExt) { @@ -422,9 +547,7 @@ public class Toecount { // TODO Auto-generated catch block e.printStackTrace(); } - } - } if (filesFromStdIn) { @@ -442,10 +565,29 @@ public class Toecount { // TODO Auto-generated catch block e.printStackTrace(); } - } System.out.println(sr.getSummaryLine()); } + private static void ensureFileExists(String fileName) throws IOException + { + if (!fileExists(fileName)) { + throw new IOException(String.format("File %s does not exists", fileName)); + } + } + + private static void ensureFileNotExists(String fileName) throws IOException + { + if (fileExists(fileName)) { + throw new IOException(String.format("File %s does not exists", fileName)); + } + } + + private static Boolean fileExists(String fileName) + { + if (fileName == null) return false; + File f = new File(fileName); + return f.exists(); + } }