1 | package pro.verron.officestamper.preset.processors.repeatparagraph; | |
2 | ||
3 | import org.docx4j.XmlUtils; | |
4 | import org.docx4j.wml.ContentAccessor; | |
5 | import org.docx4j.wml.P; | |
6 | import pro.verron.officestamper.api.*; | |
7 | import pro.verron.officestamper.core.CommentUtil; | |
8 | import pro.verron.officestamper.core.SectionUtil; | |
9 | import pro.verron.officestamper.core.StandardParagraph; | |
10 | import pro.verron.officestamper.preset.CommentProcessorFactory; | |
11 | import pro.verron.officestamper.preset.Paragraphs; | |
12 | ||
13 | import java.util.HashMap; | |
14 | import java.util.LinkedList; | |
15 | import java.util.List; | |
16 | import java.util.Map; | |
17 | ||
18 | import static java.util.Collections.emptyIterator; | |
19 | import static pro.verron.officestamper.core.SectionUtil.getPreviousSectionBreakIfPresent; | |
20 | import static pro.verron.officestamper.core.SectionUtil.hasOddNumberOfSectionBreaks; | |
21 | ||
22 | /// Class used internally to repeat document elements. | |
23 | /// Used by the lib, should not be instantiated by clients. | |
24 | /// | |
25 | /// @author Joseph Verron | |
26 | /// @author Youssouf Naciri | |
27 | /// @version ${version} | |
28 | /// @since 1.2.2 | |
29 | public class ParagraphRepeatProcessor | |
30 | extends AbstractCommentProcessor | |
31 | implements CommentProcessorFactory.IParagraphRepeatProcessor { | |
32 | ||
33 | /* TODO replace the mapping by a Paragraphs to List<Object> mapping to better reflect the change*/ | |
34 | private Map<Paragraph, Paragraphs> pToRepeat = new HashMap<>(); | |
35 | ||
36 | private ParagraphRepeatProcessor(ParagraphPlaceholderReplacer placeholderReplacer) { | |
37 | super(placeholderReplacer); | |
38 | } | |
39 | ||
40 | /// Creates a new instance of [CommentProcessor] using the provided [ParagraphPlaceholderReplacer]. | |
41 | /// | |
42 | /// @param placeholderReplacer the replacer to use for processing paragraph placeholders. | |
43 | /// | |
44 | /// @return a new instance of [ParagraphRepeatProcessor]. | |
45 | public static CommentProcessor newInstance(ParagraphPlaceholderReplacer placeholderReplacer) { | |
46 |
1
1. newInstance : replaced return value with null for pro/verron/officestamper/preset/processors/repeatparagraph/ParagraphRepeatProcessor::newInstance → KILLED |
return new ParagraphRepeatProcessor(placeholderReplacer); |
47 | } | |
48 | ||
49 | @Override public void repeatParagraph(Iterable<Object> objects) { | |
50 | var paragraph = getParagraph(); | |
51 | var comment = getCurrentCommentWrapper(); | |
52 | var elements = comment.getElements(); | |
53 | var previousSectionBreak = getPreviousSectionBreakIfPresent(elements.getFirst(), comment.getParent()); | |
54 | var oddNumberOfBreaks = hasOddNumberOfSectionBreaks(elements); | |
55 |
1
1. repeatParagraph : negated conditional → KILLED |
var iterator = objects == null ? emptyIterator() : objects.iterator(); |
56 | var toRepeat = new Paragraphs(comment, iterator, elements, previousSectionBreak, oddNumberOfBreaks); | |
57 | pToRepeat.put(paragraph, toRepeat); | |
58 | } | |
59 | ||
60 | @Override public void commitChanges(DocxPart document) { | |
61 | for (Map.Entry<Paragraph, Paragraphs> entry : pToRepeat.entrySet()) { | |
62 | var current = entry.getKey(); | |
63 | var replacement = entry.getValue(); | |
64 | var toRemove = replacement.elements(P.class); | |
65 | var toAdd = generateParagraphsToAdd(document, replacement); | |
66 |
1
1. commitChanges : removed call to pro/verron/officestamper/api/Paragraph::replace → KILLED |
current.replace(toRemove, toAdd); |
67 | } | |
68 | } | |
69 | ||
70 | private List<P> generateParagraphsToAdd(DocxPart document, Paragraphs paragraphs) { | |
71 | var paragraphsToAdd = new LinkedList<P>(); | |
72 | for (var it = paragraphs.data(); it.hasNext(); ) { | |
73 | Object expressionContext = it.next(); | |
74 | for (Object paragraphToClone : paragraphs.elements()) { | |
75 | Object clone = XmlUtils.deepCopy(paragraphToClone); | |
76 | var comment = paragraphs.comment(); | |
77 | var comment1 = comment.getComment(); | |
78 | var commentId = comment1.getId(); | |
79 |
1
1. generateParagraphsToAdd : negated conditional → KILLED |
if (clone instanceof ContentAccessor contentAccessor) { |
80 |
1
1. generateParagraphsToAdd : removed call to pro/verron/officestamper/core/CommentUtil::deleteCommentFromElements → KILLED |
CommentUtil.deleteCommentFromElements(contentAccessor.getContent(), commentId); |
81 | } | |
82 |
1
1. generateParagraphsToAdd : negated conditional → KILLED |
if (clone instanceof P p) { |
83 | var paragraph = StandardParagraph.from(document, p); | |
84 |
1
1. generateParagraphsToAdd : removed call to pro/verron/officestamper/api/ParagraphPlaceholderReplacer::resolveExpressionsForParagraph → KILLED |
placeholderReplacer.resolveExpressionsForParagraph(document, paragraph, expressionContext); |
85 | paragraphsToAdd.add(p); | |
86 | } | |
87 | } | |
88 | var sectPr = paragraphs.previousSectionBreak(); | |
89 |
3
1. generateParagraphsToAdd : negated conditional → KILLED 2. generateParagraphsToAdd : negated conditional → KILLED 3. generateParagraphsToAdd : negated conditional → KILLED |
if (paragraphs.oddNumberOfBreaks() && sectPr.isPresent() && it.hasNext()) { |
90 | assert paragraphsToAdd.peekLast() != null : "There should be at least one "; | |
91 |
1
1. generateParagraphsToAdd : removed call to pro/verron/officestamper/core/SectionUtil::applySectionBreakToParagraph → KILLED |
SectionUtil.applySectionBreakToParagraph(sectPr.get(), paragraphsToAdd.peekLast()); |
92 | } | |
93 | } | |
94 |
1
1. generateParagraphsToAdd : replaced return value with Collections.emptyList for pro/verron/officestamper/preset/processors/repeatparagraph/ParagraphRepeatProcessor::generateParagraphsToAdd → KILLED |
return paragraphsToAdd; |
95 | } | |
96 | ||
97 | @Override public void reset() { | |
98 | pToRepeat = new HashMap<>(); | |
99 | } | |
100 | } | |
Mutations | ||
46 |
1.1 |
|
55 |
1.1 |
|
66 |
1.1 |
|
79 |
1.1 |
|
80 |
1.1 |
|
82 |
1.1 |
|
84 |
1.1 |
|
89 |
1.1 2.2 3.3 |
|
91 |
1.1 |
|
94 |
1.1 |