use Constructores with parameters

This commit is contained in:
Andre Kemper
2019-04-09 07:03:56 +02:00
parent 57f0e27186
commit e60105f8b7
7 changed files with 74 additions and 63 deletions

View File

@@ -44,11 +44,13 @@ import java.io.*;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Scanner;
import java.util.logging.Logger; import java.util.logging.Logger;
//root.setNamespace(Namespace.getNamespace("http://www.energystar.gov/manageBldgs/req")); //root.setNamespace(Namespace.getNamespace("http://www.energystar.gov/manageBldgs/req"));
public class ZUGFeRDImporter { public class ZUGFeRDImporter {
/* /*
* call extract(importFilename). containsMeta() will return if ZUGFeRD data has * call extract(importFilename). containsMeta() will return if ZUGFeRD data has
* been found, afterwards you can call getBIC(), getIBAN() etc. * been found, afterwards you can call getBIC(), getIBAN() etc.
@@ -77,42 +79,28 @@ 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 extractAttempt = false;
private boolean parsed = false; private boolean parsed = false;
private String xmpString = null; // XMP metadata private String xmpString = null; // XMP metadata
private static final Logger LOG = Logger.getLogger(ZUGFeRDImporter.class.getName()); private static final Logger LOG = Logger.getLogger(ZUGFeRDImporter.class.getName());
/** public ZUGFeRDImporter(String pdfFilename) {
* 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) {
try { try {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(pdfFilename)); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(pdfFilename));
extractLowLevel(bis); extractLowLevel(bis);
bis.close(); bis.close();
} catch (IOException ioe) { } catch (IOException e) {
ioe.printStackTrace(); e.printStackTrace();
throw new ZUGFeRDExportException(e);
} }
} }
static String convertStreamToString(java.io.InputStream is) { public ZUGFeRDImporter(InputStream pdfStream) {
// source https://stackoverflow.com/questions/309424/how-do-i-read-convert-an-inputstream-into-a-string-in-java referring to try {
// https://community.oracle.com/blogs/pat/2004/10/23/stupid-scanner-tricks extractLowLevel(pdfStream);
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); } catch (IOException e) {
return s.hasNext() ? s.next() : ""; e.printStackTrace();
} throw new ZUGFeRDExportException(e);
}
/**
* get xmp metadata of the PDF, null if not available
*
* @return string
*/
public String getXMP() {
return xmpString;
} }
/** /**
@@ -121,9 +109,7 @@ public class ZUGFeRDImporter {
* *
* @param pdfStream a inputstream of a pdf file * @param pdfStream a inputstream of a pdf file
*/ */
public void extractLowLevel(InputStream pdfStream) throws IOException { private void extractLowLevel(InputStream pdfStream) throws IOException {
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());
@@ -131,7 +117,8 @@ public class ZUGFeRDImporter {
InputStream XMP = doc.getDocumentCatalog().getMetadata().exportXMPMetadata(); InputStream XMP = doc.getDocumentCatalog().getMetadata().exportXMPMetadata();
xmpString = convertStreamToString(XMP); xmpString = convertStreamToString(XMP);
etn = names.getEmbeddedFiles();
PDEmbeddedFilesNameTreeNode etn = names.getEmbeddedFiles();
if (etn == null) { if (etn == null) {
return; return;
} }
@@ -200,9 +187,6 @@ public class ZUGFeRDImporter {
DocumentBuilder builder = null; DocumentBuilder builder = null;
Document document = null; Document document = null;
if (!extractAttempt) {
throw new RuntimeException("extract() or extractLowLevel() must be used before parsing.");
}
if (!containsMeta) { if (!containsMeta) {
throw new RuntimeException("No suitable data/ZUGFeRD file could be found."); throw new RuntimeException("No suitable data/ZUGFeRD file could be found.");
} }
@@ -376,6 +360,16 @@ public class ZUGFeRDImporter {
parsed = true; 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 * @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$ return (meta != null) && (meta.length() > 0) && ((meta.contains("SpecifiedExchangedDocumentContext") //$NON-NLS-1$
/* ZF1 */ || meta.contains("ExchangedDocumentContext") /* ZF2 */)); /* 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() : "";
}
} }

View File

@@ -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 {
}

View File

@@ -49,9 +49,8 @@ public class FileChecker {
if ((!isPDF) && (!thisRun.shallIgnoreFileExt())) { if ((!isPDF) && (!thisRun.shallIgnoreFileExt())) {
return false; return false;
} }
ZUGFeRDImporter zi = new ZUGFeRDImporter(); ZUGFeRDImporter zi = new ZUGFeRDImporter(filename);
try { try {
zi.extract(filename);
if (zi.canParse()) { if (zi.canParse()) {
thisRun.incZUGFeRDCount(); thisRun.incZUGFeRDCount();
return true; return true;

View File

@@ -406,8 +406,7 @@ public class Toecount {
ensureFileNotExists(xmlName); ensureFileNotExists(xmlName);
// All params are good! continue... // All params are good! continue...
ZUGFeRDImporter zi = new ZUGFeRDImporter(); ZUGFeRDImporter zi = new ZUGFeRDImporter(pdfName);
zi.extract(pdfName);
byte[] XMLContent = zi.getRawXML(); byte[] XMLContent = zi.getRawXML();
if (XMLContent == null) { if (XMLContent == null) {
System.err.println("No ZUGFeRD XML found in PDF file"); System.err.println("No ZUGFeRD XML found in PDF file");

View File

@@ -272,10 +272,9 @@ public class MustangReaderWriterCustomXMLTest extends TestCase {
} }
// now check the contents (like MustangReaderTest) // now check the contents (like MustangReaderTest)
ZUGFeRDImporter zi = new ZUGFeRDImporter(); ZUGFeRDImporter zi = new ZUGFeRDImporter(TARGET_PDF);
zi.extract(TARGET_PDF);
// Reading ZUGFeRD
// Reading ZUGFeRD
String amount = null; String amount = null;
String bic = null; String bic = null;
String blz = null; String blz = null;
@@ -474,10 +473,9 @@ public class MustangReaderWriterCustomXMLTest extends TestCase {
} }
// now check the contents (like MustangReaderTest) // now check the contents (like MustangReaderTest)
ZUGFeRDImporter zi = new ZUGFeRDImporter(); ZUGFeRDImporter zi = new ZUGFeRDImporter(TARGET_PDF);
zi.extract(TARGET_PDF);
// Reading ZUGFeRD
// Reading ZUGFeRD
String amount = null; String amount = null;
String bic = null; String bic = null;
String blz = null; String blz = null;

View File

@@ -336,10 +336,10 @@ public class MustangReaderWriterEdgeTest extends TestCase implements IZUGFeRDExp
*/ */
public void testAImport() throws IOException { public void testAImport() throws IOException {
ZUGFeRDImporter zi = new ZUGFeRDImporter(); InputStream inputStream = this.getClass().getResourceAsStream("/MustangGnuaccountingBeispielRE-20170509_505.pdf");
zi.extractLowLevel(this.getClass().getResourceAsStream("/MustangGnuaccountingBeispielRE-20170509_505.pdf")); ZUGFeRDImporter zi = new ZUGFeRDImporter(inputStream);
// Reading ZUGFeRD
// Reading ZUGFeRD
String amount = null; String amount = null;
String bic = null; String bic = null;
String blz = null; String blz = null;
@@ -401,10 +401,9 @@ public class MustangReaderWriterEdgeTest extends TestCase implements IZUGFeRDExp
} }
// now check the contents (like MustangReaderTest) // now check the contents (like MustangReaderTest)
ZUGFeRDImporter zi = new ZUGFeRDImporter(); ZUGFeRDImporter zi = new ZUGFeRDImporter(TARGET_PDF);
zi.extract(TARGET_PDF);
// Reading ZUGFeRD
// Reading ZUGFeRD
String amount = null; String amount = null;
String bic = null; String bic = null;
String blz = null; String blz = null;

View File

@@ -346,14 +346,10 @@ public class MustangReaderWriterTest extends TestCase implements IZUGFeRDExporta
*/ */
public void testAImport() throws IOException { public void testAImport() throws IOException {
ZUGFeRDImporter zi = new ZUGFeRDImporter(); InputStream inputStream = this.getClass().getResourceAsStream("/MustangGnuaccountingBeispielRE-20170509_505.pdf");
try (InputStream inputStream = this.getClass() ZUGFeRDImporter zi = new ZUGFeRDImporter(inputStream);
.getResourceAsStream("/MustangGnuaccountingBeispielRE-20170509_505.pdf")) {
zi.extractLowLevel(inputStream);
}
// Reading ZUGFeRD // Reading ZUGFeRD
String amount = null; String amount = null;
String blz = null; String blz = null;
String bic = null; String bic = null;
@@ -384,14 +380,10 @@ public class MustangReaderWriterTest extends TestCase implements IZUGFeRDExporta
} }
public void testForeignImport() throws IOException { 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 // Reading ZUGFeRD
String amount = zi.getAmount(); String amount = zi.getAmount();
assertEquals("<?xpacket begin=\"\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n" + assertEquals("<?xpacket begin=\"\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n" +
@@ -522,10 +514,9 @@ public class MustangReaderWriterTest extends TestCase implements IZUGFeRDExporta
} }
// now check the contents (like MustangReaderTest) // now check the contents (like MustangReaderTest)
ZUGFeRDImporter zi = new ZUGFeRDImporter(); ZUGFeRDImporter zi = new ZUGFeRDImporter(TARGET_PDF);
zi.extract(TARGET_PDF);
// Reading ZUGFeRD
// Reading ZUGFeRD
String amount = null; String amount = null;
String bic = null; String bic = null;
String iban = null; String iban = null;