Added some tests, history, made it possible to skip invoking parse()

This commit is contained in:
Jochen Stärk
2018-07-20 14:30:24 +02:00
parent 2ccc244790
commit 0d87ce02ca
4 changed files with 86 additions and 19 deletions

View File

@@ -1,9 +1,19 @@
1.5.3
=====
2018-07-20
Fixed #60 nullpointerexception in ZUGFeRDimporter on some input files
and #61 missing in maven repo
Now possible to skip parse() and go from zi.extract to e.g. zi.getAmount()
1.5.2 1.5.2
===== =====
2018-06-10 2018-06-10
Fixed #57 commandline not converting PDF from A1 to A3 when adding XML to PDF (-c) Fixed #57 commandline not converting PDF from A1 to A3 when adding XML to PDF (-c)
Completed javadoc
1.5.1 1.5.1
===== =====

View File

@@ -77,7 +77,7 @@ public class ZUGFeRDImporter {
private byte[] rawXML = null; private byte[] rawXML = null;
private String bankName; private String bankName;
private boolean amountFound; private boolean amountFound;
private boolean extracted = false; private boolean extractAttempt = false;
private boolean parsed = false; private boolean parsed = false;
private static final Logger LOG = Logger.getLogger(ZUGFeRDImporter.class.getName()); private static final Logger LOG = Logger.getLogger(ZUGFeRDImporter.class.getName());
@@ -108,7 +108,7 @@ public class ZUGFeRDImporter {
*/ */
public void extractLowLevel(InputStream pdfStream) throws IOException { public void extractLowLevel(InputStream pdfStream) throws IOException {
PDEmbeddedFilesNameTreeNode etn; PDEmbeddedFilesNameTreeNode etn;
extractAttempt = true;
try (PDDocument doc = PDDocument.load(pdfStream)) { try (PDDocument doc = PDDocument.load(pdfStream)) {
// PDDocumentInformation info = doc.getDocumentInformation(); // PDDocumentInformation info = doc.getDocumentInformation();
PDDocumentNameDictionary names = new PDDocumentNameDictionary(doc.getDocumentCatalog()); PDDocumentNameDictionary names = new PDDocumentNameDictionary(doc.getDocumentCatalog());
@@ -120,15 +120,16 @@ public class ZUGFeRDImporter {
Map<String, PDComplexFileSpecification> efMap = etn.getNames(); Map<String, PDComplexFileSpecification> efMap = etn.getNames();
// String filePath = "/tmp/"; // String filePath = "/tmp/";
if (efMap!=null) { if (efMap != null) {
extractFiles(efMap); // see https://memorynotfound.com/apache-pdfbox-extract-embedded-file-pdf-document/ extractFiles(efMap); // see
// https://memorynotfound.com/apache-pdfbox-extract-embedded-file-pdf-document/
} else { } else {
List<PDNameTreeNode<PDComplexFileSpecification>> kids = etn.getKids(); List<PDNameTreeNode<PDComplexFileSpecification>> kids = etn.getKids();
for (PDNameTreeNode<PDComplexFileSpecification> node : kids) { for (PDNameTreeNode<PDComplexFileSpecification> node : kids) {
Map<String, PDComplexFileSpecification> namesL = node.getNames(); Map<String, PDComplexFileSpecification> namesL = node.getNames();
extractFiles(namesL); extractFiles(namesL);
} }
} }
} }
} }
@@ -154,7 +155,7 @@ public class ZUGFeRDImporter {
rawXML = embeddedFile.toByteArray(); rawXML = embeddedFile.toByteArray();
setMeta(new String(rawXML)); setMeta(new String(rawXML));
extracted = true;
// fos.write(embeddedFile.getByteArray()); // fos.write(embeddedFile.getByteArray());
// fos.close(); // fos.close();
} }
@@ -169,9 +170,12 @@ public class ZUGFeRDImporter {
DocumentBuilder builder = null; DocumentBuilder builder = null;
Document document = null; Document document = null;
if (!extracted) { if (!extractAttempt) {
throw new RuntimeException("extract() or extractLowLevel() must be used before parsing."); throw new RuntimeException("extract() or extractLowLevel() must be used before parsing.");
} }
if (!containsMeta) {
throw new RuntimeException("No suitable data/ZUGFeRD file could be found.");
}
factory = DocumentBuilderFactory.newInstance(); factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true); // otherwise we can not act namespace independently, i.e. use factory.setNamespaceAware(true); // otherwise we can not act namespace independently, i.e. use
@@ -350,7 +354,10 @@ public class ZUGFeRDImporter {
*/ */
public String getForeignReference() { public String getForeignReference() {
if (!parsed) { if (!parsed) {
throw new RuntimeException("use parse() before requesting a value"); throw new RuntimeException("use extract() before requesting a value");
}
if (foreignReference==null) {
parse();
} }
return foreignReference; return foreignReference;
} }
@@ -365,7 +372,10 @@ public class ZUGFeRDImporter {
*/ */
public String getBIC() { public String getBIC() {
if (!parsed) { if (!parsed) {
throw new RuntimeException("use parse() before requesting a value"); throw new RuntimeException("use extract() before requesting a value");
}
if (BIC==null) {
parse();
} }
return BIC; return BIC;
} }
@@ -388,7 +398,10 @@ public class ZUGFeRDImporter {
*/ */
public String getIBAN() { public String getIBAN() {
if (!parsed) { if (!parsed) {
throw new RuntimeException("use parse() before requesting a value"); throw new RuntimeException("use extract() before requesting a value");
}
if (IBAN==null) {
parse();
} }
return IBAN; return IBAN;
} }
@@ -399,7 +412,10 @@ public class ZUGFeRDImporter {
*/ */
public String getBankName() { public String getBankName() {
if (!parsed) { if (!parsed) {
throw new RuntimeException("use parse() before requesting a value"); throw new RuntimeException("use extract() before requesting a value");
}
if (bankName==null) {
parse();
} }
return bankName; return bankName;
} }
@@ -414,7 +430,10 @@ public class ZUGFeRDImporter {
*/ */
public String getHolder() { public String getHolder() {
if (rawXML == null) { if (rawXML == null) {
throw new RuntimeException("use parse() before requesting a value"); throw new RuntimeException("use extract() before requesting a value");
}
if (holder==null) {
parse();
} }
return holder; return holder;
} }
@@ -429,7 +448,10 @@ public class ZUGFeRDImporter {
*/ */
public String getAmount() { public String getAmount() {
if (rawXML == null) { if (rawXML == null) {
throw new RuntimeException("use parse() before requesting a value"); throw new RuntimeException("use extract() before requesting a value");
}
if (amount==null) {
parse();
} }
return amount; return amount;
} }
@@ -440,7 +462,10 @@ public class ZUGFeRDImporter {
*/ */
public String getDueDate() { public String getDueDate() {
if (rawXML == null) { if (rawXML == null) {
throw new RuntimeException("use parse() before requesting a value"); throw new RuntimeException("use extract() before requesting a value");
}
if (dueDate==null) {
parse();
} }
return dueDate; return dueDate;
} }

View File

@@ -19,6 +19,7 @@
package org.mustangproject.ZUGFeRD; package org.mustangproject.ZUGFeRD;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.math.BigDecimal; import java.math.BigDecimal;
@@ -366,6 +367,37 @@ public class MustangReaderWriterTest extends TestCase implements IZUGFeRDExporta
} }
public void testForeignImport() throws IOException {
ZUGFeRDImporter zi = new ZUGFeRDImporter();
try (InputStream inputStream = this.getClass()
.getResourceAsStream("/zugferd_invoice.pdf")) {
zi.extractLowLevel(inputStream);
}
// Reading ZUGFeRD
String amount = zi.getAmount();
// this resembles the data written in MustangReaderWriterCustomXMLTest
assertEquals(amount, "1005.55");
}
public void testMigratePDFA1ToA3() throws IOException {
// just make sure there is no Exception
InputStream SOURCE_PDF = this.getClass()
.getResourceAsStream("/MustangGnuaccountingBeispielRE-20171118_506blanko.pdf");
ZUGFeRDExporter ze = new ZUGFeRDExporterFromA1Factory().setAttachZUGFeRDHeaders(false).load(SOURCE_PDF);
File tempFile = File.createTempFile("ZUGFeRD-", "-test");
ze.export(tempFile.getName());
tempFile.deleteOnExit();
}
/** /**
* The exporter test bases on @{code * The exporter test bases on @{code
* ./src/test/MustangGnuaccountingBeispielRE-20140703_502blanko.pdf}, adds * ./src/test/MustangGnuaccountingBeispielRE-20140703_502blanko.pdf}, adds

Binary file not shown.