1 | package pro.verron.officestamper.preset.processors.replacewith; | |
2 | ||
3 | import org.docx4j.wml.R; | |
4 | import org.springframework.lang.Nullable; | |
5 | import pro.verron.officestamper.api.AbstractCommentProcessor; | |
6 | import pro.verron.officestamper.api.CommentProcessor; | |
7 | import pro.verron.officestamper.api.DocxPart; | |
8 | import pro.verron.officestamper.api.ParagraphPlaceholderReplacer; | |
9 | import pro.verron.officestamper.preset.CommentProcessorFactory; | |
10 | import pro.verron.officestamper.utils.WmlFactory; | |
11 | ||
12 | import java.util.List; | |
13 | import java.util.function.Function; | |
14 | ||
15 | import static pro.verron.officestamper.utils.WmlFactory.newText; | |
16 | ||
17 | /// Processor that replaces the current run with the provided expression. | |
18 | /// This is useful for replacing an expression in a comment with the result of the expression. | |
19 | /// | |
20 | /// @author Joseph Verron | |
21 | /// @author Tom Hombergs | |
22 | /// @version ${version} | |
23 | /// @since 1.0.7 | |
24 | public class ReplaceWithProcessor | |
25 | extends AbstractCommentProcessor | |
26 | implements CommentProcessorFactory.IReplaceWithProcessor { | |
27 | ||
28 | private final Function<R, List<Object>> nullSupplier; | |
29 | ||
30 | private ReplaceWithProcessor( | |
31 | ParagraphPlaceholderReplacer placeholderReplacer, Function<R, List<Object>> nullSupplier | |
32 | ) { | |
33 | super(placeholderReplacer); | |
34 | this.nullSupplier = nullSupplier; | |
35 | } | |
36 | ||
37 | /// Creates a new processor that replaces the current run with the result of the expression. | |
38 | /// | |
39 | /// @param pr the placeholder replacer to use | |
40 | /// | |
41 | /// @return the processor | |
42 | public static CommentProcessor newInstance(ParagraphPlaceholderReplacer pr) { | |
43 |
1
1. newInstance : replaced return value with null for pro/verron/officestamper/preset/processors/replacewith/ReplaceWithProcessor::newInstance → KILLED |
return new ReplaceWithProcessor(pr, R::getContent); |
44 | } | |
45 | ||
46 | /// {@inheritDoc} | |
47 | @Override public void commitChanges(DocxPart document) { | |
48 | // nothing to commit | |
49 | } | |
50 | ||
51 | /// {@inheritDoc} | |
52 | @Override public void reset() { | |
53 | // nothing to reset | |
54 | } | |
55 | ||
56 | /// {@inheritDoc} | |
57 | @Override public void replaceWordWith(@Nullable String expression) { | |
58 |
1
1. replaceWordWith : removed call to pro/verron/officestamper/preset/processors/replacewith/ReplaceWithProcessor::replaceWith → KILLED |
replaceWith(expression); |
59 | } | |
60 | ||
61 | @Override | |
62 | public void replaceWith(@Nullable String expression) { | |
63 | R run = this.getCurrentRun(); | |
64 |
1
1. replaceWith : negated conditional → KILLED |
if (run != null) { //TODO Remove the run-based secundary case that becomes redundant |
65 |
1
1. replaceWith : negated conditional → KILLED |
var target = expression != null ? List.of(newText(expression)) : nullSupplier.apply(run); |
66 | var runContent = run.getContent(); | |
67 |
1
1. replaceWith : removed call to java/util/List::clear → SURVIVED |
runContent.clear(); |
68 | runContent.addAll(target); | |
69 | } | |
70 | else { | |
71 | var comment = this.getCurrentCommentWrapper(); | |
72 | var from = comment.getCommentRangeStart(); | |
73 | var to = comment.getCommentRangeEnd(); | |
74 |
1
1. replaceWith : removed call to pro/verron/officestamper/api/Paragraph::replace → KILLED |
getParagraph().replace(from, to, WmlFactory.newRun(expression)); |
75 | } | |
76 | } | |
77 | } | |
Mutations | ||
43 |
1.1 |
|
58 |
1.1 |
|
64 |
1.1 |
|
65 |
1.1 |
|
67 |
1.1 |
|
74 |
1.1 |