| 1 | package pro.verron.officestamper.preset.postprocessors.cleanfootnotes; | |
| 2 | ||
| 3 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; | |
| 4 | import org.docx4j.openpackaging.parts.WordprocessingML.FootnotesPart; | |
| 5 | import org.docx4j.wml.CTFootnotes; | |
| 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 implementation that removes orphaned footnotes from a Word document. | |
| 19 | /// | |
| 20 | /// This processor analyzes the document to find footnote references and removes any footnotes that are not referenced | |
| 21 | /// in the document content. It only processes normal footnotes, ignoring special footnote types like endnotes. | |
| 22 | /// | |
| 23 | /// @author Joseph Verron | |
| 24 | /// @version ${version} | |
| 25 | public class RemoveOrphanedFootnotesProcessor | |
| 26 | implements PostProcessor { | |
| 27 | @Override | |
| 28 | public void process(WordprocessingMLPackage document) { | |
| 29 | var visitor = new NoteRefsVisitor(); | |
| 30 |
1
1. process : removed call to pro/verron/officestamper/utils/wml/WmlUtils::visitDocument → TIMED_OUT |
visitDocument(document, visitor); |
| 31 | var referencedNoteIds = visitor.referencedNoteIds(); | |
| 32 | var mainDocumentPart = document.getMainDocumentPart(); | |
| 33 | ||
| 34 | var ftnPart = mainDocumentPart.getFootnotesPart(); | |
| 35 | Optional.ofNullable(ftnPart) | |
| 36 | .stream() | |
| 37 | .map(throwing(FootnotesPart::getContents)) | |
| 38 | .map(CTFootnotes::getFootnote) | |
| 39 | .flatMap(Collection::stream) | |
| 40 | .filter(RemoveOrphanedFootnotesProcessor::normalNotes) | |
| 41 |
2
1. lambda$process$0 : negated conditional → KILLED 2. lambda$process$0 : replaced boolean return with true for pro/verron/officestamper/preset/postprocessors/cleanfootnotes/RemoveOrphanedFootnotesProcessor::lambda$process$0 → KILLED |
.filter(note -> !referencedNoteIds.contains(note.getId())) |
| 42 | .toList() | |
| 43 |
1
1. process : removed call to java/util/List::forEach → TIMED_OUT |
.forEach(WmlUtils::remove); |
| 44 | } | |
| 45 | ||
| 46 | private static boolean normalNotes(CTFtnEdn note) { | |
| 47 |
2
1. normalNotes : replaced boolean return with true for pro/verron/officestamper/preset/postprocessors/cleanfootnotes/RemoveOrphanedFootnotesProcessor::normalNotes → SURVIVED 2. normalNotes : replaced boolean return with false for pro/verron/officestamper/preset/postprocessors/cleanfootnotes/RemoveOrphanedFootnotesProcessor::normalNotes → KILLED |
return Optional.ofNullable(note.getType()) |
| 48 | .orElse(NORMAL) | |
| 49 | .equals(NORMAL); | |
| 50 | } | |
| 51 | } | |
Mutations | ||
| 30 |
1.1 |
|
| 41 |
1.1 2.2 |
|
| 43 |
1.1 |
|
| 47 |
1.1 2.2 |