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