Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/main/java/org/scriptlet4docx/docx/DocxTemplater.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class DocxTemplater {
private InputStream templateStream;
private String streamTemplateKey;
private TemplateEngine templateEngine;
private boolean spacePreserve;

/**
* Set default Template Engine
Expand Down Expand Up @@ -110,6 +111,8 @@ protected TemplateContent processCleanedTemplate(TemplateContent content, Map<St
}

private static String NEW_LINE_PLACEHOLDER = "26f679ad-e7fd-4d42-9e05-946f393c277d";
private static String WT_NO_PRESERVE = "<w:t>";
private static String WT_SPACE_PRESERVE = "<w:t xml:space=\"preserve\">";

protected static Map<String, Object> processParams(Map<String, Object> params) {
Map<String, Object> res = new HashMap<String, Object>();
Expand Down Expand Up @@ -166,6 +169,11 @@ protected String processCleanedTemplate(String template, Map<String, Object> par

int i = 0;
for (String piece : pieces) {
if (spacePreserve && i < scripts.size() && piece.endsWith(WT_NO_PRESERVE)) {
// Always preserve spaces of the following scripts
piece = piece.substring(0, piece.length() - WT_NO_PRESERVE.length()) + WT_SPACE_PRESERVE;
}

tplSkeleton.add(new Placeholder(UUID.randomUUID().toString(), piece, PlaceholderType.TEXT));

if (i < scripts.size()) {
Expand Down Expand Up @@ -398,4 +406,14 @@ public void setTemplateEngine(TemplateEngine templateEngine) {
this.templateEngine = templateEngine;
}

/**
* When spaces around scripts should be preserved.
* Defaults to <code>false</code>
*
* @param spacePreserve Preserve spaces around scripts?
*/
public void setSpacePreserve(boolean spacePreserve) {
this.spacePreserve = spacePreserve;
}

}
25 changes: 25 additions & 0 deletions src/test/java/org/scriptlet4docx/docx/DocxTemplaterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -633,4 +633,29 @@ public void testProcessScriptedTemplate_newLine() throws Exception {
assertTrue(result.contains("this is A<w:br/>"));
assertTrue(StringUtils.countMatches(result, "this is A<w:br/>") == 4);
}

@Test
public void testProcessScriptedTemplate_spacePreserveScript() throws Exception {
String template = TestUtils.readResource("/docx/DocxTemplaterTest-21.xml");
DocxTemplater templater = new DocxTemplater(none);
templater.setSpacePreserve(true);
template = templater.cleanupTemplate(template);
String result = templater.processCleanedTemplate(template, params);

assertTrue(result != null);
assertTrue(result.contains("<w:t xml:space=\"preserve\">one two </w:t>"));
assertTrue(result.contains("<w:t xml:space=\"preserve\">three</w:t>"));
}

@Test
public void testProcessScriptedTemplate_noSpacePreserveScript() throws Exception {
String template = TestUtils.readResource("/docx/DocxTemplaterTest-21.xml");
DocxTemplater templater = new DocxTemplater(none);
template = templater.cleanupTemplate(template);
String result = templater.processCleanedTemplate(template, params);

assertTrue(result != null);
assertTrue(result.contains("<w:t>one two </w:t>"));
assertTrue(result.contains("<w:t>three</w:t>"));
}
}
78 changes: 78 additions & 0 deletions src/test/resources/docx/DocxTemplaterTest-21.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 wp14">
<w:body>
<w:p w:rsidR="00851669" w:rsidRPr="00164122" w:rsidRDefault="00851669">
<w:pPr>
<w:rPr>
<w:lang w:val="en-US"/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:lang w:val="en-US"/>
</w:rPr>
<w:t>&lt;%out.print('</w:t>
</w:r>
<w:proofErr w:type="gramStart"/>
<w:r w:rsidR="00507F5D" w:rsidRPr="00084DB5">
<w:rPr>
<w:lang w:val="en-US"/>
</w:rPr>
<w:t>one</w:t>
</w:r>
<w:proofErr w:type="gramEnd"/>
<w:r w:rsidR="00507F5D" w:rsidRPr="00084DB5">
<w:rPr>
<w:lang w:val="en-US"/>
</w:rPr>
<w:t>'); %&gt; &lt;%out.print('</w:t>
</w:r>
<w:proofErr w:type="gramStart"/>
<w:r w:rsidR="00507F5D" w:rsidRPr="00084DB5">
<w:rPr>
<w:lang w:val="en-US"/>
</w:rPr>
<w:t>two</w:t>
</w:r>
<w:proofErr w:type="gramEnd"/>
<w:r w:rsidR="00507F5D" w:rsidRPr="00084DB5">
<w:rPr>
<w:lang w:val="en-US"/>
</w:rPr>
<w:t xml:space="preserve">'); %&gt; </w:t>
</w:r>
<w:r>
<w:rPr>
<w:lang w:val="en-US"/>
</w:rPr>
<w:t>&lt;%out.print</w:t>
</w:r>
<w:proofErr w:type="spellStart"/>
<w:r>
<w:rPr>
<w:lang w:val="en-US"/>
</w:rPr>
<w:t>('</w:t>
</w:r>
<w:proofErr w:type="spellEnd"/>
<w:r w:rsidR="008D04F8" w:rsidRPr="00084DB5">
<w:rPr>
<w:lang w:val="en-US"/>
</w:rPr>
<w:t>three</w:t>
</w:r>
<w:r w:rsidR="009D34AF" w:rsidRPr="00084DB5">
<w:rPr>
<w:lang w:val="en-US"/>
</w:rPr>
<w:t>'); %&gt;</w:t>
</w:r>
</w:p>
<w:sectPr w:rsidR="00851669" w:rsidRPr="00164122">
<w:pgSz w:w="11906" w:h="16838"/>
<w:pgMar w:top="1134" w:right="850" w:bottom="1134" w:left="1701" w:header="708" w:footer="708" w:gutter="0"/>
<w:cols w:space="708"/>
<w:docGrid w:linePitch="360"/>
</w:sectPr>
</w:body>
</w:document>