Skip to content

Commit

Permalink
XmlUtils.newPullParser: enable FEATURE_RELAXED if available
Browse files Browse the repository at this point in the history
  • Loading branch information
rfc2822 committed Apr 17, 2024
1 parent 6a0b597 commit 13ccb09
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/main/kotlin/at/bitfire/dav4jvm/Property.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ interface Property {
log.log(Level.WARNING, "Ignoring invalid property", e)
}
}

eventType = parser.next()
}

Expand Down
31 changes: 22 additions & 9 deletions src/main/kotlin/at/bitfire/dav4jvm/XmlUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

package at.bitfire.dav4jvm

import at.bitfire.dav4jvm.exception.DavException
import at.bitfire.dav4jvm.exception.InvalidPropertyException
import org.xmlpull.v1.XmlPullParser
import org.xmlpull.v1.XmlPullParserException
Expand All @@ -15,18 +16,30 @@ import java.io.IOException

object XmlUtils {

private val factory: XmlPullParserFactory
init {
private const val FEATURE_RELAXED = "http://xmlpull.org/v1/doc/features.html#relaxed"

private val relaxedFactory =
XmlPullParserFactory.newInstance().apply {
isNamespaceAware = true
setFeature(FEATURE_RELAXED, true)
}
private val standardFactory: XmlPullParserFactory =
XmlPullParserFactory.newInstance().apply {
isNamespaceAware = true
}

fun newPullParser(): XmlPullParser =
try {
factory = XmlPullParserFactory.newInstance()
factory.isNamespaceAware = true
} catch (e: XmlPullParserException) {
throw RuntimeException("Couldn't create XmlPullParserFactory", e)
relaxedFactory.newPullParser()
} catch (_: XmlPullParserException) {
// FEATURE_RELAXED may not be supported, try without it
null
}
}
?: standardFactory.newPullParser()
?: throw DavException("Couldn't create XML parser")

fun newPullParser() = factory.newPullParser()!!
fun newSerializer() = factory.newSerializer()!!
fun newSerializer(): XmlSerializer = standardFactory.newSerializer()
?: throw DavException("Couldn't create XML serializer")


@Throws(IOException::class, XmlPullParserException::class)
Expand Down

0 comments on commit 13ccb09

Please sign in to comment.