| 1 | package pro.verron.officestamper.preset.postprocessors.cleanendnotes; | |
| 2 | ||
| 3 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; | |
| 4 | import org.docx4j.openpackaging.parts.WordprocessingML.EndnotesPart; | |
| 5 | import org.docx4j.wml.CTEndnotes; | |
| 6 | import org.docx4j.wml.CTFtnEdn; | |
| 7 | import pro.verron.officestamper.api.PostProcessor; | |
| 8 | import pro.verron.officestamper.preset.postprocessors.NoteRefsVisitor; | |
| 9 | import pro.verron.officestamper.utils.wml.WmlUtils; | |
| 10 | ||
| 11 | import java.util.Collection; | |
| 12 | import java.util.Optional; | |
| 13 | ||
| 14 | import static org.docx4j.wml.STFtnEdn.NORMAL; | |
| 15 | import static pro.verron.officestamper.api.OfficeStamperException.throwing; | |
| 16 | import static pro.verron.officestamper.utils.wml.WmlUtils.visitDocument; | |
| 17 | ||
| 18 | /// A post-processor that removes endnotes which are not referenced in the document. | |
| 19 | /// | |
| 20 | /// This processor analyzes the document to find all endnote references and then removes any endnotes that are not | |
| 21 | /// referenced. It helps keep the document clean by eliminating orphaned endnotes. | |
| 22 | public class RemoveOrphanedEndnotesProcessor | |
| 23 | implements PostProcessor { | |
| 24 | @Override | |
| 25 | public void process(WordprocessingMLPackage document) { | |
| 26 | var visitor = new NoteRefsVisitor(); | |
| 27 |
1
1. process : removed call to pro/verron/officestamper/utils/wml/WmlUtils::visitDocument → TIMED_OUT |
visitDocument(document, visitor); |
| 28 | var referencedNoteIds = visitor.referencedNoteIds(); | |
| 29 | var mainDocumentPart = document.getMainDocumentPart(); | |
| 30 | ||
| 31 | var ednPart = mainDocumentPart.getEndNotesPart(); | |
| 32 | Optional.ofNullable(ednPart) | |
| 33 | .stream() | |
| 34 | .map(throwing(EndnotesPart::getContents)) | |
| 35 | .map(CTEndnotes::getEndnote) | |
| 36 | .flatMap(Collection::stream) | |
| 37 | .filter(RemoveOrphanedEndnotesProcessor::normalNotes) | |
| 38 |
2
1. lambda$process$0 : replaced boolean return with true for pro/verron/officestamper/preset/postprocessors/cleanendnotes/RemoveOrphanedEndnotesProcessor::lambda$process$0 → KILLED 2. lambda$process$0 : negated conditional → KILLED |
.filter(note -> !referencedNoteIds.contains(note.getId())) |
| 39 | .toList() | |
| 40 |
1
1. process : removed call to java/util/List::forEach → TIMED_OUT |
.forEach(WmlUtils::remove); |
| 41 | } | |
| 42 | ||
| 43 | private static boolean normalNotes(CTFtnEdn note) { | |
| 44 |
2
1. normalNotes : replaced boolean return with true for pro/verron/officestamper/preset/postprocessors/cleanendnotes/RemoveOrphanedEndnotesProcessor::normalNotes → SURVIVED 2. normalNotes : replaced boolean return with false for pro/verron/officestamper/preset/postprocessors/cleanendnotes/RemoveOrphanedEndnotesProcessor::normalNotes → KILLED |
return Optional.ofNullable(note.getType()) |
| 45 | .orElse(NORMAL) | |
| 46 | .equals(NORMAL); | |
| 47 | } | |
| 48 | } | |
Mutations | ||
| 27 |
1.1 |
|
| 38 |
1.1 2.2 |
|
| 40 |
1.1 |
|
| 44 |
1.1 2.2 |