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