it.discovery.jasperreports.jasper2word.J2WDocx4jJRExporter Maven / Gradle / Ivy
package it.discovery.jasperreports.jasper2word;
import it.discovery.jasperreports.jasper2word.J2WDocx4jHelper.J2WDocx4jPrintElementVisitorContext;
import it.discovery.jasperreports.jasper2word.J2WDocx4jHelper.J2WDocxPrintElementVisitor;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.export.OutputStreamExporterOutput;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.exceptions.InvalidFormatException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
/**
* A jasper to docx implementation using docx4j library.
* @author discovery
* @date 10/08/15 11.29
*/
public class J2WDocx4jJRExporter extends J2WAbstractJRExporter {
/** Jasper report exporter key */
public static final String J2W_DOCX_EXPORTER_KEY = JRPropertiesUtil.PROPERTY_PREFIX + ".j2w.docx4j.docx";
/** Jasper report exporter properties prefix */
protected static final String J2W_DOCX_EXPORTER_PROPERTIES_PREFIX = JRPropertiesUtil.PROPERTY_PREFIX + "export.j2w.docx4j.docx.";
/**
* Construct the exporter with the jasper context.
* @param jasperReportsContext The jasper context.
*/
public J2WDocx4jJRExporter(JasperReportsContext jasperReportsContext) {
super(jasperReportsContext);
}
@Override
protected J2WDocx4jExporterContext createNewExporterContext() {
try {
return new SimpleJ2WDocxExporterContext();
} catch (InvalidFormatException e) {
throw new JRRuntimeException(e);
}
}
@Override
protected Class getConfigurationInterface() {
return J2WExporterConfiguration.class;
}
@Override
protected Class getItemConfigurationInterface() {
return J2WReportConfiguration.class;
}
@Override
protected void ensureOutput() {
if (exporterOutput == null)
exporterOutput = new SimpleJ2WOutputStreamExporterOutput(true);
}
@Override
public String getExporterKey() {
return J2W_DOCX_EXPORTER_KEY;
}
@Override
public String getExporterPropertiesPrefix() {
return J2W_DOCX_EXPORTER_PROPERTIES_PREFIX;
}
@Override
protected J2WDocx4jPrintElementVisitorContext createVisitorContext() {
return new J2WDocx4jPrintElementVisitorContext();
}
@Override
protected PrintElementVisitor createPrintElementVisitor() {
return new J2WDocxPrintElementVisitor(this.allSelector, this.getJasperReportsContext());
}
@Override
protected void prepareDocument(J2WDocx4jPrintElementVisitorContext context, JasperPrint report, boolean firstReport) {
J2WDocx4jHelper.prepareNewDocument(firstReport, context, report);
}
@Override
protected void prepareNewPage(J2WDocx4jPrintElementVisitorContext context, JRPrintPage jrPage, int pageNumber, PageOrder pageOrder) {
context.resetPageStatus();
J2WDocx4jHelper.prepareNewPage(context, jrPage, pageNumber, pageOrder);
}
@Override
protected void finalizePage(J2WDocx4jPrintElementVisitorContext context, JRPrintPage jrPage, int pageNumber, PageOrder pageOrder) {
J2WDocx4jHelper.finalizePage(context, jrPage, pageNumber, pageOrder);
}
/**
* Exporter context implementation. It expose a {@link WordprocessingMLPackage document} to write into.
*/
private class SimpleJ2WDocxExporterContext extends BaseExporterContext implements J2WDocx4jExporterContext {
/** The output document */
private WordprocessingMLPackage document;
/**
* Constructor.
* @throws InvalidFormatException In case of document creating errors.
*/
private SimpleJ2WDocxExporterContext() throws InvalidFormatException {
this.document = WordprocessingMLPackage.createPackage();
}
@Override
public WordprocessingMLPackage getDocument() {
return document;
}
@Override
public void writeReport(WordprocessingMLPackage document, OutputStreamExporterOutput output) throws JRException {
try {
document.save(exporterOutput.getOutputStream());
exporterOutput.close();
} catch (Docx4JException e) {
throw new JRException("Write document error", e);
}
}
}
}