ExcelVisitor.java

1
package pro.verron.officestamper.experimental;
2
3
import jakarta.xml.bind.JAXBElement;
4
import org.docx4j.openpackaging.exceptions.Docx4JException;
5
import org.docx4j.openpackaging.packages.SpreadsheetMLPackage;
6
import org.docx4j.openpackaging.parts.DocPropsCorePart;
7
import org.docx4j.openpackaging.parts.DocPropsExtendedPart;
8
import org.docx4j.openpackaging.parts.Parts;
9
import org.docx4j.openpackaging.parts.SpreadsheetML.SharedStrings;
10
import org.docx4j.openpackaging.parts.SpreadsheetML.Styles;
11
import org.docx4j.openpackaging.parts.SpreadsheetML.WorkbookPart;
12
import org.docx4j.openpackaging.parts.SpreadsheetML.WorksheetPart;
13
import org.docx4j.openpackaging.parts.ThemePart;
14
import org.slf4j.Logger;
15
import org.slf4j.LoggerFactory;
16
import org.springframework.lang.Nullable;
17
import org.xlsx4j.sml.*;
18
import pro.verron.officestamper.api.OfficeStamperException;
19
20
import java.util.List;
21
import java.util.Map;
22
import java.util.Map.Entry;
23
import java.util.Set;
24
25
import static java.util.Arrays.stream;
26
27
/**
28
 * The ExcelVisitor class provides a mechanism for visiting different types of Excel objects.
29
 * It contains visit methods for various types of objects and performs specific actions based on the object type.
30
 * Subclasses can extend this class and override the before method to define custom behavior before visiting an object.
31
 */
32
abstract class ExcelVisitor {
33
34
    private static final Logger logger = LoggerFactory.getLogger(ExcelVisitor.class);
35
36
    private static void unexpectedVisit(Object object) {
37
        var env = System.getenv();
38
        var throwOnUnexpectedVisit = Boolean.parseBoolean(env.getOrDefault("throw-on-unexpected-visit", "false"));
39
        var message = "Unknown case : %s %s".formatted(object, object.getClass());
40 1 1. unexpectedVisit : negated conditional → KILLED
        if (throwOnUnexpectedVisit)
41
            throw new OfficeStamperException(message);
42
        else
43
            logger.debug(message);
44
    }
45
46
    private static void ignore(@Nullable Object ignored1) {
47
        logger.trace("ignored visit of '{}' object", ignored1);
48
    }
49
50
    /**
51
     * Visits the given object and performs specific operations based on its type.
52
     *
53
     * @param object the object to visit
54
     */
55
    public final void visit(@Nullable Object object) {
56 1 1. visit : removed call to pro/verron/officestamper/experimental/ExcelVisitor::before → KILLED
        before(object);
57
        try {
58 2 1. visit : removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → KILLED
2. visit : negated conditional → KILLED
            if (object instanceof SpreadsheetMLPackage element) visit(element.getParts());
59
60 2 1. visit : negated conditional → KILLED
2. visit : removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → KILLED
            else if (object instanceof Parts element) visit(element.getParts());
61 2 1. visit : negated conditional → KILLED
2. visit : removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → KILLED
            else if (object instanceof WorksheetPart element) visit(element.getContents());
62 2 1. visit : removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → SURVIVED
2. visit : negated conditional → KILLED
            else if (object instanceof WorkbookPart element) visit(element.getContents());
63 2 1. visit : removed call to pro/verron/officestamper/experimental/ExcelVisitor::ignore → SURVIVED
2. visit : negated conditional → KILLED
            else if (object instanceof DocPropsCorePart ignored) ignore(ignored);
64 2 1. visit : removed call to pro/verron/officestamper/experimental/ExcelVisitor::ignore → SURVIVED
2. visit : negated conditional → KILLED
            else if (object instanceof DocPropsExtendedPart ignored) ignore(ignored);
65 2 1. visit : removed call to pro/verron/officestamper/experimental/ExcelVisitor::ignore → SURVIVED
2. visit : negated conditional → KILLED
            else if (object instanceof Styles ignored) ignore(ignored);
66 2 1. visit : removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → KILLED
2. visit : negated conditional → KILLED
            else if (object instanceof SharedStrings element) visit(element.getContents());
67 2 1. visit : removed call to pro/verron/officestamper/experimental/ExcelVisitor::ignore → SURVIVED
2. visit : negated conditional → KILLED
            else if (object instanceof ThemePart ignored) ignore(ignored);
68
69 2 1. visit : removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → SURVIVED
2. visit : negated conditional → KILLED
            else if (object instanceof Workbook element) visit(element.getSheets());
70 2 1. visit : removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → SURVIVED
2. visit : negated conditional → KILLED
            else if (object instanceof Sheets element) visit(element.getSheet());
71 2 1. visit : removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → KILLED
2. visit : negated conditional → KILLED
            else if (object instanceof Worksheet element) visit(element.getSheetData());
72 2 1. visit : negated conditional → KILLED
2. visit : removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → KILLED
            else if (object instanceof SheetData element) visit(element.getRow());
73 2 1. visit : negated conditional → KILLED
2. visit : removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → KILLED
            else if (object instanceof Row element) visit(element.getC());
74 2 1. visit : removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → SURVIVED
2. visit : negated conditional → KILLED
            else if (object instanceof Cell element) visit(element.getIs());
75 2 1. visit : removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → SURVIVED
2. visit : negated conditional → KILLED
            else if (object instanceof CTRst element) visit(element.getR());
76 2 1. visit : removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → KILLED
2. visit : negated conditional → KILLED
            else if (object instanceof CTSst element) visit(element.getSi());
77 2 1. visit : removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → NO_COVERAGE
2. visit : negated conditional → KILLED
            else if (object instanceof CTRElt element) visit(element.getT());
78 2 1. visit : removed call to pro/verron/officestamper/experimental/ExcelVisitor::ignore → NO_COVERAGE
2. visit : negated conditional → KILLED
            else if (object instanceof CTXstringWhitespace ignored) ignore(ignored);
79 2 1. visit : removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → NO_COVERAGE
2. visit : negated conditional → KILLED
            else if (object instanceof JAXBElement<?> element) visit(element.getValue());
80 2 1. visit : removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → SURVIVED
2. visit : negated conditional → KILLED
            else if (object instanceof Sheet element) visit(element.getState());
81 2 1. visit : removed call to pro/verron/officestamper/experimental/ExcelVisitor::ignore → SURVIVED
2. visit : negated conditional → KILLED
            else if (object instanceof STSheetState ignored) ignore(ignored);
82
83 2 1. visit : removed call to java/util/List::forEach → KILLED
2. visit : negated conditional → KILLED
            else if (object instanceof List<?> element) element.forEach(this::visit);
84 2 1. visit : negated conditional → KILLED
2. visit : removed call to java/util/Set::forEach → KILLED
            else if (object instanceof Set<?> element) element.forEach(this::visit);
85 2 1. visit : negated conditional → KILLED
2. visit : removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → KILLED
            else if (object instanceof Map<?, ?> element) visit(element.entrySet());
86 2 1. visit : removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → KILLED
2. visit : negated conditional → KILLED
            else if (object instanceof Entry<?, ?> element) visit(element.getKey(), element.getValue());
87 2 1. visit : removed call to pro/verron/officestamper/experimental/ExcelVisitor::ignore → SURVIVED
2. visit : negated conditional → KILLED
            else if (object == null) ignore(null);
88 1 1. visit : removed call to pro/verron/officestamper/experimental/ExcelVisitor::unexpectedVisit → SURVIVED
            else unexpectedVisit(object);
89
        } catch (Docx4JException e) {
90
            throw new OfficeStamperException(e);
91
        }
92
    }
93
94
    private void visit(Object... objs) {
95 1 1. visit : removed call to java/util/stream/Stream::forEach → KILLED
        stream(objs).forEach(this::visit);
96
    }
97
98
    /**
99
     * This method is called before performing a visit.
100
     * It provides an opportunity to perform any necessary setup or validation
101
     * before the actual visit takes place.
102
     *
103
     * @param object the object on which the visit will be performed.
104
     */
105
    protected abstract void before(@Nullable Object object);
106
}

Mutations

40

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

56

1.1
Location : visit
Killed by : pro.verron.officestamper.test.BasicExcelTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.BasicExcelTest]/[method:testStamper()]
removed call to pro/verron/officestamper/experimental/ExcelVisitor::before → KILLED

58

1.1
Location : visit
Killed by : pro.verron.officestamper.test.BasicExcelTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.BasicExcelTest]/[method:testStamper()]
removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → KILLED

2.2
Location : visit
Killed by : pro.verron.officestamper.test.BasicExcelTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.BasicExcelTest]/[method:testStamper()]
negated conditional → KILLED

60

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

2.2
Location : visit
Killed by : pro.verron.officestamper.test.BasicExcelTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.BasicExcelTest]/[method:testStamper()]
removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → KILLED

61

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

2.2
Location : visit
Killed by : pro.verron.officestamper.test.BasicExcelTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.BasicExcelTest]/[method:testStamper()]
removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → KILLED

62

1.1
Location : visit
Killed by : none
removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → SURVIVED
Covering tests

2.2
Location : visit
Killed by : pro.verron.officestamper.test.BasicExcelTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.BasicExcelTest]/[method:testStamper()]
negated conditional → KILLED

63

1.1
Location : visit
Killed by : none
removed call to pro/verron/officestamper/experimental/ExcelVisitor::ignore → SURVIVED
Covering tests

2.2
Location : visit
Killed by : pro.verron.officestamper.test.BasicExcelTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.BasicExcelTest]/[method:testStamper()]
negated conditional → KILLED

64

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

2.2
Location : visit
Killed by : none
removed call to pro/verron/officestamper/experimental/ExcelVisitor::ignore → SURVIVED
Covering tests

65

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

2.2
Location : visit
Killed by : none
removed call to pro/verron/officestamper/experimental/ExcelVisitor::ignore → SURVIVED
Covering tests

66

1.1
Location : visit
Killed by : pro.verron.officestamper.test.BasicExcelTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.BasicExcelTest]/[method:testStamper()]
removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → KILLED

2.2
Location : visit
Killed by : pro.verron.officestamper.test.BasicExcelTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.BasicExcelTest]/[method:testStamper()]
negated conditional → KILLED

67

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

2.2
Location : visit
Killed by : none
removed call to pro/verron/officestamper/experimental/ExcelVisitor::ignore → SURVIVED
Covering tests

69

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

2.2
Location : visit
Killed by : none
removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → SURVIVED
Covering tests

70

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

2.2
Location : visit
Killed by : none
removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → SURVIVED
Covering tests

71

1.1
Location : visit
Killed by : pro.verron.officestamper.test.BasicExcelTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.BasicExcelTest]/[method:testStamper()]
removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → KILLED

2.2
Location : visit
Killed by : pro.verron.officestamper.test.BasicExcelTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.BasicExcelTest]/[method:testStamper()]
negated conditional → KILLED

72

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

2.2
Location : visit
Killed by : pro.verron.officestamper.test.BasicExcelTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.BasicExcelTest]/[method:testStamper()]
removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → KILLED

73

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

2.2
Location : visit
Killed by : pro.verron.officestamper.test.BasicExcelTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.BasicExcelTest]/[method:testStamper()]
removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → KILLED

74

1.1
Location : visit
Killed by : none
removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → SURVIVED
Covering tests

2.2
Location : visit
Killed by : pro.verron.officestamper.test.BasicExcelTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.BasicExcelTest]/[method:testStamper()]
negated conditional → KILLED

75

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

2.2
Location : visit
Killed by : none
removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → SURVIVED
Covering tests

76

1.1
Location : visit
Killed by : pro.verron.officestamper.test.BasicExcelTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.BasicExcelTest]/[method:testStamper()]
removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → KILLED

2.2
Location : visit
Killed by : pro.verron.officestamper.test.BasicExcelTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.BasicExcelTest]/[method:testStamper()]
negated conditional → KILLED

77

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

2.2
Location : visit
Killed by : none
removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → NO_COVERAGE

78

1.1
Location : visit
Killed by : none
removed call to pro/verron/officestamper/experimental/ExcelVisitor::ignore → NO_COVERAGE

2.2
Location : visit
Killed by : pro.verron.officestamper.test.BasicExcelTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.BasicExcelTest]/[method:testStamper()]
negated conditional → KILLED

79

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

2.2
Location : visit
Killed by : none
removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → NO_COVERAGE

80

1.1
Location : visit
Killed by : none
removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → SURVIVED
Covering tests

2.2
Location : visit
Killed by : pro.verron.officestamper.test.BasicExcelTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.BasicExcelTest]/[method:testStamper()]
negated conditional → KILLED

81

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

2.2
Location : visit
Killed by : none
removed call to pro/verron/officestamper/experimental/ExcelVisitor::ignore → SURVIVED
Covering tests

83

1.1
Location : visit
Killed by : pro.verron.officestamper.test.BasicExcelTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.BasicExcelTest]/[method:testStamper()]
removed call to java/util/List::forEach → KILLED

2.2
Location : visit
Killed by : pro.verron.officestamper.test.BasicExcelTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.BasicExcelTest]/[method:testStamper()]
negated conditional → KILLED

84

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

2.2
Location : visit
Killed by : pro.verron.officestamper.test.BasicExcelTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.BasicExcelTest]/[method:testStamper()]
removed call to java/util/Set::forEach → KILLED

85

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

2.2
Location : visit
Killed by : pro.verron.officestamper.test.BasicExcelTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.BasicExcelTest]/[method:testStamper()]
removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → KILLED

86

1.1
Location : visit
Killed by : pro.verron.officestamper.test.BasicExcelTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.BasicExcelTest]/[method:testStamper()]
removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → KILLED

2.2
Location : visit
Killed by : pro.verron.officestamper.test.BasicExcelTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.BasicExcelTest]/[method:testStamper()]
negated conditional → KILLED

87

1.1
Location : visit
Killed by : none
removed call to pro/verron/officestamper/experimental/ExcelVisitor::ignore → SURVIVED
Covering tests

2.2
Location : visit
Killed by : pro.verron.officestamper.test.BasicExcelTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.BasicExcelTest]/[method:testStamper()]
negated conditional → KILLED

88

1.1
Location : visit
Killed by : none
removed call to pro/verron/officestamper/experimental/ExcelVisitor::unexpectedVisit → SURVIVED
Covering tests

95

1.1
Location : visit
Killed by : pro.verron.officestamper.test.BasicExcelTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.BasicExcelTest]/[method:testStamper()]
removed call to java/util/stream/Stream::forEach → KILLED

Active mutators

Tests examined


Report generated by PIT 1.17.0