diff --git a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/reader/EInvoiceReader.kt b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/reader/EInvoiceReader.kt index 1380e0a..51ba742 100644 --- a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/reader/EInvoiceReader.kt +++ b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/reader/EInvoiceReader.kt @@ -2,6 +2,7 @@ package net.codinux.invoicing.reader import net.codinux.invoicing.mapper.MustangMapper import net.codinux.invoicing.model.Invoice +import net.codinux.log.logger import org.mustangproject.ZUGFeRD.ZUGFeRDInvoiceImporter import java.io.File import java.io.InputStream @@ -16,11 +17,19 @@ open class EInvoiceReader( ) } + private val log by logger() + + + open fun extractFromXmlOrNull(xmlFile: File) = orNull { extractFromXml(xmlFile) } open fun extractFromXml(xmlFile: File) = xmlFile.inputStream().use { extractFromXml(it) } + open fun extractFromXmlOrNull(stream: InputStream) = orNull { extractFromXml(stream) } + open fun extractFromXml(stream: InputStream) = extractFromXml(stream.reader().readText()) + open fun extractFromXmlOrNull(xml: String) = orNull { extractFromXml(xml) } + open fun extractFromXml(xml: String): Invoice { val importer = ZUGFeRDInvoiceImporter() // XRechnungImporter only reads properties but not to a Invoice object importer.fromXML(xml) @@ -28,16 +37,26 @@ open class EInvoiceReader( return extractInvoice(importer) } + + open fun extractFromPdfOrNull(pdfFile: File) = orNull { extractFromPdf(pdfFile) } + open fun extractFromPdf(pdfFile: File) = pdfFile.inputStream().use { extractFromPdf(it) } + open fun extractFromPdfOrNull(stream: InputStream) = orNull { extractFromPdf(stream) } + open fun extractFromPdf(stream: InputStream): Invoice { val importer = ZUGFeRDInvoiceImporter(stream) return extractInvoice(importer) } + + open fun extractXmlFromPdfOrNull(pdfFile: File) = orNull { extractXmlFromPdf(pdfFile) } + open fun extractXmlFromPdf(pdfFile: File) = pdfFile.inputStream().use { extractXmlFromPdf(it) } + open fun extractXmlFromPdfOrNull(stream: InputStream) = orNull { extractXmlFromPdf(stream) } + open fun extractXmlFromPdf(stream: InputStream): String { val importer = ZUGFeRDInvoiceImporter(stream) @@ -58,4 +77,13 @@ open class EInvoiceReader( return mapper.mapToInvoice(invoice) } + + protected open fun orNull(action: () -> T): T? = + try { + action() + } catch (e: Throwable) { + log.debug(e) { "Action caused an exception, but orNull() was called" } + null + } + } \ No newline at end of file