优化规则解析

pull/423/head
gedoor 5 years ago
parent 94f2766f8d
commit 9b1c700ab5
  1. 19
      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 @Keep
class AnalyzeByJSonPath { class AnalyzeByJSonPath {
private var ctx: ReadContext? = null private lateinit var ctx: ReadContext
fun parse(json: Any): AnalyzeByJSonPath { fun parse(json: Any): AnalyzeByJSonPath {
ctx = if (json is String) { ctx = if (json is String) {
@ -35,9 +35,9 @@ class AnalyzeByJSonPath {
} }
if (rules.size == 1) { if (rules.size == 1) {
if (!rule.contains("{$.")) { if (!rule.contains("{$.")) {
ctx?.let { try {
result = try { val ob = ctx.read<Any>(rule)
val ob = it.read<Any>(rule) result =
if (ob is List<*>) { if (ob is List<*>) {
val builder = StringBuilder() val builder = StringBuilder()
for (o in ob) { for (o in ob) {
@ -47,9 +47,7 @@ class AnalyzeByJSonPath {
} else { } else {
ob.toString() ob.toString()
} }
} catch (ignored: Exception) { } catch (ignored: Exception) {
rule
}
} }
return result return result
} else { } else {
@ -100,7 +98,7 @@ class AnalyzeByJSonPath {
if (rules.size == 1) { if (rules.size == 1) {
if (!rule.contains("{$.")) { if (!rule.contains("{$.")) {
try { try {
val obj = ctx!!.read<Any>(rule) ?: return result val obj = ctx.read<Any>(rule) ?: return result
if (obj is List<*>) { if (obj is List<*>) {
for (o in obj) for (o in obj)
result.add(o.toString()) result.add(o.toString())
@ -108,7 +106,6 @@ class AnalyzeByJSonPath {
result.add(obj.toString()) result.add(obj.toString())
} }
} catch (ignored: Exception) { } catch (ignored: Exception) {
result.add(rule)
} }
return result return result
} else { } else {
@ -152,7 +149,7 @@ class AnalyzeByJSonPath {
} }
internal fun getObject(rule: String): Any { internal fun getObject(rule: String): Any {
return ctx!!.read(rule) return ctx.read(rule)
} }
internal fun getList(rule: String): ArrayList<Any>? { internal fun getList(rule: String): ArrayList<Any>? {
@ -175,7 +172,7 @@ class AnalyzeByJSonPath {
} }
} }
if (rules.size == 1) { if (rules.size == 1) {
ctx?.let { ctx.let {
try { try {
return it.read<ArrayList<Any>>(rules[0]) return it.read<ArrayList<Any>>(rules[0])
} catch (e: Exception) { } catch (e: Exception) {

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

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

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

Loading…
Cancel
Save