From 57f0e27186b86b12af2952ff13303894693a8240 Mon Sep 17 00:00:00 2001 From: Andre Kemper Date: Tue, 9 Apr 2019 06:43:08 +0200 Subject: [PATCH 1/4] parsing only for tests --- src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java b/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java index f6bd06a5..e03bd5e1 100644 --- a/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java +++ b/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java @@ -195,7 +195,7 @@ public class ZUGFeRDImporter { /** * needs to be called to be able to call the getters */ - public void parse() { + protected void parse() { DocumentBuilderFactory factory = null; DocumentBuilder builder = null; Document document = null; From e60105f8b7a1e698acb970b0ea8e838d1d8d2868 Mon Sep 17 00:00:00 2001 From: Andre Kemper Date: Tue, 9 Apr 2019 07:03:56 +0200 Subject: [PATCH 2/4] use Constructores with parameters --- .../ZUGFeRD/ZUGFeRDImporter.java | 66 ++++++++++--------- .../ZUGFeRD/ZUGFeRDImporterException.java | 23 +++++++ .../mustangproject/toecount/FileChecker.java | 3 +- .../org/mustangproject/toecount/Toecount.java | 3 +- .../MustangReaderWriterCustomXMLTest.java | 10 ++- .../ZUGFeRD/MustangReaderWriterEdgeTest.java | 11 ++-- .../ZUGFeRD/MustangReaderWriterTest.java | 21 ++---- 7 files changed, 74 insertions(+), 63 deletions(-) create mode 100644 src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporterException.java diff --git a/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java b/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java index e03bd5e1..8a63b603 100644 --- a/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java +++ b/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java @@ -44,11 +44,13 @@ import java.io.*; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Scanner; import java.util.logging.Logger; //root.setNamespace(Namespace.getNamespace("http://www.energystar.gov/manageBldgs/req")); public class ZUGFeRDImporter { + /* * call extract(importFilename). containsMeta() will return if ZUGFeRD data has * been found, afterwards you can call getBIC(), getIBAN() etc. @@ -77,42 +79,28 @@ public class ZUGFeRDImporter { private byte[] rawXML = null; private String bankName; private boolean amountFound; - private boolean extractAttempt = false; private boolean parsed = false; private String xmpString = null; // XMP metadata private static final Logger LOG = Logger.getLogger(ZUGFeRDImporter.class.getName()); - /** - * Extracts a ZUGFeRD invoice from a PDF document represented by a file name. - * Errors are just logged to STDOUT. - * - * @param pdfFilename the filename of the pdf - */ - public void extract(String pdfFilename) { + public ZUGFeRDImporter(String pdfFilename) { try { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(pdfFilename)); - extractLowLevel(bis); bis.close(); - } catch (IOException ioe) { - ioe.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + throw new ZUGFeRDExportException(e); } } - static String convertStreamToString(java.io.InputStream is) { - // source https://stackoverflow.com/questions/309424/how-do-i-read-convert-an-inputstream-into-a-string-in-java referring to - // https://community.oracle.com/blogs/pat/2004/10/23/stupid-scanner-tricks - java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); - return s.hasNext() ? s.next() : ""; - } - - /** - * get xmp metadata of the PDF, null if not available - * - * @return string - */ - public String getXMP() { - return xmpString; + public ZUGFeRDImporter(InputStream pdfStream) { + try { + extractLowLevel(pdfStream); + } catch (IOException e) { + e.printStackTrace(); + throw new ZUGFeRDExportException(e); + } } /** @@ -121,9 +109,7 @@ public class ZUGFeRDImporter { * * @param pdfStream a inputstream of a pdf file */ - public void extractLowLevel(InputStream pdfStream) throws IOException { - PDEmbeddedFilesNameTreeNode etn; - extractAttempt = true; + private void extractLowLevel(InputStream pdfStream) throws IOException { try (PDDocument doc = PDDocument.load(pdfStream)) { // PDDocumentInformation info = doc.getDocumentInformation(); PDDocumentNameDictionary names = new PDDocumentNameDictionary(doc.getDocumentCatalog()); @@ -131,7 +117,8 @@ public class ZUGFeRDImporter { InputStream XMP = doc.getDocumentCatalog().getMetadata().exportXMPMetadata(); xmpString = convertStreamToString(XMP); - etn = names.getEmbeddedFiles(); + + PDEmbeddedFilesNameTreeNode etn = names.getEmbeddedFiles(); if (etn == null) { return; } @@ -200,9 +187,6 @@ public class ZUGFeRDImporter { DocumentBuilder builder = null; Document document = null; - 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."); } @@ -376,6 +360,16 @@ public class ZUGFeRDImporter { parsed = true; } + /** + * get xmp metadata of the PDF, null if not available + * + * @return string + */ + public String getXMP() { + return xmpString; + } + + /** * @return if export found parseable ZUGFeRD data */ @@ -604,4 +598,12 @@ public class ZUGFeRDImporter { return (meta != null) && (meta.length() > 0) && ((meta.contains("SpecifiedExchangedDocumentContext") //$NON-NLS-1$ /* ZF1 */ || meta.contains("ExchangedDocumentContext") /* ZF2 */)); } + + static String convertStreamToString(java.io.InputStream is) { + // source https://stackoverflow.com/questions/309424/how-do-i-read-convert-an-inputstream-into-a-string-in-java referring to + // https://community.oracle.com/blogs/pat/2004/10/23/stupid-scanner-tricks + Scanner s = new Scanner(is).useDelimiter("\\A"); + return s.hasNext() ? s.next() : ""; + } + } diff --git a/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporterException.java b/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporterException.java new file mode 100644 index 00000000..a1ea969f --- /dev/null +++ b/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporterException.java @@ -0,0 +1,23 @@ +/** ********************************************************************** + * + * Copyright 2019 ak on 09.04.19. + * + * Use is subject to license terms. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy + * of the License at http://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + * + *********************************************************************** */ +package org.mustangproject.ZUGFeRD; + +public class ZUGFeRDImporterException extends RuntimeException { + +} diff --git a/src/main/java/org/mustangproject/toecount/FileChecker.java b/src/main/java/org/mustangproject/toecount/FileChecker.java index 26142289..9ad2a9d2 100755 --- a/src/main/java/org/mustangproject/toecount/FileChecker.java +++ b/src/main/java/org/mustangproject/toecount/FileChecker.java @@ -49,9 +49,8 @@ public class FileChecker { if ((!isPDF) && (!thisRun.shallIgnoreFileExt())) { return false; } - ZUGFeRDImporter zi = new ZUGFeRDImporter(); + ZUGFeRDImporter zi = new ZUGFeRDImporter(filename); try { - zi.extract(filename); if (zi.canParse()) { thisRun.incZUGFeRDCount(); return true; diff --git a/src/main/java/org/mustangproject/toecount/Toecount.java b/src/main/java/org/mustangproject/toecount/Toecount.java index 6ae88e3d..2cf1c3b3 100755 --- a/src/main/java/org/mustangproject/toecount/Toecount.java +++ b/src/main/java/org/mustangproject/toecount/Toecount.java @@ -406,8 +406,7 @@ public class Toecount { ensureFileNotExists(xmlName); // All params are good! continue... - ZUGFeRDImporter zi = new ZUGFeRDImporter(); - zi.extract(pdfName); + ZUGFeRDImporter zi = new ZUGFeRDImporter(pdfName); byte[] XMLContent = zi.getRawXML(); if (XMLContent == null) { System.err.println("No ZUGFeRD XML found in PDF file"); diff --git a/src/test/java/org/mustangproject/ZUGFeRD/MustangReaderWriterCustomXMLTest.java b/src/test/java/org/mustangproject/ZUGFeRD/MustangReaderWriterCustomXMLTest.java index 6a69321f..44474c38 100644 --- a/src/test/java/org/mustangproject/ZUGFeRD/MustangReaderWriterCustomXMLTest.java +++ b/src/test/java/org/mustangproject/ZUGFeRD/MustangReaderWriterCustomXMLTest.java @@ -272,10 +272,9 @@ public class MustangReaderWriterCustomXMLTest extends TestCase { } // now check the contents (like MustangReaderTest) - ZUGFeRDImporter zi = new ZUGFeRDImporter(); - zi.extract(TARGET_PDF); - // Reading ZUGFeRD + ZUGFeRDImporter zi = new ZUGFeRDImporter(TARGET_PDF); + // Reading ZUGFeRD String amount = null; String bic = null; String blz = null; @@ -474,10 +473,9 @@ public class MustangReaderWriterCustomXMLTest extends TestCase { } // now check the contents (like MustangReaderTest) - ZUGFeRDImporter zi = new ZUGFeRDImporter(); - zi.extract(TARGET_PDF); - // Reading ZUGFeRD + ZUGFeRDImporter zi = new ZUGFeRDImporter(TARGET_PDF); + // Reading ZUGFeRD String amount = null; String bic = null; String blz = null; diff --git a/src/test/java/org/mustangproject/ZUGFeRD/MustangReaderWriterEdgeTest.java b/src/test/java/org/mustangproject/ZUGFeRD/MustangReaderWriterEdgeTest.java index 29c29d51..6af0cfe8 100644 --- a/src/test/java/org/mustangproject/ZUGFeRD/MustangReaderWriterEdgeTest.java +++ b/src/test/java/org/mustangproject/ZUGFeRD/MustangReaderWriterEdgeTest.java @@ -336,10 +336,10 @@ public class MustangReaderWriterEdgeTest extends TestCase implements IZUGFeRDExp */ public void testAImport() throws IOException { - ZUGFeRDImporter zi = new ZUGFeRDImporter(); - zi.extractLowLevel(this.getClass().getResourceAsStream("/MustangGnuaccountingBeispielRE-20170509_505.pdf")); - // Reading ZUGFeRD + InputStream inputStream = this.getClass().getResourceAsStream("/MustangGnuaccountingBeispielRE-20170509_505.pdf"); + ZUGFeRDImporter zi = new ZUGFeRDImporter(inputStream); + // Reading ZUGFeRD String amount = null; String bic = null; String blz = null; @@ -401,10 +401,9 @@ public class MustangReaderWriterEdgeTest extends TestCase implements IZUGFeRDExp } // now check the contents (like MustangReaderTest) - ZUGFeRDImporter zi = new ZUGFeRDImporter(); - zi.extract(TARGET_PDF); - // Reading ZUGFeRD + ZUGFeRDImporter zi = new ZUGFeRDImporter(TARGET_PDF); + // Reading ZUGFeRD String amount = null; String bic = null; String blz = null; diff --git a/src/test/java/org/mustangproject/ZUGFeRD/MustangReaderWriterTest.java b/src/test/java/org/mustangproject/ZUGFeRD/MustangReaderWriterTest.java index 469ec565..320e853d 100644 --- a/src/test/java/org/mustangproject/ZUGFeRD/MustangReaderWriterTest.java +++ b/src/test/java/org/mustangproject/ZUGFeRD/MustangReaderWriterTest.java @@ -346,14 +346,10 @@ public class MustangReaderWriterTest extends TestCase implements IZUGFeRDExporta */ public void testAImport() throws IOException { - ZUGFeRDImporter zi = new ZUGFeRDImporter(); - try (InputStream inputStream = this.getClass() - .getResourceAsStream("/MustangGnuaccountingBeispielRE-20170509_505.pdf")) { - zi.extractLowLevel(inputStream); - } + InputStream inputStream = this.getClass().getResourceAsStream("/MustangGnuaccountingBeispielRE-20170509_505.pdf"); + ZUGFeRDImporter zi = new ZUGFeRDImporter(inputStream); // Reading ZUGFeRD - String amount = null; String blz = null; String bic = null; @@ -384,14 +380,10 @@ public class MustangReaderWriterTest extends TestCase implements IZUGFeRDExporta } public void testForeignImport() throws IOException { - ZUGFeRDImporter zi = new ZUGFeRDImporter(); + InputStream inputStream = this.getClass().getResourceAsStream("/zugferd_invoice.pdf"); + ZUGFeRDImporter zi = new ZUGFeRDImporter(inputStream); - try (InputStream inputStream = this.getClass() - .getResourceAsStream("/zugferd_invoice.pdf")) { - zi.extractLowLevel(inputStream); - } // Reading ZUGFeRD - String amount = zi.getAmount(); assertEquals("\n" + @@ -522,10 +514,9 @@ public class MustangReaderWriterTest extends TestCase implements IZUGFeRDExporta } // now check the contents (like MustangReaderTest) - ZUGFeRDImporter zi = new ZUGFeRDImporter(); - zi.extract(TARGET_PDF); - // Reading ZUGFeRD + ZUGFeRDImporter zi = new ZUGFeRDImporter(TARGET_PDF); + // Reading ZUGFeRD String amount = null; String bic = null; String iban = null; From e2e957a45058c4105454ca79ab973bfe12b4838d Mon Sep 17 00:00:00 2001 From: Andre Kemper Date: Tue, 9 Apr 2019 23:25:43 +0200 Subject: [PATCH 3/4] refactoring Importer --- .../ZUGFeRD/ZUGFeRDImporter.java | 452 ++++-------------- .../MustangReaderWriterCustomXMLTest.java | 77 +-- .../ZUGFeRD/MustangReaderWriterEdgeTest.java | 72 +-- .../ZUGFeRD/MustangReaderWriterTest.java | 30 +- 4 files changed, 139 insertions(+), 492 deletions(-) diff --git a/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java b/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java index 8a63b603..9ea0b067 100644 --- a/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java +++ b/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java @@ -33,30 +33,22 @@ import org.apache.pdfbox.pdmodel.common.PDNameTreeNode; import org.apache.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification; import org.apache.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile; import org.w3c.dom.Document; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.*; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; import java.io.*; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Scanner; -import java.util.logging.Logger; - -//root.setNamespace(Namespace.getNamespace("http://www.energystar.gov/manageBldgs/req")); +import java.util.*; public class ZUGFeRDImporter { - /* - * call extract(importFilename). containsMeta() will return if ZUGFeRD data has - * been found, afterwards you can call getBIC(), getIBAN() etc. - * - */ - /** * @var if metadata has been found */ @@ -64,24 +56,12 @@ public class ZUGFeRDImporter { /** * @var the reference (i.e. invoice number) of the sender */ - private String foreignReference; - private String BLZ; - private String BIC; - private String IBAN; - private String KTO; - private String holder; - private String amount; - private String dueDate; - private HashMap additionalXMLs = new HashMap(); + private HashMap additionalXMLs = new HashMap<>(); /** * Raw XML form of the extracted data - may be directly obtained. */ private byte[] rawXML = null; - private String bankName; - private boolean amountFound; - private boolean parsed = false; private String xmpString = null; // XMP metadata - private static final Logger LOG = Logger.getLogger(ZUGFeRDImporter.class.getName()); public ZUGFeRDImporter(String pdfFilename) { try { @@ -175,189 +155,112 @@ public class ZUGFeRDImporter { } } - public HashMap getAdditionalData() { - return additionalXMLs; + private void prettyPrint(Document document) throws TransformerException { + TransformerFactory tf = TransformerFactory.newInstance(); + Transformer transformer = null; + try { + transformer = tf.newTransformer(); + } catch (TransformerConfigurationException e) { + e.printStackTrace(); + } + transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); + StringWriter writer = new StringWriter(); + transformer.transform(new DOMSource(document), new StreamResult(writer)); + String output = writer.getBuffer().toString();//.replaceAll("\n|\r", ""); + System.err.println(output); + } + + private Document getDocument() throws ParserConfigurationException, IOException, SAXException, TransformerException { + DocumentBuilderFactory xmlFact = DocumentBuilderFactory.newInstance(); + xmlFact.setNamespaceAware(false); + DocumentBuilder builder = xmlFact.newDocumentBuilder(); + Document doc = builder.parse(new ByteArrayInputStream(rawXML)); + //prettyPrint(doc); + return doc; + } + + private String extractString(String xpathStr) { + if (!containsMeta) { + throw new ZUGFeRDExportException("No suitable data/ZUGFeRD file could be found."); + } + String result; + try { + Document document = getDocument(); + XPathFactory xpathFact = XPathFactory.newInstance(); + XPath xpath = xpathFact.newXPath(); + result = xpath.evaluate(xpathStr, document); + } catch (ParserConfigurationException e) { + e.printStackTrace(); + throw new ZUGFeRDExportException(e); + } catch (IOException | SAXException | TransformerException | XPathExpressionException e) { + e.printStackTrace(); + throw new ZUGFeRDExportException(e); + } + return result; } /** - * needs to be called to be able to call the getters + * @return the reference (purpose) the sender specified for this invoice */ - protected void parse() { - DocumentBuilderFactory factory = null; - DocumentBuilder builder = null; - Document document = null; + public String getForeignReference() { + String result = extractString("//ApplicableHeaderTradeSettlement/PaymentReference"); + if(result == null || result.isEmpty()) + result = extractString("//ApplicableSupplyChainTradeSettlement/PaymentReference"); + return result; + } - if (!containsMeta) { - throw new RuntimeException("No suitable data/ZUGFeRD file could be found."); - } + /** + * @return the sender's bank's BLZ code + */ + public String getBLZ() { + return extractString("//PayeeSpecifiedCreditorFinancialInstitution/GermanBankleitzahlID"); + } - factory = DocumentBuilderFactory.newInstance(); - factory.setNamespaceAware(true); // otherwise we can not act namespace independently, i.e. use - // document.getElementsByTagNameNS("*",... - try { - builder = factory.newDocumentBuilder(); - } catch (ParserConfigurationException ex3) { - // TODO Auto-generated catch block - ex3.printStackTrace(); - } + /** + * @return the sender's bank's BIC code + */ + public String getBIC() { + return extractString("//PayeeSpecifiedCreditorFinancialInstitution/BICID"); + } - try { - InputStream bais = new ByteArrayInputStream(rawXML); - document = builder.parse(bais); - } catch (SAXException ex1) { - ex1.printStackTrace(); - } catch (IOException ex2) { - ex2.printStackTrace(); - } - NodeList ndList; + /** + * @return the sender's bankname + */ + public String getBankName() { + return extractString("/CrossIndustryInvoice/SupplyChainTradeTransaction/ApplicableHeaderTradeSettlement/SpecifiedTradeSettlementPaymentMeans/PayeeSpecifiedCreditorFinancialInstitution/Name"); + } - // rootNode = document.getDocumentElement(); - // ApplicableSupplyChainTradeSettlement - ndList = document.getDocumentElement().getElementsByTagNameNS("*", "PaymentReference"); //$NON-NLS-1$ + public String getIBAN() { + return extractString("//PayeePartyCreditorFinancialAccount/IBANID"); + } - for (int bookingIndex = 0; bookingIndex < ndList.getLength(); bookingIndex++) { - Node booking = ndList.item(bookingIndex); - // if there is a attribute in the tag number:value + public String getKTO() { + return extractString("//PayeePartyCreditorFinancialAccount/ProprietaryID"); + } - setForeignReference(booking.getTextContent()); + public String getHolder() { + return extractString("//SellerTradeParty/Name"); + } - } - /* - * ndList = document .getElementsByTagName("GermanBankleitzahlID"); - * //$NON-NLS-1$ - * - * for (int bookingIndex = 0; bookingIndex < ndList .getLength(); - * bookingIndex++) { Node booking = ndList.item(bookingIndex); // if there is a - * attribute in the tag number:value setBIC(booking.getTextContent()); - * - * } - * - * ndList = document.getElementsByTagName("ProprietaryID"); //$NON-NLS-1$ - * - * for (int bookingIndex = 0; bookingIndex < ndList .getLength(); - * bookingIndex++) { Node booking = ndList.item(bookingIndex); // if there is a - * attribute in the tag number:value setIBAN(booking.getTextContent()); - * - * } DE1234 - * - * - * DE5656565 Commerzbank - * - * - */ + /** + * @return the total payable amount + */ + public String getAmount() { + String result = extractString("//SpecifiedTradeSettlementHeaderMonetarySummation/DuePayableAmount"); + if(result == null || result.isEmpty()) + result = extractString("//SpecifiedTradeSettlementMonetarySummation/GrandTotalAmount"); + return result; + } - /*** - * we should switch to xpath like this // Create XPathFactory object - * XPathFactory xpathFactory = XPathFactory.newInstance(); - * - * // Create XPath object XPath xpath = xpathFactory.newXPath(); XPathExpression - * expr = - * xpath.compile("//*[local-name()=\"GuidelineSpecifiedDocumentContextParameter\"]/[local-name()=\"ID\"]"); - * //evaluate expression result on XML document ndList = (NodeList) - * expr.evaluate(doc, XPathConstants.NODESET); - * - */ + /** + * @return when the payment is due + */ + public String getDueDate() { + return extractString("//SpecifiedTradePaymentTerms/DueDateDateTime/DateTimeString"); + } - ndList = document.getElementsByTagNameNS("*", "PayeePartyCreditorFinancialAccount"); //$NON-NLS-1$ - for (int bookingIndex = 0; bookingIndex < ndList.getLength(); bookingIndex++) { - - Node booking = ndList.item(bookingIndex); - // there are many "name" elements, so get the one below - // SellerTradeParty - NodeList bookingDetails = booking.getChildNodes(); - - for (int detailIndex = 0; detailIndex < bookingDetails.getLength(); detailIndex++) { - Node detail = bookingDetails.item(detailIndex); - if ((detail.getLocalName() != null) && (detail.getLocalName().equals("IBANID"))) { //$NON-NLS-1$ - setIBAN(detail.getTextContent()); - } - if ((detail.getLocalName() != null) && (detail.getLocalName().equals("ProprietaryID"))) { //$NON-NLS-1$ - setKTO(detail.getTextContent()); - - } - } - - } - ndList = document.getElementsByTagNameNS("*", "PayeeSpecifiedCreditorFinancialInstitution");// ZF1 //$NON-NLS-1$ - for (int bookingIndex = 0; bookingIndex < ndList.getLength(); bookingIndex++) { - Node booking = ndList.item(bookingIndex); - // there are many "name" elements, so get the one below - // SellerTradeParty - NodeList bookingDetails = booking.getChildNodes(); - for (int detailIndex = 0; detailIndex < bookingDetails.getLength(); detailIndex++) { - Node detail = bookingDetails.item(detailIndex); - if ((detail.getLocalName() != null) && (detail.getLocalName().equals("BICID"))) { //$NON-NLS-1$ - setBIC(detail.getTextContent()); - } - if ((detail.getLocalName() != null) && (detail.getLocalName().equals("GermanBankleitzahlID"))) { //$NON-NLS-1$ - setBLZ(detail.getTextContent()); - } - if ((detail.getLocalName() != null) && (detail.getLocalName().equals("Name"))) { //$NON-NLS-1$ - setBankName(detail.getTextContent()); - } - } - - } - - ndList = document.getElementsByTagNameNS("*", "SellerTradeParty"); //$NON-NLS-1$ - - for (int bookingIndex = 0; bookingIndex < ndList.getLength(); bookingIndex++) { - Node booking = ndList.item(bookingIndex); - // there are many "name" elements, so get the one below - // SellerTradeParty - NodeList bookingDetails = booking.getChildNodes(); - for (int detailIndex = 0; detailIndex < bookingDetails.getLength(); detailIndex++) { - Node detail = bookingDetails.item(detailIndex); - if ((detail.getLocalName() != null) && (detail.getLocalName().equals("Name"))) { //$NON-NLS-1$ - setHolder(detail.getTextContent()); - } - } - - } - - ndList = document.getElementsByTagNameNS("*", "DuePayableAmount"); //$NON-NLS-1$ - - for (int bookingIndex = 0; bookingIndex < ndList.getLength(); bookingIndex++) { - Node booking = ndList.item(bookingIndex); - // if there is a attribute in the tag number:value - amountFound = true; - setAmount(booking.getTextContent()); - - } - - if (!amountFound) { - /* - * there is apparently no requirement to mention DuePayableAmount,, if it's not - * there, check for GrandTotalAmount - */ - ndList = document.getElementsByTagNameNS("*", "GrandTotalAmount"); //$NON-NLS-1$ - for (int bookingIndex = 0; bookingIndex < ndList.getLength(); bookingIndex++) { - Node booking = ndList.item(bookingIndex); - // if there is a attribute in the tag number:value - amountFound = true; - setAmount(booking.getTextContent()); - - } - - } - - ndList = document.getElementsByTagNameNS("*", "SpecifiedTradePaymentTerms"); //$NON-NLS-1$ - - for (int bookingIndex = 0; bookingIndex < ndList.getLength(); bookingIndex++) { - Node booking = ndList.item(bookingIndex); - // there are many "name" elements, so get the one below - // SellerTradeParty - NodeList bookingDetails = booking.getChildNodes(); - for (int detailIndex = 0; detailIndex < bookingDetails.getLength(); detailIndex++) { - Node detail = bookingDetails.item(detailIndex); - if ((detail.getLocalName() != null) && (detail.getLocalName().equals("DueDateDateTime"))) { //$NON-NLS-1$ - setDueDate(detail.getTextContent().trim()); - } - } - - } - - parsed = true; + public HashMap getAdditionalData() { + return additionalXMLs; } /** @@ -377,159 +280,6 @@ public class ZUGFeRDImporter { return containsMeta; } - /** - * @return the reference (purpose) the sender specified for this invoice - */ - public String getForeignReference() { - if (!parsed) { - throw new RuntimeException("use extract() before requesting a value"); - } - if (foreignReference == null) { - parse(); - } - return foreignReference; - } - - private void setForeignReference(String foreignReference) { - this.foreignReference = foreignReference; - } - - /** - * @return the sender's bank's BLZ code - */ - public String getBLZ() { - if (!parsed) { - throw new RuntimeException("use extract() before requesting a value"); - } - if (BLZ == null) { - parse(); - } - return BLZ; - } - - private void setBLZ(String blz) { - this.BLZ = blz; - } - - /** - * @return the sender's bank's BIC code - */ - public String getBIC() { - if (!parsed) { - throw new RuntimeException("use extract() before requesting a value"); - } - if (BIC == null) { - parse(); - } - return BIC; - } - - private void setBIC(String bic) { - this.BIC = bic; - } - - private void setDueDate(String dueDate) { - this.dueDate = dueDate; - } - - private void setBankName(String bankname) { - this.bankName = bankname; - } - - /** - * @return the sender's IBAN - */ - public String getIBAN() { - if (!parsed) { - throw new RuntimeException("use extract() before requesting a value"); - } - if (IBAN == null) { - parse(); - } - return IBAN; - } - - /** - * @return the sender's KTO - */ - public String getKTO() { - if (!parsed) { - throw new RuntimeException("use extract() before requesting a value"); - } - if (KTO == null) { - parse(); - } - return KTO; - } - - /** - * @return the sender's bank name - */ - public String getBankName() { - if (!parsed) { - throw new RuntimeException("use extract() before requesting a value"); - } - if (bankName == null) { - parse(); - } - return bankName; - } - - private void setIBAN(String IBAN) { - this.IBAN = IBAN; - } - - private void setKTO(String KTO) { - this.KTO = KTO; - } - - /** - * @return the name of the owner of the sender's bank account - */ - public String getHolder() { - if (rawXML == null) { - throw new RuntimeException("use extract() before requesting a value"); - } - if (holder == null) { - parse(); - } - return holder; - } - - private void setHolder(String holder) { - this.holder = holder; - } - - /** - * @return the total payable amount - */ - public String getAmount() { - if (rawXML == null) { - throw new RuntimeException("use extract() before requesting a value"); - } - if (amount == null) { - parse(); - } - return amount; - } - - /** - * @return when the payment is due - */ - public String getDueDate() { - if (rawXML == null) { - throw new RuntimeException("use extract() before requesting a value"); - } - if (dueDate == null) { - parse(); - } - return dueDate; - } - - private void setAmount(String amount) { - this.amount = amount; - } - /** * @param meta raw XML to be set */ diff --git a/src/test/java/org/mustangproject/ZUGFeRD/MustangReaderWriterCustomXMLTest.java b/src/test/java/org/mustangproject/ZUGFeRD/MustangReaderWriterCustomXMLTest.java index 44474c38..07b03609 100644 --- a/src/test/java/org/mustangproject/ZUGFeRD/MustangReaderWriterCustomXMLTest.java +++ b/src/test/java/org/mustangproject/ZUGFeRD/MustangReaderWriterCustomXMLTest.java @@ -55,14 +55,12 @@ public class MustangReaderWriterCustomXMLTest extends TestCase { // the writing part try { - InputStream SOURCE_PDF = this.getClass() - .getResourceAsStream("/MustangGnuaccountingBeispielRE-20170509_505blanko.pdf"); + InputStream SOURCE_PDF = this.getClass().getResourceAsStream("/MustangGnuaccountingBeispielRE-20170509_505blanko.pdf"); ZUGFeRDExporter zea1 = new ZUGFeRDExporterFromA1Factory().setProducer("My Application").setCreator("Test").setZUGFeRDConformanceLevel(ZUGFeRDConformanceLevel.EN16931) .load(SOURCE_PDF); - final byte[] UTF8ByteOrderMark = new byte[]{(byte) 0xef, (byte) 0xbb, - (byte) 0xbf}; + final byte[] UTF8ByteOrderMark = new byte[]{(byte) 0xef, (byte) 0xbb, (byte) 0xbf}; /* we have much more information than just in the basic profile (comfort or extended) but it's perfectly valid to provide more information, just not less. */ String ownZUGFeRDXML = new String(UTF8ByteOrderMark) + "\n" + "\n" + @@ -267,7 +265,6 @@ public class MustangReaderWriterCustomXMLTest extends TestCase { assertFalse(pdfContent.indexOf("EN 16931") == -1); } catch (IOException e) { - // TODO Auto-generated catch block e.printStackTrace(); } @@ -275,32 +272,13 @@ public class MustangReaderWriterCustomXMLTest extends TestCase { ZUGFeRDImporter zi = new ZUGFeRDImporter(TARGET_PDF); // Reading ZUGFeRD - String amount = null; - String bic = null; - String blz = null; - String iban = null; - String kto = null; - String holder = null; - String ref = null; - - if (zi.canParse()) { - zi.parse(); - amount = zi.getAmount(); - blz = zi.getBLZ(); - bic = zi.getBIC(); - iban = zi.getIBAN(); - kto = zi.getKTO(); - holder = zi.getHolder(); - ref = zi.getForeignReference(); - } - - assertEquals(amount, "571.04"); - assertEquals(blz, "41441604"); - assertEquals(bic, "COBADEFFXXX"); - assertEquals(iban, "DE88 2008 0000 0970 3757 00"); - assertEquals(kto, "44421800"); - assertEquals(holder, "Bei Spiel GmbH"); - assertEquals(ref, "RE-20171118/506"); + assertEquals(zi.getAmount(), "571.04"); + assertEquals(zi.getBLZ(), "41441604"); + assertEquals(zi.getBIC(), "COBADEFFXXX"); + assertEquals(zi.getIBAN(), "DE88 2008 0000 0970 3757 00"); + assertEquals(zi.getKTO(), "44421800"); + assertEquals(zi.getHolder(), "Bei Spiel GmbH"); + assertEquals(zi.getForeignReference(), "RE-20171118/506"); } /** @@ -317,8 +295,7 @@ public class MustangReaderWriterCustomXMLTest extends TestCase { // the writing part try { - InputStream SOURCE_PDF = this.getClass() - .getResourceAsStream("/MustangGnuaccountingBeispielRE-20170509_505blanko.pdf"); + InputStream SOURCE_PDF = this.getClass().getResourceAsStream("/MustangGnuaccountingBeispielRE-20170509_505blanko.pdf"); ZUGFeRDExporter zea1 = new ZUGFeRDExporterFromA1Factory().setProducer("My Application").setCreator("Test").setZUGFeRDConformanceLevel(ZUGFeRDConformanceLevel.BASIC) .load(SOURCE_PDF); @@ -468,7 +445,6 @@ public class MustangReaderWriterCustomXMLTest extends TestCase { assertFalse(pdfContent.indexOf("BASIC") == -1); } catch (IOException e) { - // TODO Auto-generated catch block e.printStackTrace(); } @@ -476,32 +452,13 @@ public class MustangReaderWriterCustomXMLTest extends TestCase { ZUGFeRDImporter zi = new ZUGFeRDImporter(TARGET_PDF); // Reading ZUGFeRD - String amount = null; - String bic = null; - String blz = null; - String iban = null; - String kto = null; - String holder = null; - String ref = null; - - if (zi.canParse()) { - zi.parse(); - amount = zi.getAmount(); - bic = zi.getBIC(); - blz = zi.getBLZ(); - iban = zi.getIBAN(); - kto = zi.getKTO(); - holder = zi.getHolder(); - ref = zi.getForeignReference(); - } - - assertEquals(amount, "571.04"); - assertEquals(bic, "COBADEFFXXX"); - assertEquals(blz, "41441604"); - assertEquals(iban, "DE88 2008 0000 0970 3757 00"); - assertEquals(kto, "44421800"); - assertEquals(holder, "Bei Spiel GmbH"); - assertEquals(ref, "RE-20170509/505"); + assertEquals(zi.getAmount(), "571.04"); + assertEquals(zi.getBIC(), "COBADEFFXXX"); + assertEquals(zi.getBLZ(), "41441604"); + assertEquals(zi.getIBAN(), "DE88 2008 0000 0970 3757 00"); + assertEquals(zi.getKTO(), "44421800"); + assertEquals(zi.getHolder(), "Bei Spiel GmbH"); + assertEquals(zi.getForeignReference(), "RE-20170509/505"); } } diff --git a/src/test/java/org/mustangproject/ZUGFeRD/MustangReaderWriterEdgeTest.java b/src/test/java/org/mustangproject/ZUGFeRD/MustangReaderWriterEdgeTest.java index 6af0cfe8..01b99312 100644 --- a/src/test/java/org/mustangproject/ZUGFeRD/MustangReaderWriterEdgeTest.java +++ b/src/test/java/org/mustangproject/ZUGFeRD/MustangReaderWriterEdgeTest.java @@ -340,36 +340,14 @@ public class MustangReaderWriterEdgeTest extends TestCase implements IZUGFeRDExp ZUGFeRDImporter zi = new ZUGFeRDImporter(inputStream); // Reading ZUGFeRD - String amount = null; - String bic = null; - String blz = null; - String iban = null; - String kto = null; - String holder = null; - String ref = null; - String dueDate = null; - - if (zi.canParse()) { - zi.parse(); - amount = zi.getAmount(); - bic = zi.getBIC(); - blz = zi.getBLZ(); - iban = zi.getIBAN(); - kto = zi.getKTO(); - holder = zi.getHolder(); - dueDate = zi.getDueDate(); - ref = zi.getForeignReference(); - } - - assertEquals(amount, "571.04"); - assertEquals(bic, getOwnBIC()); - assertEquals(blz, getOwnBLZ()); - assertEquals(iban, getOwnIBAN()); - assertEquals(kto, getOwnKto()); - assertEquals(holder, getOwnOrganisationName()); - - assertEquals(dueDate, "20170530"); - assertEquals(ref, getNumber()); + assertEquals(zi.getAmount(), "571.04"); + assertEquals(zi.getBIC(), getOwnBIC()); + assertEquals(zi.getBLZ(), getOwnBLZ()); + assertEquals(zi.getIBAN(), getOwnIBAN()); + assertEquals(zi.getKTO(), getOwnKto()); + assertEquals(zi.getHolder(), getOwnOrganisationName()); + assertEquals(zi.getDueDate(), "20170530"); + assertEquals(zi.getForeignReference(), getNumber()); } @@ -404,33 +382,13 @@ public class MustangReaderWriterEdgeTest extends TestCase implements IZUGFeRDExp ZUGFeRDImporter zi = new ZUGFeRDImporter(TARGET_PDF); // Reading ZUGFeRD - String amount = null; - String bic = null; - String blz = null; - String iban = null; - String kto = null; - String holder = null; - String ref = null; - - if (zi.canParse()) { - zi.parse(); - amount = zi.getAmount(); - bic = zi.getBIC(); - blz = zi.getBLZ(); - iban = zi.getIBAN(); - kto = zi.getKTO(); - holder = zi.getHolder(); - ref = zi.getForeignReference(); - } - - assertEquals(amount, "571.04"); - assertEquals(bic, getOwnBIC()); - assertEquals(blz, getOwnBLZ()); - assertEquals(iban, getOwnIBAN()); - assertEquals(kto, getOwnKto()); - assertEquals(holder, getOwnOrganisationName()); - assertEquals(ref, getNumber()); - + assertEquals(zi.getAmount(), "571.04"); + assertEquals(zi.getBIC(), getOwnBIC()); + assertEquals(zi.getBLZ(), getOwnBLZ()); + assertEquals(zi.getIBAN(), getOwnIBAN()); + assertEquals(zi.getKTO(), getOwnKto()); + assertEquals(zi.getHolder(), getOwnOrganisationName()); + assertEquals(zi.getForeignReference(), getNumber()); } } diff --git a/src/test/java/org/mustangproject/ZUGFeRD/MustangReaderWriterTest.java b/src/test/java/org/mustangproject/ZUGFeRD/MustangReaderWriterTest.java index 320e853d..c6f98cf7 100644 --- a/src/test/java/org/mustangproject/ZUGFeRD/MustangReaderWriterTest.java +++ b/src/test/java/org/mustangproject/ZUGFeRD/MustangReaderWriterTest.java @@ -359,7 +359,6 @@ public class MustangReaderWriterTest extends TestCase implements IZUGFeRDExporta String ref = null; if (zi.canParse()) { - zi.parse(); amount = zi.getAmount(); blz = zi.getBLZ(); bic = zi.getBIC(); @@ -517,29 +516,12 @@ public class MustangReaderWriterTest extends TestCase implements IZUGFeRDExporta ZUGFeRDImporter zi = new ZUGFeRDImporter(TARGET_PDF); // Reading ZUGFeRD - String amount = null; - String bic = null; - String iban = null; - String kto = null; - String holder = null; - String ref = null; - if (zi.canParse()) { - zi.parse(); - amount = zi.getAmount(); - bic = zi.getBIC(); - iban = zi.getIBAN(); - kto = zi.getKTO(); - holder = zi.getHolder(); - ref = zi.getForeignReference(); - } - - assertEquals(amount, "571.04"); - assertEquals(bic, getOwnBIC()); - assertEquals(iban, getOwnIBAN()); - assertEquals(kto, getOwnKto()); - assertEquals(holder, getOwnOrganisationName()); - assertEquals(ref, getNumber()); - + assertEquals(zi.getAmount(), "571.04"); + assertEquals(zi.getBIC(), getOwnBIC()); + assertEquals(zi.getIBAN(), getOwnIBAN()); + assertEquals(zi.getKTO(), getOwnKto()); + assertEquals(zi.getHolder(), getOwnOrganisationName()); + assertEquals(zi.getForeignReference(), getNumber()); } /** From 1b015d3cee6db8af7cf8c3178beaeb24854b5cfb Mon Sep 17 00:00:00 2001 From: Andre Kemper Date: Tue, 9 Apr 2019 23:35:44 +0200 Subject: [PATCH 4/4] merge master --- .../ZUGFeRD/MustangReaderWriterTest.java | 35 +++++-------------- 1 file changed, 9 insertions(+), 26 deletions(-) diff --git a/src/test/java/org/mustangproject/ZUGFeRD/MustangReaderWriterTest.java b/src/test/java/org/mustangproject/ZUGFeRD/MustangReaderWriterTest.java index e2fdc9f1..066863c3 100644 --- a/src/test/java/org/mustangproject/ZUGFeRD/MustangReaderWriterTest.java +++ b/src/test/java/org/mustangproject/ZUGFeRD/MustangReaderWriterTest.java @@ -514,34 +514,17 @@ public class MustangReaderWriterTest extends TestCase implements IZUGFeRDExporta } // now check the contents (like MustangReaderTest) - ZUGFeRDImporter zi = new ZUGFeRDImporter(); - zi.extract(TARGET_PDF); + ZUGFeRDImporter zi = new ZUGFeRDImporter(TARGET_PDF); + // Reading ZUGFeRD - - String amount = null; - String bic = null; - String iban = null; - String kto = null; - String holder = null; - String ref = null; - if (zi.canParse()) { - zi.parse(); - amount = zi.getAmount(); - bic = zi.getBIC(); - iban = zi.getIBAN(); - kto = zi.getKTO(); - holder = zi.getHolder(); - ref = zi.getForeignReference(); - } - - assertEquals(amount, "571.04"); - assertEquals(bic, getOwnBIC()); - assertEquals(iban, getOwnIBAN()); - assertEquals(kto, getOwnKto()); - assertEquals(holder, getOwnOrganisationName()); - assertEquals(ref, getNumber()); - + assertEquals(zi.getAmount(), "571.04"); + assertEquals(zi.getBIC(), getOwnBIC()); + assertEquals(zi.getIBAN(), getOwnIBAN()); + assertEquals(zi.getKTO(), getOwnKto()); + assertEquals(zi.getHolder(), getOwnOrganisationName()); + assertEquals(zi.getForeignReference(), getNumber()); } + /* public void testFXExport() throws Exception {