Skip to content

Commit

Permalink
Print out a warning if there are template-markers left over after doing
Browse files Browse the repository at this point in the history
the replacements, closes #6.
  • Loading branch information
centic9 committed Jan 14, 2016
1 parent 140a1b3 commit 980154b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
Binary file added samples/Template-TagSplitByFormatting.docx
Binary file not shown.
7 changes: 7 additions & 0 deletions src/main/java/org/dstadler/poi/mailmerge/MailMerge.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.List;
import java.util.logging.Logger;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlOptions;
Expand Down Expand Up @@ -105,6 +106,12 @@ private void applyLines(Data dataIn, XWPFDocument doc, String outputFile) throws
replaced = replaced.replace("${" + header + "}", value);
}

// check for missed replacements or formatting which interferes
if(replaced.contains("${")) {
log.warning("Still found template-marker after doing replacement: " +
StringUtils.abbreviate(StringUtils.substring(replaced, replaced.indexOf("${")), 200));
}

appendBody(body, replaced, first);

first = false;
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/org/dstadler/poi/mailmerge/MailMergeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,19 @@ public void testInvalidXlsx() throws Exception {
// expected here
}
}

@Test
public void testTagSplitByFormatting() throws Exception {
assertTrue("Failed to create directory 'build'", new File("build").exists() || new File("build").mkdirs());

// ensure the result file is not there
assertTrue("File should not exist or we should be able to delete it, exist: " + RESULT_FILE.exists(),
!RESULT_FILE.exists() || RESULT_FILE.delete());

// use sample files to run a full merge
MailMerge.main(new String[] {"samples/Template-TagSplitByFormatting.docx", "samples/Lines.xlsx", RESULT_FILE.getPath()});

// ensure the result file is written now
assertTrue(RESULT_FILE.exists());
}
}

0 comments on commit 980154b

Please sign in to comment.