Skip to content

Commit

Permalink
Fix bug when empty strings are replaced; update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
deividasstr committed Apr 16, 2018
1 parent 8d2ef59 commit 7d7944c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -38,7 +38,7 @@ Step 2. Add the dependency
<dependency>
<groupId>com.github.deividasstr</groupId>
<artifactId>docx-word-replacer</artifactId>
<version>0.2</version>
<version>0.3</version>
</dependency>


Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group 'com.xandryex'
version '0.2'
version '0.3'

apply plugin: 'java-library'

Expand Down
18 changes: 13 additions & 5 deletions src/main/java/com/xandryex/utils/TextReplacer.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ public void onWordFoundInPreviousCurrentNextRun(List<XWPFRun> runs, int currentR
}

private void replaceWordInPreviousCurrentNextRuns(List<XWPFRun> 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));
}

Expand All @@ -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) {
Expand Down

0 comments on commit 7d7944c

Please sign in to comment.