CommentHooker.java

1
package pro.verron.officestamper.api;
2
3
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
4
import org.docx4j.utils.TraversalUtilVisitor;
5
import org.docx4j.wml.CommentRangeStart;
6
import org.docx4j.wml.ContentAccessor;
7
import pro.verron.officestamper.utils.wml.WmlUtils;
8
9
import java.util.ArrayList;
10
import java.util.List;
11
12
import static pro.verron.officestamper.utils.wml.WmlFactory.newCtAttr;
13
import static pro.verron.officestamper.utils.wml.WmlFactory.newSmartTag;
14
15
/// The [CommentHooker] class is responsible for preparing comment processors in a Word document. It implements the
16
/// [PreProcessor] interface and provides functionality to process comment range starts and wrap them with smart tags
17
/// for further processing by the OfficeStamper engine.
18
///
19
/// This pre-processor is typically used to identify and mark comment-based expressions, making them recognizable as
20
/// hooks for subsequent processing steps.
21
public final class CommentHooker
22
        implements PreProcessor {
23
24
    /// Default constructor for CommentHooker.
25
    public CommentHooker() {
26
    }
27
28
    @Override
29
    public void process(WordprocessingMLPackage document) {
30
        var visitor = new CRSCollector();
31 1 1. process : removed call to pro/verron/officestamper/utils/wml/WmlUtils::visitDocument → KILLED
        WmlUtils.visitDocument(document, visitor);
32
        // Replaces comment range starts with smart tags
33
        for (var commentRangeStart : visitor.commentRangeStarts()) {
34
            var parent = (ContentAccessor) commentRangeStart.getParent();
35
            var siblings = parent.getContent();
36
            var crsIndex = siblings.indexOf(commentRangeStart);
37
            var tag = newSmartTag("officestamper", newCtAttr("type", "processor"), commentRangeStart);
38
            siblings.set(crsIndex, tag);
39
        }
40
    }
41
42
    /// A collector class that gathers [CommentRangeStart] elements during document traversal. This class extends
43
    /// [TraversalUtilVisitor] to collect all [CommentRangeStart] objects encountered while traversing a DOCX document
44
    /// structure.
45
    public static class CRSCollector
46
            extends TraversalUtilVisitor<CommentRangeStart> {
47
48
        private final List<CommentRangeStart> results = new ArrayList<>();
49
50
        /// Default constructor for CRSCollector.
51
        public CRSCollector() {
52
        }
53
54
        @Override
55
        public void apply(CommentRangeStart element) {
56
            results.add(element);
57
        }
58
59
        /// Returns the list of collected CommentRangeStart elements.
60
        ///
61
        /// @return a list of CommentRangeStart objects that have been collected during document traversal
62
        public List<CommentRangeStart> commentRangeStarts() {
63 1 1. commentRangeStarts : replaced return value with Collections.emptyList for pro/verron/officestamper/api/CommentHooker$CRSCollector::commentRangeStarts → KILLED
            return results;
64
        }
65
    }
66
}

Mutations

31

1.1
Location : process
Killed by : pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest]/[test-template:fails(pro.verron.officestamper.test.utils.ContextFactory)]/[test-template-invocation:#1]
removed call to pro/verron/officestamper/utils/wml/WmlUtils::visitDocument → KILLED

63

1.1
Location : commentRangeStarts
Killed by : pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest]/[test-template:fails(pro.verron.officestamper.test.utils.ContextFactory)]/[test-template-invocation:#1]
replaced return value with Collections.emptyList for pro/verron/officestamper/api/CommentHooker$CRSCollector::commentRangeStarts → KILLED

Active mutators

Tests examined


Report generated by PIT 1.22.0