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 |
|
51 |
1.1 |
|
54 |
1.1 |
|
55 |
1.1 |
|
56 |
1.1 |
|
57 |
1.1 |
|
58 |
1.1 |
|
59 |
1.1 |
|
60 |
1.1 |
|
61 |
1.1 |
|
62 |
1.1 |
|
63 |
1.1 |
|
64 |
1.1 |
|
65 |
1.1 |
|
66 |
1.1 |
|
67 |
1.1 |
|
68 |
1.1 |
|
69 |
1.1 |
|
70 |
1.1 |
|
71 |
1.1 |
|
72 |
1.1 |
|
73 |
1.1 |
|
74 |
1.1 |
|
75 |
1.1 |
|
76 |
1.1 |
|
77 |
1.1 |
|
78 |
1.1 |
|
79 |
1.1 |
|
80 |
1.1 |
|
81 |
1.1 |
|
89 |
1.1 |