Test opml version before parsing

This commit is contained in:
Shinokuni 2020-08-04 14:08:21 +02:00
parent e92367bbc1
commit 7976140834
4 changed files with 39 additions and 4 deletions

View File

@ -0,0 +1,6 @@
<opml version="1.0">
<body>
<outline title="The Verge" xmlUrl='http://www.theverge.com/rss/index.xml' htmlUrl="http://www.theverge.com" />
<outline title="TechCrunch" xmlUrl='https://techcrunch.com/feed/' htmlUrl="https://techcrunch.com/" />
</body>
</opml>

View File

@ -4,6 +4,7 @@ import android.content.Context
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.readrops.api.opml.OPMLParser
import com.readrops.api.utils.ParseException
import com.readrops.db.entities.Feed
import com.readrops.db.entities.Folder
import io.reactivex.schedulers.Schedulers
@ -37,6 +38,15 @@ class OPMLParserTest {
assertEquals(foldersAndFeeds?.get(null)?.size, 2)
}
@Test
fun opmlVersionTest() {
val stream = context.resources.assets.open("wrong_version.opml")
OPMLParser.read(stream)
.test()
.assertError(ParseException::class.java)
}
@Test
fun writeOpmlTest() {

View File

@ -2,11 +2,13 @@ package com.readrops.api.opml
import android.content.Context
import android.net.Uri
import android.util.Log
import com.readrops.api.opml.model.Body
import com.readrops.api.opml.model.Head
import com.readrops.api.opml.model.OPML
import com.readrops.api.opml.model.Outline
import com.readrops.api.utils.LibUtils
import com.readrops.api.utils.ParseException
import com.readrops.db.entities.Feed
import com.readrops.db.entities.Folder
import io.reactivex.Completable
@ -19,6 +21,8 @@ import java.io.OutputStream
object OPMLParser {
val TAG = OPMLParser.javaClass.simpleName
@JvmStatic
fun read(uri: Uri, context: Context): Single<Map<Folder?, List<Feed>>> {
return Single.create(SingleOnSubscribe<InputStream> {
@ -30,12 +34,17 @@ object OPMLParser {
@JvmStatic
fun read(stream: InputStream): Single<Map<Folder?, List<Feed>>> {
return Single.create { emitter ->
try {
val fileString = LibUtils.inputStreamToString(stream)
val serializer: Serializer = Persister()
val opml: OPML = serializer.read(OPML::class.java, fileString)
emitter.onSuccess(opmltoFoldersAndFeeds(opml))
} catch (e: Exception) {
Log.d(TAG, e.message, e)
emitter.onError(e)
}
}
}
@ -50,6 +59,9 @@ object OPMLParser {
}
private fun opmltoFoldersAndFeeds(opml: OPML): Map<Folder?, List<Feed>> {
if (opml.version != "2.0")
throw ParseException("Only 2.0 OPML specification is supported")
val foldersAndFeeds: MutableMap<Folder?, List<Feed>> = HashMap()
val body = opml.body!!

View File

@ -2,4 +2,11 @@ package com.readrops.api.utils;
public class ParseException extends Exception {
public ParseException() {
super();
}
public ParseException(String message) {
super(message);
}
}