All Downloads are FREE. Search and download functionalities are using the official Maven repository.

pro.verron.officestamper.api.StreamStamper Maven / Gradle / Ivy

Go to download

Office-stamper is a Java template engine for docx documents, forked from org.wickedsource.docx-stamper

There is a newer version: 2.6.0
Show newest version
package pro.verron.officestamper.api;

import org.docx4j.openpackaging.packages.OpcPackage;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.function.Function;

/**
 * This class implements the functionality of an OfficeStamper meant for dealing with streams of data. It delegates the executing
 * of the stamp operation to an OfficeStamper instance while providing the necessary mechanisms to work with streams.
 *
 * @param  The type of the template that can be stamped. This type must extend OpcPackage.
 * @author Joseph Verron
 * @version ${version}
 * @since 1.6.4
 */
public class StreamStamper {

    /**
     * Holds a reference to a function that takes in an InputStream and produces an instance of type T.
     */
    private final Function loader;

    /**
     * Holds a reference to an OfficeStamper used to execute the stamp operation.
     */
    private final OfficeStamper stamper;

    /**
     * Constructs a new StreamStamper with the provided loader and stamper.
     *
     * @param loader  A Function that takes in an InputStream and produces an instance of type T.
     * @param stamper An OfficeStamper used to execute the stamp operation.
     */
    public StreamStamper(
            Function loader,
            OfficeStamper stamper
    ) {
        this.loader = loader;
        this.stamper = stamper;
    }

    /**
     * Stamps the template present in the given InputStream with the context given
     * and writes the result to the provided OutputStream.
     * This method first uses the loader to load the template from the
     * InputStream into a type T instance,
     * then uses the stamper
     * to perform the stamp operation using the template and context,
     * writing the result out to the OutputStream.
     *
     * @param inputStream  template to stamp
     * @param context      context to use for stamping
     * @param outputStream output stream to write the result to
     * @throws OfficeStamperException if the stamping fails for any reason
     */
    public void stamp(
            InputStream inputStream,
            Object context,
            OutputStream outputStream
    ) throws OfficeStamperException {
        T mlPackage = loader.apply(inputStream);
        stamper.stamp(mlPackage, context, outputStream);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy