Skip to content

Commit

Permalink
Merge branch 'release/2.1.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
prof18 committed Jan 4, 2020
2 parents c800826 + d1a77ad commit 99e149e
Show file tree
Hide file tree
Showing 10 changed files with 382 additions and 79 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Articles can have following attributes:
The library is uploaded in jCenter, so you can easily add the dependency:
```Gradle
dependencies {
compile 'com.prof.rssparser:rssparser:2.1.0'
compile 'com.prof.rssparser:rssparser:2.1.1'
}
```
#### Use:
Expand Down
4 changes: 2 additions & 2 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' : 21000,
'libVersionName' : '2.1.0',
'libVersionCode' : 21001,
'libVersionName' : '2.1.1',
'compileSdk' : 29,
'minSdk' : 14,
'targetSdk' : 29,
Expand Down
4 changes: 2 additions & 2 deletions rssparser/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
defaultConfig {
minSdkVersion versions.minSdk
targetSdkVersion versions.targetSdk
versionCode 21000
versionName "2.1.0"
versionCode 21001
versionName "2.1.1"
}

buildTypes {
Expand Down
3 changes: 2 additions & 1 deletion rssparser/src/main/java/com/prof/rssparser/Image.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ package com.prof.rssparser
data class Image(
var title: String? = null,
var url: String? = null,
var link: String? = null
var link: String? = null,
var description: String? = null
)
170 changes: 97 additions & 73 deletions rssparser/src/main/java/com/prof/rssparser/core/CoreXMLParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package com.prof.rssparser.core

import android.util.Log
import com.prof.rssparser.Article
import com.prof.rssparser.Channel
import com.prof.rssparser.Image
Expand Down Expand Up @@ -62,32 +61,42 @@ object CoreXMLParser {
if (eventType == XmlPullParser.START_TAG) {
if (xmlPullParser.name.equals(RSSKeywords.RSS_CHANNEL, ignoreCase = true)) {
insideChannel = true
insideItem = false
insideChannelImage = false

} else if (xmlPullParser.name.equals(RSSKeywords.RSS_ITEM, ignoreCase = true)) {
insideItem = true
insideChannel = false
insideChannelImage = false

} else if (xmlPullParser.name.equals(RSSKeywords.RSS_CHANNEL_IMAGE, ignoreCase = true)) {
insideItem = false
insideChannel = false
insideChannelImage = true
channelImage = Image()

} else if (xmlPullParser.name.equals(RSSKeywords.RSS_ITEM_TITLE, ignoreCase = true)) {
when {
insideChannel -> channelTitle = xmlPullParser.nextText().trim()
insideChannelImage -> channelImage?.title = xmlPullParser.nextText().trim()
insideItem -> currentArticle.title = xmlPullParser.nextText().trim()
if (insideChannel) {
when {
insideChannelImage -> {
channelImage?.title = xmlPullParser.nextText().trim()
}
insideItem -> {
currentArticle.title = xmlPullParser.nextText().trim()
}
else -> {
channelTitle = xmlPullParser.nextText().trim()
}
}
}

} else if (xmlPullParser.name.equals(RSSKeywords.RSS_ITEM_LINK, ignoreCase = true)) {
when {
insideChannel -> channelLink = xmlPullParser.nextText().trim()
insideChannelImage -> channelImage?.link = xmlPullParser.nextText().trim()
insideItem -> currentArticle.link = xmlPullParser.nextText().trim()
if (insideChannel) {
when {
insideChannelImage -> {
channelImage?.link = xmlPullParser.nextText().trim()
}
insideItem -> {
currentArticle.link = xmlPullParser.nextText().trim()
}
else -> {
channelLink = xmlPullParser.nextText().trim()
}
}
}

} else if (xmlPullParser.name.equals(RSSKeywords.RSS_ITEM_AUTHOR, ignoreCase = true)) {
Expand All @@ -108,88 +117,103 @@ object CoreXMLParser {
} else if (xmlPullParser.name.equals(RSSKeywords.RSS_ITEM_URL, ignoreCase = true)) {
if (insideChannelImage) {
channelImage?.url = xmlPullParser.nextText().trim()
Log.d("PARSER", "")
}

} else if (xmlPullParser.name.equals(RSSKeywords.RSS_ITEM_ENCLOSURE, ignoreCase = true)) {
if (insideItem) {
val type = xmlPullParser.getAttributeValue(null, RSSKeywords.RSS_ITEM_TYPE)
if (type != null && type.contains("image/")) {
if (type != null && type.contains("image")) {
currentArticle.image = xmlPullParser.getAttributeValue(null, RSSKeywords.RSS_ITEM_URL)
} else {
// let's try if there is the url
val url = xmlPullParser.getAttributeValue(null, RSSKeywords.RSS_ITEM_URL)
if (url != null) {
currentArticle.image = url
}
}
}

} else if (xmlPullParser.name.equals(RSSKeywords.RSS_ITEM_DESCRIPTION, ignoreCase = true)) {
if (insideChannel) {
channelDescription = xmlPullParser.nextText().trim()
} else if (insideItem) {
val description = xmlPullParser.nextText()
currentArticle.description = description.trim()
if (currentArticle.image == null) {
currentArticle.image = getImageUrl(description)
}
}

} else if (xmlPullParser.name.equals(RSSKeywords.RSS_ITEM_CONTENT, ignoreCase = true)) {
if (insideItem) {
val content = xmlPullParser.nextText().trim()
currentArticle.content = content
if (currentArticle.image == null) {
currentArticle.image = getImageUrl(content)
if (insideItem) {
val description = xmlPullParser.nextText()
currentArticle.description = description.trim()
if (currentArticle.image == null) {
currentArticle.image = getImageUrl(description)
}
} else if (insideChannelImage) {
channelImage?.description = xmlPullParser.nextText().trim()
} else {
channelDescription = xmlPullParser.nextText().trim()
}
}

} else if (xmlPullParser.name.equals(RSSKeywords.RSS_ITEM_PUB_DATE, ignoreCase = true)) {
if (insideItem) {
val nextTokenType = xmlPullParser.next()
if (nextTokenType == XmlPullParser.TEXT) {
currentArticle.pubDate = xmlPullParser.text.trim()
}
// Skip to be able to find date inside 'tag' tag
continue
} else if (xmlPullParser.name.equals(RSSKeywords.RSS_ITEM_CONTENT, ignoreCase = true)) {
if (insideItem) {
val content = xmlPullParser.nextText().trim()
currentArticle.content = content
if (currentArticle.image == null) {
currentArticle.image = getImageUrl(content)
}
}

} else if (xmlPullParser.name.equals(RSSKeywords.RSS_ITEM_TIME, ignoreCase = true)) {
if (insideItem) {
currentArticle.pubDate = xmlPullParser.nextText()
} else if (xmlPullParser.name.equals(RSSKeywords.RSS_ITEM_PUB_DATE, ignoreCase = true)) {
if (insideItem) {
val nextTokenType = xmlPullParser.next()
if (nextTokenType == XmlPullParser.TEXT) {
currentArticle.pubDate = xmlPullParser.text.trim()
}
// Skip to be able to find date inside 'tag' tag
continue
}

} else if (xmlPullParser.name.equals(RSSKeywords.RSS_ITEM_GUID, ignoreCase = true)) {
if (insideItem) {
currentArticle.guid = xmlPullParser.nextText().trim()
}
} else if (xmlPullParser.name.equals(RSSKeywords.RSS_ITEM_TIME, ignoreCase = true)) {
if (insideItem) {
currentArticle.pubDate = xmlPullParser.nextText()
}

} else if (eventType == XmlPullParser.END_TAG && xmlPullParser.name.equals("item", ignoreCase = true)) {
// The item is correctly parsed
insideItem = false
articleList.add(currentArticle)
currentArticle = Article()
} else if (xmlPullParser.name.equals(RSSKeywords.RSS_ITEM_GUID, ignoreCase = true)) {
if (insideItem) {
currentArticle.guid = xmlPullParser.nextText().trim()
}
}
eventType = xmlPullParser.next()

} else if (eventType == XmlPullParser.END_TAG && xmlPullParser.name.equals(RSSKeywords.RSS_ITEM, ignoreCase = true)) {
// The item is correctly parsed
insideItem = false
articleList.add(currentArticle)
currentArticle = Article()
} else if (eventType == XmlPullParser.END_TAG && xmlPullParser.name.equals(RSSKeywords.RSS_CHANNEL, ignoreCase = true)) {
// The channel is correctly parsed
insideChannel = false
} else if (eventType == XmlPullParser.END_TAG && xmlPullParser.name.equals(RSSKeywords.RSS_CHANNEL_IMAGE, ignoreCase = true)) {
// The channel image is correctly parsed
insideChannelImage = false
}
return Channel(channelTitle, channelLink, channelDescription, channelImage, articleList)
eventType = xmlPullParser.next()
}
return Channel(channelTitle, channelLink, channelDescription, channelImage, articleList)
}

/**
* Finds the first img tag and get the src as featured image
*
* @param input The content in which to search for the tag
* @return The url, if there is one
*/
private fun getImageUrl(input: String): String? {

var url: String? = null
val patternImg = Pattern.compile("(<img .*?>)")
val matcherImg = patternImg.matcher(input)
if (matcherImg.find()) {
val imgTag = matcherImg.group(1)
val patternLink = Pattern.compile("src\\s*=\\s*\"(.+?)\"")
val matcherLink = patternLink.matcher(imgTag)
if (matcherLink.find()) {
url = matcherLink.group(1).trim()
}
/**
* Finds the first img tag and get the src as featured image
*
* @param input The content in which to search for the tag
* @return The url, if there is one
*/
private fun getImageUrl(input: String): String? {

var url: String? = null
val patternImg = Pattern.compile("(<img .*?>)")
val matcherImg = patternImg.matcher(input)
if (matcherImg.find()) {
val imgTag = matcherImg.group(1)
val patternLink = Pattern.compile("src\\s*=\\s*\"(.+?)\"")
val matcherLink = patternLink.matcher(imgTag)
if (matcherLink.find()) {
url = matcherLink.group(1).trim()
}
return url
}
return url
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ class CoreXMLParserImage2FeedTest {
assertEquals(channel.image?.url, "https://www.mundodeportivo.com/rsc/images/logo_MD_feed.png")
}

@Test
fun channelImageDescription_isCorrect() {
assertEquals(channel.image?.description, "Mundo Deportivo es tu diario deportivo On Line. Noticias de deporte, fútbol y del Barça")
}

@Test
@Throws
fun size_isCorrect() {
Expand Down
Loading

0 comments on commit 99e149e

Please sign in to comment.