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:
Jochen Staerk
2017-05-18 08:41:47 +02:00
parent a60cb07c9b
commit 52f997f271
103 changed files with 395 additions and 1102 deletions

79
toecount/pom.xml Executable file
View File

@@ -0,0 +1,79 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.mustangproject.ZUGFeRD</groupId>
<artifactId>toecount</artifactId>
<version>0.0.9-SNAPSHOT</version>
<name>ZUGFeRD PDF checker</name>
<description>Shows statistics on how many PDFs in a directory are ZUGFeRD-Compliant</description>
<repositories>
<repository>
<id>mustang-mvn-repo</id>
<url>https://raw.github.com/ZUGFeRD/mustangproject/mvn-repo/</url>
</repository>
<repository>
<id>sonatype-oss-public</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.mustangproject.ZUGFeRD</groupId>
<artifactId>mustang</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>com.sanityinc</groupId>
<artifactId>jargs</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>toecount.Toecount</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>toecount.Toecount</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven mvn clean compile assembly:single -->
<!-- or whatever version you use -->
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View 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();
}
}

View 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;
}
}

View 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 ".";
}
}

View 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());
}
}