| 1 | package pro.verron.officestamper.experimental; | |
| 2 | ||
| 3 | import org.docx4j.dml.CTBlip; | |
| 4 | import org.docx4j.dml.CTBlip; | |
| 5 | import org.docx4j.dml.CTBlipFillProperties; | |
| 6 | import org.docx4j.dml.CTRegularTextRun; | |
| 7 | import org.docx4j.dml.CTShapeProperties; | |
| 8 | import org.docx4j.dml.CTTextParagraph; | |
| 9 | import org.docx4j.openpackaging.packages.PresentationMLPackage; | |
| 10 | import org.docx4j.openpackaging.parts.PresentationML.SlidePart; | |
| 11 | import org.pptx4j.Pptx4jException; | |
| 12 | import org.pptx4j.pml.Shape; | |
| 13 | import org.springframework.expression.spel.SpelParserConfiguration; | |
| 14 | import org.springframework.expression.spel.standard.SpelExpressionParser; | |
| 15 | import org.springframework.expression.spel.support.StandardEvaluationContext; | |
| 16 | import pro.verron.officestamper.api.Insert; | |
| 17 | import pro.verron.officestamper.api.OfficeStamper; | |
| 18 | import pro.verron.officestamper.api.OfficeStamperException; | |
| 19 | import pro.verron.officestamper.preset.Image; | |
| 20 | import pro.verron.officestamper.utils.openpackaging.OpenPackage; | |
| 21 | ||
| 22 | import java.util.ArrayList; | |
| 23 | import java.util.List; | |
| 24 | ||
| 25 | /// The PowerpointStamper class implements the OfficeStamper interface to provide capability for stamping PowerPoint | |
| 26 | /// presentations with context and writing the result to an OutputStream. | |
| 27 | public class PowerpointStamper | |
| 28 | implements OfficeStamper<PresentationMLPackage> { | |
| 29 | /// Constructs a new instance of the PowerpointStamper class. This constructor initializes an instance of | |
| 30 | /// PowerpointStamper, which implements the OfficeStamper interface. The class provides functionality to apply | |
| 31 | /// variable-based stamping on PowerPoint templates and outputs the modified presentation. | |
| 32 | public PowerpointStamper() { | |
| 33 | // Explicit default constructor for Javadoc | |
| 34 | } | |
| 35 | ||
| 36 | @Override | |
| 37 | public PresentationMLPackage stamp(PresentationMLPackage template, Object context) | |
| 38 | throws OfficeStamperException { | |
| 39 | try { | |
| 40 | List<SlidePart> slideParts = template.getMainPresentationPart() | |
| 41 | .getSlideParts(); | |
| 42 | for (SlidePart slide : slideParts) { | |
| 43 | List<Shape> shapes = PowerpointCollector.collect(slide, Shape.class); | |
| 44 | for (Shape shape : shapes) { | |
| 45 |
1
1. stamp : removed call to pro/verron/officestamper/experimental/PowerpointStamper::processShape → KILLED |
processShape(slide, shape, context); |
| 46 | } | |
| 47 | } | |
| 48 |
1
1. stamp : replaced return value with null for pro/verron/officestamper/experimental/PowerpointStamper::stamp → KILLED |
return template; |
| 49 | } catch (Pptx4jException e) { | |
| 50 | throw new OfficeStamperException(e); | |
| 51 | } | |
| 52 | } | |
| 53 | ||
| 54 | private void processShape(SlidePart slide, Shape shape, Object context) { | |
| 55 |
1
1. processShape : negated conditional → KILLED |
if (shape.getTxBody() == null) return; |
| 56 | List<CTTextParagraph> paragraphs = new ArrayList<>(shape.getTxBody() | |
| 57 | .getP()); | |
| 58 | for (CTTextParagraph paragraph : paragraphs) { | |
| 59 | PowerpointParagraph paragraph1 = new PowerpointParagraph(new PptxPart((PresentationMLPackage) slide.getPackage()), | |
| 60 | paragraph); | |
| 61 | String string = paragraph1.asString(); | |
| 62 | for (var variable : Placeholders.findVariables(string)) { | |
| 63 | var evaluationContext = new StandardEvaluationContext(context); | |
| 64 | var parserConfiguration = new SpelParserConfiguration(); | |
| 65 | var parser = new SpelExpressionParser(parserConfiguration); | |
| 66 | var expression = parser.parseExpression(variable.content()); | |
| 67 | var value = expression.getValue(evaluationContext); | |
| 68 | ||
| 69 |
1
1. processShape : negated conditional → KILLED |
if (value instanceof Image image) { |
| 70 |
1
1. processShape : removed call to pro/verron/officestamper/experimental/PowerpointStamper::fillShapeWithImage → KILLED |
fillShapeWithImage(slide, shape, image); |
| 71 | return; | |
| 72 | } | |
| 73 | else { | |
| 74 | var replacement = new CTRegularTextRun(); | |
| 75 |
1
1. processShape : removed call to org/docx4j/dml/CTRegularTextRun::setT → KILLED |
replacement.setT(String.valueOf(value)); |
| 76 | var expression1 = variable.expression(); | |
| 77 |
1
1. processShape : removed call to pro/verron/officestamper/experimental/PowerpointParagraph::replace → KILLED |
paragraph1.replace(expression1, new Insert(replacement)); |
| 78 | } | |
| 79 | } | |
| 80 | } | |
| 81 | } | |
| 82 | ||
| 83 | private void fillShapeWithImage(SlidePart slide, Shape shape, Image image) { | |
| 84 | PresentationMLPackage presentationMLPackage = (PresentationMLPackage) slide.getPackage(); | |
| 85 | var openPackage = OpenPackage.getOrCreate(presentationMLPackage, slide); | |
| 86 | var imgPart = openPackage.findOrCreateImgPart(image::getBytes, true); | |
| 87 | var relId = imgPart.relationship() | |
| 88 | .getId(); | |
| 89 | ||
| 90 | var factory = new org.docx4j.dml.ObjectFactory(); | |
| 91 | var blipFill = factory.createCTBlipFillProperties(); | |
| 92 | var blip = factory.createCTBlip(); | |
| 93 |
1
1. fillShapeWithImage : removed call to org/docx4j/dml/CTBlip::setEmbed → SURVIVED |
blip.setEmbed(relId); |
| 94 |
1
1. fillShapeWithImage : removed call to org/docx4j/dml/CTBlipFillProperties::setBlip → KILLED |
blipFill.setBlip(blip); |
| 95 | ||
| 96 | var stretch = factory.createCTStretchInfoProperties(); | |
| 97 |
1
1. fillShapeWithImage : removed call to org/docx4j/dml/CTStretchInfoProperties::setFillRect → SURVIVED |
stretch.setFillRect(factory.createCTRelativeRect()); |
| 98 |
1
1. fillShapeWithImage : removed call to org/docx4j/dml/CTBlipFillProperties::setStretch → SURVIVED |
blipFill.setStretch(stretch); |
| 99 | ||
| 100 |
2
1. fillShapeWithImage : removed call to org/pptx4j/pml/Shape::setSpPr → NO_COVERAGE 2. fillShapeWithImage : negated conditional → SURVIVED |
if (shape.getSpPr() == null) shape.setSpPr(factory.createCTShapeProperties()); |
| 101 | shape.getSpPr() | |
| 102 |
1
1. fillShapeWithImage : removed call to org/docx4j/dml/CTShapeProperties::setBlipFill → KILLED |
.setBlipFill(blipFill); |
| 103 | ||
| 104 |
1
1. fillShapeWithImage : removed call to org/pptx4j/pml/Shape::setTxBody → KILLED |
shape.setTxBody(null); |
| 105 | } | |
| 106 | } | |
Mutations | ||
| 45 |
1.1 |
|
| 48 |
1.1 |
|
| 55 |
1.1 |
|
| 69 |
1.1 |
|
| 70 |
1.1 |
|
| 75 |
1.1 |
|
| 77 |
1.1 |
|
| 93 |
1.1 |
|
| 94 |
1.1 |
|
| 97 |
1.1 |
|
| 98 |
1.1 |
|
| 100 |
1.1 2.2 |
|
| 102 |
1.1 |
|
| 104 |
1.1 |