字符串替换的时候增加检查:替换规则不能为空,替换规则为正则表达式的时候,需要合法

(cherry picked from commit ad01a5518a2f36d4f0573ec59563296b88ad37b8)
pull/133/head
wqfantexi 5 years ago
parent ab8faa0f2e
commit 1fdab1d556
  1. 19
      app/src/main/java/io/legado/app/data/entities/ReplaceRule.kt
  2. 6
      app/src/main/java/io/legado/app/help/storage/ImportOldData.kt
  3. 13
      app/src/main/java/io/legado/app/ui/replacerule/edit/ReplaceEditDialog.kt
  4. 1
      app/src/main/res/values/strings.xml

@ -1,11 +1,14 @@
package io.legado.app.data.entities
import android.os.Parcelable
import android.text.TextUtils
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Index
import androidx.room.PrimaryKey
import kotlinx.android.parcel.Parcelize
import java.util.regex.Pattern
import java.util.regex.PatternSyntaxException
@Parcelize
@Entity(
@ -37,4 +40,20 @@ data class ReplaceRule(
override fun hashCode(): Int {
return id.hashCode()
}
fun isValid(): Boolean{
if (TextUtils.isEmpty(pattern)){
return false;
}
//判断正则表达式是否正确
if (isRegex){
try {
Pattern.compile(pattern);
}
catch (ex: PatternSyntaxException){
return false;
}
}
return true;
}
}

@ -136,13 +136,17 @@ object ImportOldData {
return bookSources.size
}
fun importOldReplaceRule(json: String): Int {
val replaceRules = mutableListOf<ReplaceRule>()
val items: List<Map<String, Any>> = Restore.jsonPath.parse(json).read("$")
for (item in items) {
val jsonItem = Restore.jsonPath.parse(item)
OldRule.jsonToReplaceRule(jsonItem.jsonString())?.let {
replaceRules.add(it)
if (it.isValid()){
replaceRules.add(it)
}
}
}
App.db.replaceRuleDao().insert(*replaceRules.toTypedArray())

@ -16,6 +16,7 @@ import io.legado.app.constant.Theme
import io.legado.app.data.entities.ReplaceRule
import io.legado.app.utils.applyTint
import io.legado.app.utils.getViewModel
import io.legado.app.utils.toast
import kotlinx.android.synthetic.main.dialog_replace_edit.*
class ReplaceEditDialog : DialogFragment(),
@ -73,9 +74,15 @@ class ReplaceEditDialog : DialogFragment(),
override fun onMenuItemClick(item: MenuItem?): Boolean {
when (item?.itemId) {
R.id.menu_save -> {
viewModel.save(getReplaceRule()) {
callBack?.onReplaceRuleSave()
dismiss()
val rule = getReplaceRule();
if (!rule.isValid()){
toast(R.string.replace_rule_invalid)
}
else{
viewModel.save(rule) {
callBack?.onReplaceRuleSave()
dismiss()
}
}
}
}

@ -204,6 +204,7 @@
<string name="clear_all_content">清除缓存会删除所有已保存章节,是否确认删除?</string>
<string name="book_source_share_url">书源共享</string>
<string name="replace_rule_summary">替换规则名称</string>
<string name="replace_rule_invalid">替换规则为空或者不满足正则表达式要求</string>
<string name="select_action">选择操作</string>
<string name="select_all">全选</string>
<string name="select_all_count">全选(%d/%d)</string>

Loading…
Cancel
Save