diff options
author | Allan Wang <me@allanwang.ca> | 2017-10-11 01:51:21 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-11 01:51:21 -0400 |
commit | fe1df730a180316f76c334879da88515a0150a42 (patch) | |
tree | 49ef0590dfbbb7f01347746a8d1f78e15682346a /app/src/main/kotlin/com/pitchedapps/frost/parsers/FrostParser.kt | |
parent | d12e0697ad34c02a8f16143c4bddbc2a02e7b3dc (diff) | |
download | frost-fe1df730a180316f76c334879da88515a0150a42.tar.gz frost-fe1df730a180316f76c334879da88515a0150a42.tar.bz2 frost-fe1df730a180316f76c334879da88515a0150a42.zip |
Search Parsing (#379)
* Update parser interface and add search parsing
* Add custom jsoup method and search parse method
* Bind new searchview
* Add search view cache
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/parsers/FrostParser.kt')
-rw-r--r-- | app/src/main/kotlin/com/pitchedapps/frost/parsers/FrostParser.kt | 57 |
1 files changed, 51 insertions, 6 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/parsers/FrostParser.kt b/app/src/main/kotlin/com/pitchedapps/frost/parsers/FrostParser.kt index 86b280a8..9e247f1e 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/parsers/FrostParser.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/parsers/FrostParser.kt @@ -1,33 +1,78 @@ package com.pitchedapps.frost.parsers +import org.jsoup.nodes.Document + /** * Created by Allan Wang on 2017-10-06. * * Interface for a given parser * Use cases should be attached as delegates to objects that implement this interface + * + * In all cases, parsing will be done from a JSoup document + * Variants accepting strings are also permitted, and they will be converted to documents accordingly */ interface FrostParser<T> { + /** + * Extracts data from the JSoup document + * In some cases, the document can be created directly from a connection + * In other times, it needs to be created from scripts, which otherwise + * won't be parsed + */ + fun parse(doc: Document): T? + + /** + * Parse a String input + */ fun parse(text: String?): T? + + /** + * Take in doc and emit debug output + */ + fun debug(doc: Document): String + + /** + * Attempts to parse input and emit a debugger + */ fun debug(text: String?): String } internal abstract class FrostParserBase<T> : FrostParser<T> { - override final fun parse(text: String?): T? - = if (text == null) null else parseImpl(text) + override final fun parse(text: String?): T? { + text ?: return null + val doc = textToDoc(text) ?: return null + return parse(doc) + } - protected abstract fun parseImpl(text: String): T? + protected abstract fun textToDoc(text: String): Document? - override final fun debug(text: String?): String { + override fun debug(text: String?): String { val result = mutableListOf<String>() result.add("Testing parser for ${this::class.java.simpleName}") if (text == null) { - result.add("Input is null") + result.add("Null text input") + return result.joinToString("\n") + } + val doc = textToDoc(text) + if (doc == null) { + result.add("Null document from text") return result.joinToString("\n") } - val output = parseImpl(text) + return debug(doc, result) + } + + override final fun debug(doc: Document): String { + val result = mutableListOf<String>() + result.add("Testing parser for ${this::class.java.simpleName}") + return debug(doc, result) + } + + private fun debug(doc: Document, result: MutableList<String>): String { + val output = parse(doc) if (output == null) { result.add("Output is null") return result.joinToString("\n") + } else { + result.add("Output is not null") } debugImpl(output, result) return result.joinToString("\n") |