Skip to content

Commit ccf65a2

Browse files
committed
Refactor parsers and data classes
- Move `PropStatParser` to object-based implementation - Update `MultiStatusParser` and `ResponseParser` to use callback directly - Remove redundant parameters from parsing functions
1 parent 32086b6 commit ccf65a2

File tree

4 files changed

+15
-18
lines changed

4 files changed

+15
-18
lines changed

src/main/kotlin/at/bitfire/dav4jvm/ktor/DavResource.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ open class DavResource(
744744
while (eventType != XmlPullParser.END_DOCUMENT) {
745745
if (eventType == XmlPullParser.START_TAG && parser.depth == 1)
746746
if (parser.propertyName() == WebDAV.MultiStatus) {
747-
return MultiStatusParser(location).parseResponse(parser, callback)
747+
return MultiStatusParser(location, callback).parseResponse(parser)
748748
// further <multistatus> elements are ignored
749749
}
750750

src/main/kotlin/at/bitfire/dav4jvm/ktor/MultiStatusParser.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ import org.xmlpull.v1.XmlPullParser
2424
* @param location location of the request (used to resolve possible relative `<href>` in responses)
2525
*/
2626
class MultiStatusParser(
27-
private val location: Url
27+
private val location: Url,
28+
private val callback: MultiResponseCallback
2829
) {
2930

30-
suspend fun parseResponse(parser: XmlPullParser, callback: MultiResponseCallback): List<Property> {
31+
suspend fun parseResponse(parser: XmlPullParser): List<Property> {
3132
val responseProperties = mutableListOf<Property>()
33+
val responseParser = ResponseParser(location, callback)
3234

3335
// <!ELEMENT multistatus (response*, responsedescription?,
3436
// sync-token?) >
@@ -38,7 +40,7 @@ class MultiStatusParser(
3840
if (eventType == XmlPullParser.START_TAG && parser.depth == depth + 1)
3941
when (parser.propertyName()) {
4042
WebDAV.Response ->
41-
ResponseParser(location).parseResponse(parser, callback)
43+
responseParser.parseResponse(parser)
4244
WebDAV.SyncToken ->
4345
XmlReader(parser).readText()?.let {
4446
responseProperties += SyncToken(it)

src/main/kotlin/at/bitfire/dav4jvm/ktor/PropStatParser.kt

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ import io.ktor.http.HttpStatusCode
1717
import org.xmlpull.v1.XmlPullParser
1818
import java.util.LinkedList
1919

20-
class PropStatParser {
20+
object PropStatParser {
21+
22+
private val ASSUMING_OK = HttpStatusCode(200, "Assuming OK")
2123

2224
fun parse(parser: XmlPullParser): PropStat {
2325
val depth = parser.depth
@@ -40,11 +42,4 @@ class PropStatParser {
4042
return PropStat(prop, status ?: ASSUMING_OK)
4143
}
4244

43-
44-
companion object {
45-
46-
private val ASSUMING_OK = HttpStatusCode(200, "Assuming OK")
47-
48-
}
49-
5045
}

src/main/kotlin/at/bitfire/dav4jvm/ktor/ResponseParser.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ import java.util.logging.Logger
2929
* @param location location of the request (used to resolve possible relative `<href>`)
3030
*/
3131
class ResponseParser(
32-
private val location: Url
32+
private val location: Url,
33+
private val callback: MultiResponseCallback
3334
) {
3435

3536
private val logger
@@ -46,13 +47,13 @@ class ResponseParser(
4647
* So if you want PROPFIND results to have a trailing slash when they are collections, make sure
4748
* that you query [at.bitfire.dav4jvm.property.webdav.ResourceType].
4849
*/
49-
suspend fun parseResponse(parser: XmlPullParser, callback: MultiResponseCallback) {
50+
suspend fun parseResponse(parser: XmlPullParser) {
5051
val depth = parser.depth
5152

5253
var hrefOrNull: Url? = null
5354
var status: HttpStatusCode? = null
5455
val propStat = mutableListOf<PropStat>()
55-
var error: List<at.bitfire.dav4jvm.Error>? = null
56+
var error: List<Error>? = null
5657
var newLocation: Url? = null
5758

5859
var eventType = parser.eventType
@@ -83,8 +84,7 @@ class ResponseParser(
8384
}
8485
}
8586

86-
87-
if(!hierarchical) {
87+
if (!hierarchical) {
8888
val urlBuilder = URLBuilder(location).takeFrom(sHref)
8989
urlBuilder.pathSegments = urlBuilder.pathSegments.filterNot { it == "." } // Drop segments that are "./"
9090
hrefOrNull = urlBuilder.build()
@@ -95,7 +95,7 @@ class ResponseParser(
9595
WebDAV.Status ->
9696
status = KtorHttpUtils.parseStatusLine(parser.nextText())
9797
WebDAV.PropStat ->
98-
PropStatParser().parse(parser).let { propStat += it }
98+
PropStatParser.parse(parser).let { propStat += it }
9999
WebDAV.Error ->
100100
error = Error.parseError(parser)
101101
WebDAV.Location ->

0 commit comments

Comments
 (0)