pull/32/head
kunfei 5 years ago
parent 535740dbdf
commit e93c60091b
  1. 142
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByJSoup.kt
  2. 20
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt

@ -17,8 +17,6 @@ import java.util.*
*/ */
class AnalyzeByJSoup { class AnalyzeByJSoup {
private val validKeys = listOf("class", "id", "tag", "text")
private var element: Element? = null private var element: Element? = null
fun parse(doc: Any): AnalyzeByJSoup { fun parse(doc: Any): AnalyzeByJSoup {
@ -75,44 +73,58 @@ class AnalyzeByJSoup {
return textS return textS
} }
//拆分规则 //拆分规则
val elementsType: String val sourceRule = SourceRule(ruleStr)
val ruleStrS: Array<String> if (isEmpty(sourceRule.elementsRule)) {
when { textS.add(element!!.data())
ruleStr.contains("&&") -> { } else {
elementsType = "&" val elementsType: String
ruleStrS = ruleStr.splitNotBlank("&&") val ruleStrS: Array<String>
} when {
ruleStr.contains("%%") -> { sourceRule.elementsRule.contains("&&") -> {
elementsType = "%" elementsType = "&"
ruleStrS = ruleStr.splitNotBlank("%%") ruleStrS = sourceRule.elementsRule.splitNotBlank("&&")
} }
else -> { sourceRule.elementsRule.contains("%%") -> {
elementsType = "|" elementsType = "%"
ruleStrS = ruleStr.splitNotBlank("||") ruleStrS = sourceRule.elementsRule.splitNotBlank("%%")
}
else -> {
elementsType = "|"
ruleStrS = sourceRule.elementsRule.splitNotBlank("||")
}
} }
} val results = ArrayList<List<String>>()
val results = ArrayList<List<String>>() for (ruleStrX in ruleStrS) {
for (ruleStrX in ruleStrS) { val temp: List<String>?
val temp: List<String>? = getResultList(ruleStrX) temp = if (sourceRule.isCss) {
if (temp != null && temp.isNotEmpty()) { val lastIndex = ruleStrX.lastIndexOf('@')
results.add(temp) getResultLast(
if (results.isNotEmpty() && elementsType == "|") { element!!.select(ruleStrX.substring(0, lastIndex)),
break ruleStrX.substring(lastIndex + 1)
)
} else {
getResultList(ruleStrX)
}
if (temp != null && temp.isNotEmpty()) {
results.add(temp)
if (results.isNotEmpty() && elementsType == "|") {
break
}
} }
} }
} if (results.size > 0) {
if (results.size > 0) { if ("%" == elementsType) {
if ("%" == elementsType) { for (i in 0 until results[0].size) {
for (i in 0 until results[0].size) { for (temp in results) {
for (temp in results) { if (i < temp.size) {
if (i < temp.size) { textS.add(temp[i])
textS.add(temp[i]) }
} }
} }
} } else {
} else { for (temp in results) {
for (temp in results) { textS.addAll(temp)
textS.addAll(temp) }
} }
} }
} }
@ -127,28 +139,39 @@ class AnalyzeByJSoup {
if (temp == null || isEmpty(rule)) { if (temp == null || isEmpty(rule)) {
return elements return elements
} }
val sourceRule = SourceRule(rule)
val elementsType: String val elementsType: String
val ruleStrS: Array<String> val ruleStrS: Array<String>
when { when {
rule.contains("&&") -> { sourceRule.elementsRule.contains("&&") -> {
elementsType = "&" elementsType = "&"
ruleStrS = rule.splitNotBlank("&&") ruleStrS = sourceRule.elementsRule.splitNotBlank("&&")
} }
rule.contains("%%") -> { sourceRule.elementsRule.contains("%%") -> {
elementsType = "%" elementsType = "%"
ruleStrS = rule.splitNotBlank("%%") ruleStrS = sourceRule.elementsRule.splitNotBlank("%%")
} }
else -> { else -> {
elementsType = "|" elementsType = "|"
ruleStrS = rule.splitNotBlank("||") ruleStrS = sourceRule.elementsRule.splitNotBlank("||")
} }
} }
val elementsList = ArrayList<Elements>() val elementsList = ArrayList<Elements>()
for (ruleStr in ruleStrS) { if (sourceRule.isCss) {
val tempS = getElementsSingle(temp, ruleStr) for (ruleStr in ruleStrS) {
elementsList.add(tempS) val tempS = temp.select(ruleStr)
if (tempS.size > 0 && elementsType == "|") { elementsList.add(tempS)
break if (tempS.size > 0 && elementsType == "|") {
break
}
}
} else {
for (ruleStr in ruleStrS) {
val tempS = getElementsSingle(temp, ruleStr)
elementsList.add(tempS)
if (tempS.size > 0 && elementsType == "|") {
break
}
} }
} }
if (elementsList.size > 0) { if (elementsList.size > 0) {
@ -206,19 +229,21 @@ class AnalyzeByJSoup {
} }
} else { } else {
val rulePcx = rule.splitNotBlank("!") val rulePcx = rule.splitNotBlank("!")
val rulePc = rulePcx[0].trim { it <= ' ' }.splitNotBlank(">") val rulePc =
val rules = rulePc[0].trim { it <= ' ' }.splitNotBlank(".") rulePcx[0].trim { it <= ' ' }.splitNotBlank(">")
val rules =
rulePc[0].trim { it <= ' ' }.splitNotBlank(".")
var filterRules: Array<String>? = null var filterRules: Array<String>? = null
var needFilterElements = rulePc.size > 1 && !isEmpty(rulePc[1].trim { it <= ' ' }) var needFilterElements = rulePc.size > 1 && !isEmpty(rulePc[1].trim { it <= ' ' })
if (needFilterElements) { if (needFilterElements) {
filterRules = rulePc[1].trim { it <= ' ' }.splitNotBlank(".") filterRules = rulePc[1].trim { it <= ' ' }.splitNotBlank(".")
filterRules[0] = filterRules[0].trim { it <= ' ' } filterRules[0] = filterRules[0].trim { it <= ' ' }
val validKeys = listOf("class", "id", "tag", "text")
if (filterRules.size < 2 || !validKeys.contains(filterRules[0]) || isEmpty(filterRules[1].trim { it <= ' ' })) { if (filterRules.size < 2 || !validKeys.contains(filterRules[0]) || isEmpty(filterRules[1].trim { it <= ' ' })) {
needFilterElements = false needFilterElements = false
} }
filterRules[1] = filterRules[1].trim { it <= ' ' } filterRules[1] = filterRules[1].trim { it <= ' ' }
} }
var isCss = false
when (rules[0]) { when (rules[0]) {
"children" -> { "children" -> {
var children = temp.children() var children = temp.children()
@ -277,12 +302,9 @@ class AnalyzeByJSoup {
elementsByText = filterElements(elementsByText, filterRules) elementsByText = filterElements(elementsByText, filterRules)
elements.addAll(elementsByText) elements.addAll(elementsByText)
} }
else -> { else -> elements.addAll(temp.select(rulePcx[0]))
elements.addAll(temp.select(rule))
isCss = true
}
} }
if (!isCss && rulePcx.size > 1) { if (rulePcx.size > 1) {
val rulePcs = rulePcx[1].splitNotBlank(":") val rulePcs = rulePcx[1].splitNotBlank(":")
for (pc in rulePcs) { for (pc in rulePcs) {
val pcInt = Integer.parseInt(pc) val pcInt = Integer.parseInt(pc)
@ -364,4 +386,18 @@ class AnalyzeByJSoup {
return textS return textS
} }
internal inner class SourceRule(ruleStr: String) {
var isCss = false
var elementsRule: String
init {
if (ruleStr.startsWith("@CSS:", true)) {
isCss = true
elementsRule = ruleStr.substring(5).trim { it <= ' ' }
} else {
elementsRule = ruleStr
}
}
}
} }

@ -123,10 +123,11 @@ class AnalyzeRule(private var book: BaseBook? = null) {
if (rule.rule.isNotEmpty()) { if (rule.rule.isNotEmpty()) {
when (rule.mode) { when (rule.mode) {
Mode.Js -> result = evalJS(rule.rule, result) Mode.Js -> result = evalJS(rule.rule, result)
Mode.JSon -> result = getAnalyzeByJSonPath(it).getStringList(rule.rule) Mode.JSon -> result =
Mode.XPath -> result = getAnalyzeByXPath(it).getStringList(rule.rule) getAnalyzeByJSonPath(it).getStringList(rule.rule)
Mode.Css -> result = getAnalyzeByJSoup(it).getStringList(rule.rule) Mode.XPath -> result =
else -> result = rule.rule getAnalyzeByXPath(it).getStringList(rule.rule)
else -> result = getAnalyzeByJSoup(it).getStringList(rule.rule)
} }
} }
if (rule.replaceRegex.isNotEmpty() && result is List<*>) { if (rule.replaceRegex.isNotEmpty() && result is List<*>) {
@ -191,12 +192,11 @@ class AnalyzeRule(private var book: BaseBook? = null) {
Mode.Js -> result = evalJS(rule.rule, it) Mode.Js -> result = evalJS(rule.rule, it)
Mode.JSon -> result = getAnalyzeByJSonPath(it).getString(rule.rule) Mode.JSon -> result = getAnalyzeByJSonPath(it).getString(rule.rule)
Mode.XPath -> result = getAnalyzeByXPath(it).getString(rule.rule) Mode.XPath -> result = getAnalyzeByXPath(it).getString(rule.rule)
Mode.Css -> result = if (isUrl && !TextUtils.isEmpty(baseUrl)) { Mode.Default -> result = if (isUrl && !TextUtils.isEmpty(baseUrl)) {
getAnalyzeByJSoup(it).getString0(rule.rule) getAnalyzeByJSoup(it).getString0(rule.rule)
} else { } else {
getAnalyzeByJSoup(it).getString(rule.rule) getAnalyzeByJSoup(it).getString(rule.rule)
} }
else -> result = rule.rule
} }
} }
if (rule.replaceRegex.isNotEmpty()) { if (rule.replaceRegex.isNotEmpty()) {
@ -230,8 +230,7 @@ class AnalyzeRule(private var book: BaseBook? = null) {
Mode.Js -> result = evalJS(rule.rule, it) Mode.Js -> result = evalJS(rule.rule, it)
Mode.JSon -> result = getAnalyzeByJSonPath(it).getObject(rule.rule) Mode.JSon -> result = getAnalyzeByJSonPath(it).getObject(rule.rule)
Mode.XPath -> result = getAnalyzeByXPath(it).getElements(rule.rule) Mode.XPath -> result = getAnalyzeByXPath(it).getElements(rule.rule)
Mode.Css -> result = getAnalyzeByJSoup(it).getElements(rule.rule) else -> result = getAnalyzeByJSoup(it).getElements(rule.rule)
else -> result = rule.rule
} }
if (rule.replaceRegex.isNotEmpty()) { if (rule.replaceRegex.isNotEmpty()) {
result = replaceRegex(result.toString(), rule) result = replaceRegex(result.toString(), rule)
@ -261,8 +260,7 @@ class AnalyzeRule(private var book: BaseBook? = null) {
} }
Mode.JSon -> result = getAnalyzeByJSonPath(it).getList(rule.rule) Mode.JSon -> result = getAnalyzeByJSonPath(it).getList(rule.rule)
Mode.XPath -> result = getAnalyzeByXPath(it).getElements(rule.rule) Mode.XPath -> result = getAnalyzeByXPath(it).getElements(rule.rule)
Mode.Css -> result = getAnalyzeByJSoup(it).getElements(rule.rule) else -> result = getAnalyzeByJSoup(it).getElements(rule.rule)
else -> result = rule.rule
} }
if (rule.replaceRegex.isNotEmpty()) { if (rule.replaceRegex.isNotEmpty()) {
result = replaceRegex(result.toString(), rule) result = replaceRegex(result.toString(), rule)
@ -479,7 +477,7 @@ class AnalyzeRule(private var book: BaseBook? = null) {
} }
enum class Mode { enum class Mode {
XPath, JSon, Default, Js, Regex, Css XPath, JSon, Default, Js
} }
fun put(key: String, value: String): String { fun put(key: String, value: String): String {

Loading…
Cancel
Save