|
|
|
@ -17,8 +17,7 @@ import javax.script.SimpleBindings |
|
|
|
|
import kotlin.collections.HashMap |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Created by REFGD. |
|
|
|
|
* 统一解析接口 |
|
|
|
|
* 解析规则获取结果 |
|
|
|
|
*/ |
|
|
|
|
@Keep |
|
|
|
|
@Suppress("unused", "RegExpRedundantEscape") |
|
|
|
@ -38,11 +37,11 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions { |
|
|
|
|
private var objectChangedJS = false |
|
|
|
|
private var objectChangedJP = false |
|
|
|
|
|
|
|
|
|
@Throws(Exception::class) |
|
|
|
|
fun setContent(content: Any?): AnalyzeRule { |
|
|
|
|
fun setContent(content: Any?, baseUrl: String? = null): AnalyzeRule { |
|
|
|
|
if (content == null) throw AssertionError("Content cannot be null") |
|
|
|
|
isJSON = content.toString().isJson() |
|
|
|
|
this.content = content |
|
|
|
|
setBaseUrl(baseUrl) |
|
|
|
|
isJSON = content.toString().isJson() |
|
|
|
|
objectChangedXP = true |
|
|
|
|
objectChangedJS = true |
|
|
|
|
objectChangedJP = true |
|
|
|
@ -50,8 +49,8 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun setBaseUrl(baseUrl: String?): AnalyzeRule { |
|
|
|
|
this.baseUrl = baseUrl |
|
|
|
|
baseUrl?.let { |
|
|
|
|
this.baseUrl = baseUrl |
|
|
|
|
try { |
|
|
|
|
baseURL = URL(baseUrl.substringBefore(",")) |
|
|
|
|
} catch (e: Exception) { |
|
|
|
@ -118,7 +117,6 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions { |
|
|
|
|
/** |
|
|
|
|
* 获取文本列表 |
|
|
|
|
*/ |
|
|
|
|
@Throws(Exception::class) |
|
|
|
|
@JvmOverloads |
|
|
|
|
fun getStringList(rule: String?, isUrl: Boolean = false): List<String>? { |
|
|
|
|
if (rule.isNullOrEmpty()) return null |
|
|
|
@ -126,7 +124,6 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions { |
|
|
|
|
return getStringList(ruleList, isUrl) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Throws(Exception::class) |
|
|
|
|
fun getStringList(ruleList: List<SourceRule>, isUrl: Boolean = false): List<String>? { |
|
|
|
|
var result: Any? = null |
|
|
|
|
val content = this.content |
|
|
|
@ -184,14 +181,12 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions { |
|
|
|
|
/** |
|
|
|
|
* 获取文本 |
|
|
|
|
*/ |
|
|
|
|
@Throws(Exception::class) |
|
|
|
|
fun getString(ruleStr: String?, isUrl: Boolean = false): String { |
|
|
|
|
if (TextUtils.isEmpty(ruleStr)) return "" |
|
|
|
|
val ruleList = splitSourceRule(ruleStr) |
|
|
|
|
return getString(ruleList, isUrl) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Throws(Exception::class) |
|
|
|
|
@JvmOverloads |
|
|
|
|
fun getString(ruleList: List<SourceRule>, isUrl: Boolean = false): String { |
|
|
|
|
var result: Any? = null |
|
|
|
@ -244,7 +239,6 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions { |
|
|
|
|
/** |
|
|
|
|
* 获取Element |
|
|
|
|
*/ |
|
|
|
|
@Throws(Exception::class) |
|
|
|
|
fun getElement(ruleStr: String): Any? { |
|
|
|
|
if (TextUtils.isEmpty(ruleStr)) return null |
|
|
|
|
var result: Any? = null |
|
|
|
@ -278,7 +272,6 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions { |
|
|
|
|
* 获取列表 |
|
|
|
|
*/ |
|
|
|
|
@Suppress("UNCHECKED_CAST") |
|
|
|
|
@Throws(Exception::class) |
|
|
|
|
fun getElements(ruleStr: String): List<Any> { |
|
|
|
|
var result: Any? = null |
|
|
|
|
val ruleList = splitSourceRule(ruleStr) |
|
|
|
@ -313,7 +306,6 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions { |
|
|
|
|
/** |
|
|
|
|
* 保存变量 |
|
|
|
|
*/ |
|
|
|
|
@Throws(Exception::class) |
|
|
|
|
private fun putRule(map: Map<String, String>) { |
|
|
|
|
for ((key, value) in map) { |
|
|
|
|
put(key, getString(value)) |
|
|
|
@ -323,7 +315,6 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions { |
|
|
|
|
/** |
|
|
|
|
* 分离put规则 |
|
|
|
|
*/ |
|
|
|
|
@Throws(Exception::class) |
|
|
|
|
private fun splitPutRule(ruleStr: String, putMap: HashMap<String, String>): String { |
|
|
|
|
var vRuleStr = ruleStr |
|
|
|
|
val putMatcher = putPattern.matcher(vRuleStr) |
|
|
|
@ -359,7 +350,6 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions { |
|
|
|
|
/** |
|
|
|
|
* 分解规则生成规则列表 |
|
|
|
|
*/ |
|
|
|
|
@Throws(Exception::class) |
|
|
|
|
fun splitSourceRule(ruleStr: String?, mode: Mode = Mode.Default): List<SourceRule> { |
|
|
|
|
var vRuleStr = ruleStr |
|
|
|
|
val ruleList = ArrayList<SourceRule>() |
|
|
|
|