pull/1876/head^2
parent
5c89ca941f
commit
9821b260a3
@ -1,43 +1,47 @@ |
|||||||
package io.legado.app.help |
package io.legado.app.help |
||||||
|
|
||||||
import io.legado.app.data.entities.ReplaceRule |
import io.legado.app.data.entities.ReplaceRule |
||||||
|
import io.legado.app.exception.NoStackTraceException |
||||||
import io.legado.app.utils.* |
import io.legado.app.utils.* |
||||||
|
|
||||||
object ReplaceAnalyzer { |
object ReplaceAnalyzer { |
||||||
|
|
||||||
fun jsonToReplaceRules(json: String): List<ReplaceRule> { |
fun jsonToReplaceRules(json: String): Result<MutableList<ReplaceRule>> { |
||||||
|
return kotlin.runCatching { |
||||||
val replaceRules = mutableListOf<ReplaceRule>() |
val replaceRules = mutableListOf<ReplaceRule>() |
||||||
val items: List<Map<String, Any>> = jsonPath.parse(json).read("$") |
val items: List<Map<String, Any>> = jsonPath.parse(json).read("$") |
||||||
for (item in items) { |
for (item in items) { |
||||||
val jsonItem = jsonPath.parse(item) |
val jsonItem = jsonPath.parse(item) |
||||||
jsonToReplaceRule(jsonItem.jsonString())?.let { |
jsonToReplaceRule(jsonItem.jsonString()).getOrThrow().let { |
||||||
if (it.isValid()) { |
if (it.isValid()) { |
||||||
replaceRules.add(it) |
replaceRules.add(it) |
||||||
} |
} |
||||||
} |
} |
||||||
} |
} |
||||||
return replaceRules |
replaceRules |
||||||
|
} |
||||||
} |
} |
||||||
|
|
||||||
private fun jsonToReplaceRule(json: String): ReplaceRule? { |
fun jsonToReplaceRule(json: String): Result<ReplaceRule> { |
||||||
val replaceRule: ReplaceRule? = GSON.fromJsonObject<ReplaceRule>(json.trim()).getOrNull() |
return runCatching { |
||||||
runCatching { |
val replaceRule: ReplaceRule? = |
||||||
|
GSON.fromJsonObject<ReplaceRule>(json.trim()).getOrNull() |
||||||
if (replaceRule == null || replaceRule.pattern.isBlank()) { |
if (replaceRule == null || replaceRule.pattern.isBlank()) { |
||||||
val jsonItem = jsonPath.parse(json.trim()) |
val jsonItem = jsonPath.parse(json.trim()) |
||||||
val rule = ReplaceRule() |
val rule = ReplaceRule() |
||||||
rule.id = jsonItem.readLong("$.id") ?: System.currentTimeMillis() |
rule.id = jsonItem.readLong("$.id") ?: System.currentTimeMillis() |
||||||
rule.pattern = jsonItem.readString("$.regex") ?: "" |
rule.pattern = jsonItem.readString("$.regex") ?: "" |
||||||
if (rule.pattern.isEmpty()) return null |
if (rule.pattern.isEmpty()) throw NoStackTraceException("格式不对") |
||||||
rule.name = jsonItem.readString("$.replaceSummary") ?: "" |
rule.name = jsonItem.readString("$.replaceSummary") ?: "" |
||||||
rule.replacement = jsonItem.readString("$.replacement") ?: "" |
rule.replacement = jsonItem.readString("$.replacement") ?: "" |
||||||
rule.isRegex = jsonItem.readBool("$.isRegex") == true |
rule.isRegex = jsonItem.readBool("$.isRegex") == true |
||||||
rule.scope = jsonItem.readString("$.useTo") |
rule.scope = jsonItem.readString("$.useTo") |
||||||
rule.isEnabled = jsonItem.readBool("$.enable") == true |
rule.isEnabled = jsonItem.readBool("$.enable") == true |
||||||
rule.order = jsonItem.readInt("$.serialNumber") ?: 0 |
rule.order = jsonItem.readInt("$.serialNumber") ?: 0 |
||||||
return rule |
return@runCatching rule |
||||||
} |
} |
||||||
|
return@runCatching replaceRule |
||||||
} |
} |
||||||
return replaceRule |
|
||||||
} |
} |
||||||
|
|
||||||
} |
} |
Loading…
Reference in new issue