This commit is contained in:
Jochen Stärk
2018-11-11 20:57:46 +01:00
parent ddadbfee94
commit 970436cf41
2 changed files with 276 additions and 252 deletions

View File

@@ -1,3 +1,8 @@
1.5.6
=====
closes #69
1.5.5 1.5.5
===== =====
2018-10-22 2018-10-22

View File

@@ -51,27 +51,47 @@ public class Toecount {
} }
private static String getUsage() { private static String getUsage() {
return "Usage: [-d,--directory] [-l,--listfromstdin] [-i,--ignorefileextension] | [-c,--combine] | [-e,--extract] | [-u,--upgrade] | [-a,--a3only] | [-h,--help]\r\n"; return "Usage: [-d,--directory] [-l,--listfromstdin] [-i,--ignorefileextension] | [-c,--combine] | [-e,--extract] | [-u,--upgrade] | [-a,--a3only] | [-h,--help] \r\n"
+ "* Count operations\n" + " -d, --directory count ZUGFeRD files in directory to be scanned\n"
+ " If it is a directory, it will recurse.\n"
+ " -l, --listfromstdin count ZUGFeRD files from a list of linefeed separated files on runtime.\n"
+ " It will start once a blank line has been entered.\n" + "\n"
+ " Additional parameter for both count operations\n"
+ " [-i, --ignorefileextension] Check for all files (*.*) instead of PDF files only (*.pdf)\n"
+ "\n" + "* Merge operations\n" + " -e, --extract extract ZUGFeRD PDF to XML file\n"
+ " Additional parameters (optional - user will be prompted if not defined)\n"
+ " [--source <filename>]: set input PDF file\n"
+ " [--out <filename>]: set output XML file\n"
+ " -u, --upgrade upgrade ZUGFeRD XML to ZUGFeRD 2 XML\n"
+ " Additional parameters (optional - user will be prompted if not defined)\n"
+ " [--source <filename>]: set input XML ZUGFeRD 1 file\n"
+ " [--out <filename>]: set output XML ZUGFeRD 2 file\n"
+ " -a, --a3only upgrade from PDF/A1 to A3 only (no ZUGFeRD data attached)\n"
+ " Additional parameters (optional - user will be prompted if not defined)\n"
+ " [--source <filename>]: set input PDF file\n"
+ " [--out <filename>]: set output PDF file\n"
+ " -c, --combine combine XML and PDF file to ZUGFeRD PDF file\n"
+ " Additional parameters (optional - user will be prompted if not defined)\n"
+ " [--source <filename>]: set input PDF file\n"
+ " [--source-xml <filename>]: set input XML file\n"
+ " [--out <filename>]: set output PDF file\n"
+ " [--zugferd-version <1|2>]: set ZUGFeRD version\n"
+ " [--zugferd-profile <...>]: set ZUGFeRD profile\n"
+ " For ZUGFeRD v1: <B>ASIC, <C>OMFORT or <E>XTENDED\n"
+ " For ZUGFeRD v2: <M>INIMUM, BASIC <W>L, <B>ASIC, <C>IUS, <E>N16931, E<X>TENDED ";
} }
private static void printHelp() { private static void printHelp() {
System.out.println("Mustangproject.org " + org.mustangproject.ZUGFeRD.Version.VERSION + " \r\n" 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" + "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" + "ZUGFeRD Metadata (http://www.zugferd.org)\r\n" + "\r\n" + getUsage() + "\r\n"
+ "\r\n" + "* Count operations\r\n" + "\t-d, --directory\tcount ZUGFeRD files in directory to be scanned\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\tIf it is a directory, it will recurse.\r\n"
+ "\t-l, --listfromstdin\tcount 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\tIt will start once a blank line has been entered.\r\n" + "\r\n"
+ "\r\n"
+ "\tAdditional parameter for both count operations\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" + "\t[-i, --ignorefileextension]\tCheck for all files (*.*) instead of PDF files only (*.pdf)\r\n"
+ "\r\n" + "\r\n" + "* Merge operations\r\n" + "\t-e, --extract\textract ZUGFeRD PDF to XML file\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\tAdditional parameters (optional - user will be prompted if not defined)\r\n"
+ "\t\t[--source <filename>]: set input PDF file\r\n" + "\t\t[--source <filename>]: set input PDF file\r\n"
+ "\t\t[--out <filename>]: set output XML file\r\n" + "\t\t[--out <filename>]: set output XML file\r\n"
@@ -91,19 +111,22 @@ public class Toecount {
+ "\t\t[--zugferd-version <1|2>]: set ZUGFeRD version\r\n" + "\t\t[--zugferd-version <1|2>]: set ZUGFeRD version\r\n"
+ "\t\t[--zugferd-profile <...>]: set ZUGFeRD profile\r\n" + "\t\t[--zugferd-profile <...>]: set ZUGFeRD profile\r\n"
+ "\t\t\tFor ZUGFeRD v1: <B>ASIC, <C>OMFORT or <E>XTENDED\r\n" + "\t\t\tFor ZUGFeRD v1: <B>ASIC, <C>OMFORT or <E>XTENDED\r\n"
+ "\t\t\tFor ZUGFeRD v2: <M>INIMUM, BASIC <W>L, <B>ASIC, <C>IUS, <E>N16931, E<X>TENDED\r\n" + "\t\t\tFor ZUGFeRD v2: <M>INIMUM, BASIC <W>L, <B>ASIC, <C>IUS, <E>N16931, E<X>TENDED\r\n");
);
} }
/*** /***
* Asks the user (repeatedly, if neccessary) on the command line for a String (offering a defaultValue) conforming to a Regex * Asks the user (repeatedly, if neccessary) on the command line for a String
* pattern * (offering a defaultValue) conforming to a Regex pattern
* *
* @param prompt the question to be asked to the user * @param prompt
* @param defaultValue the default return value if user hits enter * the question to be asked to the user
* @param pattern a regex of acceptable values * @param defaultValue
* the default return value if user hits enter
* @param pattern
* a regex of acceptable values
* @return the user answer conforming to pattern * @return the user answer conforming to pattern
* @throws Exception if pattern not compielable or IOexception on input * @throws Exception
* if pattern not compielable or IOexception on input
*/ */
protected static String getStringFromUser(String prompt, String defaultValue, String pattern) throws Exception { protected static String getStringFromUser(String prompt, String defaultValue, String pattern) throws Exception {
String input = ""; String input = "";
@@ -141,19 +164,27 @@ public class Toecount {
/*** /***
* Prompts the user for a input or output filename * Prompts the user for a input or output filename
* @param the text the user is asked *
* @param a default Filename * @param the
* @param expectedExtension will warn if filename does not match expected file extension * text the user is asked
* @param ensureFileExists will warn if file does NOT exist (for input files) * @param a
* @param ensureFileNotExists will warn if file DOES exist (for output files) * default Filename
* @param expectedExtension
* will warn if filename does not match expected file extension
* @param ensureFileExists
* will warn if file does NOT exist (for input files)
* @param ensureFileNotExists
* will warn if file DOES exist (for output files)
* *
* @return String * @return String
*/ */
protected static String getFilenameFromUser(String prompt, String defaultFilename, String expectedExtension, boolean ensureFileExists, boolean ensureFileNotExists) { protected static String getFilenameFromUser(String prompt, String defaultFilename, String expectedExtension,
boolean ensureFileExists, boolean ensureFileNotExists) {
boolean fileExistenceOK = false; boolean fileExistenceOK = false;
String selectedName = ""; String selectedName = "";
do { do {
// for a more sophisticated dialogue maybe https://github.com/mabe02/lanterna/ could be taken into account // 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 + " (default: " + defaultFilename + "):");
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in)); BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
try { try {
@@ -163,7 +194,6 @@ public class Toecount {
e.printStackTrace(); e.printStackTrace();
} }
if (selectedName.isEmpty()) { if (selectedName.isEmpty()) {
// pressed return without entering anything // pressed return without entering anything
selectedName = defaultFilename; selectedName = defaultFilename;
@@ -171,7 +201,8 @@ public class Toecount {
// error cases // error cases
if (!selectedName.toLowerCase().endsWith(expectedExtension.toLowerCase())) { if (!selectedName.toLowerCase().endsWith(expectedExtension.toLowerCase())) {
System.err.println("Expected "+expectedExtension+" extension, this may corrupt your file. Do you still want to continue?(Y|N)"); System.err.println("Expected " + expectedExtension
+ " extension, this may corrupt your file. Do you still want to continue?(Y|N)");
String selectedAnswer = ""; String selectedAnswer = "";
try { try {
selectedAnswer = buffer.readLine(); selectedAnswer = buffer.readLine();
@@ -206,7 +237,6 @@ public class Toecount {
} }
} }
} while (!fileExistenceOK); } while (!fileExistenceOK);
return selectedName; return selectedName;
@@ -214,6 +244,8 @@ public class Toecount {
// /opt/local/bin/mvn clean compile assembly:single // /opt/local/bin/mvn clean compile assembly:single
public static void main(String[] args) { public static void main(String[] args) {
try {
CmdLineParser parser = new CmdLineParser(); CmdLineParser parser = new CmdLineParser();
// Option: Help // Option: Help
@@ -307,10 +339,16 @@ public class Toecount {
printUsage(); printUsage();
System.exit(2); System.exit(2);
} }
} catch (Exception e) {
e.printStackTrace();
Logger.getLogger(Toecount.class.getName()).log(Level.SEVERE, null, e);
System.exit(-1);
} }
private static void performUpgrade(String xmlName, String outName) { }
try {
private static void performUpgrade(String xmlName, String outName) throws IOException, TransformerException {
// Get params from user if not already defined // Get params from user if not already defined
if (xmlName == null) { if (xmlName == null) {
xmlName = getFilenameFromUser("ZUGFeRD 1.0 XML source", "ZUGFeRD-invoice.xml", "xml", true, false); xmlName = getFilenameFromUser("ZUGFeRD 1.0 XML source", "ZUGFeRD-invoice.xml", "xml", true, false);
@@ -333,22 +371,18 @@ public class Toecount {
xml = zmi.migrateFromV1ToV2(xmlName); xml = zmi.migrateFromV1ToV2(xmlName);
Files.write(Paths.get(outName), xml.getBytes()); Files.write(Paths.get(outName), xml.getBytes());
System.out.println("Written to " + outName); System.out.println("Written to " + outName);
} catch (FileNotFoundException ex) { /*
Logger.getLogger(Toecount.class.getName()).log(Level.SEVERE, null, ex); * } catch (FileNotFoundException ex) {
} catch (TransformerException | UnsupportedEncodingException ex) { * Logger.getLogger(Toecount.class.getName()).log(Level.SEVERE, null, 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(String pdfName, String outName) { private static void performConvert(String pdfName, String outName) throws IOException {
/* /*
* ZUGFeRDExporter ze= new ZUGFeRDExporterFromA1Factory() * ZUGFeRDExporter ze= new ZUGFeRDExporterFromA1Factory()
* .setProducer("toecount") .setCreator(System.getProperty("user.name")) * .setProducer("toecount") .setCreator(System.getProperty("user.name"))
* .loadFromPDFA1("invoice.pdf"); * .loadFromPDFA1("invoice.pdf");
*/ */
try {
// Get params from user if not already defined // Get params from user if not already defined
if (pdfName == null) { if (pdfName == null) {
pdfName = getFilenameFromUser("Source PDF", "invoice.pdf", "pdf", true, false); pdfName = getFilenameFromUser("Source PDF", "invoice.pdf", "pdf", true, false);
@@ -369,14 +403,10 @@ public class Toecount {
ZUGFeRDExporter ze = new ZUGFeRDExporterFromA1Factory().setAttachZUGFeRDHeaders(false).load(pdfName); ZUGFeRDExporter ze = new ZUGFeRDExporterFromA1Factory().setAttachZUGFeRDHeaders(false).load(pdfName);
ze.export(outName); ze.export(outName);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Written to " + outName); System.out.println("Written to " + outName);
} }
private static void performExtract(String pdfName, String xmlName) { private static void performExtract(String pdfName, String xmlName) throws IOException {
try {
// Get params from user if not already defined // Get params from user if not already defined
if (pdfName == null) { if (pdfName == null) {
pdfName = getFilenameFromUser("Source PDF", "invoice.pdf", "pdf", true, false); pdfName = getFilenameFromUser("Source PDF", "invoice.pdf", "pdf", true, false);
@@ -404,12 +434,10 @@ public class Toecount {
Files.write(Paths.get(xmlName), XMLContent); Files.write(Paths.get(xmlName), XMLContent);
System.out.println("Written to " + xmlName); System.out.println("Written to " + xmlName);
} }
} catch (IOException e) {
e.printStackTrace();
}
} }
private static void performCombine(String pdfName, String xmlName, String outName, String zfVersion, String zfProfile) { private static void performCombine(String pdfName, String xmlName, String outName, String zfVersion,
String zfProfile) throws Exception {
/* /*
* ZUGFeRDExporter ze= new ZUGFeRDExporterFromA1Factory() * ZUGFeRDExporter ze= new ZUGFeRDExporterFromA1Factory()
* .setProducer("toecount") .setCreator(System.getProperty("user.name")) * .setProducer("toecount") .setCreator(System.getProperty("user.name"))
@@ -453,10 +481,12 @@ public class Toecount {
if (zfProfile == null) { if (zfProfile == null) {
try { try {
if (zfIntVersion == 1) { if (zfIntVersion == 1) {
zfProfile = getStringFromUser("ZUGFeRD profile b)asic, c)omfort or e)xtended", "e", "B|b|C|c|E|e"); zfProfile = getStringFromUser("ZUGFeRD profile b)asic, c)omfort or e)xtended", "e",
"B|b|C|c|E|e");
} else { } else {
zfProfile = getStringFromUser("ZUGFeRD profile [M]INIMUM, BASIC [W]L, [B]ASIC,\n" + zfProfile = getStringFromUser(
"[C]IUS, [E]N16931, E[X]TENDED", "E", "M|m|W|w|B|b|C|c|E|e|X|x|"); "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) { } catch (Exception e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
@@ -504,7 +534,8 @@ public class Toecount {
// All params are good! continue... // All params are good! continue...
ZUGFeRDExporter ze = new ZUGFeRDExporterFromA1Factory().setProducer("Toecount") ZUGFeRDExporter ze = new ZUGFeRDExporterFromA1Factory().setProducer("Toecount")
.setCreator(System.getProperty("user.name")).setZUGFeRDConformanceLevel(zfConformanceLevelProfile).load(pdfName); .setCreator(System.getProperty("user.name")).setZUGFeRDConformanceLevel(zfConformanceLevelProfile)
.load(pdfName);
ze.setZUGFeRDVersion(zfIntVersion); ze.setZUGFeRDVersion(zfIntVersion);
@@ -518,12 +549,11 @@ public class Toecount {
e.printStackTrace(); e.printStackTrace();
// } catch (JAXBException e) { // } catch (JAXBException e) {
// e.printStackTrace(); // e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} }
} }
private static void performMetrics(String directoryName, Boolean filesFromStdIn, Boolean ignoreFileExt) { private static void performMetrics(String directoryName, Boolean filesFromStdIn, Boolean ignoreFileExt)
throws IOException {
StatRun sr = new StatRun(); StatRun sr = new StatRun();
if (ignoreFileExt) { if (ignoreFileExt) {
@@ -541,19 +571,13 @@ public class Toecount {
} else if (Files.isDirectory(startingDir)) { } else if (Files.isDirectory(startingDir)) {
FileTraverser pf = new FileTraverser(sr); FileTraverser pf = new FileTraverser(sr);
try {
Files.walkFileTree(startingDir, pf); Files.walkFileTree(startingDir, pf);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
} }
if (filesFromStdIn) { if (filesFromStdIn) {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s; String s;
try {
while ((s = in.readLine()) != null && s.length() != 0) { while ((s = in.readLine()) != null && s.length() != 0) {
FileChecker fc = new FileChecker(s, sr); FileChecker fc = new FileChecker(s, sr);
@@ -561,31 +585,26 @@ public class Toecount {
System.out.print(fc.getOutputLine()); System.out.print(fc.getOutputLine());
} }
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
System.out.println(sr.getSummaryLine()); System.out.println(sr.getSummaryLine());
} }
private static void ensureFileExists(String fileName) throws IOException private static void ensureFileExists(String fileName) throws IOException {
{
if (!fileExists(fileName)) { if (!fileExists(fileName)) {
throw new IOException(String.format("File %s does not exists", fileName)); throw new IOException(String.format("File %s does not exists", fileName));
} }
} }
private static void ensureFileNotExists(String fileName) throws IOException private static void ensureFileNotExists(String fileName) throws IOException {
{
if (fileExists(fileName)) { if (fileExists(fileName)) {
throw new IOException(String.format("File %s does not exists", fileName)); throw new IOException(String.format("File %s does not exists", fileName));
} }
} }
private static Boolean fileExists(String fileName) private static Boolean fileExists(String fileName) {
{ if (fileName == null)
if (fileName == null) return false; return false;
File f = new File(fileName); File f = new File(fileName);
return f.exists(); return f.exists();
} }