Add adapter for parsing atom feed

This commit is contained in:
Shinokuni 2020-09-14 17:56:25 +02:00
parent 8b4b7b0f1c
commit 5c342a45b6
4 changed files with 91 additions and 0 deletions

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-US">
<id>tag:github.com,2008:/readrops/Readrops/commits/develop</id>
<link type="text/html" rel="alternate" href="https://github.com/readrops/Readrops/commits/develop"/>
<link type="application/atom+xml" rel="self" href="https://github.com/readrops/Readrops/commits/develop.atom"/>
<title>Recent Commits to Readrops:develop</title>
<updated>2020-09-06T21:09:59Z</updated>
<subtitle>Here is a subtitle</subtitle>
</feed>

View File

@ -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")
}
}

View File

@ -1,5 +1,6 @@
package com.readrops.api.localfeed 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.RSSFeedAdapter
import com.readrops.api.localfeed.rss.RSSItemsAdapter import com.readrops.api.localfeed.rss.RSSItemsAdapter
import com.readrops.db.entities.Feed import com.readrops.db.entities.Feed
@ -14,6 +15,7 @@ interface XmlAdapter<T> {
fun xmlFeedAdapterFactory(type: LocalRSSHelper.RSSType): XmlAdapter<Feed> { fun xmlFeedAdapterFactory(type: LocalRSSHelper.RSSType): XmlAdapter<Feed> {
return when (type) { return when (type) {
LocalRSSHelper.RSSType.RSS_2 -> RSSFeedAdapter() LocalRSSHelper.RSSType.RSS_2 -> RSSFeedAdapter()
LocalRSSHelper.RSSType.ATOM -> ATOMFeedAdapter()
else -> throw Exception("Unknown RSS type : $type") else -> throw Exception("Unknown RSS type : $type")
} }
} }

View File

@ -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<Feed> {
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")
}
}