ExcelStamper.java

1
package pro.verron.officestamper.experimental;
2
3
import org.docx4j.openpackaging.exceptions.Docx4JException;
4
import org.docx4j.openpackaging.packages.SpreadsheetMLPackage;
5
import org.springframework.expression.spel.SpelParserConfiguration;
6
import org.springframework.expression.spel.standard.SpelExpressionParser;
7
import org.springframework.expression.spel.support.StandardEvaluationContext;
8
import org.xlsx4j.sml.CTRst;
9
import pro.verron.officestamper.api.OfficeStamper;
10
import pro.verron.officestamper.api.OfficeStamperException;
11
12
import java.io.OutputStream;
13
14
import static pro.verron.officestamper.core.Placeholders.findVariables;
15
16
/**
17
 * The ExcelStamper class is an implementation of the OfficeStamper interface for stamping Excel templates.
18
 * It uses the DOCX4J library to manipulate the template and replace variable expressions with values from the context.
19
 */
20
public class ExcelStamper
21
        implements OfficeStamper<SpreadsheetMLPackage> {
22
23
    @Override
24
    public void stamp(
25
            SpreadsheetMLPackage template,
26
            Object context,
27
            OutputStream outputStream
28
    )
29
            throws OfficeStamperException {
30
        var paragraphs = ExcelCollector.collect(template, CTRst.class);
31
        for (CTRst cell : paragraphs) {
32
            var paragraph = new ExcelParagraph(cell);
33
            var string = paragraph.asString();
34
            for (var variable : findVariables(string)) {
35
                var evaluationContext = new StandardEvaluationContext(context);
36
                var parserConfiguration = new SpelParserConfiguration();
37
                var parser = new SpelExpressionParser(parserConfiguration);
38
                var expression = parser.parseExpression(variable.content());
39
                var value = expression.getValue(evaluationContext);
40
                var stringValue = String.valueOf(value);
41 1 1. stamp : removed call to pro/verron/officestamper/experimental/ExcelParagraph::replace → KILLED
                paragraph.replace(variable, stringValue);
42
            }
43
        }
44
        try {
45 1 1. stamp : removed call to org/docx4j/openpackaging/packages/SpreadsheetMLPackage::save → KILLED
            template.save(outputStream);
46
        } catch (Docx4JException e) {
47
            throw new OfficeStamperException(e);
48
        }
49
    }
50
}

Mutations

41

1.1
Location : stamp
Killed by : pro.verron.officestamper.test.BasicExcelTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.BasicExcelTest]/[method:testStamper()]
removed call to pro/verron/officestamper/experimental/ExcelParagraph::replace → KILLED

45

1.1
Location : stamp
Killed by : pro.verron.officestamper.test.BasicExcelTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.BasicExcelTest]/[method:testStamper()]
removed call to org/docx4j/openpackaging/packages/SpreadsheetMLPackage::save → KILLED

Active mutators

Tests examined


Report generated by PIT 1.17.1