Skip to content

Commit

Permalink
indicator where additional data could be validated
Browse files Browse the repository at this point in the history
  • Loading branch information
Jochen Stärk authored and Jochen Stärk committed Jun 29, 2019
1 parent 6459156 commit 3d4433a
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 62 deletions.
136 changes: 74 additions & 62 deletions src/main/java/ZUV/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,20 @@ public class Main {
static final ClassLoader cl = Main.class.getClassLoader();
private Vector<ValidationResultItem> results;
protected ValidationContext context = new ValidationContext();
private long startTime;
private boolean optionsRecognized;
private String sha1Checksum;
private boolean pdfValidity;
private String Signature;
private boolean displayXMLValidationOutput;

private static final Logger LOGGER = LoggerFactory.getLogger(Main.class.getCanonicalName()); // log output is
// ignored for the
// time being

public void run(String[] args) {

long startTime = Calendar.getInstance().getTimeInMillis();
startTime = Calendar.getInstance().getTimeInMillis();
results = new Vector<ValidationResultItem>();
/***
* prerequisite is a mvn generate-resources
Expand All @@ -44,8 +50,8 @@ public void run(String[] args) {
Option<Boolean> licenseOption = parser.addBooleanOption('l', "license");
Option<Boolean> helpOption = parser.addBooleanOption('h', "help");

boolean optionsRecognized = false;
boolean displayXMLValidationOutput = false;
optionsRecognized = false;
displayXMLValidationOutput = false;

try {
parser.parse(args);
Expand All @@ -55,8 +61,7 @@ public void run(String[] args) {
}

Boolean helpRequested = parser.getOptionValue(helpOption);
boolean pdfValidity = true;
boolean xmlValidity = true;
pdfValidity = true;

if (parser.getOptionValue(licenseOption) != null) {
optionsRecognized = true;
Expand All @@ -83,85 +88,92 @@ public void run(String[] args) {
if (!logdir.exists() || !logdir.isDirectory() || !logdir.canWrite()) {
System.err.println("Need writable subdirectory 'log' for log files.");
}
String Signature = "no PDF";
String sha1Checksum = "";
Signature = "no PDF";
sha1Checksum = "";

if ((action != null) && (action.equals("validate"))) {

System.out.println("<validation>");
optionsRecognized = performValidation(
pdfFileName, xmlFileName);
}

PDFValidator pdfv = new PDFValidator(context);
if (pdfFileName != null) {
pdfv.setFilename(pdfFileName);
pdfv.validate();
if ((!optionsRecognized) || (helpRequested != null && helpRequested.booleanValue())) {
System.out.println(
"usage: --action validate -z <ZUGFeRD PDF Filename.pdf>|-x <ZUGFeRD XML Filename.xml> [-l (shows license)]");
System.exit(-1);
}

optionsRecognized = true;
File file = new File(pdfFileName);
if (!file.exists()) {
results.add(new ValidationResultItem(ESeverity.exception, "File not found").setSection(1));
LOGGER.error("Error 1: PDF file " + pdfFileName + " not found");
}

}
private boolean performValidation(String pdfFileName, String xmlFileName) {
boolean xmlValidity;
System.out.println("<validation>");

sha1Checksum = calcSHA1(file);
PDFValidator pdfv = new PDFValidator(context);
if (pdfFileName != null) {
pdfv.setFilename(pdfFileName);
pdfv.validate();

System.out.println("<pdf>");
// Validate PDF

System.out.println(pdfv.getXMLResult());
pdfValidity = context.isValid();
Signature=context.getSignature();
context.clear();
System.out.println("</pdf>\n");
optionsRecognized = true;
File file = new File(pdfFileName);
if (!file.exists()) {
results.add(new ValidationResultItem(ESeverity.exception, "File not found").setSection(1));
LOGGER.error("Error 1: PDF file " + pdfFileName + " not found");

}

XMLValidator xv = new XMLValidator(context);
sha1Checksum = calcSHA1(file);

if (xmlFileName != null) {
optionsRecognized = true;
xv.setFilename(xmlFileName);
File file = new File(xmlFileName);
System.out.println("<pdf>");
// Validate PDF

if (file.exists()) {
sha1Checksum = calcSHA1(file);
}
displayXMLValidationOutput=true;

} else {
if (pdfv.getRawXML()!=null) {
xv.setStringContent(pdfv.getRawXML());
displayXMLValidationOutput=true;
} else {
//no XML found. This could also be an error.
}
}
System.out.println(pdfv.getXMLResult());
pdfValidity = context.isValid();
Signature=context.getSignature();
context.clear();
System.out.println("</pdf>\n");

if ((optionsRecognized)&&(displayXMLValidationOutput)) {
System.out.println("<xml>");
xv.validate();
System.out.println(xv.getXMLResult());
System.out.println("</xml>");
}

}
XMLValidator xv = new XMLValidator(context);

if (xmlFileName != null) {
optionsRecognized = true;
xv.setFilename(xmlFileName);
File file = new File(xmlFileName);

if (file.exists()) {
sha1Checksum = calcSHA1(file);
}
displayXMLValidationOutput=true;

System.out.println("</validation>");
xmlValidity=context.isValid();
long duration=Calendar.getInstance().getTimeInMillis()-startTime;

LOGGER.info("Parsed PDF:"+(pdfValidity?"valid":"invalid")+" XML:"+(xmlValidity?"valid":"invalid")+" Signature:"+Signature+" Checksum:"+sha1Checksum+" Profile:"+context.getProfile()+" Version:"+context.getVersion()+ " Took:"+duration+"ms");
if ((!pdfValidity)||(!xmlValidity)) {
System.exit(-1);
} else {
if (pdfv.getRawXML()!=null) {
xv.setStringContent(pdfv.getRawXML());
displayXMLValidationOutput=true;
} else {
//no XML found. This could also be an error.
}
}

if ((!optionsRecognized) || (helpRequested != null && helpRequested.booleanValue())) {
System.out.println(
"usage: --action validate -z <ZUGFeRD PDF Filename.pdf>|-x <ZUGFeRD XML Filename.xml> [-l (shows license)]");
System.exit(-1);
if ((optionsRecognized)&&(displayXMLValidationOutput)) {
System.out.println("<xml>");
xv.validate();
System.out.println(xv.getXMLResult());
System.out.println("</xml>");

}


System.out.println("</validation>");
xmlValidity=context.isValid();
long duration=Calendar.getInstance().getTimeInMillis()-startTime;

LOGGER.info("Parsed PDF:"+(pdfValidity?"valid":"invalid")+" XML:"+(xmlValidity?"valid":"invalid")+" Signature:"+Signature+" Checksum:"+sha1Checksum+" Profile:"+context.getProfile()+" Version:"+context.getVersion()+ " Took:"+duration+"ms");
if ((!pdfValidity)||(!xmlValidity)) {
System.exit(-1);
}
return optionsRecognized;
}

public static void main(String[] args) {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/ZUV/PDFValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.Arrays;
import java.util.Calendar;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
Expand Down Expand Up @@ -293,6 +294,15 @@ public void validate() {
LOGGER.error(e.getMessage(), e);
}

// step 4:validate additional data
HashMap<String, byte[]> additionalData=zi.getAdditionalData();
for (String filename : additionalData.keySet()) {
// validating xml in byte[] additionalData.get(filename)
}


//end

long endTime = Calendar.getInstance().getTimeInMillis();

if (pdfReport.contains("isSuccess=\"false\"")) {
Expand Down

0 comments on commit 3d4433a

Please sign in to comment.