| 1 | package pro.verron.officestamper.experimental; | |
| 2 | ||
| 3 | import org.docx4j.openpackaging.packages.SpreadsheetMLPackage; | |
| 4 | import org.springframework.expression.spel.SpelParserConfiguration; | |
| 5 | import org.springframework.expression.spel.standard.SpelExpressionParser; | |
| 6 | import org.springframework.expression.spel.support.StandardEvaluationContext; | |
| 7 | import org.xlsx4j.sml.CTRst; | |
| 8 | import pro.verron.officestamper.api.OfficeStamper; | |
| 9 | import pro.verron.officestamper.api.OfficeStamperException; | |
| 10 | ||
| 11 | import static pro.verron.officestamper.experimental.Placeholders.findVariables; | |
| 12 | ||
| 13 | /// The ExcelStamper class is an implementation of the OfficeStamper interface for stamping Excel templates. It uses the | |
| 14 | /// DOCX4J library to manipulate the template and replace variable expressions with values from the context. | |
| 15 | public class ExcelStamper | |
| 16 | implements OfficeStamper<SpreadsheetMLPackage> { | |
| 17 | ||
| 18 | /// Default constructor for the ExcelStamper class. This constructor initializes an instance of the ExcelStamper | |
| 19 | /// class, which implements the OfficeStamper interface for processing and stamping Excel templates. The class | |
| 20 | /// manipulates templates by replacing variable expressions with values from a given context. | |
| 21 | public ExcelStamper() { | |
| 22 | // Explicit default constructor to hold javadoc | |
| 23 | } | |
| 24 | ||
| 25 | @Override | |
| 26 | public SpreadsheetMLPackage stamp(SpreadsheetMLPackage template, Object context) | |
| 27 | throws OfficeStamperException { | |
| 28 | var paragraphs = ExcelCollector.collect(template, CTRst.class); | |
| 29 | for (CTRst cell : paragraphs) { | |
| 30 | var paragraph = new ExcelParagraph(cell); | |
| 31 | var string = paragraph.asString(); | |
| 32 | for (var variable : findVariables(string)) { | |
| 33 | var expression = variable.expression(); | |
| 34 | var evaluationContext = new StandardEvaluationContext(context); | |
| 35 | var parserConfiguration = new SpelParserConfiguration(); | |
| 36 | var parser = new SpelExpressionParser(parserConfiguration); | |
| 37 | var parsedExpression = parser.parseExpression(variable.content()); | |
| 38 | var value = parsedExpression.getValue(evaluationContext); | |
| 39 | var stringValue = String.valueOf(value); | |
| 40 |
1
1. stamp : removed call to pro/verron/officestamper/experimental/ExcelParagraph::replace → KILLED |
paragraph.replace(expression, stringValue); |
| 41 | } | |
| 42 | } | |
| 43 |
1
1. stamp : replaced return value with null for pro/verron/officestamper/experimental/ExcelStamper::stamp → KILLED |
return template; |
| 44 | } | |
| 45 | } | |
Mutations | ||
| 40 |
1.1 |
|
| 43 |
1.1 |