1 | package pro.verron.officestamper.preset.processors.repeat; | |
2 | ||
3 | import org.docx4j.TraversalUtil; | |
4 | import org.docx4j.XmlUtils; | |
5 | import org.docx4j.finders.ClassFinder; | |
6 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; | |
7 | import org.docx4j.wml.P; | |
8 | import org.docx4j.wml.Tbl; | |
9 | import org.docx4j.wml.Tr; | |
10 | import org.springframework.lang.Nullable; | |
11 | import pro.verron.officestamper.api.*; | |
12 | import pro.verron.officestamper.core.CommentUtil; | |
13 | import pro.verron.officestamper.core.StandardParagraph; | |
14 | import pro.verron.officestamper.preset.CommentProcessorFactory; | |
15 | ||
16 | import java.util.ArrayList; | |
17 | import java.util.HashMap; | |
18 | import java.util.List; | |
19 | import java.util.Map; | |
20 | import java.util.function.BiFunction; | |
21 | ||
22 | import static java.util.Collections.emptyList; | |
23 | import static java.util.Objects.requireNonNull; | |
24 | ||
25 | /// Repeats a table row for each element in a list. | |
26 | /// | |
27 | /// @author Joseph Verron | |
28 | /// @author Tom Hombergs | |
29 | /// @version ${version} | |
30 | /// @since 1.0.0 | |
31 | public class RepeatProcessor | |
32 | extends AbstractCommentProcessor | |
33 | implements CommentProcessorFactory.IRepeatProcessor { | |
34 | ||
35 | private final BiFunction<WordprocessingMLPackage, Tr, List<Tr>> nullSupplier; | |
36 | private Map<Tr, Iterable<Object>> tableRowsToRepeat = new HashMap<>(); | |
37 | private Map<Tr, Comment> tableRowsCommentsToRemove = new HashMap<>(); | |
38 | ||
39 | private RepeatProcessor( | |
40 | ParagraphPlaceholderReplacer placeholderReplacer, | |
41 | BiFunction<WordprocessingMLPackage, Tr, List<Tr>> nullSupplier1 | |
42 | ) { | |
43 | super(placeholderReplacer); | |
44 | nullSupplier = nullSupplier1; | |
45 | } | |
46 | ||
47 | /// Creates a new RepeatProcessor. | |
48 | /// | |
49 | /// @param pr The PlaceholderReplacer to use. | |
50 | /// | |
51 | /// @return A new RepeatProcessor. | |
52 | public static CommentProcessor newInstance(ParagraphPlaceholderReplacer pr) { | |
53 |
1
1. newInstance : replaced return value with null for pro/verron/officestamper/preset/processors/repeat/RepeatProcessor::newInstance → KILLED |
return new RepeatProcessor(pr, (document, row) -> emptyList()); |
54 | } | |
55 | ||
56 | /// {@inheritDoc} | |
57 | @Override | |
58 | public void commitChanges(DocxPart source) { | |
59 |
1
1. commitChanges : removed call to pro/verron/officestamper/preset/processors/repeat/RepeatProcessor::repeatRows → KILLED |
repeatRows(source); |
60 | } | |
61 | ||
62 | private void repeatRows(DocxPart source) { | |
63 | for (Map.Entry<Tr, Iterable<Object>> entry : tableRowsToRepeat.entrySet()) { | |
64 | Tr row = entry.getKey(); | |
65 | Iterable<Object> expressionContexts = entry.getValue(); | |
66 | ||
67 | Tbl table = (Tbl) XmlUtils.unwrap(row.getParent()); | |
68 | var content = table.getContent(); | |
69 | int index = content.indexOf(row); | |
70 | content.remove(row); | |
71 | ||
72 | List<Tr> changes; | |
73 |
1
1. repeatRows : negated conditional → KILLED |
if (expressionContexts == null) { |
74 | changes = nullSupplier.apply(source.document(), row); | |
75 | } | |
76 | else { | |
77 | changes = new ArrayList<>(); | |
78 | for (Object expressionContext : expressionContexts) { | |
79 | Tr rowClone = XmlUtils.deepCopy(row); | |
80 | Comment commentWrapper = requireNonNull(tableRowsCommentsToRemove.get(row)); | |
81 |
1
1. repeatRows : removed call to pro/verron/officestamper/core/CommentUtil::deleteCommentFromElements → KILLED |
CommentUtil.deleteCommentFromElements(commentWrapper, rowClone.getContent()); |
82 | var classFinder = new ClassFinder(P.class); | |
83 |
1
1. repeatRows : removed call to org/docx4j/TraversalUtil::visit → KILLED |
TraversalUtil.visit(rowClone, classFinder); |
84 | var objects = classFinder.results; | |
85 | for (Object object : objects) { | |
86 | P result = (P) object; | |
87 | StandardParagraph paragraph = StandardParagraph.from(source, result); | |
88 |
1
1. repeatRows : removed call to pro/verron/officestamper/api/ParagraphPlaceholderReplacer::resolveExpressionsForParagraph → KILLED |
placeholderReplacer.resolveExpressionsForParagraph(source, paragraph, expressionContext); |
89 | } | |
90 | changes.add(rowClone); | |
91 | } | |
92 | } | |
93 | content.addAll(index, changes); | |
94 | } | |
95 | } | |
96 | ||
97 | /// {@inheritDoc} | |
98 | @Override | |
99 | public void reset() { | |
100 | this.tableRowsToRepeat = new HashMap<>(); | |
101 | this.tableRowsCommentsToRemove = new HashMap<>(); | |
102 | } | |
103 | ||
104 | /// {@inheritDoc} | |
105 | @Override | |
106 | public void repeatTableRow(@Nullable Iterable<Object> objects) { | |
107 | var tr = this.getParagraph() | |
108 | .parent(Tr.class) | |
109 | .orElseThrow(OfficeStamperException.throwing("This paragraph is not in a table row.")); | |
110 | tableRowsToRepeat.put(tr, objects); | |
111 | tableRowsCommentsToRemove.put(tr, getCurrentCommentWrapper()); | |
112 | } | |
113 | ||
114 | } | |
Mutations | ||
53 |
1.1 |
|
59 |
1.1 |
|
73 |
1.1 |
|
81 |
1.1 |
|
83 |
1.1 |
|
88 |
1.1 |