From face2eb3a4389b7a7928741f5b584307d81913c3 Mon Sep 17 00:00:00 2001 From: Sasikanth Miriyampalli Date: Sat, 4 May 2024 18:18:26 +0530 Subject: [PATCH] Refactor content parsers Cleaned up the code usage and switched to using `nextText` instead of `parser.text` --- .../core/network/parser/AtomContentParser.kt | 20 ++++++------- .../core/network/parser/ContentParser.kt | 28 ++++-------------- .../core/network/parser/RssContentParser.kt | 29 ++++++++++--------- 3 files changed, 30 insertions(+), 47 deletions(-) diff --git a/core/network/src/commonMain/kotlin/dev/sasikanth/rss/reader/core/network/parser/AtomContentParser.kt b/core/network/src/commonMain/kotlin/dev/sasikanth/rss/reader/core/network/parser/AtomContentParser.kt index a69af6753..83c30ceec 100644 --- a/core/network/src/commonMain/kotlin/dev/sasikanth/rss/reader/core/network/parser/AtomContentParser.kt +++ b/core/network/src/commonMain/kotlin/dev/sasikanth/rss/reader/core/network/parser/AtomContentParser.kt @@ -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() } } @@ -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) { @@ -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() } } diff --git a/core/network/src/commonMain/kotlin/dev/sasikanth/rss/reader/core/network/parser/ContentParser.kt b/core/network/src/commonMain/kotlin/dev/sasikanth/rss/reader/core/network/parser/ContentParser.kt index 0abfe05a7..35df331c8 100644 --- a/core/network/src/commonMain/kotlin/dev/sasikanth/rss/reader/core/network/parser/ContentParser.kt +++ b/core/network/src/commonMain/kotlin/dev/sasikanth/rss/reader/core/network/parser/ContentParser.kt @@ -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 -> { diff --git a/core/network/src/commonMain/kotlin/dev/sasikanth/rss/reader/core/network/parser/RssContentParser.kt b/core/network/src/commonMain/kotlin/dev/sasikanth/rss/reader/core/network/parser/RssContentParser.kt index 81b6cf91d..d64c1a122 100644 --- a/core/network/src/commonMain/kotlin/dev/sasikanth/rss/reader/core/network/parser/RssContentParser.kt +++ b/core/network/src/commonMain/kotlin/dev/sasikanth/rss/reader/core/network/parser/RssContentParser.kt @@ -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() } } @@ -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) { @@ -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() } }