Simplify ATOMItemsAdapter fromXml method

This commit is contained in:
Shinokuni 2020-09-29 22:23:54 +02:00
parent 2bbc8c3e10
commit d958dcbf04

View File

@ -1,5 +1,6 @@
package com.readrops.api.localfeed.atom
import com.gitlab.mvysny.konsumexml.Konsumer
import com.gitlab.mvysny.konsumexml.Names
import com.gitlab.mvysny.konsumexml.allChildrenAutoIgnore
import com.gitlab.mvysny.konsumexml.konsumeXml
@ -23,12 +24,8 @@ class ATOMItemsAdapter : XmlAdapter<List<Item>> {
"title" -> title = nonNullText()
"id" -> guid = nullableText()
"updated" -> pubDate = DateUtils.stringToLocalDateTime(nonNullText())
"link" -> {
if (attributes.getValueOpt("rel") == null ||
attributes["rel"] == "alternate")
link = attributes["href"]
}
"author" -> allChildrenAutoIgnore("name") { author = text() }
"link" -> parseLink(this, this@apply)
"author" -> allChildrenAutoIgnore("name") { author = nullableText() }
"summary" -> description = nullableTextRecursively()
"content" -> content = nullableTextRecursively()
}
@ -49,6 +46,15 @@ class ATOMItemsAdapter : XmlAdapter<List<Item>> {
}
}
private fun parseLink(konsume: Konsumer, item: Item) {
konsume.apply {
if (attributes.getValueOpt("rel") == null ||
attributes["rel"] == "alternate")
item.link = attributes["href"]
}
}
private fun validateItem(item: Item) {
when {
item.title == null -> throw ParseException("Item title is required")