Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#20 fixed values containing newline characters not split in docx templates #144

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def projectName = "YARG (Yet another report generator)"
def projectDescription = "Open source library for report generation. Provided by Haulmont."
def projectUrl = "https://github.com/cuba-platform/yarg"

def artifactVersion = '2.2-SNAPSHOT'
def artifactVersion = '2.3-KLAUS'
klaus7 marked this conversation as resolved.
Show resolved Hide resolved
if (rootProject.hasProperty('buildVersion')) {
artifactVersion = rootProject['buildVersion']
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@
import org.apache.commons.io.IOUtils;
import org.docx4j.Docx4J;
import org.docx4j.TraversalUtil;
import org.docx4j.XmlUtils;
import org.docx4j.convert.in.xhtml.XHTMLImporter;
import org.docx4j.convert.in.xhtml.XHTMLImporterImpl;
import org.docx4j.convert.out.HTMLSettings;
import org.docx4j.model.structure.DocumentModel;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.io.SaveToZipFile;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.JaxbXmlPart;
import org.docx4j.openpackaging.parts.JaxbXmlPartAltChunkHost;
import org.docx4j.openpackaging.parts.WordprocessingML.AltChunkType;
import org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart;
Expand All @@ -44,6 +47,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.bind.JAXBException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
Expand All @@ -59,6 +63,8 @@
public class DocxFormatter extends AbstractFormatter {
protected static final Logger log = LoggerFactory.getLogger(DocxFormatter.class);

protected static final String NEWLINE_REPLACE_CHARACTER = "¶";

protected WordprocessingMLPackage wordprocessingMLPackage;
protected DocumentWrapper documentWrapper;
protected DocumentConverter documentConverter;
Expand Down Expand Up @@ -96,6 +102,18 @@ public void renderDocument() {
protected void updateTableOfContents() {
try {
MainDocumentPart documentPart = wordprocessingMLPackage.getMainDocumentPart();
replaceNewLines(documentPart);

DocumentModel documentModel = wordprocessingMLPackage.getDocumentModel();
if (documentModel != null && documentModel.getSections() != null) {
documentModel.getSections().forEach(section -> {
if (section.getHeaderFooterPolicy() != null) {
replaceNewLines(section.getHeaderFooterPolicy().getDefaultFooter());
replaceNewLines(section.getHeaderFooterPolicy().getDefaultFooter());
}
});
}

Document wmlDocumentEl;
try {
wmlDocumentEl = documentPart.getContents();
Expand All @@ -117,6 +135,21 @@ protected void updateTableOfContents() {
}
}

@SuppressWarnings("rawtypes,unchecked")
protected void replaceNewLines(JaxbXmlPart documentPart) {
klaus7 marked this conversation as resolved.
Show resolved Hide resolved
if (documentPart != null) {
String xml = XmlUtils.marshaltoString(documentPart.getJaxbElement(), true);
xml = xml.replace(NEWLINE_REPLACE_CHARACTER, "</w:t><w:br/><w:t>");
Object obj = null;
try {
obj = XmlUtils.unmarshalString(xml);
documentPart.setJaxbElement(obj);
} catch (JAXBException e) {
log.error("An error occurred during replacement of the newline character");
}
}
}

protected void handleUrls() {
UrlVisitor urlVisitor = new UrlVisitor(new DocxFormatterDelegate(this), wordprocessingMLPackage.getMainDocumentPart());
new TraversalUtil(wordprocessingMLPackage.getMainDocumentPart(), urlVisitor);
Expand Down Expand Up @@ -229,6 +262,15 @@ protected void writeToOutputStream(WordprocessingMLPackage mlPackage, OutputStre
saver.save(outputStream);
}

@Override
protected String formatValue(Object value, String parameterName, String fullParameterName, String stringFunction) {
String formattedValue = super.formatValue(value, parameterName, fullParameterName, stringFunction);
if (formattedValue != null) {
return formattedValue.replace("\n", NEWLINE_REPLACE_CHARACTER);
}
return formattedValue;
}

@SuppressWarnings("unchecked")
public void convertAltChunks() throws Docx4JException {
JaxbXmlPartAltChunkHost mainDocumentPart = wordprocessingMLPackage.getMainDocumentPart();
Expand Down