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