Added overloads that catch exceptions and return null on error

This commit is contained in:
dankito 2024-11-28 02:04:20 +01:00
parent 5abfa0b641
commit a1eea168e9
1 changed files with 28 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package net.codinux.invoicing.reader
import net.codinux.invoicing.mapper.MustangMapper import net.codinux.invoicing.mapper.MustangMapper
import net.codinux.invoicing.model.Invoice import net.codinux.invoicing.model.Invoice
import net.codinux.log.logger
import org.mustangproject.ZUGFeRD.ZUGFeRDInvoiceImporter import org.mustangproject.ZUGFeRD.ZUGFeRDInvoiceImporter
import java.io.File import java.io.File
import java.io.InputStream 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 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 extractFromXml(stream: InputStream) = extractFromXml(stream.reader().readText())
open fun extractFromXmlOrNull(xml: String) = orNull { extractFromXml(xml) }
open fun extractFromXml(xml: String): Invoice { open fun extractFromXml(xml: String): Invoice {
val importer = ZUGFeRDInvoiceImporter() // XRechnungImporter only reads properties but not to a Invoice object val importer = ZUGFeRDInvoiceImporter() // XRechnungImporter only reads properties but not to a Invoice object
importer.fromXML(xml) importer.fromXML(xml)
@ -28,16 +37,26 @@ open class EInvoiceReader(
return extractInvoice(importer) return extractInvoice(importer)
} }
open fun extractFromPdfOrNull(pdfFile: File) = orNull { extractFromPdf(pdfFile) }
open fun extractFromPdf(pdfFile: File) = pdfFile.inputStream().use { extractFromPdf(it) } open fun extractFromPdf(pdfFile: File) = pdfFile.inputStream().use { extractFromPdf(it) }
open fun extractFromPdfOrNull(stream: InputStream) = orNull { extractFromPdf(stream) }
open fun extractFromPdf(stream: InputStream): Invoice { open fun extractFromPdf(stream: InputStream): Invoice {
val importer = ZUGFeRDInvoiceImporter(stream) val importer = ZUGFeRDInvoiceImporter(stream)
return extractInvoice(importer) return extractInvoice(importer)
} }
open fun extractXmlFromPdfOrNull(pdfFile: File) = orNull { extractXmlFromPdf(pdfFile) }
open fun extractXmlFromPdf(pdfFile: File) = pdfFile.inputStream().use { extractXmlFromPdf(it) } open fun extractXmlFromPdf(pdfFile: File) = pdfFile.inputStream().use { extractXmlFromPdf(it) }
open fun extractXmlFromPdfOrNull(stream: InputStream) = orNull { extractXmlFromPdf(stream) }
open fun extractXmlFromPdf(stream: InputStream): String { open fun extractXmlFromPdf(stream: InputStream): String {
val importer = ZUGFeRDInvoiceImporter(stream) val importer = ZUGFeRDInvoiceImporter(stream)
@ -58,4 +77,13 @@ open class EInvoiceReader(
return mapper.mapToInvoice(invoice) return mapper.mapToInvoice(invoice)
} }
protected open fun <T> orNull(action: () -> T): T? =
try {
action()
} catch (e: Throwable) {
log.debug(e) { "Action caused an exception, but orNull() was called" }
null
}
} }