removed pdfa3-sample submodule, made mustang main (and only) module, corrected the POM, released 1.4.0 via maven, updated the developer documentation, updated toecount to 0.0.9 to use mustang 1.4.0 via maven repo
This commit is contained in:
73
toecount/src/main/java/toecount/FileChecker.java
Executable file
73
toecount/src/main/java/toecount/FileChecker.java
Executable file
@@ -0,0 +1,73 @@
|
||||
package toecount;
|
||||
|
||||
import org.mustangproject.ZUGFeRD.ZUGFeRDImporter;
|
||||
|
||||
public class FileChecker {
|
||||
String filename;
|
||||
StatRun thisRun;
|
||||
boolean isPDF=false;
|
||||
|
||||
public FileChecker(String filename, StatRun statistics) {
|
||||
this.filename=filename;
|
||||
thisRun=statistics;
|
||||
thisRun.incFileCount();
|
||||
String extension = "";
|
||||
if (!thisRun.shallIgnoreFileExt()) {
|
||||
int extIndex = filename.lastIndexOf(".");
|
||||
if (extIndex >= 0) {
|
||||
extension = filename.substring(extIndex).toLowerCase();
|
||||
isPDF=extension.equals(".pdf");// alternative check for PDF: File starts with %PDF-
|
||||
thisRun.incPDFCount();
|
||||
}
|
||||
|
||||
} else {
|
||||
thisRun.incPDFCount();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean checkForZUGFeRD() {
|
||||
if ((!isPDF)&&(!thisRun.shallIgnoreFileExt())) {
|
||||
return false;
|
||||
}
|
||||
ZUGFeRDImporter zi=new ZUGFeRDImporter();
|
||||
try {
|
||||
zi.extract(filename);
|
||||
if (zi.canParse()) {
|
||||
thisRun.incZUGFeRDCount();
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} catch (NullPointerException e) {
|
||||
// something really rare happened -- corrupted ZF?
|
||||
/***
|
||||
* e.g. Exception in thread "main" java.lang.NullPointerException
|
||||
at org.mustangproject.ZUGFeRD.ZUGFeRDImporter.extractLowLevel(ZUGFeRDImporter.java:90)
|
||||
at org.mustangproject.ZUGFeRD.ZUGFeRDImporter.extract(ZUGFeRDImporter.java:64)
|
||||
at toecount.FileChecker.checkForZUGFeRD(FileChecker.java:33)
|
||||
at toecount.Toecount.main(Toecount.java:111)
|
||||
*
|
||||
*/
|
||||
// Ignore nevertheless, most likely we're batch processing
|
||||
return false;
|
||||
} catch (Exception e2) {
|
||||
/**
|
||||
* probably thrown up from
|
||||
AM org.apache.pdfbox.pdfparser.PDFParser parse, most likely
|
||||
INFORMATION: Document is encrypted
|
||||
but also other internal PDF errors possible like
|
||||
..Okt 23, 2015 11:17:53 AM org.apache.pdfbox.pdfparser.XrefTrailerResolver setStartxref
|
||||
*/
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isPDF() {
|
||||
return isPDF;
|
||||
}
|
||||
|
||||
public String getOutputLine() {
|
||||
return thisRun.getOutputLine();
|
||||
}
|
||||
|
||||
}
|
||||
56
toecount/src/main/java/toecount/FileTraverser.java
Executable file
56
toecount/src/main/java/toecount/FileTraverser.java
Executable file
@@ -0,0 +1,56 @@
|
||||
package toecount;
|
||||
|
||||
import static java.nio.file.FileVisitResult.CONTINUE;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.FileVisitResult;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.SimpleFileVisitor;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
|
||||
public class FileTraverser extends SimpleFileVisitor<Path> {
|
||||
|
||||
|
||||
private StatRun thisRun;
|
||||
public FileTraverser(StatRun statistics) {
|
||||
this.thisRun=statistics;
|
||||
}
|
||||
|
||||
/***
|
||||
* check each file
|
||||
*/
|
||||
@Override
|
||||
public FileVisitResult visitFile(Path file, BasicFileAttributes attr) {
|
||||
if (attr.isSymbolicLink()) {
|
||||
// Not yet handled
|
||||
} else if (attr.isRegularFile()) {
|
||||
String filename = file.toString();
|
||||
FileChecker fc = new FileChecker(filename, thisRun);
|
||||
fc.checkForZUGFeRD();
|
||||
System.out.print(fc.getOutputLine());
|
||||
|
||||
}
|
||||
return CONTINUE;
|
||||
}
|
||||
|
||||
/***
|
||||
* for each directory
|
||||
*/
|
||||
@Override
|
||||
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
|
||||
// System.out.format("Directory: %s%n", dir);
|
||||
thisRun.incDirCount();
|
||||
return CONTINUE;
|
||||
}
|
||||
|
||||
|
||||
/***
|
||||
* show errors like file permission stacktraces
|
||||
*/
|
||||
@Override
|
||||
public FileVisitResult visitFileFailed(Path file, IOException exc) {
|
||||
System.err.println(exc);
|
||||
return CONTINUE;
|
||||
}
|
||||
|
||||
}
|
||||
69
toecount/src/main/java/toecount/StatRun.java
Executable file
69
toecount/src/main/java/toecount/StatRun.java
Executable file
@@ -0,0 +1,69 @@
|
||||
package toecount;
|
||||
|
||||
public class StatRun {
|
||||
private int pdfCount = 0;
|
||||
private int horseCount = 0;
|
||||
private int fileCount = 0;
|
||||
private int dirCount = 0;
|
||||
private boolean checkFileExt=true;
|
||||
|
||||
public void ignoreFileExtension() {
|
||||
checkFileExt=false;
|
||||
}
|
||||
|
||||
public boolean shallIgnoreFileExt() {
|
||||
return !checkFileExt;
|
||||
}
|
||||
public void incFileCount(){
|
||||
fileCount++;
|
||||
}
|
||||
|
||||
public void incPDFCount(){
|
||||
pdfCount++;
|
||||
}
|
||||
|
||||
public void incZUGFeRDCount(){
|
||||
horseCount++;
|
||||
}
|
||||
|
||||
public void incDirCount(){
|
||||
dirCount++;
|
||||
}
|
||||
|
||||
public int getFileCount(){
|
||||
return fileCount;
|
||||
}
|
||||
|
||||
public int getPDFCount(){
|
||||
return pdfCount;
|
||||
}
|
||||
|
||||
public int getZUGFeRDCount(){
|
||||
return horseCount;
|
||||
}
|
||||
|
||||
public int getDirCount(){
|
||||
return dirCount;
|
||||
}
|
||||
|
||||
/***
|
||||
* returns final statistics
|
||||
* @return
|
||||
*/
|
||||
public String getSummaryLine() {
|
||||
|
||||
return "\r\n===================================================================\r\n"+String.format(
|
||||
"Files:\t%d\tDirs:\t%d\tPDF:\t%d\tZUGFeRD:\t%d\r\n",
|
||||
getFileCount(), getDirCount(), getPDFCount(), getZUGFeRDCount());
|
||||
|
||||
}
|
||||
/***
|
||||
* show that something is happening
|
||||
* @return String
|
||||
*/
|
||||
public String getOutputLine() {
|
||||
return ".";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
123
toecount/src/main/java/toecount/Toecount.java
Executable file
123
toecount/src/main/java/toecount/Toecount.java
Executable file
@@ -0,0 +1,123 @@
|
||||
package toecount;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import com.sanityinc.jargs.CmdLineParser;
|
||||
import com.sanityinc.jargs.CmdLineParser.Option;
|
||||
|
||||
public class Toecount {
|
||||
//build with: /opt/local/bin/mvn clean compile assembly:single
|
||||
private static void printUsage() {
|
||||
System.err
|
||||
.println(getUsage());
|
||||
}
|
||||
|
||||
private static String getUsage() {
|
||||
return "Usage: Toecount [-d,--directory] [-l,--listfromstdin] [-i,--ignorefileextension] [-h,--help]\r\n";
|
||||
}
|
||||
private static void printHelp() {
|
||||
System.out
|
||||
.println("Mustangproject.org's Toecount 0.0.7 \r\n"
|
||||
+ "A Apache Public License command line tool for statistics on PDF invoices with\r\n"
|
||||
+ "ZUGFeRD Metadata (http://www.zugferd.de)\r\n"
|
||||
+ "\r\n"
|
||||
+ getUsage()
|
||||
+ "\t--directory= can specify a single PDF or a directory to be scanned\r\n"
|
||||
+ "\t\tIf it is a directory, it will recurse.\r\n"
|
||||
+ "\t--listfromstdin allows to specify 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 check *.* instead of *.pdf files");
|
||||
}
|
||||
|
||||
|
||||
// /opt/local/bin/mvn clean compile assembly:single
|
||||
public static void main(String[] args) {
|
||||
CmdLineParser parser = new CmdLineParser();
|
||||
Option<String> dirnameOption = parser.addStringOption('d', "directory");
|
||||
Option<Boolean> filesFromStdInOption = parser.addBooleanOption('l',
|
||||
"listfromstdin");
|
||||
Option<Boolean> ignoreFileExtOption = parser.addBooleanOption('i',
|
||||
"ignorefileextension");
|
||||
Option<Boolean> helpOption = parser.addBooleanOption('h',
|
||||
"help");
|
||||
|
||||
if (args.length==0) {
|
||||
printUsage();
|
||||
System.exit(2);
|
||||
|
||||
}
|
||||
try {
|
||||
parser.parse(args);
|
||||
} catch (CmdLineParser.OptionException e) {
|
||||
System.err.println(e.getMessage());
|
||||
printUsage();
|
||||
System.exit(2);
|
||||
}
|
||||
|
||||
String directoryName = parser.getOptionValue(dirnameOption);
|
||||
|
||||
Boolean filesFromStdIn = parser.getOptionValue(filesFromStdInOption,
|
||||
Boolean.FALSE);
|
||||
|
||||
Boolean helpRequested = parser.getOptionValue(helpOption,
|
||||
Boolean.FALSE);
|
||||
|
||||
Boolean ignoreFileExt = parser.getOptionValue(ignoreFileExtOption,
|
||||
Boolean.FALSE);
|
||||
|
||||
if (helpRequested) {
|
||||
printHelp();
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user