diff --git a/fints4javaLib/src/main/kotlin/net/dankito/fints/messages/Nachrichtenteil.kt b/fints4javaLib/src/main/kotlin/net/dankito/fints/messages/Nachrichtenteil.kt index 9d39881e..9aff09ef 100644 --- a/fints4javaLib/src/main/kotlin/net/dankito/fints/messages/Nachrichtenteil.kt +++ b/fints4javaLib/src/main/kotlin/net/dankito/fints/messages/Nachrichtenteil.kt @@ -1,8 +1,28 @@ package net.dankito.fints.messages +import net.dankito.fints.util.MessageUtils -abstract class Nachrichtenteil { + +abstract class Nachrichtenteil(protected val messageUtils: MessageUtils = MessageUtils()) { abstract fun format(): String + + open fun maskMessagePart(messagePart: String, separator: String): String { + + var maskedMessagePart = messagePart + val binaryDataRanges = messageUtils.findBinaryDataRanges(messagePart) + + val unmaskedMaskingCharacterIndices = messageUtils.findSeparatorIndices(maskedMessagePart, Separators.MaskingCharacter, binaryDataRanges) + .filter { messageUtils.doesNotMaskSeparatorOrMaskingCharacter(it, maskedMessagePart) } + + maskedMessagePart = messageUtils + .maskCharacterAtIndices(maskedMessagePart, Separators.MaskingCharacter, unmaskedMaskingCharacterIndices) + + + val separatorIndices = messageUtils.findSeparatorIndices(maskedMessagePart, separator, binaryDataRanges) + + return messageUtils.maskCharacterAtIndices(maskedMessagePart, separator, separatorIndices) + } + } \ No newline at end of file diff --git a/fints4javaLib/src/main/kotlin/net/dankito/fints/messages/Separators.kt b/fints4javaLib/src/main/kotlin/net/dankito/fints/messages/Separators.kt index 783c182a..003f235d 100644 --- a/fints4javaLib/src/main/kotlin/net/dankito/fints/messages/Separators.kt +++ b/fints4javaLib/src/main/kotlin/net/dankito/fints/messages/Separators.kt @@ -11,6 +11,10 @@ class Separators { const val DataElementsSeparator = ":" const val MaskingCharacter = "?" + + val AllSeparators = listOf(DataElementsSeparator, DataElementGroupsSeparator, SegmentSeparator) + + val AllSeparatorsAndMaskingCharacter = listOf(*AllSeparators.toTypedArray(), MaskingCharacter) } } \ No newline at end of file diff --git a/fints4javaLib/src/main/kotlin/net/dankito/fints/messages/datenelemente/basisformate/TextDatenelement.kt b/fints4javaLib/src/main/kotlin/net/dankito/fints/messages/datenelemente/basisformate/TextDatenelement.kt index ab240730..04ad4da0 100644 --- a/fints4javaLib/src/main/kotlin/net/dankito/fints/messages/datenelemente/basisformate/TextDatenelement.kt +++ b/fints4javaLib/src/main/kotlin/net/dankito/fints/messages/datenelemente/basisformate/TextDatenelement.kt @@ -2,6 +2,7 @@ package net.dankito.fints.messages.datenelemente.basisformate import net.dankito.fints.messages.Existenzstatus import net.dankito.fints.messages.HbciCharset +import net.dankito.fints.messages.Separators import net.dankito.fints.messages.datenelemente.Datenelement @@ -24,7 +25,7 @@ abstract class TextDatenelement(val value: String?, existenzstatus: Existenzstat } protected open fun formatValue(value: String): String { - return value // may overwritten in sub classes + return maskMessagePart(value, Separators.DataElementsSeparator) // may overwritten in sub classes } diff --git a/fints4javaLib/src/main/kotlin/net/dankito/fints/util/MessageUtils.kt b/fints4javaLib/src/main/kotlin/net/dankito/fints/util/MessageUtils.kt index e28182c9..cd7f5f5d 100644 --- a/fints4javaLib/src/main/kotlin/net/dankito/fints/util/MessageUtils.kt +++ b/fints4javaLib/src/main/kotlin/net/dankito/fints/util/MessageUtils.kt @@ -63,6 +63,16 @@ open class MessageUtils { return false } + open fun doesNotMaskSeparatorOrMaskingCharacter(maskingCharacterIndex: Int, messagePart: String): Boolean { + if (maskingCharacterIndex < messagePart.length - 1) { + val nextCharacter = messagePart[maskingCharacterIndex + 1] + + return Separators.AllSeparatorsAndMaskingCharacter.contains(nextCharacter.toString()) == false + } + + return true + } + open fun isInRange(index: Int, ranges: List): Boolean { for (range in ranges) { if (range.contains(index)) { @@ -73,4 +83,15 @@ open class MessageUtils { return false } + open fun maskCharacterAtIndices(unmaskedString: String, unmaskedCharacter: String, indices: List): String { + var maskedString = unmaskedString + + indices.sortedDescending().forEach { index -> + maskedString = maskedString.replaceRange(index, index + 1, Separators.MaskingCharacter + unmaskedCharacter + ) + } + + return maskedString + } + } \ No newline at end of file