From 354e00794e00f5b72bc590f1e144bb4c5965e7c1 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 6 Jul 2019 22:42:49 +0800 Subject: [PATCH 1/4] up --- app/build.gradle | 2 +- .../legado/app/model/analyzeRule/AnalyzeRule.kt | 16 +++++++++------- .../java/io/legado/app/model/book/BookList.kt | 7 +++++++ 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index c15bcc12f..08f47c12b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -82,7 +82,7 @@ dependencies { implementation 'androidx.core:core-ktx:1.2.0-alpha02' implementation 'androidx.appcompat:appcompat:1.1.0-rc01' implementation 'androidx.preference:preference:1.1.0-rc01' - implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta1' + implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta2' implementation 'com.google.android.material:material:1.1.0-alpha07' implementation 'com.google.android:flexbox:1.1.0' diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt index 19326cbf6..ed162df36 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt @@ -36,7 +36,7 @@ class AnalyzeRule(private var book: BaseBook? = null) { } @JvmOverloads - fun setContent(body: Any?, baseUrl: String? = null): AnalyzeRule { + fun setContent(body: Any?, baseUrl: String? = this.baseUrl): AnalyzeRule { if (body == null) throw AssertionError("Content cannot be null") isJSON = body.toString().isJson() `object` = body @@ -129,21 +129,23 @@ class AnalyzeRule(private var book: BaseBook? = null) { } if (result == null) return ArrayList() if (result is String) { - result = Arrays.asList((result as String?)?.htmlFormat()?.split("\n")) + result = listOf((result as String?)?.htmlFormat()?.split("\n")) } baseUrl?.let { if (isUrl && !TextUtils.isEmpty(it)) { val urlList = ArrayList() - for (url in (result as List?)!!) { - val absoluteURL = NetworkUtils.getAbsoluteURL(it, url) - if (!urlList.contains(absoluteURL)) { - urlList.add(absoluteURL) + if (result is List<*>) { + for (url in result) { + val absoluteURL = NetworkUtils.getAbsoluteURL(it, url.toString()) + if (!urlList.contains(absoluteURL)) { + urlList.add(absoluteURL) + } } } return urlList } } - return result as List? + return result as? List } /** diff --git a/app/src/main/java/io/legado/app/model/book/BookList.kt b/app/src/main/java/io/legado/app/model/book/BookList.kt index 489d694c7..a4660a4d8 100644 --- a/app/src/main/java/io/legado/app/model/book/BookList.kt +++ b/app/src/main/java/io/legado/app/model/book/BookList.kt @@ -1,5 +1,12 @@ package io.legado.app.model.book +import io.legado.app.data.entities.SearchBook + class BookList { + fun analyzeBookList(): ArrayList { + var bookList = ArrayList() + + return bookList + } } \ No newline at end of file From 08867d1fa5d09ae60a4e08d2aeca0c24eef20342 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 6 Jul 2019 22:50:57 +0800 Subject: [PATCH 2/4] up --- app/build.gradle | 2 +- .../app/model/analyzeRule/AnalyzeByJSoup.kt | 23 +------------------ 2 files changed, 2 insertions(+), 23 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 08f47c12b..c15bcc12f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -82,7 +82,7 @@ dependencies { implementation 'androidx.core:core-ktx:1.2.0-alpha02' implementation 'androidx.appcompat:appcompat:1.1.0-rc01' implementation 'androidx.preference:preference:1.1.0-rc01' - implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta2' + implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta1' implementation 'com.google.android.material:material:1.1.0-alpha07' implementation 'com.google.android:flexbox:1.1.0' diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByJSoup.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByJSoup.kt index cb93e6758..7f4167271 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByJSoup.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByJSoup.kt @@ -133,16 +133,6 @@ class AnalyzeByJSoup { } } } - if (!isEmpty(sourceRule.replaceRegex)) { - val tempList = ArrayList(textS) - textS.clear() - for (text in tempList) { - val tmpText = text.replace(sourceRule.replaceRegex.toRegex(), sourceRule.replacement) - if (tmpText.isNotEmpty()) { - textS.add(tmpText) - } - } - } return textS } @@ -409,24 +399,13 @@ class AnalyzeByJSoup { internal inner class SourceRule(ruleStr: String) { var isCss = false var elementsRule: String - var replaceRegex = "" - var replacement = "" init { if (ruleStr.startsWith("@CSS:", true)) { isCss = true elementsRule = ruleStr.substring(5).trim { it <= ' ' } } else { - val ruleStrS: Array = - ruleStr.trim { it <= ' ' }.split("#".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() - //分离正则表达式 - elementsRule = ruleStrS[0] - if (ruleStrS.size > 1) { - replaceRegex = ruleStrS[1] - } - if (ruleStrS.size > 2) { - replacement = ruleStrS[2] - } + elementsRule = ruleStr } } } From 55c5606f1bbd70f57249e038c603db7ba1cac109 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 6 Jul 2019 23:49:53 +0800 Subject: [PATCH 3/4] up --- .../app/model/analyzeRule/AnalyzeRule.kt | 168 +++++++++++++----- 1 file changed, 121 insertions(+), 47 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt index ed162df36..e47c57a0d 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt @@ -20,7 +20,7 @@ import javax.script.SimpleBindings @Keep class AnalyzeRule(private var book: BaseBook? = null) { private var `object`: Any? = null - private var isJSON: Boolean? = false + private var isJSON: Boolean = false private var baseUrl: String? = null private var analyzeByXPath: AnalyzeByXPath? = null @@ -50,8 +50,8 @@ class AnalyzeRule(private var book: BaseBook? = null) { /** * 获取XPath解析类 */ - private fun getAnalyzeByXPath(o: Any?): AnalyzeByXPath { - return if (o != null) { + private fun getAnalyzeByXPath(o: Any): AnalyzeByXPath { + return if (o != `object`) { AnalyzeByXPath().parse(o) } else getAnalyzeByXPath() } @@ -68,8 +68,8 @@ class AnalyzeRule(private var book: BaseBook? = null) { /** * 获取JSOUP解析类 */ - private fun getAnalyzeByJSoup(o: Any?): AnalyzeByJSoup { - return if (o != null) { + private fun getAnalyzeByJSoup(o: Any): AnalyzeByJSoup { + return if (o != `object`) { AnalyzeByJSoup().parse(o) } else getAnalyzeByJSoup() } @@ -86,8 +86,8 @@ class AnalyzeRule(private var book: BaseBook? = null) { /** * 获取JSON解析类 */ - private fun getAnalyzeByJSonPath(o: Any?): AnalyzeByJSonPath { - return if (o != null) { + private fun getAnalyzeByJSonPath(o: Any): AnalyzeByJSonPath { + return if (o != `object`) { AnalyzeByJSonPath().parse(o) } else getAnalyzeByJSonPath() } @@ -116,15 +116,32 @@ class AnalyzeRule(private var book: BaseBook? = null) { @Throws(Exception::class) fun getStringList(ruleList: List, isUrl: Boolean): List? { var result: Any? = null - for (rule in ruleList) { - when (rule.mode) { - Mode.Js -> { - if (result == null) result = `object` - result = evalJS(rule.rule, result) + `object`?.let { o -> + if (ruleList.isNotEmpty()) { + if (ruleList.isNotEmpty()) result = o + for (rule in ruleList) { + result?.let { + if (rule.rule.isNotEmpty()) { + when (rule.mode) { + Mode.Js -> result = evalJS(rule.rule, result) + Mode.JSon -> result = + getAnalyzeByJSonPath(it).getStringList(rule.rule) + Mode.XPath -> result = + getAnalyzeByXPath(it).getStringList(rule.rule) + else -> result = getAnalyzeByJSoup(it).getStringList(rule.rule) + } + } + if (rule.replaceRegex.isNotEmpty() && result is List<*>) { + val newList = ArrayList() + for (item in result as List<*>) { + newList.add(replaceRegex(item.toString(), rule)) + } + result = newList + } else if (rule.replaceRegex.isNotEmpty()) { + result = replaceRegex(result.toString(), rule) + } + } } - Mode.JSon -> result = getAnalyzeByJSonPath(result).getStringList(rule.rule) - Mode.XPath -> result = getAnalyzeByXPath(result).getStringList(rule.rule) - else -> result = getAnalyzeByJSoup(result).getStringList(rule.rule) } } if (result == null) return ArrayList() @@ -167,19 +184,24 @@ class AnalyzeRule(private var book: BaseBook? = null) { @JvmOverloads fun getString(ruleList: List, isUrl: Boolean = false): String { var result: Any? = null - for (rule in ruleList) { - if (rule.rule.isNotBlank()) { - when (rule.mode) { - Mode.Js -> { - if (result == null) result = `object` - result = evalJS(rule.rule, result) + `object`?.let { o -> + if (ruleList.isNotEmpty()) result = o + for (rule in ruleList) { + result?.let { + if (rule.rule.isNotBlank()) { + when (rule.mode) { + Mode.Js -> result = evalJS(rule.rule, it) + Mode.JSon -> result = getAnalyzeByJSonPath(it).getString(rule.rule) + Mode.XPath -> result = getAnalyzeByXPath(it).getString(rule.rule) + Mode.Default -> result = if (isUrl && !TextUtils.isEmpty(baseUrl)) { + getAnalyzeByJSoup(it).getString0(rule.rule) + } else { + getAnalyzeByJSoup(it).getString(rule.rule) + } + } } - Mode.JSon -> result = getAnalyzeByJSonPath(result).getString(rule.rule) - Mode.XPath -> result = getAnalyzeByXPath(result).getString(rule.rule) - Mode.Default -> result = if (isUrl && !TextUtils.isEmpty(baseUrl)) { - getAnalyzeByJSoup(result).getString0(rule.rule) - } else { - getAnalyzeByJSoup(result).getString(rule.rule) + if (rule.replaceRegex.isNotEmpty()) { + result = replaceRegex(result.toString(), rule) } } } @@ -201,15 +223,20 @@ class AnalyzeRule(private var book: BaseBook? = null) { if (TextUtils.isEmpty(ruleStr)) return null var result: Any? = null val ruleList = splitSourceRule(ruleStr) - for (rule in ruleList) { - when (rule.mode) { - Mode.Js -> { - if (result == null) result = `object` - result = evalJS(rule.rule, result) + `object`?.let { o -> + if (ruleList.isNotEmpty()) result = o + for (rule in ruleList) { + result?.let { + when (rule.mode) { + Mode.Js -> result = evalJS(rule.rule, it) + Mode.JSon -> result = getAnalyzeByJSonPath(it).getObject(rule.rule) + Mode.XPath -> result = getAnalyzeByXPath(it).getElements(rule.rule) + else -> result = getAnalyzeByJSoup(it).getElements(rule.rule) + } + if (rule.replaceRegex.isNotEmpty()) { + result = replaceRegex(result.toString(), rule) + } } - Mode.JSon -> result = getAnalyzeByJSonPath(result).getObject(rule.rule) - Mode.XPath -> result = getAnalyzeByXPath(result).getElements(rule.rule) - else -> result = getAnalyzeByJSoup(result).getElements(rule.rule) } } return result @@ -223,15 +250,23 @@ class AnalyzeRule(private var book: BaseBook? = null) { fun getElements(ruleStr: String): List { var result: Any? = null val ruleList = splitSourceRule(ruleStr) - for (rule in ruleList) { - when (rule.mode) { - Mode.Js -> { - if (result == null) result = `object` - result = evalJS(rule.rule, result) + `object`?.let { o -> + if (ruleList.isNotEmpty()) result = o + for (rule in ruleList) { + result?.let { + when (rule.mode) { + Mode.Js -> { + if (result == null) result = `object` + result = evalJS(rule.rule, result) + } + Mode.JSon -> result = getAnalyzeByJSonPath(it).getList(rule.rule) + Mode.XPath -> result = getAnalyzeByXPath(it).getElements(rule.rule) + else -> result = getAnalyzeByJSoup(it).getElements(rule.rule) + } + if (rule.replaceRegex.isNotEmpty()) { + result = replaceRegex(result.toString(), rule) + } } - Mode.JSon -> result = getAnalyzeByJSonPath(result).getList(rule.rule) - Mode.XPath -> result = getAnalyzeByXPath(result).getElements(rule.rule) - else -> result = getAnalyzeByJSoup(result).getElements(rule.rule) } } result?.let { @@ -240,6 +275,7 @@ class AnalyzeRule(private var book: BaseBook? = null) { return ArrayList() } + /** * 保存变量 */ @@ -283,6 +319,27 @@ class AnalyzeRule(private var book: BaseBook? = null) { return vRuleStr } + /** + * 正则替换 + */ + private fun replaceRegex(result: String, rule: SourceRule): String { + var result = result + if (rule.replaceRegex.isNotEmpty()) { + result = if (rule.replaceFirst) { + val pattern = Pattern.compile(rule.replaceRegex) + val matcher = pattern.matcher(result) + if (matcher.find()) { + matcher.group(0).replaceFirst(rule.replaceRegex.toRegex(), rule.replacement) + } else { + "" + } + } else { + result.replace(rule.replaceRegex.toRegex(), rule.replacement) + } + } + return result + } + /** * 替换JS */ @@ -329,7 +386,7 @@ class AnalyzeRule(private var book: BaseBook? = null) { mode = Mode.JSon vRuleStr = vRuleStr.substring(6) } - else -> mode = if (isJSON!!) { + else -> mode = if (isJSON) { Mode.JSon } else { Mode.Default @@ -370,14 +427,17 @@ class AnalyzeRule(private var book: BaseBook? = null) { inner class SourceRule internal constructor(ruleStr: String, mainMode: Mode) { internal var mode: Mode internal var rule: String + internal var replaceRegex = "" + internal var replacement = "" + internal var replaceFirst = false init { this.mode = mainMode if (mode == Mode.Js) { - if (ruleStr.startsWith("")) { - rule = ruleStr.substring(4, ruleStr.lastIndexOf("<")) + rule = if (ruleStr.startsWith("")) { + ruleStr.substring(4, ruleStr.lastIndexOf("<")) } else { - rule = ruleStr.substring(4) + ruleStr.substring(4) } } else { when { @@ -400,6 +460,20 @@ class AnalyzeRule(private var book: BaseBook? = null) { else -> rule = ruleStr } } + //分离正则表达式 + val ruleStrS = + rule.trim { it <= ' ' }.split("##".toRegex()).dropLastWhile { it.isEmpty() } + .toTypedArray() + rule = ruleStrS[0] + if (ruleStrS.size > 1) { + replaceRegex = ruleStrS[1] + } + if (ruleStrS.size > 2) { + replacement = ruleStrS[2] + } + if (ruleStrS.size > 3) { + replaceFirst = true + } } } From a18c598ee86526583d3cc8bc2c755c1c3a44b492 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 7 Jul 2019 17:18:07 +0800 Subject: [PATCH 4/4] up --- app/src/main/java/io/legado/app/utils/FileUtils.kt | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/io/legado/app/utils/FileUtils.kt b/app/src/main/java/io/legado/app/utils/FileUtils.kt index e64869d11..e2ecef64e 100644 --- a/app/src/main/java/io/legado/app/utils/FileUtils.kt +++ b/app/src/main/java/io/legado/app/utils/FileUtils.kt @@ -1,10 +1,8 @@ package io.legado.app.utils -import android.annotation.TargetApi import android.content.ContentUris import android.content.Context import android.net.Uri -import android.os.Build import android.os.Environment import android.os.storage.StorageManager import android.provider.DocumentsContract @@ -171,8 +169,8 @@ object FileUtils { selection, selectionArgs, null - )!!.use { cursor -> - if (cursor != null && cursor.moveToFirst()) { + )?.use { cursor -> + if (cursor.moveToFirst()) { val index = cursor.getColumnIndexOrThrow(column) return cursor.getString(index) }