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