mirror of https://github.com/readrops/Readrops.git
Remove old getRSSContentTYpe method
This commit is contained in:
parent
6025202346
commit
8aec040fbc
|
@ -13,10 +13,6 @@ object LocalRSSHelper {
|
|||
private const val JSONFEED_CONTENT_TYPE = "application/feed+json"
|
||||
private const val JSON_CONTENT_TYPE = "application/json"
|
||||
|
||||
private const val RSS_1_REGEX = "<rdf:RDF.*xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\""
|
||||
private const val RSS_2_REGEX = "rss.*version=\"2.0\""
|
||||
private const val ATOM_REGEX = "<feed.* xmlns=\"http://www.w3.org/2005/Atom\""
|
||||
|
||||
const val RSS_1_ROOT_NAME = "RDF"
|
||||
const val RSS_2_ROOT_NAME = "rss"
|
||||
const val ATOM_ROOT_NAME = "feed"
|
||||
|
@ -35,34 +31,9 @@ object LocalRSSHelper {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Guess RSS type based on xml content
|
||||
*/
|
||||
fun getRSSContentType(content: InputStream): RSSType {
|
||||
val stringBuffer = StringBuffer()
|
||||
val reader = content.bufferedReader()
|
||||
|
||||
// we get the first 10 lines which should be sufficient to get the type,
|
||||
// otherwise iterating over the whole file could be too slow
|
||||
for (i in 0..9) stringBuffer.append(reader.readLine())
|
||||
|
||||
val string = stringBuffer.toString()
|
||||
val type = when {
|
||||
RSS_1_REGEX.toRegex().containsMatchIn(string) -> RSSType.RSS_1
|
||||
RSS_2_REGEX.toRegex().containsMatchIn(string) -> RSSType.RSS_2
|
||||
ATOM_REGEX.toRegex().containsMatchIn(string) -> RSSType.ATOM
|
||||
else -> RSSType.UNKNOWN
|
||||
}
|
||||
|
||||
reader.close()
|
||||
content.close()
|
||||
return type
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun isRSSType(type: String?): Boolean {
|
||||
return if (type != null) getRSSType(type) != RSSType.UNKNOWN else false
|
||||
}
|
||||
fun isRSSType(type: String?): Boolean =
|
||||
if (type != null) getRSSType(type) != RSSType.UNKNOWN else false
|
||||
|
||||
fun guessRSSType(konsumer: Konsumer): RSSType = when {
|
||||
konsumer.checkRoot(RSS_1_ROOT_NAME) -> RSSType.RSS_1
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package com.readrops.api.localfeed
|
||||
|
||||
import com.gitlab.mvysny.konsumexml.Names
|
||||
import com.gitlab.mvysny.konsumexml.konsumeXml
|
||||
import junit.framework.TestCase.*
|
||||
import org.junit.Test
|
||||
import java.io.ByteArrayInputStream
|
||||
|
@ -31,49 +33,46 @@ class LocalRSSHelperTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun rss1ContentTest() {
|
||||
assertEquals(LocalRSSHelper.getRSSContentType(ByteArrayInputStream(
|
||||
"""<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss1full.xsl"?>
|
||||
<?xml-stylesheet type="text/css" media="screen" href="http://rss.slashdot.org/~d/styles/itemcontent.css"?>
|
||||
<rdf:RDF xmlns:admin="http://webns.net/mvcb/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
|
||||
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/"
|
||||
""".trimIndent().toByteArray()
|
||||
)), LocalRSSHelper.RSSType.RSS_1)
|
||||
}
|
||||
fun guessRSSTypeRSS1Test() {
|
||||
val xml = """
|
||||
<RDF>
|
||||
<title></title>
|
||||
<description></description>
|
||||
</RDF>
|
||||
""".trimIndent()
|
||||
|
||||
val konsumer = xml.konsumeXml().nextElement(Names.of("RDF"))!!
|
||||
|
||||
@Test
|
||||
fun rss2ContentTest() {
|
||||
assertEquals(LocalRSSHelper.getRSSContentType(ByteArrayInputStream(
|
||||
"""<rss
|
||||
xmlns:content="http://purl.org/rss/1.0/modules/content/"
|
||||
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
|
||||
xmlns:dc="http://purl.org/dc/elements/1.1/"
|
||||
version="2.0"
|
||||
xmlns:atom="http://www.w3.org/2005/Atom"
|
||||
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
|
||||
xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
|
||||
</rss>""".toByteArray()
|
||||
)), LocalRSSHelper.RSSType.RSS_2)
|
||||
assertEquals(LocalRSSHelper.guessRSSType(konsumer), LocalRSSHelper.RSSType.RSS_1)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun atomContentTest() {
|
||||
assertEquals(LocalRSSHelper.getRSSContentType(ByteArrayInputStream(
|
||||
"""<feed xmlns="http://www.w3.org/2005/Atom">
|
||||
|
||||
</feed>""".toByteArray()
|
||||
)), LocalRSSHelper.RSSType.ATOM)
|
||||
fun guessRSSTypeATOMTest() {
|
||||
val xml = """
|
||||
<feed>
|
||||
<title></title>
|
||||
<description></description>
|
||||
</feed>
|
||||
""".trimIndent()
|
||||
|
||||
val konsumer = xml.konsumeXml().nextElement(Names.of("feed"))!!
|
||||
|
||||
assertEquals(LocalRSSHelper.guessRSSType(konsumer), LocalRSSHelper.RSSType.ATOM)
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun unknownContentTest() {
|
||||
assertEquals(LocalRSSHelper.getRSSContentType(ByteArrayInputStream(
|
||||
"""<html>
|
||||
<body>
|
||||
</body>
|
||||
</html>""".trimMargin().toByteArray()
|
||||
)), LocalRSSHelper.RSSType.UNKNOWN)
|
||||
fun guessRSSTypeRSS2Test() {
|
||||
val xml = """
|
||||
<rss>
|
||||
<title></title>
|
||||
<description></description>
|
||||
</rss>
|
||||
""".trimIndent()
|
||||
|
||||
val konsumer = xml.konsumeXml().nextElement(Names.of("rss"))!!
|
||||
|
||||
assertEquals(LocalRSSHelper.guessRSSType(konsumer), LocalRSSHelper.RSSType.RSS_2)
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue