ParagraphRepeatProcessor.java

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 1 1. generateParagraphsToAdd : negated conditional → KILLED
                if (clone instanceof ContentAccessor contentAccessor) {
78 1 1. generateParagraphsToAdd : removed call to pro/verron/officestamper/core/CommentUtil::deleteCommentFromElements → KILLED
                    CommentUtil.deleteCommentFromElements(comment, contentAccessor.getContent());
79
                }
80 1 1. generateParagraphsToAdd : negated conditional → KILLED
                if (clone instanceof P p) {
81
                    var paragraph = StandardParagraph.from(document, p);
82 1 1. generateParagraphsToAdd : removed call to pro/verron/officestamper/api/ParagraphPlaceholderReplacer::resolveExpressionsForParagraph → KILLED
                    placeholderReplacer.resolveExpressionsForParagraph(document, paragraph, expressionContext);
83
                    paragraphsToAdd.add(p);
84
                }
85
            }
86
            var sectPr = paragraphs.previousSectionBreak();
87 3 1. generateParagraphsToAdd : negated conditional → KILLED
2. generateParagraphsToAdd : negated conditional → KILLED
3. generateParagraphsToAdd : negated conditional → KILLED
            if (paragraphs.oddNumberOfBreaks() && sectPr.isPresent() && it.hasNext()) {
88
                assert paragraphsToAdd.peekLast() != null : "There should be at least one ";
89 1 1. generateParagraphsToAdd : removed call to pro/verron/officestamper/core/SectionUtil::applySectionBreakToParagraph → KILLED
                SectionUtil.applySectionBreakToParagraph(sectPr.get(), paragraphsToAdd.peekLast());
90
            }
91
        }
92 1 1. generateParagraphsToAdd : replaced return value with Collections.emptyList for pro/verron/officestamper/preset/processors/repeatparagraph/ParagraphRepeatProcessor::generateParagraphsToAdd → KILLED
        return paragraphsToAdd;
93
    }
94
95
    @Override public void reset() {
96
        pToRepeat = new HashMap<>();
97
    }
98
}

Mutations

46

1.1
Location : newInstance
Killed by : pro.verron.officestamper.test.ResolutionTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ResolutionTest]/[test-template:testStaticResolution(java.lang.String, boolean, boolean, boolean, java.lang.String, java.lang.String)]/[test-template-invocation:#6]
replaced return value with null for pro/verron/officestamper/preset/processors/repeatparagraph/ParagraphRepeatProcessor::newInstance → KILLED

55

1.1
Location : repeatParagraph
Killed by : pro.verron.officestamper.test.ProcessorRepeatParagraphTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ProcessorRepeatParagraphTest]/[method:shouldAcceptSet()]
negated conditional → KILLED

66

1.1
Location : commitChanges
Killed by : pro.verron.officestamper.test.ProcessorRepeatParagraphTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ProcessorRepeatParagraphTest]/[method:shouldAcceptSet()]
removed call to pro/verron/officestamper/api/Paragraph::replace → KILLED

77

1.1
Location : generateParagraphsToAdd
Killed by : pro.verron.officestamper.test.ProcessorRepeatParagraphTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ProcessorRepeatParagraphTest]/[method:shouldAcceptSet()]
negated conditional → KILLED

78

1.1
Location : generateParagraphsToAdd
Killed by : pro.verron.officestamper.test.ProcessorRepeatParagraphTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ProcessorRepeatParagraphTest]/[method:shouldAcceptSet()]
removed call to pro/verron/officestamper/core/CommentUtil::deleteCommentFromElements → KILLED

80

1.1
Location : generateParagraphsToAdd
Killed by : pro.verron.officestamper.test.ProcessorRepeatParagraphTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ProcessorRepeatParagraphTest]/[method:shouldAcceptSet()]
negated conditional → KILLED

82

1.1
Location : generateParagraphsToAdd
Killed by : pro.verron.officestamper.test.ProcessorRepeatParagraphTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ProcessorRepeatParagraphTest]/[method:shouldAcceptSet()]
removed call to pro/verron/officestamper/api/ParagraphPlaceholderReplacer::resolveExpressionsForParagraph → KILLED

87

1.1
Location : generateParagraphsToAdd
Killed by : pro.verron.officestamper.test.ProcessorRepeatParagraphTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ProcessorRepeatParagraphTest]/[test-template:features(java.lang.String, pro.verron.officestamper.api.OfficeStamperConfiguration, java.lang.Object, java.io.InputStream, java.lang.String)]/[test-template-invocation:#3]
negated conditional → KILLED

2.2
Location : generateParagraphsToAdd
Killed by : pro.verron.officestamper.test.ProcessorRepeatParagraphTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ProcessorRepeatParagraphTest]/[test-template:features(java.lang.String, pro.verron.officestamper.api.OfficeStamperConfiguration, java.lang.Object, java.io.InputStream, java.lang.String)]/[test-template-invocation:#2]
negated conditional → KILLED

3.3
Location : generateParagraphsToAdd
Killed by : pro.verron.officestamper.test.ProcessorRepeatParagraphTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ProcessorRepeatParagraphTest]/[test-template:features(java.lang.String, pro.verron.officestamper.api.OfficeStamperConfiguration, java.lang.Object, java.io.InputStream, java.lang.String)]/[test-template-invocation:#3]
negated conditional → KILLED

89

1.1
Location : generateParagraphsToAdd
Killed by : pro.verron.officestamper.test.ProcessorRepeatParagraphTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ProcessorRepeatParagraphTest]/[test-template:features(java.lang.String, pro.verron.officestamper.api.OfficeStamperConfiguration, java.lang.Object, java.io.InputStream, java.lang.String)]/[test-template-invocation:#3]
removed call to pro/verron/officestamper/core/SectionUtil::applySectionBreakToParagraph → KILLED

92

1.1
Location : generateParagraphsToAdd
Killed by : pro.verron.officestamper.test.ProcessorRepeatParagraphTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ProcessorRepeatParagraphTest]/[method:shouldAcceptSet()]
replaced return value with Collections.emptyList for pro/verron/officestamper/preset/processors/repeatparagraph/ParagraphRepeatProcessor::generateParagraphsToAdd → KILLED

Active mutators

Tests examined


Report generated by PIT 1.21.0