1. 改进jsonPath,jsoup,xpath中的规则切分方法,允许查询规则的正则或字符串中存在“&&”,“||”,“%%”而不切错。

----------------------

2. 加强AnalyzeByJSoup中的索引写法

    /**
     * '.'开头表示选择元素,或'!'开头排除那些元素。两者都支持以索引列表按顺序指定元素列表
     * ':'分隔不同索引或区间
     * 区间格式为 start~end+step,其中start为0可省略,end为-1可省略。
     * 索引,区间两端及间隔都支持负数
     * 例如 tag.div.-1:3~-2+-10:2
     * 特殊用法 tag.div.-1~0 可在任意地方让列表反向
     * */

 -------------------
 已经测试三天没发现大问题,坤飞大大可以再测试看看
pull/1094/head
bushixuanqi 3 years ago
parent f93f080a5e
commit 5d10c5ab83
  1. 154
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByJSonPath.kt
  2. 477
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByJSoup.kt
  3. 71
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByXPath.kt
  4. 515
      app/src/main/java/io/legado/app/model/analyzeRule/RuleAnalyzer.kt

@ -1,78 +1,77 @@
package io.legado.app.model.analyzeRule package io.legado.app.model.analyzeRule
import android.text.TextUtils
import androidx.annotation.Keep import androidx.annotation.Keep
import com.jayway.jsonpath.JsonPath import com.jayway.jsonpath.JsonPath
import com.jayway.jsonpath.ReadContext import com.jayway.jsonpath.ReadContext
import io.legado.app.utils.splitNotBlank
import java.util.* import java.util.*
import java.util.regex.Pattern
@Suppress("RegExpRedundantEscape") @Suppress("RegExpRedundantEscape")
@Keep @Keep
class AnalyzeByJSonPath(json: Any) { class AnalyzeByJSonPath(json: Any) {
companion object { companion object {
private val jsonRulePattern = Pattern.compile("(?<=\\{)\\$\\..+?(?=\\})")
fun parse(json: Any): ReadContext { fun parse(json: Any): ReadContext {
return when (json) { return when (json) {
is ReadContext -> json is ReadContext -> json
is String -> JsonPath.parse(json) is String -> JsonPath.parse(json) //JsonPath.parse<String>(json)
else -> JsonPath.parse(json) else -> JsonPath.parse(json) //JsonPath.parse<Any>(json)
} }
} }
} }
private var ctx: ReadContext = parse(json) private var ctx: ReadContext = parse(json)
/**
* 改进解析方法
* 解决阅读&&||与jsonPath支持的&&||之间的冲突
* 解决{$.rule}形式规则可能匹配错误的问题旧规则正则解析内容含}的json文本用规则中的字段去匹配这种内容时会匹配错误.现改用平衡嵌套方法解决这个问题
* */
fun getString(rule: String): String? { fun getString(rule: String): String? {
if (TextUtils.isEmpty(rule)) return null if (rule.isEmpty()) return null
var result = "" var result: String
val rules: Array<String> val ruleAnalyzes = RuleAnalyzer(rule)
val elementsType: String val rules = ruleAnalyzes.splitRule("&&","||")
if (rule.contains("&&")) {
rules = rule.splitNotBlank("&&")
elementsType = "&"
} else {
rules = rule.splitNotBlank("||")
elementsType = "|"
}
if (rules.size == 1) { if (rules.size == 1) {
if (!rule.contains("{$.")) {
ruleAnalyzes.reSetPos() //将pos重置为0,复用解析器
result = ruleAnalyzes.innerRule("{$."){ getString(it) } //替换所有{$.rule...}
if (result.isEmpty()) { //st为空,表明无成功替换的内嵌规则
try { try {
val ob = ctx.read<Any>(rule) val ob = ctx.read<Any>(rule)
result =
if (ob is List<*>) { result =(if (ob is List<*>) {
val builder = StringBuilder() val builder = StringBuilder()
for (o in ob) { for (o in ob) {
builder.append(o).append("\n") builder.append(o).append("\n")
} }
builder.toString().replace("\\n$".toRegex(), "")
} else { builder.deleteCharAt(builder.lastIndex) //删除末尾赘余换行
ob.toString()
} builder
} else ob).toString()
} catch (ignored: Exception) { } catch (ignored: Exception) {
} }
return result
} else {
result = rule
val matcher = jsonRulePattern.matcher(rule)
while (matcher.find()) {
result = result.replace(
String.format("{%s}", matcher.group()),
getString(matcher.group())!!
)
} }
return result return result
}
} else { } else {
val textList = arrayListOf<String>() val textList = arrayListOf<String>()
for (rl in rules) { for (rl in rules) {
val temp = getString(rl) val temp = getString(rl)
if (!temp.isNullOrEmpty()) { if (!temp.isNullOrEmpty()) {
textList.add(temp) textList.add(temp)
if (elementsType == "|") { if (ruleAnalyzes.elementsType == "||") {
break break
} }
} }
@ -83,59 +82,48 @@ class AnalyzeByJSonPath(json: Any) {
internal fun getStringList(rule: String): List<String> { internal fun getStringList(rule: String): List<String> {
val result = ArrayList<String>() val result = ArrayList<String>()
if (TextUtils.isEmpty(rule)) return result if (rule.isEmpty()) return result
val rules: Array<String> val ruleAnalyzes = RuleAnalyzer(rule)
val elementsType: String val rules = ruleAnalyzes.splitRule("&&","||","%%")
when {
rule.contains("&&") -> {
rules = rule.splitNotBlank("&&")
elementsType = "&"
}
rule.contains("%%") -> {
rules = rule.splitNotBlank("%%")
elementsType = "%"
}
else -> {
rules = rule.splitNotBlank("||")
elementsType = "|"
}
}
if (rules.size == 1) { if (rules.size == 1) {
if (!rule.contains("{$.")) {
ruleAnalyzes.reSetPos() //将pos重置为0,复用解析器
val st = ruleAnalyzes.innerRule("{$."){ getString(it) } //替换所有{$.rule...}
if (st.isEmpty()) { //st为空,表明无成功替换的内嵌规则
try { try {
val obj = ctx.read<Any>(rule) ?: return result
val obj = ctx.read<Any>(rule) //kotlin的Any型返回值不包含null ,删除赘余 ?: return result
if (obj is List<*>) { if (obj is List<*>) {
for (o in obj)
result.add(o.toString()) for (o in obj) result.add(o.toString())
} else {
result.add(obj.toString()) } else result.add(obj.toString())
}
} catch (ignored: Exception) { } catch (ignored: Exception) {
} }
}else result.add(st)
return result return result
} else {
val matcher = jsonRulePattern.matcher(rule)
while (matcher.find()) {
val stringList = getStringList(matcher.group())
for (s in stringList) {
result.add(rule.replace(String.format("{%s}", matcher.group()), s))
}
}
return result
}
} else { } else {
val results = ArrayList<List<String>>() val results = ArrayList<List<String>>()
for (rl in rules) { for (rl in rules) {
val temp = getStringList(rl) val temp = getStringList(rl)
if (temp.isNotEmpty()) { if (temp.isNotEmpty()) {
results.add(temp) results.add(temp)
if (temp.isNotEmpty() && elementsType == "|") { if (temp.isNotEmpty() && ruleAnalyzes.elementsType == "||") {
break break
} }
} }
} }
if (results.size > 0) { if (results.size > 0) {
if ("%" == elementsType) { if ("%%" == ruleAnalyzes.elementsType) {
for (i in results[0].indices) { for (i in results[0].indices) {
for (temp in results) { for (temp in results) {
if (i < temp.size) { if (i < temp.size) {
@ -159,23 +147,9 @@ class AnalyzeByJSonPath(json: Any) {
internal fun getList(rule: String): ArrayList<Any>? { internal fun getList(rule: String): ArrayList<Any>? {
val result = ArrayList<Any>() val result = ArrayList<Any>()
if (TextUtils.isEmpty(rule)) return result if (rule.isEmpty()) return result
val elementsType: String val ruleAnalyzes = RuleAnalyzer(rule)
val rules: Array<String> val rules = ruleAnalyzes.splitRule("&&","||","%%")
when {
rule.contains("&&") -> {
rules = rule.splitNotBlank("&&")
elementsType = "&"
}
rule.contains("%%") -> {
rules = rule.splitNotBlank("%%")
elementsType = "%"
}
else -> {
rules = rule.splitNotBlank("||")
elementsType = "|"
}
}
if (rules.size == 1) { if (rules.size == 1) {
ctx.let { ctx.let {
try { try {
@ -191,13 +165,13 @@ class AnalyzeByJSonPath(json: Any) {
val temp = getList(rl) val temp = getList(rl)
if (temp != null && temp.isNotEmpty()) { if (temp != null && temp.isNotEmpty()) {
results.add(temp) results.add(temp)
if (temp.isNotEmpty() && elementsType == "|") { if (temp.isNotEmpty() && ruleAnalyzes.elementsType == "||") {
break break
} }
} }
} }
if (results.size > 0) { if (results.size > 0) {
if ("%" == elementsType) { if ("%%" == ruleAnalyzes.elementsType) {
for (i in 0 until results[0].size) { for (i in 0 until results[0].size) {
for (temp in results) { for (temp in results) {
if (i < temp.size) { if (i < temp.size) {

@ -1,9 +1,7 @@
package io.legado.app.model.analyzeRule package io.legado.app.model.analyzeRule
import android.text.TextUtils.isEmpty
import android.text.TextUtils.join import android.text.TextUtils.join
import androidx.annotation.Keep import androidx.annotation.Keep
import io.legado.app.utils.splitNotBlank
import org.jsoup.Jsoup import org.jsoup.Jsoup
import org.jsoup.nodes.Element import org.jsoup.nodes.Element
import org.jsoup.select.Collector import org.jsoup.select.Collector
@ -19,6 +17,9 @@ import java.util.*
@Keep @Keep
class AnalyzeByJSoup(doc: Any) { class AnalyzeByJSoup(doc: Any) {
companion object { companion object {
/**
* "class", "id", "tag", "text", "children"
*/
val validKeys = arrayOf("class", "id", "tag", "text", "children") val validKeys = arrayOf("class", "id", "tag", "text", "children")
fun parse(doc: Any): Element { fun parse(doc: Any): Element {
@ -36,67 +37,44 @@ class AnalyzeByJSoup(doc: Any) {
/** /**
* 获取列表 * 获取列表
*/ */
internal fun getElements(rule: String): Elements { internal fun getElements(rule: String) = getElements(element, rule)
return getElements(element, rule)
}
/** /**
* 合并内容列表,得到内容 * 合并内容列表,得到内容
*/ */
internal fun getString(ruleStr: String): String? { internal fun getString(ruleStr: String) =
if (isEmpty(ruleStr)) { if(ruleStr.isEmpty()) null
return null else getStringList(ruleStr).takeIf { it.isNotEmpty() }?.joinToString("\n")
}
val textS = getStringList(ruleStr)
return if (textS.isEmpty()) {
null
} else {
textS.joinToString("\n")
}
}
/** /**
* 获取一个字符串 * 获取一个字符串
*/ */
internal fun getString0(ruleStr: String): String { internal fun getString0(ruleStr: String) = getStringList(ruleStr).let{ if ( it.isEmpty() ) "" else it[0] }
val urlList = getStringList(ruleStr)
return if (urlList.isNotEmpty()) {
urlList[0]
} else ""
}
/** /**
* 获取所有内容列表 * 获取所有内容列表
*/ */
internal fun getStringList(ruleStr: String): List<String> { internal fun getStringList(ruleStr: String): List<String> {
val textS = ArrayList<String>() val textS = ArrayList<String>()
if (isEmpty(ruleStr)) {
return textS if (ruleStr.isEmpty()) return textS
}
//拆分规则 //拆分规则
val sourceRule = SourceRule(ruleStr) val sourceRule = SourceRule(ruleStr)
if (isEmpty(sourceRule.elementsRule)) {
if (sourceRule.elementsRule.isEmpty()) {
textS.add(element.data() ?: "") textS.add(element.data() ?: "")
} else { } else {
val elementsType: String
val ruleStrS: Array<String> val ruleAnalyzes = RuleAnalyzer(sourceRule.elementsRule)
when { val ruleStrS = ruleAnalyzes.splitRule("&&","||" ,"%%")
sourceRule.elementsRule.contains("&&") -> {
elementsType = "&"
ruleStrS = sourceRule.elementsRule.splitNotBlank("&&")
}
sourceRule.elementsRule.contains("%%") -> {
elementsType = "%"
ruleStrS = sourceRule.elementsRule.splitNotBlank("%%")
}
else -> {
elementsType = "|"
ruleStrS = sourceRule.elementsRule.splitNotBlank("||")
}
}
val results = ArrayList<List<String>>() val results = ArrayList<List<String>>()
for (ruleStrX in ruleStrS) { for (ruleStrX in ruleStrS) {
val temp: List<String>? = val temp: List<String>? =
if (sourceRule.isCss) { if (sourceRule.isCss) {
val lastIndex = ruleStrX.lastIndexOf('@') val lastIndex = ruleStrX.lastIndexOf('@')
@ -107,15 +85,17 @@ class AnalyzeByJSoup(doc: Any) {
} else { } else {
getResultList(ruleStrX) getResultList(ruleStrX)
} }
if (!temp.isNullOrEmpty()) { if (!temp.isNullOrEmpty()) {
results.add(temp)
if (results.isNotEmpty() && elementsType == "|") { results.add(temp) //!temp.isNullOrEmpty()时,results.isNotEmpty()为true
break
} if (ruleAnalyzes.elementsType == "||") break
} }
} }
if (results.size > 0) { if (results.size > 0) {
if ("%" == elementsType) { if ("%%" == ruleAnalyzes.elementsType) {
for (i in results[0].indices) { for (i in results[0].indices) {
for (temp in results) { for (temp in results) {
if (i < temp.size) { if (i < temp.size) {
@ -137,47 +117,56 @@ class AnalyzeByJSoup(doc: Any) {
* 获取Elements * 获取Elements
*/ */
private fun getElements(temp: Element?, rule: String): Elements { private fun getElements(temp: Element?, rule: String): Elements {
if (temp == null || rule.isEmpty()) return Elements()
val elements = Elements() val elements = Elements()
if (temp == null || isEmpty(rule)) {
return elements
}
val sourceRule = SourceRule(rule) val sourceRule = SourceRule(rule)
val elementsType: String val ruleAnalyzes = RuleAnalyzer(sourceRule.elementsRule)
val ruleStrS: Array<String> val ruleStrS = ruleAnalyzes.splitRule("&&","||","%%")
when {
sourceRule.elementsRule.contains("&&") -> {
elementsType = "&"
ruleStrS = sourceRule.elementsRule.splitNotBlank("&&")
}
sourceRule.elementsRule.contains("%%") -> {
elementsType = "%"
ruleStrS = sourceRule.elementsRule.splitNotBlank("%%")
}
else -> {
elementsType = "|"
ruleStrS = sourceRule.elementsRule.splitNotBlank("||")
}
}
val elementsList = ArrayList<Elements>() val elementsList = ArrayList<Elements>()
if (sourceRule.isCss) { if (sourceRule.isCss) {
for (ruleStr in ruleStrS) { for (ruleStr in ruleStrS) {
val tempS = temp.select(ruleStr) val tempS = temp.select(ruleStr)
elementsList.add(tempS) elementsList.add(tempS)
if (tempS.size > 0 && elementsType == "|") { if (tempS.size > 0 && ruleAnalyzes.elementsType == "||") {
break break
} }
} }
} else { } else {
for (ruleStr in ruleStrS) { for (ruleStr in ruleStrS) {
val tempS = getElementsSingle(temp, ruleStr) //将原getElementsSingle函数调用的函数的部分代码内联过来,方便简化getElementsSingle函数
elementsList.add(tempS)
if (tempS.size > 0 && elementsType == "|") { val rsRule = RuleAnalyzer(ruleStr)
if( rsRule.peek() =='@' || rsRule.peek() < '!' ) rsRule.advance() // 修剪当前规则之前的"@"或者空白符
val rs = rsRule.splitRule("@")
val el = if (rs.size > 1) {
val el = Elements()
el.add(temp)
for (rl in rs) {
val es = Elements()
for (et in el) {
es.addAll(getElements(et, rl))
}
el.clear()
el.addAll(es)
}
el
}else getElementsSingle(temp,ruleStr)
elementsList.add(el)
if (el.size > 0 && ruleAnalyzes.elementsType == "||") {
break break
} }
} }
} }
if (elementsList.size > 0) { if (elementsList.size > 0) {
if ("%" == elementsType) { if ("%%" == ruleAnalyzes.elementsType) {
for (i in 0 until elementsList[0].size) { for (i in 0 until elementsList[0].size) {
for (es in elementsList) { for (es in elementsList) {
if (i < es.size) { if (i < es.size) {
@ -194,134 +183,154 @@ class AnalyzeByJSoup(doc: Any) {
return elements return elements
} }
private fun filterElements(elements: Elements, rules: Array<String>?): Elements {
if (rules == null || rules.size < 2) return elements
val result = Elements()
for (element in elements) {
var isOk = false
when (rules[0]) {
"class" -> isOk = element.getElementsByClass(rules[1]).size > 0
"id" -> isOk = element.getElementById(rules[1]) != null
"tag" -> isOk = element.getElementsByTag(rules[1]).size > 0
"text" -> isOk = element.getElementsContainingOwnText(rules[1]).size > 0
}
if (isOk) {
result.add(element)
}
}
return result
}
/** /**
* 获取Elements按照一个规则 * '.'开头表示选择元素,'!'开头排除那些元素两者都支持以索引列表按顺序指定元素列表
*/ * ':'分隔不同索引或区间
private fun getElementsSingle(temp: Element, rule: String): Elements { * 区间格式为 start~end+step其中start为0可省略end为-1可省略
val elements = Elements() * 索引区间两端及间隔都支持负数
try { * 例如 tag.div.-1:3~-2+-10:2
val rs = rule.trim { it <= ' ' }.splitNotBlank("@") * 特殊用法 tag.div.-1~0 可在任意地方让列表反向
if (rs.size > 1) { * */
elements.add(temp) fun findIndexSet( rule:String ): IndexSet {
for (rl in rs) {
val es = Elements() val indexSet = IndexSet()
for (et in elements) {
es.addAll(getElements(et, rl)) val rus = rule.trim{ it <= ' '}
}
elements.clear() var last = rus.length
elements.addAll(es) var step = 0 //区间步长,为0表示没设置区间
} var curInt: Int //当前数字
} else { var end = 0 //暂存区间结束数字
val rulePcx = rule.split("!")
val rulePc = rulePcx[0].trim { it <= ' ' }.split(">") var range = false //true表示当前在区间开头,false表示当前在区间结尾
val rules = rulePc[0].trim { it <= ' ' }.split(".") var curMinus = false //当前数字是否为负
var filterRules: Array<String>? = null var curEndMinus = false //当前区间右端数字是否为负
var needFilterElements = rulePc.size > 1 && !isEmpty(rulePc[1].trim { it <= ' ' }) var curStepMinus = false //当前区间间隔数字是否为负
if (needFilterElements) {
filterRules = rulePc[1].trim { it <= ' ' }.split(".").toTypedArray() var l = "" //暂存数字字符串
filterRules[0] = filterRules[0].trim { it <= ' ' }
if (filterRules.size < 2 while (last --> 1 ){ //逆向遍历,至少有两位前置字符,如 p.
|| !validKeys.contains(filterRules[0])
|| filterRules[1].trim { it <= ' ' }.isEmpty() val rl = rus[last]
) { if(rl == ' ' )continue //跳过空格
needFilterElements = false if( rl in '0'..'9') l+= rl //将数值累接入临时字串中,遇到分界符才取出
} else if(rl == '-') curMinus = true
filterRules[1] = filterRules[1].trim { it <= ' ' } else if( rl in arrayOf('+','~','!','.',':')) { //分界符号 '+','~','!','.',':'
}
when (rules[0]) { when ( rl ) {
"children" -> {
var children = temp.children() '+' ->{
if (needFilterElements) curStepMinus = curMinus
children = filterElements(children, filterRules) step = l.toInt() //区间间隔数
elements.addAll(children)
}
"class" -> {
var elementsByClass = temp.getElementsByClass(rules[1])
if (rules.size == 3 && rules[2].isNotEmpty()) {
val index = Integer.parseInt(rules[2])
if (index < 0) {
elements.add(elementsByClass[elementsByClass.size + index])
} else {
elements.add(elementsByClass[index])
}
} else {
if (needFilterElements)
elementsByClass = filterElements(elementsByClass, filterRules)
elements.addAll(elementsByClass)
}
} }
"tag" -> {
var elementsByTag = temp.getElementsByTag(rules[1]) '~' -> {
if (rules.size == 3 && rules[2].isNotEmpty()) { range = true
val index = Integer.parseInt(rules[2]) curEndMinus = curMinus
if (index < 0) {
elements.add(elementsByTag[elementsByTag.size + index]) if (l.isEmpty()) {
} else { end = -1 //省略区间右端,设置为-1
elements.add(elementsByTag[index]) continue
} else end = l.toInt()
} }
} else {
if (needFilterElements) else -> {
elementsByTag = filterElements(elementsByTag, filterRules)
elements.addAll(elementsByTag) curInt = if(l.isEmpty()) 0 /* 省略区间左端,设置为0 */ else if(curMinus) - l.toInt() else l.toInt() //区间左端数,省略则为最左边
indexSet.indexs.add( //压入以下值,为保证查找顺序,区间和单个索引都添加到同一集合
if ( range ) {
range = false //重置
if (curEndMinus) {
end = -end
curEndMinus = false //重置
} }
//没设置间隔时,间隔为1。将区间的三项数据压入,在获取到元素数量后再计算负数索引,并展开区间
if( step == 0 ) Triple(curInt, end, 1)
else {
if (curStepMinus) {
step = -step
curStepMinus = false //重置
} }
"id" -> {
var elementsById = Collector.collect(Evaluator.Id(rules[1]), temp) val stepx = step
if (rules.size == 3 && rules[2].isNotEmpty()) { step = 0 //重置
val index = Integer.parseInt(rules[2])
if (index < 0) { //将区间的三项数据压入,在获取到元素数量后再计算负数索引,并展开区间
elements.add(elementsById[elementsById.size + index]) Triple(curInt, end, stepx)
} else {
elements.add(elementsById[index])
} }
} else {
if (needFilterElements) }else curInt //压入单个索引,在获取到元素数量后再计算负数索引
elementsById = filterElements(elementsById, filterRules)
elements.addAll(elementsById) )
if( rl == '!' || rl == '.' ) return indexSet.apply{
split = rl
beforeRule = rus.substring(0, last)
} }
} }
"text" -> {
var elementsByText = temp.getElementsContainingOwnText(rules[1])
if (needFilterElements)
elementsByText = filterElements(elementsByText, filterRules)
elements.addAll(elementsByText)
} }
else -> elements.addAll(temp.select(rulePcx[0])) l = "" //清空
curMinus = false //重置
} }
if (rulePcx.size > 1) {
val rulePcs = rulePcx[1].splitNotBlank(":") else break
for (pc in rulePcs) {
val pcInt = Integer.parseInt(pc)
if (pcInt < 0 && elements.size + pcInt >= 0) {
elements[elements.size + pcInt] = null
} else if (Integer.parseInt(pc) < elements.size) {
elements[Integer.parseInt(pc)] = null
} }
return indexSet.apply{ beforeRule = rus } //非索引格式
} }
/**
* 获取Elements按照一个规则
*/
private fun getElementsSingle(temp: Element, rule: String): Elements {
var elements = Elements()
val fi = findIndexSet(rule) //执行索引列表处理器
val (filterType,ruleStr) = fi //获取操作类型及非索引部分的规则字串
// val rulePc = rulePcx[0].trim { it <= ' ' }.split(">")
// jsoup中,当前节点是参与选择的,tag.div 与 tag.div@tag.div 结果相同
// 此处">"效果和“@”完全相同,且容易让人误解成选择子节点,实际并不是。以后不允许这种无意义的写法
val rules = ruleStr.split(".")
elements.addAll(
when (rules[0]) {
"children" -> temp.children()
"class" -> temp.getElementsByClass(rules[1])
"tag" -> temp.getElementsByTag(rules[1])
"id" -> Collector.collect(Evaluator.Id(rules[1]), temp)
"text" -> temp.getElementsContainingOwnText(rules[1])
else -> temp.select(ruleStr)
} )
val indexSet = fi.getIndexs(elements.size) //传入元素数量,处理负数索引及索引越界问题,生成可用索引集合。
if(filterType == '!'){ //排除
for (pcInt in indexSet) elements[pcInt] = null
elements.removeAll( Elements().apply { add(null) } )
}else if(filterType == '.'){ //选择
val es = Elements() val es = Elements()
es.add(null)
elements.removeAll(es) for (pcInt in indexSet) es.add(elements[pcInt])
}
} elements = es
} catch (ignore: Exception) {
} }
return elements return elements
@ -331,13 +340,21 @@ class AnalyzeByJSoup(doc: Any) {
* 获取内容列表 * 获取内容列表
*/ */
private fun getResultList(ruleStr: String): List<String>? { private fun getResultList(ruleStr: String): List<String>? {
if (isEmpty(ruleStr)) {
return null if (ruleStr.isEmpty()) return null
}
var elements = Elements() var elements = Elements()
elements.add(element) elements.add(element)
val rules = ruleStr.splitNotBlank("@")
for (i in 0 until rules.size - 1) { val rule = RuleAnalyzer(ruleStr) //创建解析
while( rule.peek() =='@' || rule.peek() < '!' ) rule.advance() // 修剪当前规则之前的"@"或者空白符
val rules = rule.splitRule("@") // 切割成列表
val last = rules.size - 1
for (i in 0 until last) {
val es = Elements() val es = Elements()
for (elt in elements) { for (elt in elements) {
es.addAll(getElementsSingle(elt, rules[i])) es.addAll(getElementsSingle(elt, rules[i]))
@ -345,9 +362,7 @@ class AnalyzeByJSoup(doc: Any) {
elements.clear() elements.clear()
elements = es elements = es
} }
return if (elements.isEmpty()) { return if (elements.isEmpty()) null else getResultLast(elements, rules[last])
null
} else getResultLast(elements, rules[rules.size - 1])
} }
/** /**
@ -365,7 +380,7 @@ class AnalyzeByJSoup(doc: Any) {
val contentEs = element.textNodes() val contentEs = element.textNodes()
for (item in contentEs) { for (item in contentEs) {
val temp = item.text().trim { it <= ' ' } val temp = item.text().trim { it <= ' ' }
if (!isEmpty(temp)) { if (temp.isNotEmpty()) {
tn.add(temp) tn.add(temp)
} }
} }
@ -382,12 +397,14 @@ class AnalyzeByJSoup(doc: Any) {
} }
"all" -> textS.add(elements.outerHtml()) "all" -> textS.add(elements.outerHtml())
else -> for (element in elements) { else -> for (element in elements) {
val url = element.attr(lastRule) val url = element.attr(lastRule)
if (!isEmpty(url) && !textS.contains(url)) {
if(url.isEmpty() || textS.contains(url)) break
textS.add(url) textS.add(url)
} }
} }
}
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
} }
@ -395,17 +412,67 @@ class AnalyzeByJSoup(doc: Any) {
return textS return textS
} }
data class IndexSet(var split:Char = ' ',
var beforeRule:String = "",
val indexs:MutableList<Any> = mutableListOf()){
fun getIndexs(len:Int): MutableSet<Int> {
val indexSet = mutableSetOf<Int>()
val lastIndexs = indexs.size - 1
for (ix in lastIndexs downTo 0 ){ //逆向遍历,还原顺序
if(indexs[ix] is Triple<*, *, *>){ //区间
var (start, end, step) = indexs[ix] as Triple<Int, Int, Int> //还原储存时的类型
if (start >= 0) {
if (start >= len) start = len - 1 //右端越界,设置为最大索引
} else start = if (-start <= len) len + start /* 将负索引转正 */ else 0 //左端越界,设置为最小索引
if (end >= 0) {
if (end >= len) end = len - 1 //右端越界,设置为最大索引
} else end = if (-end <= len) len + end /* 将负索引转正 */ else 0 //左端越界,设置为最小索引
if (start == end || step >= len) { //两端相同,区间里只有一个数。或间隔过大,区间实际上仅有首位
indexSet.add(start)
continue
}
step = if (step > 0) step else if (-step < len) step + len else 1 //最小正数间隔为1
//将区间展开到集合中,允许列表反向。
indexSet.addAll(if (end > start) start..end step step else start downTo end step step)
}else{//单个索引
val it = indexs[ix] as Int //还原储存时的类型
if(it in 0 until len) indexSet.add(it) //将正数不越界的索引添加到集合
else if(it < 0 && len >= -it) indexSet.add(it + len) //将负数不越界的索引添加到集合
}
}
return indexSet
}
}
internal inner class SourceRule(ruleStr: String) { internal inner class SourceRule(ruleStr: String) {
var isCss = false var isCss = false
var elementsRule: String var elementsRule: String = if (ruleStr.startsWith("@CSS:", true)) {
init {
if (ruleStr.startsWith("@CSS:", true)) {
isCss = true isCss = true
elementsRule = ruleStr.substring(5).trim { it <= ' ' } ruleStr.substring(5).trim { it <= ' ' }
} else { } else {
elementsRule = ruleStr ruleStr
}
} }
} }

@ -2,7 +2,6 @@ package io.legado.app.model.analyzeRule
import android.text.TextUtils import android.text.TextUtils
import androidx.annotation.Keep import androidx.annotation.Keep
import io.legado.app.utils.splitNotBlank
import org.jsoup.nodes.Document import org.jsoup.nodes.Document
import org.jsoup.nodes.Element import org.jsoup.nodes.Element
import org.jsoup.select.Elements import org.jsoup.select.Elements
@ -27,10 +26,10 @@ class AnalyzeByXPath(doc: Any) {
private fun strToJXDocument(html: String): JXDocument { private fun strToJXDocument(html: String): JXDocument {
var html1 = html var html1 = html
if (html1.endsWith("</td>")) { if (html1.endsWith("</td>")) {
html1 = String.format("<tr>%s</tr>", html1) html1 = "<tr>${html1}</tr>"
} }
if (html1.endsWith("</tr>") || html1.endsWith("</tbody>")) { if (html1.endsWith("</tr>") || html1.endsWith("</tbody>")) {
html1 = String.format("<table>%s</table>", html1) html1 = "<table>${html1}</table>"
} }
return JXDocument.create(html1) return JXDocument.create(html1)
} }
@ -45,26 +44,13 @@ class AnalyzeByXPath(doc: Any) {
} }
internal fun getElements(xPath: String): List<JXNode>? { internal fun getElements(xPath: String): List<JXNode>? {
if (TextUtils.isEmpty(xPath)) {
return null if(xPath.isEmpty()) return null
}
val jxNodes = ArrayList<JXNode>() val jxNodes = ArrayList<JXNode>()
val elementsType: String val ruleAnalyzes = RuleAnalyzer(xPath)
val rules: Array<String> val rules = ruleAnalyzes.splitRule("&&","||","%%")
when {
xPath.contains("&&") -> {
rules = xPath.splitNotBlank("&&")
elementsType = "&"
}
xPath.contains("%%") -> {
rules = xPath.splitNotBlank("%%")
elementsType = "%"
}
else -> {
rules = xPath.splitNotBlank("||")
elementsType = "|"
}
}
if (rules.size == 1) { if (rules.size == 1) {
return getResult(rules[0]) return getResult(rules[0])
} else { } else {
@ -73,13 +59,13 @@ class AnalyzeByXPath(doc: Any) {
val temp = getElements(rl) val temp = getElements(rl)
if (temp != null && temp.isNotEmpty()) { if (temp != null && temp.isNotEmpty()) {
results.add(temp) results.add(temp)
if (temp.isNotEmpty() && elementsType == "|") { if (temp.isNotEmpty() && ruleAnalyzes.elementsType == "||") {
break break
} }
} }
} }
if (results.size > 0) { if (results.size > 0) {
if ("%" == elementsType) { if ("%%" == ruleAnalyzes.elementsType) {
for (i in results[0].indices) { for (i in results[0].indices) {
for (temp in results) { for (temp in results) {
if (i < temp.size) { if (i < temp.size) {
@ -98,23 +84,11 @@ class AnalyzeByXPath(doc: Any) {
} }
internal fun getStringList(xPath: String): List<String> { internal fun getStringList(xPath: String): List<String> {
val result = ArrayList<String>() val result = ArrayList<String>()
val elementsType: String val ruleAnalyzes = RuleAnalyzer(xPath)
val rules: Array<String> val rules = ruleAnalyzes.splitRule("&&","||","%%")
when {
xPath.contains("&&") -> {
rules = xPath.splitNotBlank("&&")
elementsType = "&"
}
xPath.contains("%%") -> {
rules = xPath.splitNotBlank("%%")
elementsType = "%"
}
else -> {
rules = xPath.splitNotBlank("||")
elementsType = "|"
}
}
if (rules.size == 1) { if (rules.size == 1) {
getResult(xPath)?.map { getResult(xPath)?.map {
result.add(it.asString()) result.add(it.asString())
@ -126,13 +100,13 @@ class AnalyzeByXPath(doc: Any) {
val temp = getStringList(rl) val temp = getStringList(rl)
if (temp.isNotEmpty()) { if (temp.isNotEmpty()) {
results.add(temp) results.add(temp)
if (temp.isNotEmpty() && elementsType == "|") { if (temp.isNotEmpty() && ruleAnalyzes.elementsType == "||") {
break break
} }
} }
} }
if (results.size > 0) { if (results.size > 0) {
if ("%" == elementsType) { if ("%%" == ruleAnalyzes.elementsType) {
for (i in results[0].indices) { for (i in results[0].indices) {
for (temp in results) { for (temp in results) {
if (i < temp.size) { if (i < temp.size) {
@ -151,15 +125,8 @@ class AnalyzeByXPath(doc: Any) {
} }
fun getString(rule: String): String? { fun getString(rule: String): String? {
val rules: Array<String> val ruleAnalyzes = RuleAnalyzer(rule)
val elementsType: String val rules = ruleAnalyzes.splitRule("&&","||")
if (rule.contains("&&")) {
rules = rule.splitNotBlank("&&")
elementsType = "&"
} else {
rules = rule.splitNotBlank("||")
elementsType = "|"
}
if (rules.size == 1) { if (rules.size == 1) {
getResult(rule)?.let { getResult(rule)?.let {
return TextUtils.join("\n", it) return TextUtils.join("\n", it)
@ -171,7 +138,7 @@ class AnalyzeByXPath(doc: Any) {
val temp = getString(rl) val temp = getString(rl)
if (!temp.isNullOrEmpty()) { if (!temp.isNullOrEmpty()) {
textList.add(temp) textList.add(temp)
if (elementsType == "|") { if (ruleAnalyzes.elementsType == "||") {
break break
} }
} }

@ -0,0 +1,515 @@
package io.legado.app.model.analyzeRule
//通用的规则切分处理
class RuleAnalyzer(data: String) {
private var queue: String = data //被处理字符串
private var pos = 0 //处理到的位置
private var start = 0 //每次处理字段的开始
private var end:Int = queue.length //每次处理字段的终点
private var step:Int = 0 //分割字符的长度
var elementsType = ""
//当前平衡字段
fun currBalancedString( stepStart:Int = 1 , stepEnd:Int = 1): String { //stepStart平衡字符的起始分隔字串长度,stepEnd平衡字符的结束分隔字串长度
return queue.substring(start+stepStart,pos-stepEnd) //当前平衡字段
}
//将pos重置为0,方便复用
fun reSetPos() {
pos = 0
}
//当前拉取字段
fun currString(): String {
return queue.substring(start,pos) //当前拉取到的字段
}
//剩余字串
fun remainingString(): String {
start = pos
pos = queue.length
return queue.substring(start)
}
/**
* pos位置回退
*/
fun back(num :Int = 0) {
if(num == 0)pos = start //回退
else pos -= num
}
/**
* pos位置后移
*/
fun advance(num :Int = 1) {
pos+=num
}
/**
* 是否已无剩余字符?
* @return 若剩余字串中已无字符则返回true
*/
val isEmpty: Boolean
get() = queue.length - pos == 0 //是否处理到最后
/**
* 检索并返回首字符,但pos不变
* @return 首字符若为空则为 0 号字符
*/
fun peek(): Char { //检索首字符
return if (isEmpty) 0.toChar() else queue[pos]
}
/**
* 消耗剩余字串中一个字符
* @return 返回剩余字串中的下个字符
*/
fun consume(): Char {
return queue[pos++]
}
/**
* 字串与剩余字串是否匹配不区分大小写
* @param seq 字符串被检查
* @return 若下字符串匹配返回 true
*/
fun matches(seq: String): Boolean {
return queue.regionMatches(pos, seq, 0, seq.length, ignoreCase = true)
}
/**
* 测试下个字符是否与序列中相应位置的字符相等
* @param seq 被检查的字符列表
* @return 相等就为 true 不相等则为 false
*/
fun matchesAny(vararg seq: Char): Boolean {
if (isEmpty) return false
for (c in seq) {
if (queue[pos] == c) {
return true
}
}
return false
}
/**
* 测试下个字符是否与参数列表里的序列存在匹配 不区分大小写
* @param seq 被不区分大小写检查的字符串列表
* @return 只要匹配就为 true 没有匹配则为 false
*/
fun matchesAny(vararg seq: String): Boolean {
for (s in seq) {
if (matches(s)) {
step = s.length
return true
}
}
return false
}
/**
* 从剩余字串中拉出一个字符串直到但不包括匹配序列或剩余字串用完
* @param seq 分隔字符 **区分大小写**
* @return 是否找到相应字段
*/
fun consumeTo(seq: String,setStartPos:Boolean = true): Boolean {
if(setStartPos)start = pos //将处理到的位置设置为规则起点
val offset = queue.indexOf(seq, pos)
return if (offset != -1) {
pos = offset
true
} else false
}
/**
* 从剩余字串中拉出一个字符串直到但不包括匹配序列匹配参数列表中一项即为匹配或剩余字串用完
* @param f 消费函数返回true表示消费fasle表示不消费
* @param setStartPos 设置开始消费位置
* @return 消耗的字符串
*/
fun consumeToAny(setStartPos:Boolean = true, f:()->Boolean,): Boolean {
if(setStartPos)start = pos //将处理到的位置设置为规则起点
while (!isEmpty && !f()) {
pos++
}
return !isEmpty
}
//其中js只要符合语法,就不用避开任何阅读关键字,自由发挥
fun chompJsBalanced(f: ((Char) -> Boolean?) = {
if ( it == '{' )true //开始嵌套一层
else if ( it == '}') false //闭合一层嵌套
else null
} ): Boolean {
start = pos
var depth = 0 //嵌套深度
var bracketsDepth = 0 //[]嵌套深度
var inSingleQuote = false //单引号
var inDoubleQuote = false //双引号
var inOtherQuote = false //js原始字串分隔字符
var regex = false //正则
var commit = false //单行注释
var commits = false //多行注释
do {
if (isEmpty) break
var c = consume()
if (c != '\\') { //非转义字符
if (c == '\'' && !commits && !commit && !regex && !inDoubleQuote && !inOtherQuote) inSingleQuote = !inSingleQuote //匹配具有语法功能的单引号
else if (c == '"' && !commits && !commit && !regex && !inSingleQuote && !inOtherQuote) inDoubleQuote = !inDoubleQuote //匹配具有语法功能的双引号
else if (c == '`' && !commits && !commit && !regex && !inSingleQuote && !inDoubleQuote) inOtherQuote = !inOtherQuote //匹配具有语法功能的'`'
else if (c == '/' && !commits && !commit && !regex && !inSingleQuote && !inDoubleQuote && !inOtherQuote) { //匹配注释或正则起点
c = consume()
when(c){
'/'->commit=true //匹配单行注释起点
'*'->commits=true //匹配多行注释起点
else ->regex=true //匹配正则起点
}
}
else if(commits && c == '*') { //匹配多行注释终点
c = consume()
if(c == '/')commits = false
}
else if(regex && c == '/') { //正则的终点或[]平衡
if(c == '/')regex = false//匹配正则终点
//为了保证当open为( 且 close 为 )时,正则中[(]或[)]的合法性。故对[]这对在任何规则中都平衡的成对符号做匹配。
// 注:正则里[(]、[)]、[{]、[}]都是合法的,所以只有[]必须平衡。
else if ( c == '[' )bracketsDepth++ //开始嵌套一层[]
else if ( c== ']') bracketsDepth-- //闭合一层嵌套[]
}
if (commits || commit || regex || inSingleQuote || inDoubleQuote || inOtherQuote) continue //语法单元未匹配结束,直接进入下个循环
val fn = f(c)
if (fn == null) continue
if (fn) depth++ else depth-- //嵌套或者闭合
}else { //转义字符
var next = consume() //拉出被转义字符
if(commit && next == 'n') commit = false //匹配单行注释终点。当前为\,下个为n,表示换行
else if (!commits && !commit && next == '\\') {
consume() //当前为\,下个为\,双重转义中"\\"表示转义字符本身,根据if条件"\\"字串不在注释中,则只能在字串或正则中
next = consume() //拉出下个字符,因为在双重转义的字串或正则中,类似于 \\/ 这样的结构才是转义结构
if(next == '\\')consume() //若为转义字符则继续拉出,因为双重转义中转义字符成对存在,即 \\\\
}
}
} while (depth > 0 || bracketsDepth >0) //拉出全部符合js语法的字段
if(depth > 0 || bracketsDepth >0) start = pos
return pos > start
}
/**
* 在双重转义字串中拉出一个规则平衡组
*/
fun chompRuleBalanced(open: Char = '[', close: Char = ']',f: ((Char) ->Boolean?)? = null ): Boolean {
start = pos
var depth = 0 //嵌套深度
var otherDepth = 0 //其他对称符合嵌套深度
var inSingleQuote = false //单引号
var inDoubleQuote = false //双引号
do {
if (isEmpty) break
val c = consume()
if (c != ESC) { //非转义字符
if (c == '\'' && !inDoubleQuote) inSingleQuote = !inSingleQuote //匹配具有语法功能的单引号
else if (c == '"' && !inSingleQuote) inDoubleQuote = !inDoubleQuote //匹配具有语法功能的双引号
if (inSingleQuote || inDoubleQuote) continue //语法单元未匹配结束,直接进入下个循环
if ( c == open )depth++ //开始嵌套一层
else if ( c== close) depth-- //闭合一层嵌套
else if(depth == 0 && f != null) { //处于默认嵌套中的非默认字符不需要平衡,仅depth为0时默认嵌套全部闭合,此字符才进行嵌套
val fn = f(c)
if (fn == null) continue
if (fn) otherDepth++ else otherDepth--
}
}else { //转义字符
var next = consume() //拉出被转义字符,匹配\/、\"、\'等
if (next == ESC) {
consume() //当前为\,下个为\,双重转义中"\\"表示转义字符本身,根据语法特征当前字段在字串或正则中
next = consume() //拉出下个字符,因为在双重转义的字串或正则中,类似于 \\/ 这样的结构才是转义结构
if(next == ESC)consume() //若为转义字符则继续拉出,因为双重转义中转义字符成对存在,即 \\\\
}
}
} while (depth > 0 || otherDepth > 0) //拉出一个平衡字串
return !(depth > 0 || otherDepth > 0) //平衡返回false,不平衡返回true
}
/**
* 不用正则,不到最后不切片也不用中间变量存储,只在序列中标记当前查找字段的开头结尾,到返回时才切片,高效快速准确切割规则
* 解决jsonPath自带的"&&""||"与阅读的规则冲突,以及规则正则或字符串中包含"&&""||""%%"而导致的冲突
*/
tailrec fun splitRule(vararg split: String): Array<String>{ //首段匹配,elementsType为空
if (!consumeToAny { matchesAny(* split) }) return arrayOf(queue) //未找到分隔符
end = pos
val st = if( consumeToAny(false){ matchesAny( '(','[' ) } )pos else -1 //查找筛选器
pos = end
if(st == -1) {
var rule = arrayOf(queue.substring(0, pos)) //压入分隔的首段规则到数组
pos += step //跳过分隔符
elementsType = queue.substring(pos - step, pos) //设置组合类型
while (consumeToAny { matchesAny(* split) }) { //循环切分规则压入数组
rule += queue.substring(start, pos)
pos += step //跳过分隔符
}
rule+= queue.substring(start) //将剩余字段压入数组末尾
return rule
}
val rule = if(st >pos ){ //先匹配到st1pos,表明"&&","||"不在选择器中,将选择器前"&&","||"分隔的字段依次压入数组
var rule = arrayOf(queue.substring(0, pos)) //压入分隔的首段规则到数组
pos += step //跳过分隔符
elementsType = queue.substring(pos - step, pos) //设置组合类型
while (pos < st && consumeToAny { matchesAny( * split ) }) {
rule += queue.substring(start, pos) //循环切分规则压入数组
pos += step //跳过分隔符
}
rule
}else null
pos = st //位置推移到筛选器处
val next = if(queue[pos] == '[' ) ']' else ')' //平衡组末尾字符
return if (rule == null) { //rule为空,首段未匹配完成
if(!chompRuleBalanced(queue[pos],next)) throw Error(queue.substring(0, start)+"后未平衡") //拉出一个筛选器,不平衡则报错
splitRule(* split) //递归调用首段匹配
} else {
val start0 = start //记录当前规则开头位置
if(!chompRuleBalanced(queue[pos],next)) throw Error(queue.substring(0, start)+"后未平衡") //拉出一个筛选器,不平衡则报错
start = start0 //筛选器的开头不是本段规则开头,故恢复开头设置
splitRule(rule) //首段已匹配,但当前段匹配未完成,调用二段匹配
}
}
@JvmName("splitRuleNext")
private tailrec fun splitRule(rules:Array<String>): Array<String>{ //二段匹配被调用,elementsType非空(已在首段赋值),直接按elementsType查找,比首段采用的方式更快
if (!consumeTo(elementsType,false)) return rules + queue.substring(start) //此处consumeTo(...)开始位置不是规则的开始位置,start沿用上次设置
end = pos
val st = if( consumeToAny(false){ matchesAny( '(','[' ) } )pos else -1 //查找筛选器
pos = end
if(st == -1) {
var rule = rules + queue.substring(start, pos) //压入本次分隔的首段规则到数组
pos += step //跳过分隔符
while (consumeTo(elementsType)) { //循环切分规则压入数组
rule += queue.substring(start, pos)
pos += step //跳过分隔符
}
rule += queue.substring(start) //将剩余字段压入数组末尾
return rule
}
val rule = if(st > pos ){//先匹配到st1pos,表明"&&","||"不在选择器中,将选择器前"&&","||"分隔的字段依次压入数组
var rule = rules + queue.substring(start, pos) //压入本次分隔的首段规则到数组
pos += step //跳过分隔符
while (pos < st && consumeTo(elementsType)) { //循环切分规则压入数组
rule += queue.substring(start, pos)
pos += step //跳过分隔符
}
rule
}else rules
pos = st //位置推移到筛选器处
val next = if(queue[pos] == '[' ) ']' else ')' //平衡组末尾字符
val start0 = start //记录当前规则开头位置
if(!chompRuleBalanced(queue[pos],next)) throw Error(queue.substring(0, start)+"后未平衡") //拉出一个筛选器,不平衡时返回true,表示未平衡
start = start0 //筛选器平衡,但筛选器的开头不是当前规则开头,故恢复开头设置
return splitRule(rule) //递归匹配
}
/**
* 替换内嵌规则
* @param inner 起始标志,{$. {{
* @param startStep 不属于规则部分的前置字符长度{$.{不属于规则的组成部分故startStep为1
* @param endStep 不属于规则部分的后置字符长度}}长度为2
* @param fr 查找到内嵌规则时用于解析的函数
*
* */
fun innerRule( inner:String,startStep:Int = 1,endStep:Int = 1,fr:(String)->String?): String {
val start0 = pos //规则匹配前起点
val st = StringBuilder()
while (!isEmpty && consumeTo(inner)) { //拉取成功返回true,ruleAnalyzes里的字符序列索引变量pos后移相应位置,否则返回false,且isEmpty为true
val start1 = start //记录拉取前起点
if (chompRuleBalanced {//拉出一个以[]为默认嵌套、以{}为补充嵌套的平衡字段
when (it) {
'{' -> true
'}' -> false
else -> null
}
}) {
val frv= fr(currBalancedString(startStep,endStep))
if(frv != null) {
st.append(queue.substring(start1,start)+frv) //压入内嵌规则前的内容,及内嵌规则解析得到的字符串
continue //获取内容成功,继续选择下个内嵌规则
}
}
start = start1 //拉出字段不平衡,重置起点
pos = start + inner.length //拉出字段不平衡,inner只是个普通字串,规则回退到开头,并跳到此inner后继续匹配
}
//匹配前起点与当前规则起点相同,证明无替换成功的内嵌规则,返回空字符串。否则返回替换后的字符串
return if(start0 == start) "" else {
st.append(remainingString()) //压入剩余字符串
st.toString()
}
}
// /**
// * 匹配并返回标签中的属性键字串(字母、数字、-、_、:)
// * @return 属性键字串
// */
// fun consumeAttributeKey(start:Int = pos): String {
// while (!isEmpty && (Character.isLetterOrDigit(queue[pos]) || matchesAny('-', '_', ':'))) pos++
// return queue.substring(start, pos)
// }
// fun splitRule(query:String,item:String = "other",listItem:String = "allInOne"):String{
//
// val cuurItem = item //当前项类型,list->列表项 mulu->章节列表项 url->链接项 search->搜索链接项 find发现链接列表项 other->其他项
// val cuurList = listItem//当前界面总列表项类型,allInOne,json,xml,kotin,java
// var Reverse = false //是否反转列表
//
// consumeWhitespace() //消耗开头空白
// var fisrt = consume() //拉出并消费首字符
//
// when(item){
// "search" ->
// "find" ->
// "mulu" -> if(fisrt == '-'){
// Reverse=true //开启反转
// consumeWhitespace() //拉出所有空白符
// fisrt = consume() //首字符后移
// }
// else ->
//
// }
//
// return query
// }
companion object {
/**
* 转义字符
*/
private const val ESC = '\\'
/**
* 阅读共有分隔字串起始部分
* "##","@@","{{","{[","<js>", "@js:"
*/
val splitList =arrayOf("##","@@","{{","{[","<js>", "@js:")
/**
* 发现名称-链接分隔字串
* "::"
*/
const val splitListFaXian = "::"
/**
* 目录专有起始字符
* "-"
*/
const val splitListMulu = "-"
/**
* 结果为元素列表的 all in one 模式起始字符
* "+"
*/
const val splitListTongYi = "+"
/**
* 结果为元素列表的项的同规则组合结构
* "||","&&","%%"
*/
val splitListReSplit = arrayOf("||","&&","%%")
/**
* js脚本结束字串
* "</js>"
*/
const val splitListEndJS = "</js>"
/**
*内嵌js结束字串
* "}}"
*/
const val splitListEndInnerJS = "}}"
/**
* 内嵌规则结束字串
* "]}"
*/
const val splitListEndInnerRule = "]}"
/**
* '[', ']', '(', ')','{','}'
*/
val splitListPublic = charArrayOf('[', ']', '(', ')','{','}')
/**
* '*',"/","//",":","::","@","|","@xpath:"
*/
val splitListXpath = arrayOf("*","/","//",":","::","@","|","@xpath:")
/**
* '*','$',".","..", "@json:"
*/
val splitListJson = arrayOf('*','$',".","..", "@json:")
/**
* '*',"+","~",".",",","|","@","@css:",":"
*/
val splitListCss = arrayOf('*',"+","~",".",",","|","@","@css:",":")
/**
* "-",".","!","@","@@"
*/
val splitListDefault = arrayOf("-",".","!","@","@@")
}
}
Loading…
Cancel
Save