diff --git a/api/src/main/java/com/readrops/api/localfeed/XmlAdapter.kt b/api/src/main/java/com/readrops/api/localfeed/XmlAdapter.kt index 798e476d..732d495a 100644 --- a/api/src/main/java/com/readrops/api/localfeed/XmlAdapter.kt +++ b/api/src/main/java/com/readrops/api/localfeed/XmlAdapter.kt @@ -11,27 +11,24 @@ import com.readrops.db.entities.Item import java.io.InputStream interface XmlAdapter { - + fun fromXml(inputStream: InputStream): T companion object { - fun xmlFeedAdapterFactory(type: LocalRSSHelper.RSSType): XmlAdapter { - 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 = 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> { - 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> = + 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 } diff --git a/api/src/main/java/com/readrops/api/localfeed/atom/ATOMFeedAdapter.kt b/api/src/main/java/com/readrops/api/localfeed/atom/ATOMFeedAdapter.kt index 4e88f5c8..7497083c 100644 --- a/api/src/main/java/com/readrops/api/localfeed/atom/ATOMFeedAdapter.kt +++ b/api/src/main/java/com/readrops/api/localfeed/atom/ATOMFeedAdapter.kt @@ -37,13 +37,13 @@ class ATOMFeedAdapter : XmlAdapter { } } - 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 { diff --git a/api/src/main/java/com/readrops/api/localfeed/json/JSONFeedAdapter.kt b/api/src/main/java/com/readrops/api/localfeed/json/JSONFeedAdapter.kt index 80086873..f95772eb 100644 --- a/api/src/main/java/com/readrops/api/localfeed/json/JSONFeedAdapter.kt +++ b/api/src/main/java/com/readrops/api/localfeed/json/JSONFeedAdapter.kt @@ -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 { diff --git a/api/src/main/java/com/readrops/api/localfeed/json/JSONItemsAdapter.kt b/api/src/main/java/com/readrops/api/localfeed/json/JSONItemsAdapter.kt index 71e26506..e4ca8f2f 100644 --- a/api/src/main/java/com/readrops/api/localfeed/json/JSONItemsAdapter.kt +++ b/api/src/main/java/com/readrops/api/localfeed/json/JSONItemsAdapter.kt @@ -17,48 +17,46 @@ class JSONItemsAdapter : JsonAdapter>() { // not useful } - override fun fromJson(reader: JsonReader): List { - return try { - val items = arrayListOf() - 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) { - reader.beginArray() + override fun fromJson(reader: JsonReader): List = try { + val items = arrayListOf() + 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) = 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>() { 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>() { reader.beginArray() while (reader.hasNext()) { - authors.add(parseAuthor(reader)) + authors += parseAuthor(reader) } reader.endArray() @@ -103,11 +101,10 @@ class JSONItemsAdapter : JsonAdapter>() { 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 { diff --git a/api/src/main/java/com/readrops/api/localfeed/rss2/RSS2ItemsAdapter.kt b/api/src/main/java/com/readrops/api/localfeed/rss2/RSS2ItemsAdapter.kt index 3d37adbe..1f73875a 100644 --- a/api/src/main/java/com/readrops/api/localfeed/rss2/RSS2ItemsAdapter.kt +++ b/api/src/main/java/com/readrops/api/localfeed/rss2/RSS2ItemsAdapter.kt @@ -58,10 +58,10 @@ class RSS2ItemsAdapter : XmlAdapter> { } } - 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> { } } - private fun finalizeItem(item: Item, creators: List) { - item.apply { - validateItem(this) + private fun finalizeItem(item: Item, creators: List) = 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) {