Skip to content

Commit

Permalink
Refactor content parsers
Browse files Browse the repository at this point in the history
Cleaned up the code usage and switched to using `nextText` instead of `parser.text`
  • Loading branch information
msasikanth committed May 4, 2024
1 parent 60b62f2 commit face2eb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,22 @@ internal object AtomContentParser : ContentParser() {
if (parser.eventType != EventType.START_TAG) continue
when (val name = parser.name) {
TAG_TITLE -> {
title = readTagText(name, parser)
title = parser.nextText()
}
TAG_LINK -> {
if (link.isNullOrBlank()) {
link = readAtomLink(name, parser)
} else {
skip(parser)
parser.skip()
}
}
TAG_SUBTITLE -> {
description = readTagText(name, parser)
description = parser.nextText()
}
TAG_ATOM_ENTRY -> {
posts.add(readAtomEntry(parser, link))
}
else -> skip(parser)
else -> parser.skip()
}
}

Expand Down Expand Up @@ -108,18 +108,18 @@ internal object AtomContentParser : ContentParser() {

when (val tagName = parser.name) {
TAG_TITLE -> {
title = readTagText(tagName, parser)
title = parser.nextText()
}
TAG_LINK -> {
if (link.isNullOrBlank()) {
link = readAtomLink(tagName, parser)
} else {
skip(parser)
parser.skip()
}
}
TAG_CONTENT,
TAG_SUMMARY -> {
rawContent = readTagText(tagName, parser).trimIndent()
rawContent = parser.nextText().trimIndent()

val htmlContent = HtmlContentParser.parse(htmlContent = rawContent)
if (image.isNullOrBlank() && htmlContent != null) {
Expand All @@ -131,12 +131,12 @@ internal object AtomContentParser : ContentParser() {
TAG_PUBLISHED,
TAG_UPDATED -> {
if (date.isNullOrBlank()) {
date = readTagText(tagName, parser)
date = parser.nextText()
} else {
skip(parser)
parser.skip()
}
}
else -> skip(parser)
else -> parser.skip()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,15 @@ abstract class ContentParser {

abstract fun parse(feedUrl: String, parser: XmlPullParser): FeedPayload

fun readAttrText(attrName: String, parser: XmlPullParser): String? {
val url = parser.getAttributeValue(parser.namespace, attrName)
skip(parser)
return url
fun XmlPullParser.attrText(attrName: String): String? {
return getAttributeValue(namespace, attrName).also { skip() }
}

fun readTagText(tagName: String, parser: XmlPullParser): String {
parser.require(EventType.START_TAG, parser.namespace, tagName)
val title = readText(parser)
parser.require(EventType.END_TAG, parser.namespace, tagName)
return title
}

private fun readText(parser: XmlPullParser): String {
var result = ""
if (parser.next() == EventType.TEXT) {
result = parser.text
parser.nextTag()
}
return result
}

fun skip(parser: XmlPullParser) {
parser.require(EventType.START_TAG, parser.namespace, null)
fun XmlPullParser.skip() {
require(EventType.START_TAG, namespace, null)
var depth = 1
while (depth != 0) {
when (parser.next()) {
when (next()) {
EventType.END_TAG -> depth--
EventType.START_TAG -> depth++
else -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,25 @@ internal object RssContentParser : ContentParser() {

while (parser.next() != EventType.END_TAG) {
if (parser.eventType != EventType.START_TAG) continue

when (val name = parser.name) {
TAG_TITLE -> {
title = readTagText(name, parser)
title = parser.nextText()
}
TAG_LINK -> {
if (link.isNullOrBlank()) {
link = readTagText(name, parser)
link = parser.nextText()
} else {
skip(parser)
parser.skip()
}
}
TAG_DESCRIPTION -> {
description = readTagText(name, parser)
description = parser.nextText()
}
TAG_RSS_ITEM -> {
posts.add(readRssItem(parser, link))
}
else -> skip(parser)
else -> parser.skip()
}
}

Expand Down Expand Up @@ -113,16 +114,16 @@ internal object RssContentParser : ContentParser() {

when {
name == TAG_TITLE -> {
title = readTagText(name, parser)
title = parser.nextText()
}
link.isNullOrBlank() && (name == TAG_LINK || name == TAG_URL) -> {
link = readTagText(name, parser)
link = parser.nextText()
}
name == TAG_ENCLOSURE && link.isNullOrBlank() -> {
link = readAttrText(ATTR_URL, parser)
link = parser.attrText(ATTR_URL)
}
name == TAG_DESCRIPTION || name == TAG_CONTENT_ENCODED -> {
rawContent = readTagText(name, parser).trimIndent()
rawContent = parser.nextText().trimIndent()

val htmlContent = HtmlContentParser.parse(htmlContent = rawContent)
if (image.isNullOrBlank() && htmlContent != null) {
Expand All @@ -132,18 +133,18 @@ internal object RssContentParser : ContentParser() {
description = htmlContent?.content?.ifBlank { rawContent.trim() } ?: rawContent.trim()
}
name == TAG_PUB_DATE -> {
date = readTagText(name, parser)
date = parser.nextText()
}
image.isNullOrBlank() && hasRssImageUrl(name, parser) -> {
image = readAttrText(ATTR_URL, parser)
image = parser.attrText(ATTR_URL)
}
image.isNullOrBlank() && name == TAG_FEATURED_IMAGE -> {
image = readTagText(name, parser)
image = parser.nextText()
}
commentsLink.isNullOrBlank() && name == TAG_COMMENTS -> {
commentsLink = readTagText(name, parser)
commentsLink = parser.nextText()
}
else -> skip(parser)
else -> parser.skip()
}
}

Expand Down

0 comments on commit face2eb

Please sign in to comment.