use Constructores with parameters
This commit is contained in:
@@ -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() : "";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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("<?xpacket begin=\"\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\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;
|
||||
|
||||
Reference in New Issue
Block a user