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 | /// The ExcelStamper class is an implementation of the OfficeStamper interface for stamping Excel templates. | |
17 | /// It uses the DOCX4J library to manipulate the template and replace variable expressions with values from the context. | |
18 | public class ExcelStamper | |
19 | implements OfficeStamper<SpreadsheetMLPackage> { | |
20 | ||
21 | /// Default constructor for the ExcelStamper class. | |
22 | /// This constructor initializes an instance of the ExcelStamper class, | |
23 | /// which implements the OfficeStamper interface for processing and | |
24 | /// stamping Excel templates. The class manipulates templates by replacing | |
25 | /// variable expressions with values from a given context. | |
26 | public ExcelStamper(){ | |
27 | // Explicit default constructor to hold javadoc | |
28 | } | |
29 | ||
30 | @Override | |
31 | public void stamp( | |
32 | SpreadsheetMLPackage template, | |
33 | Object context, | |
34 | OutputStream outputStream | |
35 | ) | |
36 | throws OfficeStamperException { | |
37 | var paragraphs = ExcelCollector.collect(template, CTRst.class); | |
38 | for (CTRst cell : paragraphs) { | |
39 | var paragraph = new ExcelParagraph(cell); | |
40 | var string = paragraph.asString(); | |
41 | for (var variable : findVariables(string)) { | |
42 | var evaluationContext = new StandardEvaluationContext(context); | |
43 | var parserConfiguration = new SpelParserConfiguration(); | |
44 | var parser = new SpelExpressionParser(parserConfiguration); | |
45 | var expression = parser.parseExpression(variable.content()); | |
46 | var value = expression.getValue(evaluationContext); | |
47 | var stringValue = String.valueOf(value); | |
48 |
1
1. stamp : removed call to pro/verron/officestamper/experimental/ExcelParagraph::replace → KILLED |
paragraph.replace(variable, stringValue); |
49 | } | |
50 | } | |
51 | try { | |
52 |
1
1. stamp : removed call to org/docx4j/openpackaging/packages/SpreadsheetMLPackage::save → KILLED |
template.save(outputStream); |
53 | } catch (Docx4JException e) { | |
54 | throw new OfficeStamperException(e); | |
55 | } | |
56 | } | |
57 | } | |
Mutations | ||
48 |
1.1 |
|
52 |
1.1 |