Parse recursively items content/description
This commit is contained in:
parent
847739c559
commit
de383d5e7d
@ -53,7 +53,7 @@ dependencies {
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
|
||||
androidTestImplementation 'com.squareup.okhttp3:mockwebserver:4.8.1'
|
||||
|
||||
implementation 'com.gitlab.mvysny.konsume-xml:konsume-xml:0.11'
|
||||
implementation 'com.gitlab.mvysny.konsume-xml:konsume-xml:0.12'
|
||||
|
||||
implementation 'com.squareup.okhttp3:okhttp:4.8.1'
|
||||
|
||||
|
@ -48,19 +48,19 @@ class RSS2ItemsAdapterTest {
|
||||
@Test
|
||||
fun noTitleTest() {
|
||||
val stream = context.resources.assets.open("localfeed/rss2/rss_items_no_title.xml")
|
||||
Assert.assertThrows("Item title can't be null", ParseException::class.java) { adapter.fromXml(stream) }
|
||||
Assert.assertThrows("Item title is required", ParseException::class.java) { adapter.fromXml(stream) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun noLinkTest() {
|
||||
val stream = context.resources.assets.open("localfeed/rss2/rss_items_no_link.xml")
|
||||
Assert.assertThrows("Item link can't be null", ParseException::class.java) { adapter.fromXml(stream) }
|
||||
Assert.assertThrows("Item link is required", ParseException::class.java) { adapter.fromXml(stream) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun noDateTest() {
|
||||
val stream = context.resources.assets.open("localfeed/rss2/rss_items_no_date.xml")
|
||||
Assert.assertThrows("Item date can't be null", ParseException::class.java) { adapter.fromXml(stream) }
|
||||
Assert.assertThrows("Item date is required", ParseException::class.java) { adapter.fromXml(stream) }
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -4,10 +4,7 @@ import com.gitlab.mvysny.konsumexml.Names
|
||||
import com.gitlab.mvysny.konsumexml.allChildrenAutoIgnore
|
||||
import com.gitlab.mvysny.konsumexml.konsumeXml
|
||||
import com.readrops.api.localfeed.XmlAdapter
|
||||
import com.readrops.api.utils.DateUtils
|
||||
import com.readrops.api.utils.ParseException
|
||||
import com.readrops.api.utils.nonNullText
|
||||
import com.readrops.api.utils.nullableText
|
||||
import com.readrops.api.utils.*
|
||||
import com.readrops.db.entities.Item
|
||||
import java.io.InputStream
|
||||
|
||||
@ -32,8 +29,8 @@ class ATOMItemsAdapter : XmlAdapter<List<Item>> {
|
||||
link = attributes["href"]
|
||||
}
|
||||
"author" -> allChildrenAutoIgnore("name") { author = text() }
|
||||
"summary" -> description = nullableText()
|
||||
"content" -> content = nullableText()
|
||||
"summary" -> description = nullableTextRecursively()
|
||||
"content" -> content = nullableTextRecursively()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,10 +4,7 @@ import com.gitlab.mvysny.konsumexml.Names
|
||||
import com.gitlab.mvysny.konsumexml.allChildrenAutoIgnore
|
||||
import com.gitlab.mvysny.konsumexml.konsumeXml
|
||||
import com.readrops.api.localfeed.XmlAdapter
|
||||
import com.readrops.api.utils.DateUtils
|
||||
import com.readrops.api.utils.ParseException
|
||||
import com.readrops.api.utils.nonNullText
|
||||
import com.readrops.api.utils.nullableText
|
||||
import com.readrops.api.utils.*
|
||||
import com.readrops.db.entities.Item
|
||||
import java.io.InputStream
|
||||
|
||||
@ -31,7 +28,7 @@ class RSS1ItemsAdapter : XmlAdapter<List<Item>> {
|
||||
"link" -> link = nullableText()
|
||||
"dc:date" -> pubDate = DateUtils.stringToLocalDateTime(nonNullText())
|
||||
"dc:creator" -> authors += nullableText()
|
||||
"description" -> description = nullableText(failOnElement = false)
|
||||
"description" -> description = nullableTextRecursively()
|
||||
else -> skipContents()
|
||||
}
|
||||
}
|
||||
|
@ -30,8 +30,8 @@ class RSS2ItemsAdapter : XmlAdapter<List<Item>> {
|
||||
"pubDate" -> pubDate = DateUtils.stringToLocalDateTime(nonNullText())
|
||||
"dc:date" -> pubDate = DateUtils.stringToLocalDateTime(nonNullText())
|
||||
"guid" -> guid = nullableText()
|
||||
"description" -> description = text(failOnElement = false)
|
||||
"content:encoded" -> content = nullableText(failOnElement = false)
|
||||
"description" -> description = nullableTextRecursively()
|
||||
"content:encoded" -> content = nullableTextRecursively()
|
||||
"enclosure" -> parseEnclosure(this, enclosures)
|
||||
"media:content" -> parseMediaContent(this, mediaContents)
|
||||
"media:group" -> allChildrenAutoIgnore("content") {
|
||||
@ -80,9 +80,9 @@ class RSS2ItemsAdapter : XmlAdapter<List<Item>> {
|
||||
|
||||
private fun validateItem(item: Item) {
|
||||
when {
|
||||
item.title == null -> throw ParseException("Item title can't be null")
|
||||
item.link == null -> throw ParseException("Item link can't be null")
|
||||
item.pubDate == null -> throw ParseException("Item date can't be null")
|
||||
item.title == null -> throw ParseException("Item title is required")
|
||||
item.link == null -> throw ParseException("Item link is required")
|
||||
item.pubDate == null -> throw ParseException("Item date is required")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2,13 +2,19 @@ package com.readrops.api.utils
|
||||
|
||||
import com.gitlab.mvysny.konsumexml.Konsumer
|
||||
import com.gitlab.mvysny.konsumexml.Whitespace
|
||||
import com.gitlab.mvysny.konsumexml.textRecursively
|
||||
|
||||
fun Konsumer.nonNullText(failOnElement: Boolean = true): String {
|
||||
val text = text(failOnElement = failOnElement, whitespace = Whitespace.preserve)
|
||||
return if (text.isNotEmpty()) text else throw ParseException("Xml field $name can't be null")
|
||||
return if (text.isNotEmpty()) text else throw ParseException("$name text can't be null")
|
||||
}
|
||||
|
||||
fun Konsumer.nullableText(failOnElement: Boolean = true): String? {
|
||||
val text = text(failOnElement = failOnElement, whitespace = Whitespace.preserve)
|
||||
return if (text.isNotEmpty()) text else null
|
||||
}
|
||||
|
||||
fun Konsumer.nullableTextRecursively(): String? {
|
||||
val text = textRecursively()
|
||||
return if (text.isNotEmpty()) text else null
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user