Skip to content

Commit

Permalink
Merge branch 'release/3.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
prof18 committed Jul 29, 2020
2 parents 92e6c2b + 48d1ae2 commit dbe0616
Show file tree
Hide file tree
Showing 20 changed files with 182 additions and 51 deletions.
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 from a string.

## Installation

Expand Down Expand Up @@ -119,6 +119,24 @@ For flushing the cache:
parser.flushCache(url)
```

For simply parsing raw data:
- A `suspend` function `Parser#parser(rawRssFeed)` which returns the channel.

```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
Expand Down Expand Up @@ -156,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
Expand Down Expand Up @@ -231,4 +254,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)

</details>
</details>
9 changes: 3 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -32,10 +32,7 @@ buildscript {

repositories {
google()
maven {
url 'https://maven.google.com/'
name 'Google'
}
mavenCentral()
jcenter()
}

Expand Down
48 changes: 42 additions & 6 deletions rssparser/src/main/java/com/prof/rssparser/Parser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@ 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
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
Expand All @@ -39,6 +45,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
Expand All @@ -52,6 +59,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)

Expand All @@ -67,12 +75,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 }
Expand Down Expand Up @@ -181,6 +188,35 @@ 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)

/**
* 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -16,4 +16,4 @@ object CacheConstants {
const val DELETE_CACHED_FEED = """
DELETE FROM $CACHED_FEEDS_TABLE_NAME WHERE $CACHED_FEEDS_URL_HASH = :urlHash
"""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -27,4 +27,4 @@ abstract class CacheDatabase: RoomDatabase() {
return INSTANCE as CacheDatabase
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
Expand Down Expand Up @@ -149,4 +149,4 @@ class CacheManager(internal val database: CacheDatabase, // internal just for cl
return INSTANCE as CacheManager
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -15,4 +15,4 @@ class CachedFeed(
var cachedDate: Long,
@ColumnInfo(name = "library_version")
var libraryVersion: Int
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand All @@ -18,4 +18,4 @@ abstract class CachedFeedDao {

@Query(CacheConstants.DELETE_CACHED_FEED)
abstract suspend fun deleteFeed(urlHash: Int)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,4 +20,4 @@ object CoreXMLFetcher {
val byteStream = response.body()!!.byteStream()
return byteStream.bufferedReader(charset).use { it.readText() }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
internal class XMLFetcher(private val url: String, private val okHttpClient: OkHttpClient?, private val charset: Charset) : Callable<String> {

@Throws(Exception::class)
override fun call(): String {
return CoreXMLFetcher.fetchXML(url = url, okHttpClient = okHttpClient, charset = charset)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Channel> {
internal class XMLParser(var xml: String) : Callable<Channel> {

@Throws(Exception::class)
override fun call(): Channel {
return CoreXMLParser.parseXML(xml)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package com.prof.rssparser.utils

object RSSKeywords {
internal object RSSKeywords {

// channel
const val RSS_CHANNEL = "channel"
Expand Down
5 changes: 3 additions & 2 deletions samplekotlin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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}"
}
Expand Down
Loading

0 comments on commit dbe0616

Please sign in to comment.