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