|
|
@ -19,7 +19,7 @@ import javax.script.SimpleBindings |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@Keep |
|
|
|
@Keep |
|
|
|
class AnalyzeRule(private var book: BaseBook? = null) { |
|
|
|
class AnalyzeRule(private var book: BaseBook? = null) { |
|
|
|
private var `object`: Any? = null |
|
|
|
private var content: Any? = null |
|
|
|
private var isJSON: Boolean = false |
|
|
|
private var isJSON: Boolean = false |
|
|
|
private var baseUrl: String? = null |
|
|
|
private var baseUrl: String? = null |
|
|
|
|
|
|
|
|
|
|
@ -36,10 +36,10 @@ class AnalyzeRule(private var book: BaseBook? = null) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@JvmOverloads |
|
|
|
@JvmOverloads |
|
|
|
fun setContent(body: Any?, baseUrl: String? = this.baseUrl): AnalyzeRule { |
|
|
|
fun setContent(content: Any?, baseUrl: String? = this.baseUrl): AnalyzeRule { |
|
|
|
if (body == null) throw AssertionError("Content cannot be null") |
|
|
|
if (content == null) throw AssertionError("Content cannot be null") |
|
|
|
isJSON = body.toString().isJson() |
|
|
|
isJSON = content.toString().isJson() |
|
|
|
`object` = body |
|
|
|
this.content = content |
|
|
|
this.baseUrl = baseUrl?.split("\n".toRegex(), 1)?.get(0) |
|
|
|
this.baseUrl = baseUrl?.split("\n".toRegex(), 1)?.get(0) |
|
|
|
objectChangedXP = true |
|
|
|
objectChangedXP = true |
|
|
|
objectChangedJS = true |
|
|
|
objectChangedJS = true |
|
|
@ -51,7 +51,7 @@ class AnalyzeRule(private var book: BaseBook? = null) { |
|
|
|
* 获取XPath解析类 |
|
|
|
* 获取XPath解析类 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private fun getAnalyzeByXPath(o: Any): AnalyzeByXPath { |
|
|
|
private fun getAnalyzeByXPath(o: Any): AnalyzeByXPath { |
|
|
|
return if (o != `object`) { |
|
|
|
return if (o != content) { |
|
|
|
AnalyzeByXPath().parse(o) |
|
|
|
AnalyzeByXPath().parse(o) |
|
|
|
} else getAnalyzeByXPath() |
|
|
|
} else getAnalyzeByXPath() |
|
|
|
} |
|
|
|
} |
|
|
@ -59,7 +59,7 @@ class AnalyzeRule(private var book: BaseBook? = null) { |
|
|
|
private fun getAnalyzeByXPath(): AnalyzeByXPath { |
|
|
|
private fun getAnalyzeByXPath(): AnalyzeByXPath { |
|
|
|
if (analyzeByXPath == null || objectChangedXP) { |
|
|
|
if (analyzeByXPath == null || objectChangedXP) { |
|
|
|
analyzeByXPath = AnalyzeByXPath() |
|
|
|
analyzeByXPath = AnalyzeByXPath() |
|
|
|
analyzeByXPath!!.parse(`object`!!) |
|
|
|
analyzeByXPath!!.parse(content!!) |
|
|
|
objectChangedXP = false |
|
|
|
objectChangedXP = false |
|
|
|
} |
|
|
|
} |
|
|
|
return analyzeByXPath as AnalyzeByXPath |
|
|
|
return analyzeByXPath as AnalyzeByXPath |
|
|
@ -69,7 +69,7 @@ class AnalyzeRule(private var book: BaseBook? = null) { |
|
|
|
* 获取JSOUP解析类 |
|
|
|
* 获取JSOUP解析类 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private fun getAnalyzeByJSoup(o: Any): AnalyzeByJSoup { |
|
|
|
private fun getAnalyzeByJSoup(o: Any): AnalyzeByJSoup { |
|
|
|
return if (o != `object`) { |
|
|
|
return if (o != content) { |
|
|
|
AnalyzeByJSoup().parse(o) |
|
|
|
AnalyzeByJSoup().parse(o) |
|
|
|
} else getAnalyzeByJSoup() |
|
|
|
} else getAnalyzeByJSoup() |
|
|
|
} |
|
|
|
} |
|
|
@ -77,7 +77,7 @@ class AnalyzeRule(private var book: BaseBook? = null) { |
|
|
|
private fun getAnalyzeByJSoup(): AnalyzeByJSoup { |
|
|
|
private fun getAnalyzeByJSoup(): AnalyzeByJSoup { |
|
|
|
if (analyzeByJSoup == null || objectChangedJS) { |
|
|
|
if (analyzeByJSoup == null || objectChangedJS) { |
|
|
|
analyzeByJSoup = AnalyzeByJSoup() |
|
|
|
analyzeByJSoup = AnalyzeByJSoup() |
|
|
|
analyzeByJSoup!!.parse(`object`!!) |
|
|
|
analyzeByJSoup!!.parse(content!!) |
|
|
|
objectChangedJS = false |
|
|
|
objectChangedJS = false |
|
|
|
} |
|
|
|
} |
|
|
|
return analyzeByJSoup as AnalyzeByJSoup |
|
|
|
return analyzeByJSoup as AnalyzeByJSoup |
|
|
@ -87,7 +87,7 @@ class AnalyzeRule(private var book: BaseBook? = null) { |
|
|
|
* 获取JSON解析类 |
|
|
|
* 获取JSON解析类 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private fun getAnalyzeByJSonPath(o: Any): AnalyzeByJSonPath { |
|
|
|
private fun getAnalyzeByJSonPath(o: Any): AnalyzeByJSonPath { |
|
|
|
return if (o != `object`) { |
|
|
|
return if (o != content) { |
|
|
|
AnalyzeByJSonPath().parse(o) |
|
|
|
AnalyzeByJSonPath().parse(o) |
|
|
|
} else getAnalyzeByJSonPath() |
|
|
|
} else getAnalyzeByJSonPath() |
|
|
|
} |
|
|
|
} |
|
|
@ -95,7 +95,7 @@ class AnalyzeRule(private var book: BaseBook? = null) { |
|
|
|
private fun getAnalyzeByJSonPath(): AnalyzeByJSonPath { |
|
|
|
private fun getAnalyzeByJSonPath(): AnalyzeByJSonPath { |
|
|
|
if (analyzeByJSonPath == null || objectChangedJP) { |
|
|
|
if (analyzeByJSonPath == null || objectChangedJP) { |
|
|
|
analyzeByJSonPath = AnalyzeByJSonPath() |
|
|
|
analyzeByJSonPath = AnalyzeByJSonPath() |
|
|
|
analyzeByJSonPath!!.parse(`object`!!) |
|
|
|
analyzeByJSonPath!!.parse(content!!) |
|
|
|
objectChangedJP = false |
|
|
|
objectChangedJP = false |
|
|
|
} |
|
|
|
} |
|
|
|
return analyzeByJSonPath as AnalyzeByJSonPath |
|
|
|
return analyzeByJSonPath as AnalyzeByJSonPath |
|
|
@ -116,7 +116,7 @@ class AnalyzeRule(private var book: BaseBook? = null) { |
|
|
|
@Throws(Exception::class) |
|
|
|
@Throws(Exception::class) |
|
|
|
fun getStringList(ruleList: List<SourceRule>, isUrl: Boolean): List<String>? { |
|
|
|
fun getStringList(ruleList: List<SourceRule>, isUrl: Boolean): List<String>? { |
|
|
|
var result: Any? = null |
|
|
|
var result: Any? = null |
|
|
|
`object`?.let { o -> |
|
|
|
content?.let { o -> |
|
|
|
if (ruleList.isNotEmpty()) { |
|
|
|
if (ruleList.isNotEmpty()) { |
|
|
|
if (ruleList.isNotEmpty()) result = o |
|
|
|
if (ruleList.isNotEmpty()) result = o |
|
|
|
for (rule in ruleList) { |
|
|
|
for (rule in ruleList) { |
|
|
@ -184,7 +184,7 @@ class AnalyzeRule(private var book: BaseBook? = null) { |
|
|
|
@JvmOverloads |
|
|
|
@JvmOverloads |
|
|
|
fun getString(ruleList: List<SourceRule>, isUrl: Boolean = false): String { |
|
|
|
fun getString(ruleList: List<SourceRule>, isUrl: Boolean = false): String { |
|
|
|
var result: Any? = null |
|
|
|
var result: Any? = null |
|
|
|
`object`?.let { o -> |
|
|
|
content?.let { o -> |
|
|
|
if (ruleList.isNotEmpty()) result = o |
|
|
|
if (ruleList.isNotEmpty()) result = o |
|
|
|
for (rule in ruleList) { |
|
|
|
for (rule in ruleList) { |
|
|
|
result?.let { |
|
|
|
result?.let { |
|
|
@ -223,7 +223,7 @@ class AnalyzeRule(private var book: BaseBook? = null) { |
|
|
|
if (TextUtils.isEmpty(ruleStr)) return null |
|
|
|
if (TextUtils.isEmpty(ruleStr)) return null |
|
|
|
var result: Any? = null |
|
|
|
var result: Any? = null |
|
|
|
val ruleList = splitSourceRule(ruleStr) |
|
|
|
val ruleList = splitSourceRule(ruleStr) |
|
|
|
`object`?.let { o -> |
|
|
|
content?.let { o -> |
|
|
|
if (ruleList.isNotEmpty()) result = o |
|
|
|
if (ruleList.isNotEmpty()) result = o |
|
|
|
for (rule in ruleList) { |
|
|
|
for (rule in ruleList) { |
|
|
|
result?.let { |
|
|
|
result?.let { |
|
|
@ -250,13 +250,13 @@ class AnalyzeRule(private var book: BaseBook? = null) { |
|
|
|
fun getElements(ruleStr: String): List<Any> { |
|
|
|
fun getElements(ruleStr: String): List<Any> { |
|
|
|
var result: Any? = null |
|
|
|
var result: Any? = null |
|
|
|
val ruleList = splitSourceRule(ruleStr) |
|
|
|
val ruleList = splitSourceRule(ruleStr) |
|
|
|
`object`?.let { o -> |
|
|
|
content?.let { o -> |
|
|
|
if (ruleList.isNotEmpty()) result = o |
|
|
|
if (ruleList.isNotEmpty()) result = o |
|
|
|
for (rule in ruleList) { |
|
|
|
for (rule in ruleList) { |
|
|
|
result?.let { |
|
|
|
result?.let { |
|
|
|
when (rule.mode) { |
|
|
|
when (rule.mode) { |
|
|
|
Mode.Js -> { |
|
|
|
Mode.Js -> { |
|
|
|
if (result == null) result = `object` |
|
|
|
if (result == null) result = content |
|
|
|
result = evalJS(rule.rule, result) |
|
|
|
result = evalJS(rule.rule, result) |
|
|
|
} |
|
|
|
} |
|
|
|
Mode.JSon -> result = getAnalyzeByJSonPath(it).getList(rule.rule) |
|
|
|
Mode.JSon -> result = getAnalyzeByJSonPath(it).getList(rule.rule) |
|
|
@ -352,7 +352,7 @@ class AnalyzeRule(private var book: BaseBook? = null) { |
|
|
|
val sb = StringBuffer(ruleStr.length) |
|
|
|
val sb = StringBuffer(ruleStr.length) |
|
|
|
val expMatcher = EXP_PATTERN.matcher(ruleStr) |
|
|
|
val expMatcher = EXP_PATTERN.matcher(ruleStr) |
|
|
|
while (expMatcher.find()) { |
|
|
|
while (expMatcher.find()) { |
|
|
|
jsEval = evalJS(expMatcher.group(1), `object`) |
|
|
|
jsEval = evalJS(expMatcher.group(1), content) |
|
|
|
if (jsEval is String) { |
|
|
|
if (jsEval is String) { |
|
|
|
expMatcher.appendReplacement(sb, jsEval) |
|
|
|
expMatcher.appendReplacement(sb, jsEval) |
|
|
|
} else if (jsEval is Double && jsEval % 1.0 == 0.0) { |
|
|
|
} else if (jsEval is Double && jsEval % 1.0 == 0.0) { |
|
|
|