| 1 | package pro.verron.officestamper.api; | |
| 2 | ||
| 3 | import org.docx4j.openpackaging.packages.OpcPackage; | |
| 4 | ||
| 5 | import java.io.InputStream; | |
| 6 | import java.io.OutputStream; | |
| 7 | import java.util.function.BiConsumer; | |
| 8 | import java.util.function.Function; | |
| 9 | ||
| 10 | /// This class implements the functionality of an [OfficeStamper] meant for dealing with streams of data. It delegates | |
| 11 | /// the execution of the stamp operation to an [OfficeStamper] instance while providing the necessary mechanisms to work | |
| 12 | /// with streams. | |
| 13 | /// | |
| 14 | /// @param <T> The type of the template that can be stamped. This type must extend [OpcPackage]. | |
| 15 | /// | |
| 16 | /// @author Joseph Verron | |
| 17 | /// @version ${version} | |
| 18 | /// @since 1.6.4 | |
| 19 | public class StreamStamper<T extends OpcPackage> { | |
| 20 | ||
| 21 | /// Holds a reference to a function that takes in an [InputStream] and produces an instance of type [T]. | |
| 22 | private final Function<InputStream, T> loader; | |
| 23 | ||
| 24 | /// Holds a reference to an [OfficeStamper] used to execute the stamp operation. | |
| 25 | private final OfficeStamper<T> stamper; | |
| 26 | ||
| 27 | /// Holds a reference to a consumer that exports the stamped document to an [OutputStream]. | |
| 28 | private final BiConsumer<T, OutputStream> exporter; | |
| 29 | ||
| 30 | /// Constructs a new [StreamStamper] with the provided loader, stamper and exporter. | |
| 31 | /// | |
| 32 | /// @param loader A Function that takes in an [InputStream] and produces an instance of type [T]. | |
| 33 | /// @param stamper An [OfficeStamper] used to execute the stamp operation. | |
| 34 | /// @param exporter A [BiConsumer] that exports the stamped document to an [OutputStream]. | |
| 35 | public StreamStamper( | |
| 36 | Function<InputStream, T> loader, | |
| 37 | OfficeStamper<T> stamper, | |
| 38 | BiConsumer<T, OutputStream> exporter | |
| 39 | ) { | |
| 40 | this.loader = loader; | |
| 41 | this.stamper = stamper; | |
| 42 | this.exporter = exporter; | |
| 43 | } | |
| 44 | ||
| 45 | /// Stamps the template present in the given InputStream with the context given and writes the result to the | |
| 46 | /// provided [OutputStream]. This method first uses the loader to load the template from the [InputStream] into a | |
| 47 | /// type [T] instance, then uses the stamper to perform the stamp operation using the template and context, writing | |
| 48 | /// the result out to the OutputStream. | |
| 49 | /// | |
| 50 | /// @param inputStream template to stamp | |
| 51 | /// @param context context to use for stamping | |
| 52 | /// @param outputStream output stream to write the result to | |
| 53 | /// | |
| 54 | /// @throws OfficeStamperException if the stamping fails for any reason | |
| 55 | public void stamp(InputStream inputStream, Object context, OutputStream outputStream) | |
| 56 | throws OfficeStamperException { | |
| 57 | var template = loader.apply(inputStream); | |
| 58 | var stamped = stamper.stamp(template, context); | |
| 59 |
1
1. stamp : removed call to java/util/function/BiConsumer::accept → NO_COVERAGE |
exporter.accept(stamped, outputStream); |
| 60 | } | |
| 61 | } | |
Mutations | ||
| 59 |
1.1 |