diff --git a/api/src/androidTest/assets/localfeed/atom/atom_feed.xml b/api/src/androidTest/assets/localfeed/atom/atom_feed.xml new file mode 100644 index 00000000..797a7917 --- /dev/null +++ b/api/src/androidTest/assets/localfeed/atom/atom_feed.xml @@ -0,0 +1,9 @@ + + + tag:github.com,2008:/readrops/Readrops/commits/develop + + + Recent Commits to Readrops:develop + 2020-09-06T21:09:59Z + Here is a subtitle + \ No newline at end of file diff --git a/api/src/androidTest/java/com/readrops/api/localfeed/atom/ATOMFeedAdapterTest.kt b/api/src/androidTest/java/com/readrops/api/localfeed/atom/ATOMFeedAdapterTest.kt new file mode 100644 index 00000000..57711b22 --- /dev/null +++ b/api/src/androidTest/java/com/readrops/api/localfeed/atom/ATOMFeedAdapterTest.kt @@ -0,0 +1,28 @@ +package com.readrops.api.localfeed.atom + +import android.content.Context +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.test.platform.app.InstrumentationRegistry +import junit.framework.TestCase.assertEquals +import org.junit.Test +import org.junit.runner.RunWith + +@RunWith(AndroidJUnit4::class) +class ATOMFeedAdapterTest { + + private val context: Context = InstrumentationRegistry.getInstrumentation().context + + private val adapter = ATOMFeedAdapter() + + @Test + fun normalCasesTest() { + val stream = context.assets.open("localfeed/atom/atom_feed.xml") + + val feed = adapter.fromXml(stream) + + assertEquals(feed.name, "Recent Commits to Readrops:develop") + assertEquals(feed.url, "https://github.com/readrops/Readrops/commits/develop.atom") + assertEquals(feed.siteUrl, "https://github.com/readrops/Readrops/commits/develop") + assertEquals(feed.description, "Here is a subtitle") + } +} \ No newline at end of file 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 874d2944..c41a4478 100644 --- a/api/src/main/java/com/readrops/api/localfeed/XmlAdapter.kt +++ b/api/src/main/java/com/readrops/api/localfeed/XmlAdapter.kt @@ -1,5 +1,6 @@ package com.readrops.api.localfeed +import com.readrops.api.localfeed.atom.ATOMFeedAdapter import com.readrops.api.localfeed.rss.RSSFeedAdapter import com.readrops.api.localfeed.rss.RSSItemsAdapter import com.readrops.db.entities.Feed @@ -14,6 +15,7 @@ interface XmlAdapter { fun xmlFeedAdapterFactory(type: LocalRSSHelper.RSSType): XmlAdapter { return when (type) { LocalRSSHelper.RSSType.RSS_2 -> RSSFeedAdapter() + LocalRSSHelper.RSSType.ATOM -> ATOMFeedAdapter() else -> throw Exception("Unknown RSS type : $type") } } 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 new file mode 100644 index 00000000..e53a4bf2 --- /dev/null +++ b/api/src/main/java/com/readrops/api/localfeed/atom/ATOMFeedAdapter.kt @@ -0,0 +1,52 @@ +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 +import com.readrops.api.localfeed.XmlAdapter +import com.readrops.api.utils.ParseException +import com.readrops.api.utils.nonNullText +import com.readrops.api.utils.nullableText +import com.readrops.db.entities.Feed +import java.io.InputStream + +class ATOMFeedAdapter : XmlAdapter { + + override fun fromXml(inputStream: InputStream): Feed { + val konsume = inputStream.konsumeXml() + val feed = Feed() + + return try { + konsume.child("feed") { + allChildrenAutoIgnore(names) { + with(feed) { + when (tagName) { + "title" -> name = nonNullText() + "link" -> parseLink(this@allChildrenAutoIgnore, feed) + "subtitle" -> description = nullableText() + } + } + } + } + + konsume.close() + feed + } catch (e: Exception) { + throw ParseException(e.message) + } + } + + private fun parseLink(konsume: Konsumer, feed: Feed) { + val rel = konsume.attributes["rel"] + + if (rel == "self") + feed.url = konsume.attributes["href"] + else if (rel == "alternate") + feed.siteUrl = konsume.attributes["href"] + } + + companion object { + val names = Names.of("title", "link", "subtitle") + } +} \ No newline at end of file