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

Mutations

39

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

51

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

54

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

55

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

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::visit → KILLED

57

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

58

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

59

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

60

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

61

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

62

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

63

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

64

1.1
Location : visit
Killed by : none
removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → 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()]
removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → KILLED

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

67

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

68

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

69

1.1
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()]
removed call to pro/verron/officestamper/experimental/ExcelVisitor::visit → KILLED

71

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

72

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

73

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

74

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

75

1.1
Location : visit
Killed by : none
removed call to pro/verron/officestamper/experimental/ExcelVisitor::ignore → 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 java/util/List::forEach → KILLED

77

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/Set::forEach → KILLED

78

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

79

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

80

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

81

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

89

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.21.0