Skip to content

Commit

Permalink
Fix xsd:dayTimeDuration validation
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcelKonrad authored and manuelprinz committed Jan 8, 2025
1 parent 24817b0 commit c7466bf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,9 @@ private val YEAR_MONTH_DURATION_MATCHER =

fun String.isValidYearMonthDuration(): Boolean = YEAR_MONTH_DURATION_MATCHER.test(this)

private const val DU_TIME_FRAG = """(T((\d+H)(\d+M)?(\d+(\.\d+)?S)?|(\d+M)(\d+(\.\d+)?S)?|(\d+(\.\d+)?S)))"""
private val DAY_TIME_DURATION_MATCHER =
Pattern.compile("""^-?P((\d+H)(\d+M)?(\d+(\.\d+)?S)?|(\d+M)(\d+(\.\d+)?S)?|(\d+(\.\d+)?S))$""").asMatchPredicate()
Pattern.compile("""^-?P((\d+D)$DU_TIME_FRAG?|$DU_TIME_FRAG$)""").asMatchPredicate()

fun String.isValidDayTimeDuration(): Boolean = DAY_TIME_DURATION_MATCHER.test(this)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -577,38 +577,46 @@ internal class XSDValidationTest {
@JvmStatic
fun validDayTimeDuration(): Iterator<String> = iterator {
val signs = listOf("", "-")
val ds = listOf(null, "0", "00", "2", "17", "25")
val hs = listOf(null, "0", "00", "2", "17", "25")
val ms = listOf(null, "0", "00", "2", "41", "65")
val ss = listOf(null, "0", "00", "2", "41", "65")
val fs = listOf(null, "0", "00", "2", "54", "78956")

for (sign in signs) {
for (h in hs) {
for (m in ms) {
for (s in ss) {
for (f in fs) {
val duration = buildString {
append(sign)
append("P")
if (h != null) {
append(h)
append("H")
}
if (m != null) {
append(m)
append("M")
}
if (s != null) {
append(s)
if (f != null) {
append(".")
append(f)
for (d in ds) {
for (h in hs) {
for (m in ms) {
for (s in ss) {
for (f in fs) {
val duration = buildString {
append(sign)
append("P")
if (d != null) {
append(d)
append("D")
}
append("T")
if (h != null) {
append(h)
append("H")
}
if (m != null) {
append(m)
append("M")
}
if (s != null) {
append(s)
if (f != null) {
append(".")
append(f)
}
append("S")
}
append("S")
}
}
if (!duration.endsWith("P")) {
yield(duration)
if (!duration.endsWith("PT") && !duration.endsWith("DT")) {
yield(duration)
}
}
}
}
Expand Down

0 comments on commit c7466bf

Please sign in to comment.