diff --git a/History.md b/History.md index 31dd4c86..645d4b64 100644 --- a/History.md +++ b/History.md @@ -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 ===== 2018-06-10 Fixed #57 commandline not converting PDF from A1 to A3 when adding XML to PDF (-c) -Completed javadoc 1.5.1 ===== diff --git a/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java b/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java index d78ac515..538082fe 100644 --- a/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java +++ b/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java @@ -77,7 +77,7 @@ public class ZUGFeRDImporter { private byte[] rawXML = null; private String bankName; private boolean amountFound; - private boolean extracted = false; + private boolean extractAttempt = false; private boolean parsed = false; private static final Logger LOG = Logger.getLogger(ZUGFeRDImporter.class.getName()); @@ -108,7 +108,7 @@ public class ZUGFeRDImporter { */ public void extractLowLevel(InputStream pdfStream) throws IOException { PDEmbeddedFilesNameTreeNode etn; - + extractAttempt = true; try (PDDocument doc = PDDocument.load(pdfStream)) { // PDDocumentInformation info = doc.getDocumentInformation(); PDDocumentNameDictionary names = new PDDocumentNameDictionary(doc.getDocumentCatalog()); @@ -120,15 +120,16 @@ public class ZUGFeRDImporter { Map efMap = etn.getNames(); // String filePath = "/tmp/"; - if (efMap!=null) { - extractFiles(efMap); // see https://memorynotfound.com/apache-pdfbox-extract-embedded-file-pdf-document/ + if (efMap != null) { + extractFiles(efMap); // see + // https://memorynotfound.com/apache-pdfbox-extract-embedded-file-pdf-document/ } else { - List> kids = etn.getKids(); - for (PDNameTreeNode node : kids) { - Map namesL = node.getNames(); - extractFiles(namesL); - } + List> kids = etn.getKids(); + for (PDNameTreeNode node : kids) { + Map namesL = node.getNames(); + extractFiles(namesL); + } } } } @@ -154,7 +155,7 @@ public class ZUGFeRDImporter { rawXML = embeddedFile.toByteArray(); setMeta(new String(rawXML)); - extracted = true; + // fos.write(embeddedFile.getByteArray()); // fos.close(); } @@ -169,9 +170,12 @@ public class ZUGFeRDImporter { DocumentBuilder builder = null; Document document = null; - if (!extracted) { + if (!extractAttempt) { 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.setNamespaceAware(true); // otherwise we can not act namespace independently, i.e. use @@ -350,7 +354,10 @@ public class ZUGFeRDImporter { */ public String getForeignReference() { 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; } @@ -365,7 +372,10 @@ public class ZUGFeRDImporter { */ public String getBIC() { 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; } @@ -388,7 +398,10 @@ public class ZUGFeRDImporter { */ public String getIBAN() { 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; } @@ -399,7 +412,10 @@ public class ZUGFeRDImporter { */ public String getBankName() { 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; } @@ -414,7 +430,10 @@ public class ZUGFeRDImporter { */ public String getHolder() { 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; } @@ -429,7 +448,10 @@ public class ZUGFeRDImporter { */ public String getAmount() { 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; } @@ -440,7 +462,10 @@ public class ZUGFeRDImporter { */ public String getDueDate() { 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; } diff --git a/src/test/java/org/mustangproject/ZUGFeRD/MustangReaderWriterTest.java b/src/test/java/org/mustangproject/ZUGFeRD/MustangReaderWriterTest.java index d8cde149..a78c5510 100644 --- a/src/test/java/org/mustangproject/ZUGFeRD/MustangReaderWriterTest.java +++ b/src/test/java/org/mustangproject/ZUGFeRD/MustangReaderWriterTest.java @@ -19,6 +19,7 @@ package org.mustangproject.ZUGFeRD; import java.io.ByteArrayOutputStream; +import java.io.File; import java.io.IOException; import java.io.InputStream; 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 * ./src/test/MustangGnuaccountingBeispielRE-20140703_502blanko.pdf}, adds diff --git a/src/test/resources/zugferd_invoice.pdf b/src/test/resources/zugferd_invoice.pdf new file mode 100644 index 00000000..6dd5ca93 Binary files /dev/null and b/src/test/resources/zugferd_invoice.pdf differ