Skip to content

Commit 4ca104d

Browse files
feat(watermarker): add Trendmark validation option for increased robustness (#214)
1 parent 821966b commit 4ca104d

3 files changed

Lines changed: 33 additions & 4 deletions

File tree

docs/docs/03-usage/10-watermarker/index.mdx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,14 @@ returned
6464
= [**true**/false]: when true only the most frequent Watermark is returned
6565
- if multiple Watermarks appear with the same (highest) frequency a warning is returned with the Watermarks
6666
- this is separate from the squashing above, duplicates of the most frequent Watermark(s) are still returned if squash = false
67-
- [`ErrorOnInvalidUTF8`](https://github.com/FraunhoferISST/TREND/blob/eb5f3b62bc31a63f985fa87c01522281adcb7c3e/watermarker/src/commonMain/kotlin/watermarks/TextWatermark.kt#L123)
67+
- [`errorOnInvalidUTF8`](https://github.com/FraunhoferISST/TREND/blob/eb5f3b62bc31a63f985fa87c01522281adcb7c3e/watermarker/src/commonMain/kotlin/watermarks/TextWatermark.kt#L123)
6868
= [true/**false**]: For functions returning TextWatermarks only, when true throws a
6969
CharacterCodingException when encountering malformed UTF8 Bytes
70+
- [`validateAll`](https://github.com/FraunhoferISST/TREND/blob/8b47f1a373769656506a615436b558e362a8952c/watermarker/src/commonMain/kotlin/watermarks/Trendmark.kt#L56)
71+
= [**true**/false]: For functions returning Trendmarks only, when true calls the
72+
Trendmark validate() function, which will check for valid characteristics depending on the
73+
Trendmark type. See the [Trendmark](../../development/watermarker/trendmark) section for more
74+
details.
7075

7176
## Kotlin Library: Special Characteristics
7277
Kotlin uses so-called

watermarker/src/commonMain/kotlin/Watermarker.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ open class Watermarker {
178178
*
179179
* When [squash] is true: watermarks with the same content are merged.
180180
* When [singleWatermark] is true: only the most frequent watermark is returned.
181+
* When [validateAll] is true: All resulting Trendmarks are validated to check for errors.
181182
*
182183
* Returns a warning if some watermarks could not be converted to Trendmarks.
183184
* Returns an error if no watermark could be converted to a Trendmark.
@@ -186,8 +187,19 @@ open class Watermarker {
186187
text: String,
187188
squash: Boolean = true,
188189
singleWatermark: Boolean = true,
189-
): Result<List<Trendmark>> =
190-
textGetWatermarks(text, squash, singleWatermark).toTrendmarks("$SOURCE.textGetTrendmarks")
190+
validateAll: Boolean = true,
191+
): Result<List<Trendmark>> {
192+
val result =
193+
textGetWatermarks(text, squash, singleWatermark)
194+
.toTrendmarks("$SOURCE.textGetTrendmarks")
195+
if (validateAll && result.hasValue && result.value!!.isNotEmpty()) {
196+
for (trendmark in result.value) {
197+
val validationStatus = trendmark.validate()
198+
result.appendStatus(validationStatus)
199+
}
200+
}
201+
return result
202+
}
191203

192204
/**
193205
* Returns all watermarks in [text] as TextWatermarks.

watermarker/src/jvmMain/kotlin/JvmWatermarker.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ class JvmWatermarker : Watermarker() {
215215
* When [fileType] is null the type is taken from [source]'s extension.
216216
* When [squash] is true: watermarks with the same content are merged.
217217
* When [singleWatermark] is true: only the most frequent watermark is returned.
218+
* When [validateAll] is true: All resulting Trendmarks are validated to check for errors.
218219
*
219220
* Returns a warning if some watermarks could not be converted to Trendmarks.
220221
* Returns an error if no watermark could be converted to a Trendmark.
@@ -224,8 +225,18 @@ class JvmWatermarker : Watermarker() {
224225
fileType: String? = null,
225226
squash: Boolean = true,
226227
singleWatermark: Boolean = true,
228+
validateAll: Boolean = true,
227229
): Result<List<Trendmark>> {
228-
return getWatermarks(source, fileType, squash, singleWatermark).toTrendmarks(SOURCE)
230+
val result = getWatermarks(source, fileType, squash, singleWatermark).toTrendmarks(SOURCE)
231+
232+
if (validateAll && result.hasValue && result.value!!.isNotEmpty()) {
233+
for (trendmark in result.value) {
234+
val validationStatus = trendmark.validate()
235+
result.appendStatus(validationStatus)
236+
}
237+
}
238+
239+
return result
229240
}
230241

231242
/**
@@ -260,6 +271,7 @@ class JvmWatermarker : Watermarker() {
260271
/**
261272
* Removes all watermarks in [source] and returns them.
262273
*
274+
* When [singleWatermark] is true: only the most frequent watermark is returned.
263275
* When [fileType] is null the type is taken from [source]'s extension.
264276
*/
265277
fun removeWatermarks(

0 commit comments

Comments
 (0)