TableResolver.java

1
package pro.verron.officestamper.preset.processors.table;
2
3
import jakarta.xml.bind.JAXBElement;
4
import org.docx4j.XmlUtils;
5
import org.docx4j.wml.ContentAccessor;
6
import org.docx4j.wml.Tbl;
7
import org.docx4j.wml.Tc;
8
import org.docx4j.wml.Tr;
9
import org.jspecify.annotations.Nullable;
10
import pro.verron.officestamper.api.CommentProcessor;
11
import pro.verron.officestamper.api.ProcessorContext;
12
import pro.verron.officestamper.preset.CommentProcessorFactory;
13
import pro.verron.officestamper.preset.StampTable;
14
import pro.verron.officestamper.utils.wml.WmlFactory;
15
16
import java.util.List;
17
18
import static pro.verron.officestamper.api.OfficeStamperException.throwing;
19
20
/// TableResolver class.
21
///
22
/// @author Joseph Verron
23
/// @since 1.6.2
24
public class TableResolver
25
        extends CommentProcessor
26
        implements CommentProcessorFactory.ITableResolver {
27
28
    /// Constructs a new TableResolver with the given processor context.
29
    ///
30
    /// @param processorContext the context in which this processor operates
31
    public TableResolver(ProcessorContext processorContext) {
32
        super(processorContext);
33
    }
34
35
    @Override
36
    public void resolveTable(@Nullable StampTable givenTable) {
37
        var tbl = paragraph().parent(Tbl.class)
38
                             .orElseThrow(throwing("Paragraph is not within a table!"));
39 1 1. resolveTable : negated conditional → KILLED
        if (givenTable != null) {
40 1 1. resolveTable : removed call to pro/verron/officestamper/preset/processors/table/TableResolver::replaceTableInplace → KILLED
            replaceTableInplace(tbl, givenTable);
41
        }
42
        else {
43
            List<Object> tableParentContent = ((ContentAccessor) tbl.getParent()).getContent();
44
            tableParentContent.remove(tbl);
45
        }
46
    }
47
48
    private void replaceTableInplace(Tbl wordTable, StampTable stampedTable) {
49
        var headers = stampedTable.headers();
50
51
        var rows = wordTable.getContent();
52
        var headerRow = (Tr) rows.get(0);
53
        var firstDataRow = (Tr) rows.get(1);
54
55 1 1. replaceTableInplace : removed call to pro/verron/officestamper/preset/processors/table/TableResolver::growAndFillRow → KILLED
        growAndFillRow(headerRow, headers);
56
57 1 1. replaceTableInplace : negated conditional → KILLED
        if (stampedTable.isEmpty()) rows.remove(firstDataRow);
58
        else {
59 1 1. replaceTableInplace : removed call to pro/verron/officestamper/preset/processors/table/TableResolver::growAndFillRow → KILLED
            growAndFillRow(firstDataRow, stampedTable.getFirst());
60
            for (var rowContent : stampedTable.subList(1, stampedTable.size()))
61
                rows.add(copyRowFromTemplate(firstDataRow, rowContent));
62
        }
63
    }
64
65
    private void growAndFillRow(Tr row, List<String> values) {
66
        List<Object> cellRowContent = row.getContent();
67
68
        //Replace text in first cell
69
        JAXBElement<Tc> cell0 = (JAXBElement<Tc>) cellRowContent.getFirst();
70
        Tc cell0tc = cell0.getValue();
71 2 1. growAndFillRow : removed call to pro/verron/officestamper/preset/processors/table/TableResolver::setCellText → KILLED
2. growAndFillRow : negated conditional → KILLED
        setCellText(cell0tc, values.isEmpty() ? "" : values.getFirst());
72
73 2 1. growAndFillRow : changed conditional boundary → SURVIVED
2. growAndFillRow : negated conditional → KILLED
        if (values.size() > 1) {
74
            //Copy the first cell and replace content for each remaining value
75
            for (String cellContent : values.subList(1, values.size())) {
76
                JAXBElement<Tc> xmlCell = XmlUtils.deepCopy(cell0);
77 1 1. growAndFillRow : removed call to pro/verron/officestamper/preset/processors/table/TableResolver::setCellText → KILLED
                setCellText(xmlCell.getValue(), cellContent);
78
                cellRowContent.add(xmlCell);
79
            }
80
        }
81
    }
82
83
    private Tr copyRowFromTemplate(Tr firstDataRow, List<String> rowContent) {
84
        Tr newXmlRow = XmlUtils.deepCopy(firstDataRow);
85
        List<Object> xmlRow = newXmlRow.getContent();
86 2 1. copyRowFromTemplate : negated conditional → KILLED
2. copyRowFromTemplate : changed conditional boundary → KILLED
        for (int i = 0; i < rowContent.size(); i++) {
87
            String cellContent = rowContent.get(i);
88
            Tc xmlCell = ((JAXBElement<Tc>) xmlRow.get(i)).getValue();
89 1 1. copyRowFromTemplate : removed call to pro/verron/officestamper/preset/processors/table/TableResolver::setCellText → KILLED
            setCellText(xmlCell, cellContent);
90
        }
91 1 1. copyRowFromTemplate : replaced return value with null for pro/verron/officestamper/preset/processors/table/TableResolver::copyRowFromTemplate → KILLED
        return newXmlRow;
92
    }
93
94
    private void setCellText(Tc tableCell, String content) {
95
        var tableCellContent = tableCell.getContent();
96 1 1. setCellText : removed call to java/util/List::clear → KILLED
        tableCellContent.clear();
97
        tableCellContent.add(WmlFactory.newParagraph(content));
98
    }
99
}

Mutations

39

1.1
Location : resolveTable
Killed by : pro.verron.officestamper.test.StampTableTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.StampTableTest]/[test-template:stampTableTest(pro.verron.officestamper.test.utils.ContextFactory)]/[test-template-invocation:#2]
negated conditional → KILLED

40

1.1
Location : resolveTable
Killed by : pro.verron.officestamper.test.StampTableTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.StampTableTest]/[test-template:stampTableTest(pro.verron.officestamper.test.utils.ContextFactory)]/[test-template-invocation:#2]
removed call to pro/verron/officestamper/preset/processors/table/TableResolver::replaceTableInplace → KILLED

55

1.1
Location : replaceTableInplace
Killed by : pro.verron.officestamper.test.StampTableTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.StampTableTest]/[test-template:stampTableTest(pro.verron.officestamper.test.utils.ContextFactory)]/[test-template-invocation:#2]
removed call to pro/verron/officestamper/preset/processors/table/TableResolver::growAndFillRow → KILLED

57

1.1
Location : replaceTableInplace
Killed by : pro.verron.officestamper.test.StampTableTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.StampTableTest]/[test-template:stampTableTest(pro.verron.officestamper.test.utils.ContextFactory)]/[test-template-invocation:#2]
negated conditional → KILLED

59

1.1
Location : replaceTableInplace
Killed by : pro.verron.officestamper.test.StampTableTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.StampTableTest]/[test-template:stampTableTest(pro.verron.officestamper.test.utils.ContextFactory)]/[test-template-invocation:#2]
removed call to pro/verron/officestamper/preset/processors/table/TableResolver::growAndFillRow → KILLED

71

1.1
Location : growAndFillRow
Killed by : pro.verron.officestamper.test.StampTableTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.StampTableTest]/[test-template:stampTableTest(pro.verron.officestamper.test.utils.ContextFactory)]/[test-template-invocation:#2]
removed call to pro/verron/officestamper/preset/processors/table/TableResolver::setCellText → KILLED

2.2
Location : growAndFillRow
Killed by : pro.verron.officestamper.test.StampTableTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.StampTableTest]/[test-template:stampTableTest(pro.verron.officestamper.test.utils.ContextFactory)]/[test-template-invocation:#2]
negated conditional → KILLED

73

1.1
Location : growAndFillRow
Killed by : pro.verron.officestamper.test.StampTableTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.StampTableTest]/[test-template:stampTableTest(pro.verron.officestamper.test.utils.ContextFactory)]/[test-template-invocation:#2]
negated conditional → KILLED

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

77

1.1
Location : growAndFillRow
Killed by : pro.verron.officestamper.test.StampTableTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.StampTableTest]/[test-template:stampTableTest(pro.verron.officestamper.test.utils.ContextFactory)]/[test-template-invocation:#2]
removed call to pro/verron/officestamper/preset/processors/table/TableResolver::setCellText → KILLED

86

1.1
Location : copyRowFromTemplate
Killed by : pro.verron.officestamper.test.StampTableTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.StampTableTest]/[test-template:stampTableTest(pro.verron.officestamper.test.utils.ContextFactory)]/[test-template-invocation:#2]
negated conditional → KILLED

2.2
Location : copyRowFromTemplate
Killed by : pro.verron.officestamper.test.StampTableTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.StampTableTest]/[test-template:stampTableTest(pro.verron.officestamper.test.utils.ContextFactory)]/[test-template-invocation:#2]
changed conditional boundary → KILLED

89

1.1
Location : copyRowFromTemplate
Killed by : pro.verron.officestamper.test.StampTableTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.StampTableTest]/[test-template:stampTableTest(pro.verron.officestamper.test.utils.ContextFactory)]/[test-template-invocation:#2]
removed call to pro/verron/officestamper/preset/processors/table/TableResolver::setCellText → KILLED

91

1.1
Location : copyRowFromTemplate
Killed by : pro.verron.officestamper.test.StampTableTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.StampTableTest]/[test-template:stampTableTest(pro.verron.officestamper.test.utils.ContextFactory)]/[test-template-invocation:#2]
replaced return value with null for pro/verron/officestamper/preset/processors/table/TableResolver::copyRowFromTemplate → KILLED

96

1.1
Location : setCellText
Killed by : pro.verron.officestamper.test.StampTableTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.StampTableTest]/[test-template:stampTableTest(pro.verron.officestamper.test.utils.ContextFactory)]/[test-template-invocation:#2]
removed call to java/util/List::clear → KILLED

Active mutators

Tests examined


Report generated by PIT 1.25.5 support