Simplified removing new line characters

This commit is contained in:
dankito 2020-05-20 20:53:54 +02:00
parent 96454a742d
commit 08b21980f9
1 changed files with 10 additions and 9 deletions

View File

@ -74,6 +74,7 @@ open class Mt940Parser : IMt940Parser {
protected open fun splitIntoSingleAccountStatements(mt940String: String): List<String> { protected open fun splitIntoSingleAccountStatements(mt940String: String): List<String> {
return mt940String.split("\n-") return mt940String.split("\n-")
.map { it.replace("\n", "").replace("\r", "") }
} }
@ -102,7 +103,7 @@ open class Mt940Parser : IMt940Parser {
while (matcher.find()) { while (matcher.find()) {
if (lastMatchEnd > 0) { if (lastMatchEnd > 0) {
val previousStatement = accountStatementString.substring(lastMatchEnd, matcher.start()).replace("\n", "") val previousStatement = accountStatementString.substring(lastMatchEnd, matcher.start())
result.add(Pair(lastMatchedCode, previousStatement)) result.add(Pair(lastMatchedCode, previousStatement))
} }
@ -111,7 +112,7 @@ open class Mt940Parser : IMt940Parser {
} }
if (lastMatchEnd > 0) { if (lastMatchEnd > 0) {
val previousStatement = accountStatementString.substring(lastMatchEnd, accountStatementString.length).replace("\n", "") val previousStatement = accountStatementString.substring(lastMatchEnd, accountStatementString.length)
result.add(Pair(lastMatchedCode, previousStatement)) result.add(Pair(lastMatchedCode, previousStatement))
} }
@ -199,7 +200,7 @@ open class Mt940Parser : IMt940Parser {
val turnover = parseTurnover(pair.second) val turnover = parseTurnover(pair.second)
val nextPair = if (index < fieldsByCode.size - 1) fieldsByCode.get(index + 1) else null val nextPair = if (index < fieldsByCode.size - 1) fieldsByCode.get(index + 1) else null
val details = if (nextPair?.first == TransactionDetailsCode) parseNullableTransactionDetails(nextPair?.second) else null val details = if (nextPair?.first == TransactionDetailsCode) parseNullableTransactionDetails(nextPair.second) else null
transactions.add(Transaction(turnover, details)) transactions.add(Transaction(turnover, details))
} }
@ -254,24 +255,24 @@ open class Mt940Parser : IMt940Parser {
customerReference, bankReference) customerReference, bankReference)
} }
protected open fun parseNullableTransactionDetails(fieldValue: String): TransactionDetails? { protected open fun parseNullableTransactionDetails(detailsString: String): TransactionDetails? {
try { try {
val details = parseTransactionDetails(fieldValue) val details = parseTransactionDetails(detailsString)
mapUsage(details) mapUsage(details)
return details return details
} catch (e: Exception) { } catch (e: Exception) {
log.error("Could not parse transaction details from field value '$fieldValue'", e) log.error("Could not parse transaction details from field value '$detailsString'", e)
} }
return null return null
} }
private fun parseTransactionDetails(fieldValue: String): TransactionDetails { private fun parseTransactionDetails(detailsString: String): TransactionDetails {
// e. g. starts with 0 -> Inlandszahlungsverkehr, starts with '3' -> Wertpapiergeschäft // e. g. starts with 0 -> Inlandszahlungsverkehr, starts with '3' -> Wertpapiergeschäft
// see Finanzdatenformate p. 209 - 215 // see Finanzdatenformate p. 209 - 215
val geschaeftsvorfallCode = fieldValue.substring(0, 2) // TODO: may map val geschaeftsvorfallCode = detailsString.substring(0, 2) // TODO: may map
val usage = StringBuilder("") val usage = StringBuilder("")
val otherPartyName = StringBuilder("") val otherPartyName = StringBuilder("")
@ -281,7 +282,7 @@ open class Mt940Parser : IMt940Parser {
var primaNotaNumber: String? = null var primaNotaNumber: String? = null
var textKeySupplement: String? = null var textKeySupplement: String? = null
fieldValue.replace("\r", "").replace("\n", "").substring(3).split('?').forEach { subField -> detailsString.substring(3).split('?').forEach { subField ->
if (subField.isNotEmpty()) { if (subField.isNotEmpty()) {
val fieldCode = subField.substring(0, 2).toInt() val fieldCode = subField.substring(0, 2).toInt()
val fieldValue = subField.substring(2) val fieldValue = subField.substring(2)