From 69b80ef8a18fe5da9790cea6cccd26a462fd16a7 Mon Sep 17 00:00:00 2001 From: gedoor Date: Tue, 28 Sep 2021 09:27:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../model/analyzeRule/AnalyzeByJSonPath.kt | 33 +++------ .../app/model/analyzeRule/AnalyzeByJSoup.kt | 72 +++++++++---------- .../app/model/analyzeRule/RuleAnalyzer.kt | 11 ++- 3 files changed, 45 insertions(+), 71 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 a2f01b009..ae27c223f 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 @@ -41,24 +41,13 @@ class AnalyzeByJSonPath(json: Any) { result = ruleAnalyzes.innerRule("{$.") { getString(it) } //替换所有{$.rule...} if (result.isEmpty()) { //st为空,表明无成功替换的内嵌规则 - try { - val ob = ctx.read(rule) - - result = (if (ob is List<*>) { - - val builder = StringBuilder() - for (o in ob) { - builder.append(o).append("\n") - } - - builder.deleteCharAt(builder.lastIndex) //删除末尾赘余换行 - - builder - - } else ob).toString() - + result = if (ob is List<*>) { + ob.joinToString("\n") + } else { + ob.toString() + } } catch (ignored: Exception) { } @@ -90,21 +79,15 @@ class AnalyzeByJSonPath(json: Any) { if (rules.size == 1) { ruleAnalyzes.reSetPos() //将pos重置为0,复用解析器 - val st = ruleAnalyzes.innerRule("{$.") { getString(it) } //替换所有{$.rule...} - if (st.isEmpty()) { //st为空,表明无成功替换的内嵌规则 - try { - val obj = ctx.read(rule) //kotlin的Any型返回值不包含null ,删除赘余 ?: return result - if (obj is List<*>) { - for (o in obj) result.add(o.toString()) - - } else result.add(obj.toString()) - + } else { + result.add(obj.toString()) + } } catch (ignored: Exception) { } 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 ffb08f274..f485bc7b0 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 @@ -1,7 +1,6 @@ package io.legado.app.model.analyzeRule import androidx.annotation.Keep -import io.legado.app.utils.printOnDebug import org.jsoup.Jsoup import org.jsoup.nodes.Element import org.jsoup.select.Collector @@ -209,55 +208,50 @@ class AnalyzeByJSoup(doc: Any) { */ private fun getResultLast(elements: Elements, lastRule: String): ArrayList { val textS = ArrayList() - try { - when (lastRule) { - "text" -> for (element in elements) { - val text = element.text() + when (lastRule) { + "text" -> for (element in elements) { + val text = element.text() + if (text.isNotEmpty()) { + textS.add(text) + } + } + "textNodes" -> for (element in elements) { + val tn = arrayListOf() + val contentEs = element.textNodes() + for (item in contentEs) { + val text = item.text().trim { it <= ' ' } if (text.isNotEmpty()) { - textS.add(text) + tn.add(text) } } - "textNodes" -> for (element in elements) { - val tn = arrayListOf() - val contentEs = element.textNodes() - for (item in contentEs) { - val text = item.text().trim { it <= ' ' } - if (text.isNotEmpty()) { - tn.add(text) - } - } - if (tn.isNotEmpty()) { - textS.add(tn.joinToString("\n")) - } + if (tn.isNotEmpty()) { + textS.add(tn.joinToString("\n")) } - "ownText" -> for (element in elements) { - val text = element.ownText() - if (text.isNotEmpty()) { - textS.add(text) - } + } + "ownText" -> for (element in elements) { + val text = element.ownText() + if (text.isNotEmpty()) { + textS.add(text) } - "html" -> { - elements.select("script").remove() - elements.select("style").remove() - val html = elements.outerHtml() - if (html.isNotEmpty()) { - textS.add(html) - } + } + "html" -> { + elements.select("script").remove() + elements.select("style").remove() + val html = elements.outerHtml() + if (html.isNotEmpty()) { + textS.add(html) } - "all" -> textS.add(elements.outerHtml()) - else -> for (element in elements) { + } + "all" -> textS.add(elements.outerHtml()) + else -> for (element in elements) { - val url = element.attr(lastRule) + val url = element.attr(lastRule) - if (url.isBlank() || textS.contains(url)) continue + if (url.isBlank() || textS.contains(url)) continue - textS.add(url) - } + textS.add(url) } - } catch (e: Exception) { - e.printOnDebug() } - return textS } diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/RuleAnalyzer.kt b/app/src/main/java/io/legado/app/model/analyzeRule/RuleAnalyzer.kt index 93749dc22..5fd91e15d 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/RuleAnalyzer.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/RuleAnalyzer.kt @@ -11,7 +11,6 @@ class RuleAnalyzer(data: String, code: Boolean = false) { private var rule = ArrayList() //分割出的规则列表 private var step: Int = 0 //分割字符的长度 var elementsType = "" //当前分割字符串 - var innerType = true //是否为内嵌{{}} fun trim() { // 修剪当前规则之前的"@"或者空白符 if (queue[pos] == '@' || queue[pos] < '!') { //在while里重复设置start和startX会拖慢执行速度,所以先来个判断是否存在需要修剪的字段,最后再一次性设置start和startX @@ -33,7 +32,7 @@ class RuleAnalyzer(data: String, code: Boolean = false) { * @param seq 查找的字符串 **区分大小写** * @return 是否找到相应字段。 */ - fun consumeTo(seq: String): Boolean { + private fun consumeTo(seq: String): Boolean { start = pos //将处理到的位置设置为规则起点 val offset = queue.indexOf(seq, pos) return if (offset != -1) { @@ -47,7 +46,7 @@ class RuleAnalyzer(data: String, code: Boolean = false) { * @param seq 匹配字符串序列 * @return 成功返回true并设置间隔,失败则直接返回fasle */ - fun consumeToAny(vararg seq: String): Boolean { + private fun consumeToAny(vararg seq: String): Boolean { var pos = pos //声明新变量记录匹配位置,不更改类本身的位置 @@ -89,7 +88,7 @@ class RuleAnalyzer(data: String, code: Boolean = false) { /** * 拉出一个非内嵌代码平衡组,存在转义文本 */ - fun chompCodeBalanced(open: Char, close: Char): Boolean { + private fun chompCodeBalanced(open: Char, close: Char): Boolean { var pos = pos //声明临时变量记录匹配位置,匹配成功后才同步到类的pos @@ -129,7 +128,7 @@ class RuleAnalyzer(data: String, code: Boolean = false) { /** * 拉出一个规则平衡组,经过仔细测试xpath和jsoup中,引号内转义字符无效。 */ - fun chompRuleBalanced(open: Char, close: Char): Boolean { + private fun chompRuleBalanced(open: Char, close: Char): Boolean { var pos = pos //声明临时变量记录匹配位置,匹配成功后才同步到类的pos var depth = 0 //嵌套深度 @@ -365,8 +364,6 @@ class RuleAnalyzer(data: String, code: Boolean = false) { }.toString() } - val ruleTypeList = ArrayList() - //设置平衡组函数,json或JavaScript时设置成chompCodeBalanced,否则为chompRuleBalanced val chompBalanced = if (code) ::chompCodeBalanced else ::chompRuleBalanced