Improve some adapters syntax

This commit is contained in:
Shinokuni 2021-04-14 17:58:33 +02:00
parent d6f98b0d08
commit fa7b7346cd
5 changed files with 80 additions and 90 deletions

View File

@ -11,27 +11,24 @@ import com.readrops.db.entities.Item
import java.io.InputStream
interface XmlAdapter<T> {
fun fromXml(inputStream: InputStream): T
companion object {
fun xmlFeedAdapterFactory(type: LocalRSSHelper.RSSType): XmlAdapter<Feed> {
return when (type) {
LocalRSSHelper.RSSType.RSS_1 -> RSS1FeedAdapter()
LocalRSSHelper.RSSType.RSS_2 -> RSS2FeedAdapter()
LocalRSSHelper.RSSType.ATOM -> ATOMFeedAdapter()
else -> throw IllegalArgumentException("Unknown RSS type : $type")
}
fun xmlFeedAdapterFactory(type: LocalRSSHelper.RSSType): XmlAdapter<Feed> = when (type) {
LocalRSSHelper.RSSType.RSS_1 -> RSS1FeedAdapter()
LocalRSSHelper.RSSType.RSS_2 -> RSS2FeedAdapter()
LocalRSSHelper.RSSType.ATOM -> ATOMFeedAdapter()
else -> throw IllegalArgumentException("Unknown RSS type : $type")
}
fun xmlItemsAdapterFactory(type: LocalRSSHelper.RSSType): XmlAdapter<List<Item>> {
return when (type) {
LocalRSSHelper.RSSType.RSS_1 -> RSS1ItemsAdapter()
LocalRSSHelper.RSSType.RSS_2 -> RSS2ItemsAdapter()
LocalRSSHelper.RSSType.ATOM -> ATOMItemsAdapter()
else -> throw IllegalArgumentException("Unknown RSS type : $type")
}
}
fun xmlItemsAdapterFactory(type: LocalRSSHelper.RSSType): XmlAdapter<List<Item>> =
when (type) {
LocalRSSHelper.RSSType.RSS_1 -> RSS1ItemsAdapter()
LocalRSSHelper.RSSType.RSS_2 -> RSS2ItemsAdapter()
LocalRSSHelper.RSSType.ATOM -> ATOMItemsAdapter()
else -> throw IllegalArgumentException("Unknown RSS type : $type")
}
const val AUTHORS_MAX = 4
}

View File

@ -37,13 +37,13 @@ class ATOMFeedAdapter : XmlAdapter<Feed> {
}
}
private fun parseLink(konsume: Konsumer, feed: Feed) {
val rel = konsume.attributes.getValueOpt("rel")
private fun parseLink(konsume: Konsumer, feed: Feed) = with(konsume) {
val rel = attributes.getValueOpt("rel")
if (rel == "self")
feed.url = konsume.attributes["href"]
feed.url = attributes["href"]
else if (rel == "alternate")
feed.siteUrl = konsume.attributes["href"]
feed.siteUrl = attributes["href"]
}
companion object {

View File

@ -14,28 +14,26 @@ class JSONFeedAdapter {
fun toJson(feed: Feed) = ""
@FromJson
fun fromJson(reader: JsonReader): Feed {
return try {
val feed = Feed()
reader.beginObject()
fun fromJson(reader: JsonReader): Feed = try {
val feed = Feed()
reader.beginObject()
while (reader.hasNext()) {
with(feed) {
when (reader.selectName(names)) {
0 -> name = reader.nextNonEmptyString()
1 -> siteUrl = reader.nextNullableString()
2 -> url = reader.nextNullableString()
3 -> description = reader.nextNullableString()
else -> reader.skipValue()
}
while (reader.hasNext()) {
with(feed) {
when (reader.selectName(names)) {
0 -> name = reader.nextNonEmptyString()
1 -> siteUrl = reader.nextNullableString()
2 -> url = reader.nextNullableString()
3 -> description = reader.nextNullableString()
else -> reader.skipValue()
}
}
reader.endObject()
feed
} catch (e: Exception) {
throw ParseException(e.message)
}
reader.endObject()
feed
} catch (e: Exception) {
throw ParseException(e.message)
}
companion object {

View File

@ -17,48 +17,46 @@ class JSONItemsAdapter : JsonAdapter<List<Item>>() {
// not useful
}
override fun fromJson(reader: JsonReader): List<Item> {
return try {
val items = arrayListOf<Item>()
reader.beginObject()
while (reader.hasNext()) {
when (reader.nextName()) {
"items" -> parseItems(reader, items)
else -> reader.skipValue()
}
}
items
} catch (e: Exception) {
throw ParseException(e.message)
}
}
private fun parseItems(reader: JsonReader, items: MutableList<Item>) {
reader.beginArray()
override fun fromJson(reader: JsonReader): List<Item> = try {
val items = arrayListOf<Item>()
reader.beginObject()
while (reader.hasNext()) {
reader.beginObject()
when (reader.nextName()) {
"items" -> parseItems(reader, items)
else -> reader.skipValue()
}
}
items
} catch (e: Exception) {
throw ParseException(e.message)
}
private fun parseItems(reader: JsonReader, items: MutableList<Item>) = with(reader) {
beginArray()
while (hasNext()) {
beginObject()
val item = Item()
var contentText: String? = null
var contentHtml: String? = null
while (reader.hasNext()) {
while (hasNext()) {
with(item) {
when (reader.selectName(names)) {
0 -> guid = reader.nextNonEmptyString()
1 -> link = reader.nextNonEmptyString()
2 -> title = reader.nextNonEmptyString()
3 -> contentHtml = reader.nextNullableString()
4 -> contentText = reader.nextNullableString()
5 -> description = reader.nextNullableString()
6 -> imageLink = reader.nextNullableString()
7 -> pubDate = DateUtils.parse(reader.nextNullableString())
when (selectName(names)) {
0 -> guid = nextNonEmptyString()
1 -> link = nextNonEmptyString()
2 -> title = nextNonEmptyString()
3 -> contentHtml = nextNullableString()
4 -> contentText = nextNullableString()
5 -> description = nextNullableString()
6 -> imageLink = nextNullableString()
7 -> pubDate = DateUtils.parse(nextNullableString())
8 -> author = parseAuthor(reader) // jsonfeed 1.0
9 -> author = parseAuthors(reader) // jsonfeed 1.1
else -> reader.skipValue()
else -> skipValue()
}
}
}
@ -67,11 +65,11 @@ class JSONItemsAdapter : JsonAdapter<List<Item>>() {
item.content = if (contentHtml != null) contentHtml else contentText
if (item.pubDate == null) item.pubDate = LocalDateTime.now()
reader.endObject()
endObject()
items += item
}
reader.endArray()
endArray()
}
private fun parseAuthor(reader: JsonReader): String? {
@ -94,7 +92,7 @@ class JSONItemsAdapter : JsonAdapter<List<Item>>() {
reader.beginArray()
while (reader.hasNext()) {
authors.add(parseAuthor(reader))
authors += parseAuthor(reader)
}
reader.endArray()
@ -103,11 +101,10 @@ class JSONItemsAdapter : JsonAdapter<List<Item>>() {
authors.filterNotNull().joinToString(limit = AUTHORS_MAX) else null
}
private fun validateItem(item: Item) {
when {
item.title == null -> throw ParseException("Item title is required")
item.link == null -> throw ParseException("Item link is required")
}
private fun validateItem(item: Item): Boolean = when {
item.title == null -> throw ParseException("Item title is required")
item.link == null -> throw ParseException("Item link is required")
else -> true
}
companion object {

View File

@ -58,10 +58,10 @@ class RSS2ItemsAdapter : XmlAdapter<List<Item>> {
}
}
private fun parseEnclosure(konsumer: Konsumer, item: Item) {
if (konsumer.attributes.getValueOpt("type") != null
&& ApiUtils.isMimeImage(konsumer.attributes["type"]) && item.imageLink == null)
item.imageLink = konsumer.attributes.getValueOpt("url")
private fun parseEnclosure(konsumer: Konsumer, item: Item) = with(konsumer) {
if (attributes.getValueOpt("type") != null
&& ApiUtils.isMimeImage(attributes["type"]) && item.imageLink == null)
item.imageLink = attributes.getValueOpt("url")
}
private fun isMediumImage(konsumer: Konsumer) = with(konsumer) {
@ -88,15 +88,13 @@ class RSS2ItemsAdapter : XmlAdapter<List<Item>> {
}
}
private fun finalizeItem(item: Item, creators: List<String?>) {
item.apply {
validateItem(this)
private fun finalizeItem(item: Item, creators: List<String?>) = with(item) {
validateItem(this)
if (pubDate == null) pubDate = LocalDateTime.now()
if (guid == null) guid = link
if (author == null && creators.filterNotNull().isNotEmpty())
author = creators.filterNotNull().joinToString(limit = AUTHORS_MAX)
}
if (pubDate == null) pubDate = LocalDateTime.now()
if (guid == null) guid = link
if (author == null && creators.filterNotNull().isNotEmpty())
author = creators.filterNotNull().joinToString(limit = AUTHORS_MAX)
}
private fun validateItem(item: Item) {