HookRemover.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.CTSmartTagRun;
6
import org.docx4j.wml.ContentAccessor;
7
8
import java.util.ArrayList;
9
import java.util.List;
10
11
import static pro.verron.officestamper.utils.wml.WmlUtils.visitDocument;
12
13
/// A post-processor implementation that removes smart tags from a WordprocessingML document.
14
///
15
/// This processor is designed to clean up documents by removing specific smart tags (hooks) while preserving their
16
/// content. It operates on the document's XML structure to find and eliminate smart tag elements.
17
///
18
/// This is particularly useful for removing the `officestamper` smart tags used during the stamping process to leave a
19
/// clean document.
20
public final class HookRemover
21
        implements PostProcessor {
22
    private final String element;
23
24
25
    /// Constructs a new [HookRemover] with the specified element name.
26
    ///
27
    /// @param element the name of the element to be removed from the document
28
    public HookRemover(String element) {this.element = element;}
29
30
    @Override
31
    public void process(WordprocessingMLPackage document) {
32
        var visitor = new TagsVisitor(element);
33 1 1. process : removed call to pro/verron/officestamper/utils/wml/WmlUtils::visitDocument → KILLED
        visitDocument(document, visitor);
34
        // Replaces tags with their content in parent
35
        for (CTSmartTagRun tag : visitor.getTags()) {
36
            var parent = (ContentAccessor) tag.getParent();
37
            var siblings = parent.getContent();
38
            var index = siblings.indexOf(tag);
39
            siblings.remove(tag);
40
            siblings.addAll(index, tag.getContent());
41
        }
42
    }
43
44
    static class TagsVisitor
45
            extends TraversalUtilVisitor<CTSmartTagRun> {
46
        private final String element;
47
        private final List<CTSmartTagRun> results = new ArrayList<>();
48
49
        TagsVisitor(String element) {this.element = element;}
50
51
        @Override
52
        public void apply(CTSmartTagRun tag) {
53 1 1. apply : negated conditional → TIMED_OUT
            if (element.equals(tag.getElement())) results.add(tag);
54
        }
55
56
        List<CTSmartTagRun> getTags() {
57 1 1. getTags : replaced return value with Collections.emptyList for pro/verron/officestamper/api/HookRemover$TagsVisitor::getTags → TIMED_OUT
            return results;
58
        }
59
    }
60
}

Mutations

33

1.1
Location : process
Killed by : pro.verron.officestamper.test.DefaultTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.DefaultTests]/[test-template:features(pro.verron.officestamper.api.OfficeStamperConfiguration, java.lang.Object, org.docx4j.openpackaging.packages.WordprocessingMLPackage, java.lang.String)]/[test-template-invocation:#27]
removed call to pro/verron/officestamper/utils/wml/WmlUtils::visitDocument → KILLED

53

1.1
Location : apply
Killed by : none
negated conditional → TIMED_OUT

57

1.1
Location : getTags
Killed by : none
replaced return value with Collections.emptyList for pro/verron/officestamper/api/HookRemover$TagsVisitor::getTags → TIMED_OUT

Active mutators

Tests examined


Report generated by PIT 1.22.0