corrected reattempt on nonexistent files, added version selection
This commit is contained in:
@@ -1,17 +1,17 @@
|
|||||||
package org.mustangproject.toecount;
|
package org.mustangproject.toecount;
|
||||||
|
|
||||||
|
/***
|
||||||
|
* This is the command line interface to mustangproject
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
import javax.xml.bind.JAXBException;
|
|
||||||
import javax.xml.transform.TransformerException;
|
|
||||||
|
|
||||||
import org.mustangproject.ZUGFeRD.ZUGFeRDExporter;
|
import org.mustangproject.ZUGFeRD.ZUGFeRDExporter;
|
||||||
import org.mustangproject.ZUGFeRD.ZUGFeRDExporterFromA1Factory;
|
import org.mustangproject.ZUGFeRD.ZUGFeRDExporterFromA1Factory;
|
||||||
@@ -41,15 +41,58 @@ public class Toecount {
|
|||||||
+ "\t--listfromstdin=count ZUGFeRD files from a list of linefeed separated files on runtime.\r\n"
|
+ "\t--listfromstdin=count 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"
|
||||||
+ "\t--ignorefileextension=if PDF files are counted check *.* instead of *.pdf files"
|
+ "\t--ignorefileextension=if PDF files are counted check *.* instead of *.pdf files"
|
||||||
+ "Merge operations"
|
+ "Merge operations" + "\t--combine= combine XML and PDF file to ZUGFeRD PDF file\r\n"
|
||||||
+ "\t--combine= combine XML and PDF file to ZUGFeRD PDF file\r\n"
|
|
||||||
+ "\t--extract= extract ZUGFeRD PDF to XML 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--upgrade= upgrade ZUGFeRD XML to ZUGFeRD 2 XML\r\n"
|
||||||
+ "\t--a3only= upgrade from PDF/A1 to A3 only \r\n"
|
+ "\t--a3only= upgrade from PDF/A1 to A3 only (no ZUGFeRD data attached) \r\n"
|
||||||
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* Asks the user for a String (offering a defaultValue) conforming to a Regex
|
||||||
|
* pattern
|
||||||
|
*
|
||||||
|
* @param prompt
|
||||||
|
* @param defaultValue
|
||||||
|
* @param pattern
|
||||||
|
* @return
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
protected static String getStringFromUser(String prompt, String defaultValue, String pattern) throws Exception {
|
||||||
|
String input = "";
|
||||||
|
if (!defaultValue.matches(pattern)) {
|
||||||
|
throw new Exception("Default value must match pattern");
|
||||||
|
}
|
||||||
|
boolean firstInput = true;
|
||||||
|
do {
|
||||||
|
// for a more sophisticated dialogue maybe https://github.com/mabe02/lanterna/
|
||||||
|
// could be taken into account
|
||||||
|
System.out.print(prompt + " (default: " + defaultValue + ")");
|
||||||
|
if (!firstInput) {
|
||||||
|
System.out.print("\n(allowed pattern: " + pattern + ")");
|
||||||
|
|
||||||
|
}
|
||||||
|
System.out.print(":");
|
||||||
|
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
|
||||||
|
try {
|
||||||
|
input = buffer.readLine();
|
||||||
|
} catch (IOException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (input.isEmpty()) {
|
||||||
|
// pressed return without entering anything
|
||||||
|
input = defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
firstInput = false;
|
||||||
|
} while (!input.matches(pattern));
|
||||||
|
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
|
||||||
/***
|
/***
|
||||||
* Prompts the user for a input or output filename
|
* Prompts the user for a input or output filename
|
||||||
* @param prompt
|
* @param prompt
|
||||||
@@ -80,6 +123,7 @@ public class Toecount {
|
|||||||
selectedName = defaultFilename;
|
selectedName = defaultFilename;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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="";
|
||||||
@@ -94,20 +138,18 @@ public class Toecount {
|
|||||||
System.exit(-1);
|
System.exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
} else if (ensureFileExists) {
|
||||||
|
|
||||||
if (ensureFileExists) {
|
|
||||||
File f = new File(selectedName);
|
File f = new File(selectedName);
|
||||||
if (f.exists()) {
|
if (f.exists()) {
|
||||||
fileExistenceOK = true;
|
fileExistenceOK = true;
|
||||||
} else {
|
} else {
|
||||||
System.out.println("File does not exist, try again or CTRL+C to cancel");
|
System.out.println("File does not exist, try again or CTRL+C to cancel");
|
||||||
// discard the input, a scanner.reset is not sufficient
|
// discard the input, a scanner.reset is not sufficient
|
||||||
|
fileExistenceOK = false;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fileExistenceOK=true;
|
fileExistenceOK=true;
|
||||||
}
|
|
||||||
if (ensureFileNotExists) {
|
if (ensureFileNotExists) {
|
||||||
File f = new File(selectedName);
|
File f = new File(selectedName);
|
||||||
if (f.exists()) {
|
if (f.exists()) {
|
||||||
@@ -119,6 +161,7 @@ public class Toecount {
|
|||||||
} else {
|
} else {
|
||||||
fileExistenceOK=true;
|
fileExistenceOK=true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
} while (!fileExistenceOK);
|
} while (!fileExistenceOK);
|
||||||
@@ -228,9 +271,21 @@ public class Toecount {
|
|||||||
pdfName = getFilenameFromUser("Source PDF", "invoice.pdf", "pdf", true, false);
|
pdfName = getFilenameFromUser("Source PDF", "invoice.pdf", "pdf", true, false);
|
||||||
xmlName = getFilenameFromUser("ZUGFeRD XML", "ZUGFeRD-invoice.xml", "xml", true, false);
|
xmlName = getFilenameFromUser("ZUGFeRD XML", "ZUGFeRD-invoice.xml", "xml", true, false);
|
||||||
outName = getFilenameFromUser("Ouput PDF", "invoice.ZUGFeRD.pdf", "pdf", false, true);
|
outName = getFilenameFromUser("Ouput PDF", "invoice.ZUGFeRD.pdf", "pdf", false, true);
|
||||||
|
String versionInput = "";
|
||||||
|
try {
|
||||||
|
versionInput = getStringFromUser("ZUGFeRD version", "1", "1|2");
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
// get version
|
||||||
|
// get profile
|
||||||
|
|
||||||
ZUGFeRDExporter ze = new ZUGFeRDExporterFromA3Factory().setProducer("Toecount")
|
ZUGFeRDExporter ze = new ZUGFeRDExporterFromA3Factory().setProducer("Toecount")
|
||||||
.setCreator(System.getProperty("user.name")).load(pdfName);
|
.setCreator(System.getProperty("user.name")).load(pdfName);
|
||||||
|
ze.setZUGFeRDVersion(Integer.valueOf(versionInput));
|
||||||
|
|
||||||
ze.setZUGFeRDXMLData(Files.readAllBytes(Paths.get(xmlName)));
|
ze.setZUGFeRDXMLData(Files.readAllBytes(Paths.get(xmlName)));
|
||||||
|
|
||||||
ze.export(outName);
|
ze.export(outName);
|
||||||
@@ -265,8 +320,7 @@ public class Toecount {
|
|||||||
pdfName = getFilenameFromUser("Source PDF", "invoice.pdf", "pdf", true, false);
|
pdfName = getFilenameFromUser("Source PDF", "invoice.pdf", "pdf", true, false);
|
||||||
outName = getFilenameFromUser("Target PDF", "invoice.a3.pdf", "pdf", false, true);
|
outName = getFilenameFromUser("Target PDF", "invoice.a3.pdf", "pdf", false, true);
|
||||||
|
|
||||||
ZUGFeRDExporter ze = new ZUGFeRDExporterFromA1Factory().setAttachZUGFeRDHeaders(false)
|
ZUGFeRDExporter ze = new ZUGFeRDExporterFromA1Factory().setAttachZUGFeRDHeaders(false).load(pdfName);
|
||||||
.load(pdfName);
|
|
||||||
|
|
||||||
ze.export(outName);
|
ze.export(outName);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user