🛠️ XML parser for FB2
* New XmlTextParser to parse FB2
This commit is contained in:
parent
78b3b04df3
commit
697efb118a
2 changed files with 55 additions and 2 deletions
|
|
@ -13,6 +13,7 @@ import ua.acclorite.book_story.data.parser.epub.EpubTextParser
|
|||
import ua.acclorite.book_story.data.parser.html.HtmlTextParser
|
||||
import ua.acclorite.book_story.data.parser.pdf.PdfTextParser
|
||||
import ua.acclorite.book_story.data.parser.txt.TxtTextParser
|
||||
import ua.acclorite.book_story.data.parser.xml.XmlTextParser
|
||||
import ua.acclorite.book_story.domain.file.CachedFile
|
||||
import ua.acclorite.book_story.domain.reader.ReaderText
|
||||
import javax.inject.Inject
|
||||
|
|
@ -26,7 +27,8 @@ class TextParserImpl @Inject constructor(
|
|||
|
||||
// Document parser (HTML+Markdown)
|
||||
private val epubTextParser: EpubTextParser,
|
||||
private val htmlTextParser: HtmlTextParser
|
||||
private val htmlTextParser: HtmlTextParser,
|
||||
private val xmlTextParser: XmlTextParser
|
||||
) : TextParser {
|
||||
|
||||
override suspend fun parse(cachedFile: CachedFile): List<ReaderText> {
|
||||
|
|
@ -51,7 +53,7 @@ class TextParserImpl @Inject constructor(
|
|||
}
|
||||
|
||||
".fb2" -> {
|
||||
htmlTextParser.parse(cachedFile)
|
||||
xmlTextParser.parse(cachedFile)
|
||||
}
|
||||
|
||||
".html" -> {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
* Book's Story — free and open-source Material You eBook reader.
|
||||
* Copyright (C) 2024-2025 Acclorite
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
package ua.acclorite.book_story.data.parser.xml
|
||||
|
||||
import android.util.Log
|
||||
import kotlinx.coroutines.yield
|
||||
import org.jsoup.Jsoup
|
||||
import org.jsoup.parser.Parser
|
||||
import ua.acclorite.book_story.data.parser.DocumentParser
|
||||
import ua.acclorite.book_story.data.parser.TextParser
|
||||
import ua.acclorite.book_story.domain.file.CachedFile
|
||||
import ua.acclorite.book_story.domain.reader.ReaderText
|
||||
import javax.inject.Inject
|
||||
|
||||
private const val XML_TAG = "XML Parser"
|
||||
|
||||
class XmlTextParser @Inject constructor(
|
||||
private val documentParser: DocumentParser
|
||||
) : TextParser {
|
||||
|
||||
override suspend fun parse(cachedFile: CachedFile): List<ReaderText> {
|
||||
Log.i(XML_TAG, "Started XML parsing: ${cachedFile.name}.")
|
||||
|
||||
return try {
|
||||
val readerText = cachedFile.openInputStream()?.use { stream ->
|
||||
documentParser.parseDocument(Jsoup.parse(stream, null, "", Parser.xmlParser()))
|
||||
}
|
||||
|
||||
yield()
|
||||
|
||||
if (
|
||||
readerText.isNullOrEmpty() ||
|
||||
readerText.filterIsInstance<ReaderText.Text>().isEmpty() ||
|
||||
readerText.filterIsInstance<ReaderText.Chapter>().isEmpty()
|
||||
) {
|
||||
Log.e(XML_TAG, "Could not extract text from XML.")
|
||||
return emptyList()
|
||||
}
|
||||
|
||||
Log.i(XML_TAG, "Successfully finished XML parsing.")
|
||||
readerText
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
emptyList()
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue