js参数添加doc,json数据时doc为com.jayway.jsonpath.ReadContext,网页数据doc为org.jsoup.nodes.Element

pull/473/head
gedoor 4 years ago
parent 7e1d5162a7
commit 5d317efe8f
  1. 18
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByJSonPath.kt
  2. 14
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByJSoup.kt
  3. 9
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByXPath.kt
  4. 20
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.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) {
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("(?<=\\{)\\$\\..+?(?=\\})")
}
}

@ -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) {
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)
/**
* 获取列表
*/

@ -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 {

@ -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

Loading…
Cancel
Save