From 5d317efe8fdba5d8b0e8927c63dfaee0cd3a9a68 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 8 Nov 2020 10:52:18 +0800 Subject: [PATCH] =?UTF-8?q?js=E5=8F=82=E6=95=B0=E6=B7=BB=E5=8A=A0doc,json?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=97=B6doc=E4=B8=BAcom.jayway.jsonpath.Read?= =?UTF-8?q?Context,=E7=BD=91=E9=A1=B5=E6=95=B0=E6=8D=AEdoc=E4=B8=BAorg.jso?= =?UTF-8?q?up.nodes.Element?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../model/analyzeRule/AnalyzeByJSonPath.kt | 22 ++++++++++--------- .../app/model/analyzeRule/AnalyzeByJSoup.kt | 20 ++++++++--------- .../app/model/analyzeRule/AnalyzeByXPath.kt | 9 ++++---- .../app/model/analyzeRule/AnalyzeRule.kt | 20 ++++++++++++----- 4 files changed, 40 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByJSonPath.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByJSonPath.kt index 85b4e1649..62bde5a88 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByJSonPath.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByJSonPath.kt @@ -10,17 +10,22 @@ import java.util.regex.Pattern @Suppress("RegExpRedundantEscape") @Keep -class AnalyzeByJSonPath { - private lateinit var ctx: ReadContext +class AnalyzeByJSonPath(json: Any) { - fun parse(json: Any): AnalyzeByJSonPath { - ctx = when (json) { - is String -> JsonPath.parse(json) - else -> JsonPath.parse(json) + companion object { + private val jsonRulePattern = Pattern.compile("(?<=\\{)\\$\\..+?(?=\\})") + + fun parse(json: Any): ReadContext { + return when (json) { + is ReadContext -> json + is String -> JsonPath.parse(json) + else -> JsonPath.parse(json) + } } - return this } + private var ctx: ReadContext = parse(json) + fun getString(rule: String): String? { if (TextUtils.isEmpty(rule)) return null var result = "" @@ -210,7 +215,4 @@ class AnalyzeByJSonPath { return result } - companion object { - private val jsonRulePattern = Pattern.compile("(?<=\\{)\\$\\..+?(?=\\})") - } } diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByJSoup.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByJSoup.kt index 4976bc14a..b6d41ad76 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByJSoup.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByJSoup.kt @@ -17,22 +17,22 @@ import java.util.* * 书源规则解析 */ @Keep -class AnalyzeByJSoup { +class AnalyzeByJSoup(doc: Any) { companion object { val validKeys = arrayOf("class", "id", "tag", "text", "children") - } - - private lateinit var element: Element - fun parse(doc: Any): AnalyzeByJSoup { - element = when (doc) { - is Element -> doc - is JXNode -> if (doc.isElement) doc.asElement() else Jsoup.parse(doc.toString()) - else -> Jsoup.parse(doc.toString()) + fun parse(doc: Any): Element { + return when (doc) { + is Element -> doc + is JXNode -> if (doc.isElement) doc.asElement() else Jsoup.parse(doc.toString()) + else -> Jsoup.parse(doc.toString()) + } } - return this + } + private var element: Element = parse(doc) + /** * 获取列表 */ diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByXPath.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByXPath.kt index db3e5129a..5954ccdf2 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByXPath.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByXPath.kt @@ -11,18 +11,17 @@ import org.seimicrawler.xpath.JXNode import java.util.* @Keep -class AnalyzeByXPath { - private lateinit var jxNode: Any +class AnalyzeByXPath(doc: Any) { + private var jxNode: Any = parse(doc) - fun parse(doc: Any): AnalyzeByXPath { - jxNode = when (doc) { + private fun parse(doc: Any): Any { + return when (doc) { is JXNode -> if (doc.isElement) doc else strToJXDocument(doc.toString()) is Document -> JXDocument.create(doc) is Element -> JXDocument.create(Elements(doc)) is Elements -> JXDocument.create(doc) else -> strToJXDocument(doc.toString()) } - return this } private fun strToJXDocument(html: String): JXDocument { diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt index bd7b09236..16e201287 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt @@ -23,6 +23,8 @@ import kotlin.collections.HashMap @Suppress("unused", "RegExpRedundantEscape") class AnalyzeRule(var book: BaseBook? = null) : JsExtensions { var chapter: BookChapter? = null + var doc: Any? = null + private set private var content: Any? = null private var baseUrl: String? = null private var baseURL: URL? = null @@ -42,6 +44,11 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions { this.content = content setBaseUrl(baseUrl) isJSON = content.toString().isJson() + doc = if (isJSON) { + AnalyzeByJSonPath.parse(content) + } else { + AnalyzeByJSoup.parse(content) + } objectChangedXP = true objectChangedJS = true objectChangedJP = true @@ -65,10 +72,10 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions { */ private fun getAnalyzeByXPath(o: Any): AnalyzeByXPath { return if (o != content) { - AnalyzeByXPath().parse(o) + AnalyzeByXPath(o) } else { if (analyzeByXPath == null || objectChangedXP) { - analyzeByXPath = AnalyzeByXPath().parse(content!!) + analyzeByXPath = AnalyzeByXPath(content!!) objectChangedXP = false } analyzeByXPath!! @@ -80,10 +87,10 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions { */ private fun getAnalyzeByJSoup(o: Any): AnalyzeByJSoup { return if (o != content) { - AnalyzeByJSoup().parse(o) + AnalyzeByJSoup(o) } else { if (analyzeByJSoup == null || objectChangedJS) { - analyzeByJSoup = AnalyzeByJSoup().parse(content!!) + analyzeByJSoup = AnalyzeByJSoup(content!!) objectChangedJS = false } analyzeByJSoup!! @@ -95,10 +102,10 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions { */ private fun getAnalyzeByJSonPath(o: Any): AnalyzeByJSonPath { return if (o != content) { - AnalyzeByJSonPath().parse(o) + AnalyzeByJSonPath(o) } else { if (analyzeByJSonPath == null || objectChangedJP) { - analyzeByJSonPath = AnalyzeByJSonPath().parse(content!!) + analyzeByJSonPath = AnalyzeByJSonPath(content!!) objectChangedJP = false } analyzeByJSonPath!! @@ -621,6 +628,7 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions { private fun evalJS(jsStr: String, result: Any?): Any? { val bindings = SimpleBindings() bindings["java"] = this + bindings["doc"] = doc bindings["book"] = book bindings["result"] = result bindings["baseUrl"] = baseUrl