whitespace corrections+switch to ioutils
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
- Fix #389: ClassCastException: ZUGFeRDExporterFromA3
|
- Fix #389: ClassCastException: ZUGFeRDExporterFromA3
|
||||||
- jakarta support #372
|
- jakarta support #372
|
||||||
- Upgrade to PDFBox 3 #373
|
- Upgrade to PDFBox 3 #373
|
||||||
|
- Requires Java 11
|
||||||
- #397
|
- #397
|
||||||
- #392 CLI: action combine: --ignorefileextension to ignore PDF/A input file errors dosen't work
|
- #392 CLI: action combine: --ignorefileextension to ignore PDF/A input file errors dosen't work
|
||||||
- for CLI combine, fx is now default
|
- for CLI combine, fx is now default
|
||||||
|
|||||||
@@ -14,10 +14,7 @@ package org.mustangproject.ZUGFeRD;
|
|||||||
* @author jstaerk
|
* @author jstaerk
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
import java.io.*;
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
@@ -43,8 +40,8 @@ import javax.xml.xpath.XPathExpression;
|
|||||||
import javax.xml.xpath.XPathExpressionException;
|
import javax.xml.xpath.XPathExpressionException;
|
||||||
import javax.xml.xpath.XPathFactory;
|
import javax.xml.xpath.XPathFactory;
|
||||||
|
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.apache.pdfbox.Loader;
|
import org.apache.pdfbox.Loader;
|
||||||
import org.apache.pdfbox.io.IOUtils;
|
|
||||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
import org.apache.pdfbox.pdmodel.PDDocumentNameDictionary;
|
import org.apache.pdfbox.pdmodel.PDDocumentNameDictionary;
|
||||||
import org.apache.pdfbox.pdmodel.PDEmbeddedFilesNameTreeNode;
|
import org.apache.pdfbox.pdmodel.PDEmbeddedFilesNameTreeNode;
|
||||||
@@ -786,11 +783,11 @@ public class ZUGFeRDImporter {
|
|||||||
|
|
||||||
|
|
||||||
static String convertStreamToString(java.io.InputStream is) {
|
static String convertStreamToString(java.io.InputStream is) {
|
||||||
// TODO wouldn't we use IOUtils.toByteArray nowadays???
|
try {
|
||||||
// source https://stackoverflow.com/questions/309424/how-do-i-read-convert-an-inputstream-into-a-string-in-java referring to
|
return IOUtils.toString(is, StandardCharsets.UTF_8);
|
||||||
// https://community.oracle.com/blogs/pat/2004/10/23/stupid-scanner-tricks
|
} catch (IOException e) {
|
||||||
final Scanner s = new Scanner(is, StandardCharsets.UTF_8).useDelimiter("\\A");
|
throw new UncheckedIOException(e);
|
||||||
return s.hasNext() ? s.next() : "";
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -76,131 +76,131 @@ public class ZUGFeRDValidator {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String internalValidate (String contextFilename, InputStream inputStream, long inputLength) {
|
private String internalValidate(String contextFilename, InputStream inputStream, long inputLength) {
|
||||||
context.clear();
|
context.clear();
|
||||||
StringBuilder finalStringResult = new StringBuilder();
|
StringBuilder finalStringResult = new StringBuilder();
|
||||||
SimpleDateFormat isoDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
SimpleDateFormat isoDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
Date date = new Date();
|
Date date = new Date();
|
||||||
startTime = Calendar.getInstance().getTimeInMillis();
|
startTime = Calendar.getInstance().getTimeInMillis();
|
||||||
context.setFilename(contextFilename);// fallback to provided name
|
context.setFilename(contextFilename);// fallback to provided name
|
||||||
finalStringResult.append("<validation filename='").append (contextFilename).append ("' datetime='").append (isoDF.format(date)).append ("'>");
|
finalStringResult.append("<validation filename='").append(contextFilename).append("' datetime='").append(isoDF.format(date)).append("'>");
|
||||||
|
|
||||||
boolean isPDF = false;
|
boolean isPDF = false;
|
||||||
byte[] content = null;
|
byte[] content = null;
|
||||||
try {
|
try {
|
||||||
|
|
||||||
if (contextFilename == null || contextFilename.isEmpty ()) {
|
if (contextFilename == null || contextFilename.isEmpty()) {
|
||||||
optionsRecognized = false;
|
optionsRecognized = false;
|
||||||
context.addResultItem(new ValidationResultItem(ESeverity.fatal, "Filename not specified").setSection(10)
|
context.addResultItem(new ValidationResultItem(ESeverity.fatal, "Filename not specified").setSection(10)
|
||||||
.setPart(EPart.pdf));
|
.setPart(EPart.pdf));
|
||||||
}
|
}
|
||||||
|
|
||||||
PDFValidator pdfv = new PDFValidator(context);
|
PDFValidator pdfv = new PDFValidator(context);
|
||||||
if (inputStream == null) {
|
if (inputStream == null) {
|
||||||
context.addResultItem(
|
context.addResultItem(
|
||||||
new ValidationResultItem(ESeverity.fatal, "File not found").setSection(1).setPart(EPart.pdf));
|
new ValidationResultItem(ESeverity.fatal, "File not found").setSection(1).setPart(EPart.pdf));
|
||||||
} else if (inputLength < 32) {
|
} else if (inputLength < 32) {
|
||||||
// with less than 32 bytes it can not even be a proper XML file
|
// with less than 32 bytes it can not even be a proper XML file
|
||||||
// Except it is "<?xml version='1.0'?><xml/>" LOL
|
// Except it is "<?xml version='1.0'?><xml/>" LOL
|
||||||
context.addResultItem(
|
context.addResultItem(
|
||||||
new ValidationResultItem(ESeverity.fatal, "File too small").setSection(5).setPart(EPart.pdf));
|
new ValidationResultItem(ESeverity.fatal, "File too small").setSection(5).setPart(EPart.pdf));
|
||||||
} else if (inputLength >= Integer.MAX_VALUE) {
|
} else if (inputLength >= Integer.MAX_VALUE) {
|
||||||
// Byte arrays are limited to 2GB in Java
|
// Byte arrays are limited to 2GB in Java
|
||||||
context.addResultItem(
|
context.addResultItem(
|
||||||
new ValidationResultItem(ESeverity.fatal, "File too big").setSection(5).setPart(EPart.pdf));
|
new ValidationResultItem(ESeverity.fatal, "File too big").setSection(5).setPart(EPart.pdf));
|
||||||
} else {
|
} else {
|
||||||
content = IOUtils.toByteArray(inputStream);
|
content = IOUtils.toByteArray(inputStream);
|
||||||
XMLValidator xv = new XMLValidator(context);
|
XMLValidator xv = new XMLValidator(context);
|
||||||
if (disableNotices) {
|
if (disableNotices) {
|
||||||
xv.disableNotices();
|
xv.disableNotices();
|
||||||
}
|
}
|
||||||
isPDF = ByteArraySearcher.startsWith(content, new byte[] {'%', 'P', 'D', 'F'});
|
isPDF = ByteArraySearcher.startsWith(content, new byte[]{'%', 'P', 'D', 'F'});
|
||||||
if (isPDF) {
|
if (isPDF) {
|
||||||
// Avoid reading again from file
|
// Avoid reading again from file
|
||||||
pdfv.setFilenameAndContents(contextFilename, content);
|
pdfv.setFilenameAndContents(contextFilename, content);
|
||||||
|
|
||||||
optionsRecognized = true;
|
optionsRecognized = true;
|
||||||
finalStringResult.append("<pdf>");
|
finalStringResult.append("<pdf>");
|
||||||
try {
|
try {
|
||||||
pdfv.validate();
|
pdfv.validate();
|
||||||
|
|
||||||
sha1Checksum = calcSHA1(content);
|
sha1Checksum = calcSHA1(content);
|
||||||
|
|
||||||
// Validate PDF
|
// Validate PDF
|
||||||
|
|
||||||
getPdfValidationResults(finalStringResult, pdfv, xv);
|
getPdfValidationResults(finalStringResult, pdfv, xv);
|
||||||
} catch (IrrecoverableValidationError irx) {
|
} catch (IrrecoverableValidationError irx) {
|
||||||
LOGGER.info(irx.getMessage());
|
LOGGER.info(irx.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
finalStringResult.append("</pdf>\n");
|
finalStringResult.append("</pdf>\n");
|
||||||
|
|
||||||
context.clearCustomXML();
|
context.clearCustomXML();
|
||||||
} else {
|
} else {
|
||||||
boolean isXML = false;
|
boolean isXML = false;
|
||||||
String xmlAsString = null;
|
String xmlAsString = null;
|
||||||
try {
|
try {
|
||||||
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
||||||
DocumentBuilder db = dbf.newDocumentBuilder();
|
DocumentBuilder db = dbf.newDocumentBuilder();
|
||||||
|
|
||||||
content = XMLTools.removeBOM(content);
|
content = XMLTools.removeBOM(content);
|
||||||
xmlAsString = new String(content, StandardCharsets.UTF_8);
|
xmlAsString = new String(content, StandardCharsets.UTF_8);
|
||||||
InputSource is = new InputSource(new StringReader(xmlAsString));
|
InputSource is = new InputSource(new StringReader(xmlAsString));
|
||||||
Document doc = db.parse(is);
|
Document doc = db.parse(is);
|
||||||
|
|
||||||
Element root = doc.getDocumentElement();
|
Element root = doc.getDocumentElement();
|
||||||
isXML = true;//no exception so far
|
isXML = true;//no exception so far
|
||||||
|
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
// probably no xml file, sth like SAXParseException content not allowed in prolog
|
// probably no xml file, sth like SAXParseException content not allowed in prolog
|
||||||
// ignore isXML is already false
|
// ignore isXML is already false
|
||||||
// in the tests, this may error-out anyway
|
// in the tests, this may error-out anyway
|
||||||
LOGGER.info("No XML part provided");
|
LOGGER.info("No XML part provided");
|
||||||
}
|
}
|
||||||
if (isXML) {
|
if (isXML) {
|
||||||
pdfValidity = true;
|
pdfValidity = true;
|
||||||
optionsRecognized = true;
|
optionsRecognized = true;
|
||||||
xv.setStringContent (xmlAsString);
|
xv.setStringContent(xmlAsString);
|
||||||
xv.setAutoload(false);
|
xv.setAutoload(false);
|
||||||
xv.setFilename(contextFilename);
|
xv.setFilename(contextFilename);
|
||||||
sha1Checksum = calcSHA1(content);
|
sha1Checksum = calcSHA1(content);
|
||||||
|
|
||||||
displayXMLValidationOutput = true;
|
displayXMLValidationOutput = true;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
optionsRecognized = false;
|
optionsRecognized = false;
|
||||||
context.addResultItem(new ValidationResultItem(ESeverity.exception,
|
context.addResultItem(new ValidationResultItem(ESeverity.exception,
|
||||||
"File does not look like PDF nor XML (contains neither %PDF nor <?xml)").setSection(8));
|
"File does not look like PDF nor XML (contains neither %PDF nor <?xml)").setSection(8));
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ((optionsRecognized) && (displayXMLValidationOutput)) {
|
if ((optionsRecognized) && (displayXMLValidationOutput)) {
|
||||||
finalStringResult.append("<xml>");
|
finalStringResult.append("<xml>");
|
||||||
try {
|
try {
|
||||||
xv.validate();
|
xv.validate();
|
||||||
} catch (IrrecoverableValidationError irx) {
|
} catch (IrrecoverableValidationError irx) {
|
||||||
LOGGER.info("The hell");
|
LOGGER.error("XML validation threw an exception ", irx);
|
||||||
}
|
}
|
||||||
finalStringResult.append(xv.getXMLResult());
|
finalStringResult.append(xv.getXMLResult());
|
||||||
finalStringResult.append("</xml>");
|
finalStringResult.append("</xml>");
|
||||||
context.clearCustomXML();
|
context.clearCustomXML();
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((isPDF) && (!pdfValidity)) {
|
if ((isPDF) && (!pdfValidity)) {
|
||||||
context.setInvalid();
|
context.setInvalid();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
} catch (IrrecoverableValidationError | IOException irx) {
|
} catch (IrrecoverableValidationError | IOException irx) {
|
||||||
LOGGER.info(irx.getMessage());
|
LOGGER.info(irx.getMessage());
|
||||||
context.setInvalid ();
|
context.setInvalid();
|
||||||
} finally {
|
} finally {
|
||||||
finalStringResult.append(context.getXMLResult());
|
finalStringResult.append(context.getXMLResult());
|
||||||
finalStringResult.append("</validation>");
|
finalStringResult.append("</validation>");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return formatOutput(finalStringResult, isPDF);
|
return formatOutput(finalStringResult, isPDF);
|
||||||
}
|
}
|
||||||
|
|
||||||
/***
|
/***
|
||||||
@@ -210,61 +210,59 @@ public class ZUGFeRDValidator {
|
|||||||
* @return a xml string with the validation result
|
* @return a xml string with the validation result
|
||||||
*/
|
*/
|
||||||
public String validate(String filename) {
|
public String validate(String filename) {
|
||||||
String contextFilename;
|
String contextFilename;
|
||||||
InputStream inputStream;
|
InputStream inputStream;
|
||||||
long inputLength;
|
long inputLength;
|
||||||
if (filename == null) {
|
if (filename == null) {
|
||||||
// No filename provided
|
// No filename provided
|
||||||
contextFilename = "";
|
contextFilename = "";
|
||||||
inputStream = null;
|
inputStream = null;
|
||||||
inputLength = 0;
|
inputLength = 0;
|
||||||
} else {
|
} else {
|
||||||
File file = new File(filename);
|
File file = new File(filename);
|
||||||
// set filename without path
|
// set filename without path
|
||||||
contextFilename = file.getName ();
|
contextFilename = file.getName();
|
||||||
if (file.isFile ()) {
|
if (file.isFile()) {
|
||||||
try {
|
try {
|
||||||
inputStream = new FileInputStream (file);
|
inputStream = new FileInputStream(file);
|
||||||
inputLength = Files.size (file.toPath ());
|
inputLength = Files.size(file.toPath());
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
throw new UncheckedIOException (ex);
|
throw new UncheckedIOException(ex);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Non-existing or Directory
|
// Non-existing or Directory
|
||||||
inputStream = null;
|
inputStream = null;
|
||||||
inputLength = 0;
|
inputLength = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return internalValidate (contextFilename, inputStream, inputLength);
|
return internalValidate(contextFilename, inputStream, inputLength);
|
||||||
} finally {
|
} finally {
|
||||||
StreamHelper.close (inputStream);
|
StreamHelper.close(inputStream);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String validate(InputStream inputStream, String fileNameOfInputStream) {
|
public String validate(InputStream inputStream, String fileNameOfInputStream) {
|
||||||
long inputLength;
|
long inputLength;
|
||||||
try {
|
try {
|
||||||
inputLength = inputStream == null ? 0 : inputStream.available ();
|
inputLength = inputStream == null ? 0 : inputStream.available();
|
||||||
}
|
} catch (IOException ex) {
|
||||||
catch (IOException ex) {
|
throw new UncheckedIOException(ex);
|
||||||
throw new UncheckedIOException (ex);
|
}
|
||||||
}
|
try {
|
||||||
try {
|
return internalValidate(fileNameOfInputStream, inputStream, inputLength);
|
||||||
return internalValidate (fileNameOfInputStream, inputStream, inputLength);
|
} finally {
|
||||||
} finally {
|
StreamHelper.close(inputStream);
|
||||||
StreamHelper.close (inputStream);
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public String validate(byte[] bytes, String fileNameOfInputStream) {
|
public String validate(byte[] bytes, String fileNameOfInputStream) {
|
||||||
try(ByteArrayInputStream bais = new ByteArrayInputStream (bytes)) {
|
try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes)) {
|
||||||
return internalValidate (fileNameOfInputStream, bais, bytes.length);
|
return internalValidate(fileNameOfInputStream, bais, bytes.length);
|
||||||
}
|
} catch (IOException ex) {
|
||||||
catch (IOException ex) {
|
throw new UncheckedIOException(ex);
|
||||||
throw new UncheckedIOException (ex);
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private void getPdfValidationResults(StringBuilder finalStringResult, PDFValidator pdfv, XMLValidator xv) throws IrrecoverableValidationError {
|
private void getPdfValidationResults(StringBuilder finalStringResult, PDFValidator pdfv, XMLValidator xv) throws IrrecoverableValidationError {
|
||||||
finalStringResult.append(pdfv.getXMLResult());
|
finalStringResult.append(pdfv.getXMLResult());
|
||||||
|
|||||||
Reference in New Issue
Block a user