优化规则解析

pull/423/head
gedoor 4 years ago
parent 94f2766f8d
commit 9b1c700ab5
  1. 17
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByJSonPath.kt
  2. 6
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByJSoup.kt
  3. 46
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByXPath.kt
  4. 2
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt

@ -10,7 +10,7 @@ import java.util.regex.Pattern
@Keep
class AnalyzeByJSonPath {
private var ctx: ReadContext? = null
private lateinit var ctx: ReadContext
fun parse(json: Any): AnalyzeByJSonPath {
ctx = if (json is String) {
@ -35,9 +35,9 @@ class AnalyzeByJSonPath {
}
if (rules.size == 1) {
if (!rule.contains("{$.")) {
ctx?.let {
result = try {
val ob = it.read<Any>(rule)
try {
val ob = ctx.read<Any>(rule)
result =
if (ob is List<*>) {
val builder = StringBuilder()
for (o in ob) {
@ -48,8 +48,6 @@ class AnalyzeByJSonPath {
ob.toString()
}
} catch (ignored: Exception) {
rule
}
}
return result
} else {
@ -100,7 +98,7 @@ class AnalyzeByJSonPath {
if (rules.size == 1) {
if (!rule.contains("{$.")) {
try {
val obj = ctx!!.read<Any>(rule) ?: return result
val obj = ctx.read<Any>(rule) ?: return result
if (obj is List<*>) {
for (o in obj)
result.add(o.toString())
@ -108,7 +106,6 @@ class AnalyzeByJSonPath {
result.add(obj.toString())
}
} catch (ignored: Exception) {
result.add(rule)
}
return result
} else {
@ -152,7 +149,7 @@ class AnalyzeByJSonPath {
}
internal fun getObject(rule: String): Any {
return ctx!!.read(rule)
return ctx.read(rule)
}
internal fun getList(rule: String): ArrayList<Any>? {
@ -175,7 +172,7 @@ class AnalyzeByJSonPath {
}
}
if (rules.size == 1) {
ctx?.let {
ctx.let {
try {
return it.read<ArrayList<Any>>(rules[0])
} catch (e: Exception) {

@ -22,7 +22,7 @@ class AnalyzeByJSoup {
val validKeys = arrayOf("class", "id", "tag", "text", "children")
}
private var element: Element? = null
private lateinit var element: Element
fun parse(doc: Any): AnalyzeByJSoup {
element = if (doc is Element) {
@ -83,7 +83,7 @@ class AnalyzeByJSoup {
//拆分规则
val sourceRule = SourceRule(ruleStr)
if (isEmpty(sourceRule.elementsRule)) {
textS.add(element?.data() ?: "")
textS.add(element.data() ?: "")
} else {
val elementsType: String
val ruleStrS: Array<String>
@ -107,7 +107,7 @@ class AnalyzeByJSoup {
temp = if (sourceRule.isCss) {
val lastIndex = ruleStrX.lastIndexOf('@')
getResultLast(
element!!.select(ruleStrX.substring(0, lastIndex)),
element.select(ruleStrX.substring(0, lastIndex)),
ruleStrX.substring(lastIndex + 1)
)
} else {

@ -12,28 +12,15 @@ import java.util.*
@Keep
class AnalyzeByXPath {
private var jxDocument: JXDocument? = null
private var jxNode: JXNode? = null
private lateinit var jxNode: Any
fun parse(doc: Any): AnalyzeByXPath {
if (doc is JXNode) {
jxNode = doc
if (jxNode?.isElement == false) {
jxDocument = strToJXDocument(doc.toString())
jxNode = null
}
} else if (doc is Document) {
jxDocument = JXDocument.create(doc)
jxNode = null
} else if (doc is Element) {
jxDocument = JXDocument.create(Elements(doc))
jxNode = null
} else if (doc is Elements) {
jxDocument = JXDocument.create(doc)
jxNode = null
} else {
jxDocument = strToJXDocument(doc.toString())
jxNode = null
jxNode = 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
}
@ -49,6 +36,15 @@ class AnalyzeByXPath {
return JXDocument.create(html1)
}
private fun getResult(xPath: String): List<JXNode>? {
val node = jxNode
return if (node is JXNode) {
node.sel(xPath)
} else {
(node as JXDocument).selN(xPath)
}
}
internal fun getElements(xPath: String): List<JXNode>? {
if (TextUtils.isEmpty(xPath)) {
return null
@ -71,7 +67,7 @@ class AnalyzeByXPath {
}
}
if (rules.size == 1) {
return jxNode?.sel(rules[0]) ?: jxDocument?.selN(rules[0])
return getResult(rules[0])
} else {
val results = ArrayList<List<JXNode>>()
for (rl in rules) {
@ -121,8 +117,7 @@ class AnalyzeByXPath {
}
}
if (rules.size == 1) {
val jxNodes = jxNode?.sel(xPath) ?: jxDocument?.selN(xPath)
jxNodes?.map {
getResult(xPath)?.map {
result.add(it.asString())
}
return result
@ -167,9 +162,8 @@ class AnalyzeByXPath {
elementsType = "|"
}
if (rules.size == 1) {
val jxNodes = jxNode?.sel(rule) ?: jxDocument?.selN(rule)
jxNodes?.let {
return TextUtils.join("\n", jxNodes)
getResult(rule)?.let {
return TextUtils.join("\n", it)
}
return null
} else {

@ -461,7 +461,7 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions {
mode = Mode.Regex
}
splitRegex(tmp)
} else if (mode != Mode.Js && mode != Mode.Json && mode != Mode.Regex
} else if (mode != Mode.Js && mode != Mode.Regex
&& evalMatcher.start() == 0
) {
mode = Mode.Regex

Loading…
Cancel
Save