PlaceholderReplacer.java

1
package pro.verron.officestamper.core;
2
3
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
4
import org.docx4j.wml.*;
5
import org.jvnet.jaxb2_commons.ppp.Child;
6
import org.springframework.expression.spel.SpelEvaluationException;
7
import org.springframework.expression.spel.SpelParseException;
8
import pro.verron.officestamper.api.*;
9
import pro.verron.officestamper.utils.WmlFactory;
10
11
/**
12
 * Replaces expressions in a document with the values provided by the {@link ExpressionResolver}.
13
 *
14
 * @author Joseph Verron
15
 * @author Tom Hombergs
16
 * @version ${version}
17
 * @since 1.0.0
18
 */
19
public class PlaceholderReplacer
20
        implements ParagraphPlaceholderReplacer {
21
22
    private final ExpressionResolver resolver;
23
    private final ObjectResolverRegistry registry;
24
    private final Placeholder lineBreakPlaceholder;
25
    private final ExceptionResolver exceptionResolver;
26
27
    /**
28
     * <p>Constructor for PlaceholderReplacer.</p>
29
     *
30
     * @param registry             the registry containing all available type resolvers.
31
     * @param resolver             the expression resolver used to resolve expressions in the document.
32
     * @param linebreakPlaceholder if set to a non-null value,
33
     *                             all occurrences of this placeholder will be
34
     *                             replaced with a line break.
35
     */
36
    public PlaceholderReplacer(
37
            ObjectResolverRegistry registry,
38
            ExpressionResolver resolver,
39
            Placeholder linebreakPlaceholder,
40
            ExceptionResolver exceptionResolver
41
    ) {
42
        this.registry = registry;
43
        this.resolver = resolver;
44
        this.lineBreakPlaceholder = linebreakPlaceholder;
45
        this.exceptionResolver = exceptionResolver;
46
    }
47
48
    /**
49
     * Finds expressions in a document and resolves them against the specified context object.
50
     * The resolved values will then replace the expressions in the document.
51
     *
52
     * @param expressionContext the context root
53
     */
54
    public void resolveExpressions(DocxPart document, Object expressionContext) {
55
        document.streamParagraphs()
56 2 1. lambda$resolveExpressions$0 : removed call to pro/verron/officestamper/core/PlaceholderReplacer::resolveExpressionsForParagraph → KILLED
2. resolveExpressions : removed call to java/util/stream/Stream::forEach → KILLED
                .forEach(paragraph -> resolveExpressionsForParagraph(document, paragraph, expressionContext));
57
    }
58
59
    /**
60
     * Finds expressions in the given paragraph and replaces them with the values provided by the expression resolver.
61
     *
62
     * @param docxPart  the document in which to replace all expressions.
63
     * @param paragraph the paragraph in which to replace expressions.
64
     * @param context   the context root
65
     */
66
    @Override public void resolveExpressionsForParagraph(
67
            DocxPart docxPart,
68
            Paragraph paragraph,
69
            Object context
70
    ) {
71
        var expressions = Placeholders.findVariables(paragraph);
72
        for (var expression : expressions) {
73
            var replacement = resolve(docxPart, context, expression);
74 1 1. resolveExpressionsForParagraph : removed call to pro/verron/officestamper/api/Paragraph::replace → KILLED
            paragraph.replace(expression, replacement);
75
        }
76 1 1. resolveExpressionsForParagraph : removed call to pro/verron/officestamper/api/Paragraph::replace → KILLED
        paragraph.replace(lineBreakPlaceholder, getBr());
77
    }
78
79
    private R resolve(DocxPart docxPart, Object context, Placeholder placeholder) {
80
        try {
81 1 1. resolve : removed call to pro/verron/officestamper/core/ExpressionResolver::setContext → KILLED
            resolver.setContext(context);
82
            var resolution = resolver.resolve(placeholder);
83 1 1. resolve : replaced return value with null for pro/verron/officestamper/core/PlaceholderReplacer::resolve → KILLED
            return registry.resolve(docxPart, placeholder, resolution);
84
        } catch (SpelEvaluationException
85
                 | SpelParseException
86
                 | OfficeStamperException e) {
87
            var message = "Expression %s could not be resolved against context of type %s"
88
                    .formatted(placeholder.expression(), context.getClass().getSimpleName());
89
            var resolution = exceptionResolver.resolve(placeholder, message, e);
90 1 1. resolve : replaced return value with null for pro/verron/officestamper/core/PlaceholderReplacer::resolve → KILLED
            return WmlFactory.newRun(resolution);
91
        }
92
    }
93
94
    private static Child getBr() {
95
        var br = new Br();
96 1 1. getBr : removed call to org/docx4j/wml/Br::setType → SURVIVED
        br.setType(STBrType.TEXT_WRAPPING);
97 1 1. getBr : removed call to org/docx4j/wml/Br::setClear → SURVIVED
        br.setClear(null);
98 1 1. getBr : replaced return value with null for pro/verron/officestamper/core/PlaceholderReplacer::getBr → KILLED
        return br;
99
    }
100
101
    @Override
102
    public void resolveExpressionsForParagraph(Paragraph paragraph, Object context, WordprocessingMLPackage document) {
103
        throw new OfficeStamperException("Should not be called, since deprecated");
104
    }
105
}

Mutations

56

1.1
Location : lambda$resolveExpressions$0
Killed by : pro.verron.officestamper.test.ResolutionTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ResolutionTest]/[test-template:testCustomResolution(java.lang.String, boolean, boolean, boolean, boolean, boolean, java.lang.String, boolean, java.lang.String)]/[test-template-invocation:#30]
removed call to pro/verron/officestamper/core/PlaceholderReplacer::resolveExpressionsForParagraph → KILLED

2.2
Location : resolveExpressions
Killed by : pro.verron.officestamper.test.ResolutionTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ResolutionTest]/[test-template:testCustomResolution(java.lang.String, boolean, boolean, boolean, boolean, boolean, java.lang.String, boolean, java.lang.String)]/[test-template-invocation:#30]
removed call to java/util/stream/Stream::forEach → KILLED

74

1.1
Location : resolveExpressionsForParagraph
Killed by : pro.verron.officestamper.test.ResolutionTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ResolutionTest]/[test-template:testCustomResolution(java.lang.String, boolean, boolean, boolean, boolean, boolean, java.lang.String, boolean, java.lang.String)]/[test-template-invocation:#28]
removed call to pro/verron/officestamper/api/Paragraph::replace → KILLED

76

1.1
Location : resolveExpressionsForParagraph
Killed by : pro.verron.officestamper.test.DefaultTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.DefaultTests]/[test-template:features(java.lang.String, pro.verron.officestamper.api.OfficeStamperConfiguration, java.lang.Object, java.io.InputStream, java.lang.String)]/[test-template-invocation:#34]
removed call to pro/verron/officestamper/api/Paragraph::replace → KILLED

81

1.1
Location : resolve
Killed by : pro.verron.officestamper.test.ResolutionTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ResolutionTest]/[test-template:testCustomResolution(java.lang.String, boolean, boolean, boolean, boolean, boolean, java.lang.String, boolean, java.lang.String)]/[test-template-invocation:#28]
removed call to pro/verron/officestamper/core/ExpressionResolver::setContext → KILLED

83

1.1
Location : resolve
Killed by : pro.verron.officestamper.test.ResolutionTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ResolutionTest]/[test-template:testCustomResolution(java.lang.String, boolean, boolean, boolean, boolean, boolean, java.lang.String, boolean, java.lang.String)]/[test-template-invocation:#28]
replaced return value with null for pro/verron/officestamper/core/PlaceholderReplacer::resolve → KILLED

90

1.1
Location : resolve
Killed by : pro.verron.officestamper.test.ResolutionTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ResolutionTest]/[test-template:testCustomResolution(java.lang.String, boolean, boolean, boolean, boolean, boolean, java.lang.String, boolean, java.lang.String)]/[test-template-invocation:#3]
replaced return value with null for pro/verron/officestamper/core/PlaceholderReplacer::resolve → KILLED

96

1.1
Location : getBr
Killed by : none
removed call to org/docx4j/wml/Br::setType → SURVIVED
Covering tests

97

1.1
Location : getBr
Killed by : none
removed call to org/docx4j/wml/Br::setClear → SURVIVED
Covering tests

98

1.1
Location : getBr
Killed by : pro.verron.officestamper.test.ResolutionTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ResolutionTest]/[test-template:testCustomResolution(java.lang.String, boolean, boolean, boolean, boolean, boolean, java.lang.String, boolean, java.lang.String)]/[test-template-invocation:#28]
replaced return value with null for pro/verron/officestamper/core/PlaceholderReplacer::getBr → KILLED

Active mutators

Tests examined


Report generated by PIT 1.17.1