From 0e95678fa958b989d5472a04d4b28f6466393dfb Mon Sep 17 00:00:00 2001 From: Giorgos Neokleous Date: Sat, 11 Jul 2020 13:17:19 +0100 Subject: [PATCH 1/7] Restrict Access to internal by changing Public API --- .../main/java/com/prof/rssparser/caching/CacheConstants.kt | 4 ++-- .../src/main/java/com/prof/rssparser/caching/CacheDatabase.kt | 4 ++-- .../src/main/java/com/prof/rssparser/caching/CacheManager.kt | 4 ++-- .../src/main/java/com/prof/rssparser/caching/CachedFeed.kt | 4 ++-- .../src/main/java/com/prof/rssparser/caching/CachedFeedDao.kt | 4 ++-- .../src/main/java/com/prof/rssparser/core/CoreXMLFetcher.kt | 4 ++-- .../src/main/java/com/prof/rssparser/core/CoreXMLParser.kt | 2 +- .../src/main/java/com/prof/rssparser/engine/XMLFetcher.kt | 4 ++-- .../src/main/java/com/prof/rssparser/engine/XMLParser.kt | 4 ++-- .../com/prof/rssparser/enginecoroutine/CoroutineEngine.kt | 2 +- .../src/main/java/com/prof/rssparser/utils/RSSKeywords.kt | 2 +- 11 files changed, 19 insertions(+), 19 deletions(-) diff --git a/rssparser/src/main/java/com/prof/rssparser/caching/CacheConstants.kt b/rssparser/src/main/java/com/prof/rssparser/caching/CacheConstants.kt index 3dbc559c..b408e7b5 100644 --- a/rssparser/src/main/java/com/prof/rssparser/caching/CacheConstants.kt +++ b/rssparser/src/main/java/com/prof/rssparser/caching/CacheConstants.kt @@ -1,6 +1,6 @@ package com.prof.rssparser.caching -object CacheConstants { +internal object CacheConstants { const val CACHED_FEEDS_TABLE_NAME = "feeds" const val CACHED_FEEDS_URL_HASH = "url_hash" @@ -16,4 +16,4 @@ object CacheConstants { const val DELETE_CACHED_FEED = """ DELETE FROM $CACHED_FEEDS_TABLE_NAME WHERE $CACHED_FEEDS_URL_HASH = :urlHash """ -} \ No newline at end of file +} diff --git a/rssparser/src/main/java/com/prof/rssparser/caching/CacheDatabase.kt b/rssparser/src/main/java/com/prof/rssparser/caching/CacheDatabase.kt index 11f8cee6..d349d3ab 100644 --- a/rssparser/src/main/java/com/prof/rssparser/caching/CacheDatabase.kt +++ b/rssparser/src/main/java/com/prof/rssparser/caching/CacheDatabase.kt @@ -6,7 +6,7 @@ import androidx.room.Room import androidx.room.RoomDatabase @Database(entities = [CachedFeed::class], version = 1) -abstract class CacheDatabase: RoomDatabase() { +internal abstract class CacheDatabase: RoomDatabase() { abstract fun cachedProjectsDao(): CachedFeedDao companion object { @@ -27,4 +27,4 @@ abstract class CacheDatabase: RoomDatabase() { return INSTANCE as CacheDatabase } } -} \ No newline at end of file +} diff --git a/rssparser/src/main/java/com/prof/rssparser/caching/CacheManager.kt b/rssparser/src/main/java/com/prof/rssparser/caching/CacheManager.kt index 564788e6..2f0609cb 100644 --- a/rssparser/src/main/java/com/prof/rssparser/caching/CacheManager.kt +++ b/rssparser/src/main/java/com/prof/rssparser/caching/CacheManager.kt @@ -19,7 +19,7 @@ import java.io.ObjectOutputStream * the test, to provide a TestCoroutineDispatcher * */ -class CacheManager(internal val database: CacheDatabase, // internal just for close db during testing +internal class CacheManager(internal val database: CacheDatabase, // internal just for close db during testing private val cacheDurationMillis: Long, private val coroutineDispatcher: CoroutineDispatcher = Dispatchers.IO ) { @@ -149,4 +149,4 @@ class CacheManager(internal val database: CacheDatabase, // internal just for cl return INSTANCE as CacheManager } } -} \ No newline at end of file +} diff --git a/rssparser/src/main/java/com/prof/rssparser/caching/CachedFeed.kt b/rssparser/src/main/java/com/prof/rssparser/caching/CachedFeed.kt index fbf4d1c5..18d4540d 100644 --- a/rssparser/src/main/java/com/prof/rssparser/caching/CachedFeed.kt +++ b/rssparser/src/main/java/com/prof/rssparser/caching/CachedFeed.kt @@ -5,7 +5,7 @@ import androidx.room.Entity import androidx.room.PrimaryKey @Entity(tableName = CacheConstants.CACHED_FEEDS_TABLE_NAME) -class CachedFeed( +internal class CachedFeed( @PrimaryKey @ColumnInfo(name = "url_hash") var urlHash: Int, @@ -15,4 +15,4 @@ class CachedFeed( var cachedDate: Long, @ColumnInfo(name = "library_version") var libraryVersion: Int -) \ No newline at end of file +) diff --git a/rssparser/src/main/java/com/prof/rssparser/caching/CachedFeedDao.kt b/rssparser/src/main/java/com/prof/rssparser/caching/CachedFeedDao.kt index e89f4201..a00dff01 100644 --- a/rssparser/src/main/java/com/prof/rssparser/caching/CachedFeedDao.kt +++ b/rssparser/src/main/java/com/prof/rssparser/caching/CachedFeedDao.kt @@ -6,7 +6,7 @@ import androidx.room.OnConflictStrategy import androidx.room.Query @Dao -abstract class CachedFeedDao { +internal abstract class CachedFeedDao { @Query(CacheConstants.QUERY_GET_CACHED_PROJECT) abstract suspend fun getCachedProject(urlHash: Int): CachedFeed? @@ -18,4 +18,4 @@ abstract class CachedFeedDao { @Query(CacheConstants.DELETE_CACHED_FEED) abstract suspend fun deleteFeed(urlHash: Int) -} \ No newline at end of file +} diff --git a/rssparser/src/main/java/com/prof/rssparser/core/CoreXMLFetcher.kt b/rssparser/src/main/java/com/prof/rssparser/core/CoreXMLFetcher.kt index 1bcd38ac..525880ef 100644 --- a/rssparser/src/main/java/com/prof/rssparser/core/CoreXMLFetcher.kt +++ b/rssparser/src/main/java/com/prof/rssparser/core/CoreXMLFetcher.kt @@ -5,7 +5,7 @@ import okhttp3.Request import java.lang.Exception import java.nio.charset.Charset -object CoreXMLFetcher { +internal object CoreXMLFetcher { @Throws(Exception::class) fun fetchXML(url: String, okHttpClient: OkHttpClient? = null, charset: Charset): String { var client = okHttpClient @@ -20,4 +20,4 @@ object CoreXMLFetcher { val byteStream = response.body()!!.byteStream() return byteStream.bufferedReader(charset).use { it.readText() } } -} \ No newline at end of file +} diff --git a/rssparser/src/main/java/com/prof/rssparser/core/CoreXMLParser.kt b/rssparser/src/main/java/com/prof/rssparser/core/CoreXMLParser.kt index 948411e1..b681723f 100644 --- a/rssparser/src/main/java/com/prof/rssparser/core/CoreXMLParser.kt +++ b/rssparser/src/main/java/com/prof/rssparser/core/CoreXMLParser.kt @@ -30,7 +30,7 @@ import java.io.InputStreamReader import java.io.Reader import java.util.regex.Pattern -object CoreXMLParser { +internal object CoreXMLParser { @Throws(XmlPullParserException::class, IOException::class) fun parseXML(xml: String): Channel { diff --git a/rssparser/src/main/java/com/prof/rssparser/engine/XMLFetcher.kt b/rssparser/src/main/java/com/prof/rssparser/engine/XMLFetcher.kt index 7b5d7e78..6e628087 100644 --- a/rssparser/src/main/java/com/prof/rssparser/engine/XMLFetcher.kt +++ b/rssparser/src/main/java/com/prof/rssparser/engine/XMLFetcher.kt @@ -22,10 +22,10 @@ import okhttp3.OkHttpClient import java.nio.charset.Charset import java.util.concurrent.Callable -class XMLFetcher(private val url: String, private val okHttpClient: OkHttpClient?, private val charset: Charset) : Callable { +internal class XMLFetcher(private val url: String, private val okHttpClient: OkHttpClient?, private val charset: Charset) : Callable { @Throws(Exception::class) override fun call(): String { return CoreXMLFetcher.fetchXML(url = url, okHttpClient = okHttpClient, charset = charset) } -} \ No newline at end of file +} diff --git a/rssparser/src/main/java/com/prof/rssparser/engine/XMLParser.kt b/rssparser/src/main/java/com/prof/rssparser/engine/XMLParser.kt index 036873ae..869092e8 100644 --- a/rssparser/src/main/java/com/prof/rssparser/engine/XMLParser.kt +++ b/rssparser/src/main/java/com/prof/rssparser/engine/XMLParser.kt @@ -21,10 +21,10 @@ import com.prof.rssparser.Channel import com.prof.rssparser.core.CoreXMLParser import java.util.concurrent.Callable -class XMLParser(var xml: String) : Callable { +internal class XMLParser(var xml: String) : Callable { @Throws(Exception::class) override fun call(): Channel { return CoreXMLParser.parseXML(xml) } -} \ No newline at end of file +} diff --git a/rssparser/src/main/java/com/prof/rssparser/enginecoroutine/CoroutineEngine.kt b/rssparser/src/main/java/com/prof/rssparser/enginecoroutine/CoroutineEngine.kt index f4ca3ddd..dab82593 100644 --- a/rssparser/src/main/java/com/prof/rssparser/enginecoroutine/CoroutineEngine.kt +++ b/rssparser/src/main/java/com/prof/rssparser/enginecoroutine/CoroutineEngine.kt @@ -8,7 +8,7 @@ import kotlinx.coroutines.withContext import okhttp3.OkHttpClient import java.nio.charset.Charset -object CoroutineEngine { +internal object CoroutineEngine { @Throws(Exception::class) suspend fun fetchXML(url: String, okHttpClient: OkHttpClient?, charset: Charset): String = withContext(Dispatchers.IO) { diff --git a/rssparser/src/main/java/com/prof/rssparser/utils/RSSKeywords.kt b/rssparser/src/main/java/com/prof/rssparser/utils/RSSKeywords.kt index 8ceadb2c..0cad445e 100644 --- a/rssparser/src/main/java/com/prof/rssparser/utils/RSSKeywords.kt +++ b/rssparser/src/main/java/com/prof/rssparser/utils/RSSKeywords.kt @@ -17,7 +17,7 @@ package com.prof.rssparser.utils -object RSSKeywords { +internal object RSSKeywords { // channel const val RSS_CHANNEL = "channel" From d269487b1b39b97f65725655156790af4bb9340f Mon Sep 17 00:00:00 2001 From: Giorgos Neokleous Date: Sat, 11 Jul 2020 17:26:00 +0100 Subject: [PATCH 2/7] expose a parse method to be able to just parse raw data --- .../main/java/com/prof/rssparser/Parser.kt | 26 ++++++++++++++----- .../sample/kotlin/util/AlertDialogHelper.kt | 4 +++ 2 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 samplekotlin/src/main/java/com/prof/rssparser/sample/kotlin/util/AlertDialogHelper.kt diff --git a/rssparser/src/main/java/com/prof/rssparser/Parser.kt b/rssparser/src/main/java/com/prof/rssparser/Parser.kt index ec7d39f4..2bcef9a5 100644 --- a/rssparser/src/main/java/com/prof/rssparser/Parser.kt +++ b/rssparser/src/main/java/com/prof/rssparser/Parser.kt @@ -23,7 +23,11 @@ import com.prof.rssparser.caching.CacheManager import com.prof.rssparser.engine.XMLFetcher import com.prof.rssparser.engine.XMLParser import com.prof.rssparser.enginecoroutine.CoroutineEngine -import kotlinx.coroutines.* +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job +import kotlinx.coroutines.cancel +import kotlinx.coroutines.isActive +import kotlinx.coroutines.withContext import okhttp3.OkHttpClient import java.nio.charset.Charset import java.util.concurrent.ExecutorService @@ -39,6 +43,7 @@ class Parser private constructor(private var okHttpClient: OkHttpClient? = null, // private variables private var onComplete: OnTaskCompleted? = null private lateinit var service: ExecutorService + // Internal to make testable internal var cacheManager: CacheManager? = null internal var executorService: ExecutorService? = null @@ -52,6 +57,7 @@ class Parser private constructor(private var okHttpClient: OkHttpClient? = null, // Just for back compatibility @Deprecated("Use the builder to create the parser object") constructor(okHttpClient: OkHttpClient? = null) : this(okHttpClient, Charsets.UTF_8, null, null) + @Deprecated("Use the builder to create the parser object") constructor() : this(null, Charsets.UTF_8, null, null) @@ -67,12 +73,11 @@ class Parser private constructor(private var okHttpClient: OkHttpClient? = null, * */ data class Builder( - private var okHttpClient: OkHttpClient? = null, - private var charset: Charset = Charsets.UTF_8, - private var context: Context? = null, - private var cacheExpirationMillis: Long? = null + private var okHttpClient: OkHttpClient? = null, + private var charset: Charset = Charsets.UTF_8, + private var context: Context? = null, + private var cacheExpirationMillis: Long? = null ) { - fun okHttpClient(okHttpClient: OkHttpClient) = apply { this.okHttpClient = okHttpClient } fun charset(charset: Charset) = apply { this.charset = charset } fun context(context: Context) = apply { this.context = context } @@ -181,6 +186,15 @@ class Parser private constructor(private var okHttpClient: OkHttpClient? = null, } } + /** + * Parses the [rawRssFeed] into a [Channel]. + * + * @exception Exception if something goes wrong during the parsing of the RSS feed. + * @param rawRssFeed The Raw data of the Rss Feed. + */ + @Throws(Exception::class) + suspend fun parse(rawRssFeed: String): Channel = CoroutineEngine.parseXML(rawRssFeed) + /** * * Flush the cache for the provided url diff --git a/samplekotlin/src/main/java/com/prof/rssparser/sample/kotlin/util/AlertDialogHelper.kt b/samplekotlin/src/main/java/com/prof/rssparser/sample/kotlin/util/AlertDialogHelper.kt new file mode 100644 index 00000000..536df6bf --- /dev/null +++ b/samplekotlin/src/main/java/com/prof/rssparser/sample/kotlin/util/AlertDialogHelper.kt @@ -0,0 +1,4 @@ +package com.prof.rssparser.sample.kotlin.util + +class InputAlertDialog { +} From 3399a774ff0b2498df972784129afccee3f26c2f Mon Sep 17 00:00:00 2001 From: Giorgos Neokleous Date: Sat, 11 Jul 2020 17:26:25 +0100 Subject: [PATCH 3/7] add example in samplekotlin for the parse of raw data --- samplekotlin/build.gradle | 1 + .../rssparser/sample/kotlin/MainActivity.kt | 42 ++++++++++++------- .../rssparser/sample/kotlin/MainViewModel.kt | 27 +++++++++++- .../sample/kotlin/util/AlertDialogHelper.kt | 26 +++++++++++- samplekotlin/src/main/res/menu/menu_main.xml | 5 +++ samplekotlin/src/main/res/values/strings.xml | 4 ++ 6 files changed, 88 insertions(+), 17 deletions(-) diff --git a/samplekotlin/build.gradle b/samplekotlin/build.gradle index eb801027..80e019af 100644 --- a/samplekotlin/build.gradle +++ b/samplekotlin/build.gradle @@ -52,6 +52,7 @@ dependencies { implementation "androidx.lifecycle:lifecycle-extensions:${versions.lifecycle}" implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:${versions.lifecycle}" implementation "androidx.activity:activity-ktx:${versions.activityAndroidx}" + implementation "com.squareup.okhttp3:okhttp:${versions.okhttp}" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${versions.kotlin}" } diff --git a/samplekotlin/src/main/java/com/prof/rssparser/sample/kotlin/MainActivity.kt b/samplekotlin/src/main/java/com/prof/rssparser/sample/kotlin/MainActivity.kt index c6c8ef6f..d3a2e15c 100644 --- a/samplekotlin/src/main/java/com/prof/rssparser/sample/kotlin/MainActivity.kt +++ b/samplekotlin/src/main/java/com/prof/rssparser/sample/kotlin/MainActivity.kt @@ -37,8 +37,12 @@ import androidx.recyclerview.widget.DefaultItemAnimator import androidx.recyclerview.widget.LinearLayoutManager import com.google.android.material.snackbar.Snackbar import com.prof.rssparser.Parser -import kotlinx.android.synthetic.main.activity_main.* -import kotlinx.android.synthetic.main.content_main.* +import com.prof.rssparser.sample.kotlin.util.AlertDialogHelper +import kotlinx.android.synthetic.main.activity_main.toolbar +import kotlinx.android.synthetic.main.content_main.progressBar +import kotlinx.android.synthetic.main.content_main.recycler_view +import kotlinx.android.synthetic.main.content_main.root_layout +import kotlinx.android.synthetic.main.content_main.swipe_layout class MainActivity : AppCompatActivity() { @@ -54,11 +58,11 @@ class MainActivity : AppCompatActivity() { setSupportActionBar(toolbar) parser = Parser.Builder() - .context(this) - // If you want to provide a custom charset (the default is utf-8): - // .charset(Charset.forName("ISO-8859-7")) - .cacheExpirationMillis(24L * 60L * 60L * 100L) // one day - .build() + .context(this) + // If you want to provide a custom charset (the default is utf-8): + // .charset(Charset.forName("ISO-8859-7")) + .cacheExpirationMillis(24L * 60L * 60L * 100L) // one day + .build() recycler_view.layoutManager = LinearLayoutManager(this) recycler_view.itemAnimator = DefaultItemAnimator() @@ -97,10 +101,10 @@ class MainActivity : AppCompatActivity() { if (!isOnline()) { val builder = AlertDialog.Builder(this) builder.setMessage(R.string.alert_message) - .setTitle(R.string.alert_title) - .setCancelable(false) - .setPositiveButton(R.string.alert_positive - ) { dialog, id -> finish() } + .setTitle(R.string.alert_title) + .setCancelable(false) + .setPositiveButton(R.string.alert_positive + ) { dialog, id -> finish() } val alert = builder.create() alert.show() @@ -119,16 +123,24 @@ class MainActivity : AppCompatActivity() { val id = item.itemId if (id == R.id.action_settings) { - val alertDialog = androidx.appcompat.app.AlertDialog.Builder(this@MainActivity).create() + val alertDialog = AlertDialog.Builder(this@MainActivity).create() alertDialog.setTitle(R.string.app_name) alertDialog.setMessage(Html.fromHtml(this@MainActivity.getString(R.string.info_text) + - " GitHub." + - this@MainActivity.getString(R.string.author))) - alertDialog.setButton(androidx.appcompat.app.AlertDialog.BUTTON_NEUTRAL, "OK" + " GitHub." + + this@MainActivity.getString(R.string.author))) + alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK" ) { dialog, which -> dialog.dismiss() } alertDialog.show() (alertDialog.findViewById(android.R.id.message) as TextView).movementMethod = LinkMovementMethod.getInstance() + } else if (id == R.id.action_raw_parser) { + AlertDialogHelper( + title = R.string.alert_raw_parser_title, + positiveButton = R.string.alert_raw_parser_positive, + negativeButton = R.string.alert_raw_parser_negative + ).buildInputDialog(this) { url -> + viewModel.fetchForUrlAndParseRawRata(url) + }.show() } return super.onOptionsItemSelected(item) diff --git a/samplekotlin/src/main/java/com/prof/rssparser/sample/kotlin/MainViewModel.kt b/samplekotlin/src/main/java/com/prof/rssparser/sample/kotlin/MainViewModel.kt index 0062e6c8..f204b35a 100644 --- a/samplekotlin/src/main/java/com/prof/rssparser/sample/kotlin/MainViewModel.kt +++ b/samplekotlin/src/main/java/com/prof/rssparser/sample/kotlin/MainViewModel.kt @@ -23,7 +23,10 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.prof.rssparser.Channel import com.prof.rssparser.Parser +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import okhttp3.OkHttpClient +import okhttp3.Request class MainViewModel : ViewModel() { @@ -38,6 +41,10 @@ class MainViewModel : ViewModel() { val rssChannel: LiveData get() = _rssChannel + private val okHttpClient by lazy { + OkHttpClient() + } + fun onSnackbarShowed() { _snackbar.value = null } @@ -54,4 +61,22 @@ class MainViewModel : ViewModel() { } } } -} \ No newline at end of file + + fun fetchForUrlAndParseRawRata(url: String) { + val parser = Parser.Builder().build() + + viewModelScope.launch(Dispatchers.IO) { + val request = Request.Builder() + .url(url) + .build() + val result = okHttpClient.newCall(request).execute() + val raw = runCatching { result.body()?.string() }.getOrNull() + if(raw == null){ + _snackbar.postValue("Something went wrong!") + } else{ + val channel = parser.parse(raw) + _rssChannel.postValue(channel) + } + } + } +} diff --git a/samplekotlin/src/main/java/com/prof/rssparser/sample/kotlin/util/AlertDialogHelper.kt b/samplekotlin/src/main/java/com/prof/rssparser/sample/kotlin/util/AlertDialogHelper.kt index 536df6bf..75d411f2 100644 --- a/samplekotlin/src/main/java/com/prof/rssparser/sample/kotlin/util/AlertDialogHelper.kt +++ b/samplekotlin/src/main/java/com/prof/rssparser/sample/kotlin/util/AlertDialogHelper.kt @@ -1,4 +1,28 @@ package com.prof.rssparser.sample.kotlin.util -class InputAlertDialog { +import android.app.AlertDialog +import android.content.Context +import android.widget.EditText +import androidx.annotation.StringRes + +class AlertDialogHelper( + @StringRes private val title: Int, + @StringRes private val positiveButton: Int, + @StringRes private val negativeButton: Int +) { + fun buildInputDialog( + context: Context, + onPositiveAction: (url: String) -> Unit + ): AlertDialog { + val editText = EditText(context) + return AlertDialog.Builder(context) + .setTitle(title) + .setView(editText) + .setNegativeButton(negativeButton) { dialog, _ -> + dialog.dismiss() + }.setPositiveButton(positiveButton) { dialog, _ -> + onPositiveAction(editText.text?.toString().orEmpty()) + dialog.dismiss() + }.create() + } } diff --git a/samplekotlin/src/main/res/menu/menu_main.xml b/samplekotlin/src/main/res/menu/menu_main.xml index 9a45594f..ff385bfd 100644 --- a/samplekotlin/src/main/res/menu/menu_main.xml +++ b/samplekotlin/src/main/res/menu/menu_main.xml @@ -2,6 +2,11 @@ xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context="com.prof.rssparser.example.MainActivity"> + This is a simple app that shows the use of RSS Parse library. <br /><br />You can find the source code on <br /><br />Written by Marco Gomiero + Parse raw data from URL + Add an RSS feed url + Ok + Cancel From 508de68dc0cb996b17a87371da25b48523729f06 Mon Sep 17 00:00:00 2001 From: Giorgos Neokleous Date: Sat, 11 Jul 2020 18:03:29 +0100 Subject: [PATCH 4/7] add raw parsing into readme --- README.md | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6ef667b5..7f7f92a4 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ The library provides a caching layer to avoid wasting data with very long feeds. ### RSS Parsing from string -COMING SOON +The library provides a way of simply parsing raw data into POJOs. ## Installation @@ -119,6 +119,23 @@ For flushing the cache: parser.flushCache(url) ``` +For simply parsing raw data: + +```kotlin +// parser without any network or caching configuration +val parser = Parser.Builder().build() + +viewModelScope.launch { + try { + val channel = parser.parse(raw) + // Do something with your data + } catch (e: Exception) { + e.printStackTrace() + // Handle the exception + } +} +``` + You can give a look to the full Kotlin sample by clicking [here](https://github.com/prof18/RSS-Parser/tree/master/samplekotlin) ### Java @@ -231,4 +248,4 @@ If you are using RSS Parser in your app and would like to be listed here, please * [MarioDiscepolo.com](https://play.google.com/store/apps/details?id=com.prof.mariodiscepolo&hl=it) - \ No newline at end of file + From 82106f67e12541599e0fc02167b6633296b3393a Mon Sep 17 00:00:00 2001 From: Giorgos Neokleous Date: Sun, 12 Jul 2020 10:49:20 +0100 Subject: [PATCH 5/7] add java compatibility method for parsing raw strings --- README.md | 2 ++ .../main/java/com/prof/rssparser/Parser.kt | 22 +++++++++++++++++++ .../rssparser/sample/kotlin/MainViewModel.kt | 4 ++-- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7f7f92a4..bcaecef3 100644 --- a/README.md +++ b/README.md @@ -120,6 +120,8 @@ parser.flushCache(url) ``` For simply parsing raw data: +- A `suspend` function `Parser#parser(rawRssFeed)` which returns the channel. +- A java compatible function `Parser.parser(rawRssFeed, OnTaskCompleted)` which calls the listener once done. ```kotlin // parser without any network or caching configuration diff --git a/rssparser/src/main/java/com/prof/rssparser/Parser.kt b/rssparser/src/main/java/com/prof/rssparser/Parser.kt index 2bcef9a5..3f58bc0c 100644 --- a/rssparser/src/main/java/com/prof/rssparser/Parser.kt +++ b/rssparser/src/main/java/com/prof/rssparser/Parser.kt @@ -19,7 +19,9 @@ package com.prof.rssparser import android.content.Context import android.util.Log +import androidx.annotation.WorkerThread import com.prof.rssparser.caching.CacheManager +import com.prof.rssparser.core.CoreXMLParser import com.prof.rssparser.engine.XMLFetcher import com.prof.rssparser.engine.XMLParser import com.prof.rssparser.enginecoroutine.CoroutineEngine @@ -195,6 +197,26 @@ class Parser private constructor(private var okHttpClient: OkHttpClient? = null, @Throws(Exception::class) suspend fun parse(rawRssFeed: String): Channel = CoroutineEngine.parseXML(rawRssFeed) + /** + * Parses the [rawRssFeed] into a [Channel] and notifies the [listener]. + * + * If the operation is successful, then [OnTaskCompleted.onTaskCompleted] is called with + * the parsed channel. Otherwise, [OnTaskCompleted.onError] is called. + * + * @exception Exception if something goes wrong during the parsing of the RSS feed. + * @param rawRssFeed The Raw data of the Rss Feed. + * @param listener Completion listener + */ + @Throws(Exception::class) + @WorkerThread + fun parse(rawRssFeed: String, listener: OnTaskCompleted) { + try { + listener.onTaskCompleted(CoreXMLParser.parseXML(rawRssFeed)) + } catch (exception: Exception) { + listener.onError(exception) + } + } + /** * * Flush the cache for the provided url diff --git a/samplekotlin/src/main/java/com/prof/rssparser/sample/kotlin/MainViewModel.kt b/samplekotlin/src/main/java/com/prof/rssparser/sample/kotlin/MainViewModel.kt index f204b35a..40b1b683 100644 --- a/samplekotlin/src/main/java/com/prof/rssparser/sample/kotlin/MainViewModel.kt +++ b/samplekotlin/src/main/java/com/prof/rssparser/sample/kotlin/MainViewModel.kt @@ -71,9 +71,9 @@ class MainViewModel : ViewModel() { .build() val result = okHttpClient.newCall(request).execute() val raw = runCatching { result.body()?.string() }.getOrNull() - if(raw == null){ + if (raw == null) { _snackbar.postValue("Something went wrong!") - } else{ + } else { val channel = parser.parse(raw) _rssChannel.postValue(channel) } From f1749b0e8ea6be1ac16ae0e9b419e815bbb2ae6f Mon Sep 17 00:00:00 2001 From: Marco Gomiero Date: Wed, 29 Jul 2020 21:38:42 +0200 Subject: [PATCH 6/7] Improve readme --- README.md | 8 ++++++-- build.gradle | 5 +---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index bcaecef3..bc25ee13 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ The library provides a caching layer to avoid wasting data with very long feeds. ### RSS Parsing from string -The library provides a way of simply parsing raw data into POJOs. +The library provides a way of simply parsing raw data from a string. ## Installation @@ -121,7 +121,6 @@ parser.flushCache(url) For simply parsing raw data: - A `suspend` function `Parser#parser(rawRssFeed)` which returns the channel. -- A java compatible function `Parser.parser(rawRssFeed, OnTaskCompleted)` which calls the listener once done. ```kotlin // parser without any network or caching configuration @@ -175,6 +174,11 @@ parser.onFinish(new OnTaskCompleted() { parser.execute(urlString); ``` +For parsing raw data there is a java compatible function which calls the listeners `OnTaskCompleted` and `onError` once done. + +`Parser.parser(rawRssFeed, OnTaskCompleted)` + + The full Java sample is available [here](https://github.com/prof18/RSS-Parser/tree/master/samplejava) #### Version 1.4.4 and below diff --git a/build.gradle b/build.gradle index c6e9a30a..7f416c92 100644 --- a/build.gradle +++ b/build.gradle @@ -32,10 +32,7 @@ buildscript { repositories { google() - maven { - url 'https://maven.google.com/' - name 'Google' - } + mavenCentral() jcenter() } From 48d1ae2fd50afb86d3926fb6be3c299318ec52b3 Mon Sep 17 00:00:00 2001 From: Marco Gomiero Date: Wed, 29 Jul 2020 21:41:52 +0200 Subject: [PATCH 7/7] Version bump --- build.gradle | 4 ++-- samplekotlin/build.gradle | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/build.gradle b/build.gradle index 7f416c92..75a5bde5 100644 --- a/build.gradle +++ b/build.gradle @@ -3,8 +3,8 @@ apply plugin: "com.github.ben-manes.versions" buildscript { ext.versions = [ - 'libVersionCode' : 30000, - 'libVersionName' : '3.0.0', + 'libVersionCode' : 31000, + 'libVersionName' : '3.1.0', 'compileSdk' : 29, 'minSdk' : 14, 'targetSdk' : 29, diff --git a/samplekotlin/build.gradle b/samplekotlin/build.gradle index 80e019af..2128b6af 100644 --- a/samplekotlin/build.gradle +++ b/samplekotlin/build.gradle @@ -10,8 +10,8 @@ android { applicationId "com.prof.rssparser.sample.kotlin" minSdkVersion versions.minSdk targetSdkVersion versions.targetSdk - versionCode 30000 - versionName "3.0.0" + versionCode 31000 + versionName "3.1.0" } buildTypes { release {