Skip to content

Commit

Permalink
fixed a bug where fragmented placeholders were not replaced correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
thombergs committed Jan 6, 2016
1 parent 8818e35 commit 35a8107
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
15 changes: 1 addition & 14 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.wickedsource</groupId>
<artifactId>docx-stamper</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>
<packaging>jar</packaging>
<name>docx-stamper</name>
<description>Template engine for .docx documents.</description>
Expand Down Expand Up @@ -126,19 +126,6 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public int cleanPlaceholder(String placeholder) {
}
} else if (isFirstRun) {
// put the whole replacement into the first affected run
run.replace(affectedRunsMatchStartIndex, affectedRunsMatchEndIndex, "");
run.replace(matchStartIndex, matchEndIndex, "");
replacementIndex = run.getIndexInParent();
} else if (isLastRun) {
// replace the last part of the match with empty string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.wickedsource.docxstamper.replace;

import org.docx4j.wml.P;
import org.junit.Assert;
import org.junit.Test;
import org.wickedsource.docxstamper.util.ParagraphUtil;
Expand Down Expand Up @@ -66,6 +67,28 @@ public void replaceFirstReplacesOverlappingRuns() {

}

@Test
public void replaceFirstWorksWithFragmentedParagraph() {
P p = ParagraphUtil.create("Eine Weitergabe an Dritte ist nicht zulässig.", "Leihfrist bis", ": ", " ", "${", "leihfrist", "}",
"Ort der Aufbewahrung: ", " ", "${", "aufbewahrungsOrt", "}",
"Ort und Datum, Unterschrift");
p.getContent().add(1, new Object()); // add random Object to simulate other docx-Object
p.getContent().add(2, new Object());
p.getContent().add(6, new Object());
p.getContent().add(7, new Object());
p.getContent().add(9, new Object());
p.getContent().add(11, new Object());
p.getContent().add(13, new Object());
p.getContent().add(15, new Object());
p.getContent().add(17, new Object());
p.getContent().add(19, new Object());
p.getContent().add(21, new Object());
ParagraphWrapper aggregator = new ParagraphWrapper(p);
int replacementIndex = aggregator.cleanPlaceholder("${leihfrist}");
Assert.assertEquals(8, replacementIndex);
Assert.assertEquals("Eine Weitergabe an Dritte ist nicht zulässig.Leihfrist bis: Ort der Aufbewahrung: ${aufbewahrungsOrt}Ort und Datum, Unterschrift", aggregator.getText());
}

private ParagraphWrapper createLoremIpsumAggregator() {
ParagraphWrapper aggregator = new ParagraphWrapper(ParagraphUtil.create("lorem", " ", "ipsum"));
return aggregator;
Expand Down

0 comments on commit 35a8107

Please sign in to comment.