Preprocessors.java

1
package pro.verron.officestamper.preset;
2
3
import org.docx4j.TraversalUtil;
4
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
5
import org.docx4j.utils.TraversalUtilVisitor;
6
import org.docx4j.wml.ContentAccessor;
7
import org.docx4j.wml.ProofErr;
8
import org.docx4j.wml.R;
9
import org.docx4j.wml.RPr;
10
import pro.verron.officestamper.api.OfficeStamperException;
11
import pro.verron.officestamper.api.PreProcessor;
12
13
import java.util.ArrayList;
14
import java.util.LinkedHashSet;
15
import java.util.List;
16
import java.util.Objects;
17
18
/**
19
 * A helper class that provides pre-processing functionality for WordprocessingMLPackage documents.
20
 */
21
public class Preprocessors {
22
23
    private Preprocessors() {
24
        throw new OfficeStamperException("Preprocessors cannot be instantiated");
25
    }
26
27
    /**
28
     * Returns a PreProcessor object that merges same style runs that are next to each other in a
29
     * WordprocessingMLPackage document.
30
     *
31
     * @return a PreProcessor object that merges similar runs.
32
     */
33
    public static PreProcessor mergeSimilarRuns() {
34 1 1. mergeSimilarRuns : replaced return value with null for pro/verron/officestamper/preset/Preprocessors::mergeSimilarRuns → KILLED
        return new MergeSameStyleRuns();
35
    }
36
37
    /**
38
     * Returns a PreProcessor object that removes all {@link ProofErr} elements from the WordprocessingMLPackage
39
     * document.
40
     *
41
     * @return a PreProcessor object that removes ProofErr elements.
42
     */
43
    public static PreProcessor removeLanguageProof() {
44 1 1. removeLanguageProof : replaced return value with null for pro/verron/officestamper/preset/Preprocessors::removeLanguageProof → KILLED
        return new RemoveProofErrors();
45
    }
46
47
    private static class MergeSameStyleRuns
48
            implements PreProcessor {
49
50
        /**
51
         * {@inheritDoc}
52
         */
53
        @Override
54
        public void process(WordprocessingMLPackage document) {
55
            var mainDocumentPart = document.getMainDocumentPart();
56
            var visitor = new SimilarRunVisitor();
57 1 1. process : removed call to org/docx4j/TraversalUtil::visit → SURVIVED
            TraversalUtil.visit(mainDocumentPart, visitor);
58
            for (List<R> similarStyleRuns : visitor.getSimilarStyleRuns()) {
59
                R firstRun = similarStyleRuns.get(0);
60
                var runContent = firstRun.getContent();
61
                var firstRunContent = new LinkedHashSet<>(runContent);
62
                var firstRunParentContent = ((ContentAccessor) firstRun.getParent()).getContent();
63
                for (R r : similarStyleRuns.subList(1, similarStyleRuns.size())) {
64
                    firstRunParentContent.remove(r);
65
                    firstRunContent.addAll(r.getContent());
66
                }
67 1 1. process : removed call to java/util/List::clear → KILLED
                runContent.clear();
68
                runContent.addAll(firstRunContent);
69
            }
70
        }
71
72
        private static class SimilarRunVisitor
73
                extends TraversalUtilVisitor<R> {
74
75
            private final List<List<R>> similarStyleRuns = new ArrayList<>();
76
77
            public List<List<R>> getSimilarStyleRuns() {
78 1 1. getSimilarStyleRuns : replaced return value with Collections.emptyList for pro/verron/officestamper/preset/Preprocessors$MergeSameStyleRuns$SimilarRunVisitor::getSimilarStyleRuns → SURVIVED
                return similarStyleRuns;
79
            }
80
81
            @Override
82
            public void apply(R element, Object parent, List<Object> siblings) {
83
                RPr rPr = element.getRPr();
84
                int currentIndex = siblings.indexOf(element);
85
                List<R> similarStyleConcurrentRun = siblings
86
                        .stream()
87
                        .skip(currentIndex)
88 3 1. lambda$apply$0 : negated conditional → SURVIVED
2. lambda$apply$0 : negated conditional → SURVIVED
3. lambda$apply$0 : replaced boolean return with true for pro/verron/officestamper/preset/Preprocessors$MergeSameStyleRuns$SimilarRunVisitor::lambda$apply$0 → KILLED
                        .takeWhile(o -> o instanceof R run && Objects.equals(run.getRPr(), rPr))
89
                        .map(R.class::cast)
90
                        .toList();
91
92 2 1. apply : negated conditional → SURVIVED
2. apply : changed conditional boundary → SURVIVED
                if (similarStyleConcurrentRun.size() > 1)
93
                    similarStyleRuns.add(similarStyleConcurrentRun);
94
            }
95
        }
96
    }
97
98
    private static class RemoveProofErrors
99
            implements PreProcessor {
100
101
        /**
102
         * {@inheritDoc}
103
         */
104
        @Override
105
        public void process(WordprocessingMLPackage document) {
106
            var mainDocumentPart = document.getMainDocumentPart();
107
            var visitor = new ProofErrVisitor();
108 1 1. process : removed call to org/docx4j/TraversalUtil::visit → SURVIVED
            TraversalUtil.visit(mainDocumentPart, visitor);
109
            for (ProofErr proofErr : visitor.getProofErrs()) {
110
                var proofErrParent = proofErr.getParent();
111 1 1. process : negated conditional → SURVIVED
                if (proofErrParent instanceof ContentAccessor parent) {
112
                    var parentContent = parent.getContent();
113
                    parentContent.remove(proofErr);
114
                }
115
            }
116
        }
117
118
        private static class ProofErrVisitor
119
                extends TraversalUtilVisitor<ProofErr> {
120
            private final List<ProofErr> proofErrs = new ArrayList<>();
121
122
            @Override
123
            public void apply(ProofErr element, Object parent1, List<Object> siblings) {
124
                proofErrs.add(element);
125
            }
126
127
            public List<ProofErr> getProofErrs() {
128 1 1. getProofErrs : replaced return value with Collections.emptyList for pro/verron/officestamper/preset/Preprocessors$RemoveProofErrors$ProofErrVisitor::getProofErrs → SURVIVED
                return proofErrs;
129
            }
130
        }
131
    }
132
}

Mutations

34

1.1
Location : mergeSimilarRuns
Killed by : pro.verron.officestamper.test.RegressionTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.RegressionTests]/[method:test64()]
replaced return value with null for pro/verron/officestamper/preset/Preprocessors::mergeSimilarRuns → KILLED

44

1.1
Location : removeLanguageProof
Killed by : pro.verron.officestamper.test.RegressionTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.RegressionTests]/[method:test64()]
replaced return value with null for pro/verron/officestamper/preset/Preprocessors::removeLanguageProof → KILLED

57

1.1
Location : process
Killed by : none
removed call to org/docx4j/TraversalUtil::visit → SURVIVED
Covering tests

67

1.1
Location : process
Killed by : pro.verron.officestamper.test.SpelInstantiationTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.SpelInstantiationTest]/[method:testDateInstantiationAndResolution()]
removed call to java/util/List::clear → KILLED

78

1.1
Location : getSimilarStyleRuns
Killed by : none
replaced return value with Collections.emptyList for pro/verron/officestamper/preset/Preprocessors$MergeSameStyleRuns$SimilarRunVisitor::getSimilarStyleRuns → SURVIVED
Covering tests

88

1.1
Location : lambda$apply$0
Killed by : none
negated conditional → SURVIVED
Covering tests

2.2
Location : lambda$apply$0
Killed by : none
negated conditional → SURVIVED Covering tests

3.3
Location : lambda$apply$0
Killed by : pro.verron.officestamper.test.RegressionTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.RegressionTests]/[test-template:test52(pro.verron.officestamper.test.RegressionTests$Conditions, java.lang.String)]/[test-template-invocation:#1]
replaced boolean return with true for pro/verron/officestamper/preset/Preprocessors$MergeSameStyleRuns$SimilarRunVisitor::lambda$apply$0 → KILLED

92

1.1
Location : apply
Killed by : none
negated conditional → SURVIVED
Covering tests

2.2
Location : apply
Killed by : none
changed conditional boundary → SURVIVED Covering tests

108

1.1
Location : process
Killed by : none
removed call to org/docx4j/TraversalUtil::visit → SURVIVED
Covering tests

111

1.1
Location : process
Killed by : none
negated conditional → SURVIVED
Covering tests

128

1.1
Location : getProofErrs
Killed by : none
replaced return value with Collections.emptyList for pro/verron/officestamper/preset/Preprocessors$RemoveProofErrors$ProofErrVisitor::getProofErrs → SURVIVED
Covering tests

Active mutators

Tests examined


Report generated by PIT 1.17.0