From 7d7944cf6c752a6035a9a98c7a2aed274bb43f03 Mon Sep 17 00:00:00 2001 From: Deividas Strioga Date: Mon, 16 Apr 2018 09:47:33 +0300 Subject: [PATCH] Fix bug when empty strings are replaced; update readme --- README.md | 4 ++-- build.gradle | 2 +- .../java/com/xandryex/utils/TextReplacer.java | 18 +++++++++++++----- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 35a7f0e..ef9002b 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ Add it in your root build.gradle at the end of repositories: Step 2. Add the dependency dependencies { - implementation 'com.github.deividasstr:docx-word-replacer:0.2' + implementation 'com.github.deividasstr:docx-word-replacer:0.3' } Maven @@ -38,7 +38,7 @@ Step 2. Add the dependency com.github.deividasstr docx-word-replacer - 0.2 + 0.3 diff --git a/build.gradle b/build.gradle index 61ee8e4..9a77f99 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ group 'com.xandryex' -version '0.2' +version '0.3' apply plugin: 'java-library' diff --git a/src/main/java/com/xandryex/utils/TextReplacer.java b/src/main/java/com/xandryex/utils/TextReplacer.java index 9c85f97..9519628 100644 --- a/src/main/java/com/xandryex/utils/TextReplacer.java +++ b/src/main/java/com/xandryex/utils/TextReplacer.java @@ -36,8 +36,12 @@ public void onWordFoundInPreviousCurrentNextRun(List runs, int currentR } private void replaceWordInPreviousCurrentNextRuns(List runs, int currentRun) { - replaceNotFullBookmarkInRun(runs.get(currentRun - 1)); - deleteTextFromRun(runs.get(currentRun)); + boolean replacedInPreviousRun = replaceRunTextStart(runs.get(currentRun - 1)); + if (replacedInPreviousRun) { + deleteTextFromRun(runs.get(currentRun)); + } else { + replaceRunTextStart(runs.get(currentRun)); + } cleanRunTextStart(runs.get(currentRun + 1)); } @@ -52,11 +56,15 @@ private void replaceWordInRun(XWPFRun run) { run.setText(replacedText, DEFAULT_TEXT_POS); } - private void replaceNotFullBookmarkInRun(XWPFRun run) { + private boolean replaceRunTextStart(XWPFRun run) { String text = run.getText(DEFAULT_TEXT_POS); String remainingBookmark = getRemainingBookmarkStart(text, bookmark); - text = text.replace(remainingBookmark, replacement); - run.setText(text, DEFAULT_TEXT_POS); + if (!remainingBookmark.isEmpty()) { + text = text.replace(remainingBookmark, replacement); + run.setText(text, DEFAULT_TEXT_POS); + return true; + } + return false; } private void cleanRunTextStart(XWPFRun run) {