-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add post-processors to remove orphaned footnotes and endnotes
Introduce processors to clean up unused footnotes and endnotes in Word documents by removing unreferenced notes. Integrate these processors into the default configuration to ensure document consistency after stamping.
- Loading branch information
1 parent
6b19a2a
commit 480eecb
Showing
10 changed files
with
215 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
engine/src/main/java/pro/verron/officestamper/preset/Postprocessors.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package pro.verron.officestamper.preset; | ||
|
||
import pro.verron.officestamper.api.OfficeStamperException; | ||
import pro.verron.officestamper.api.PostProcessor; | ||
import pro.verron.officestamper.preset.postprocessors.cleanendnotes.RemoveOrphanedEndnotesProcessor; | ||
import pro.verron.officestamper.preset.postprocessors.cleanfootnotes.RemoveOrphanedFootnotesProcessor; | ||
|
||
public class Postprocessors { | ||
private Postprocessors() { | ||
throw new OfficeStamperException("This is a utility class and cannot be instantiated"); | ||
} | ||
|
||
public static PostProcessor removeOrphanedFootnotes() { | ||
return new RemoveOrphanedFootnotesProcessor(); | ||
} | ||
|
||
public static PostProcessor removeOrphanedEndnotes() { | ||
return new RemoveOrphanedEndnotesProcessor(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
engine/src/main/java/pro/verron/officestamper/preset/postprocessors/NoteRefsVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package pro.verron.officestamper.preset.postprocessors; | ||
|
||
import org.docx4j.utils.TraversalUtilVisitor; | ||
import org.docx4j.wml.CTFtnEdnRef; | ||
|
||
import java.math.BigInteger; | ||
import java.util.SortedSet; | ||
import java.util.TreeSet; | ||
|
||
public class NoteRefsVisitor | ||
extends TraversalUtilVisitor<CTFtnEdnRef> { | ||
private final SortedSet<BigInteger> ids = new TreeSet<>(); | ||
|
||
@Override | ||
public void apply(CTFtnEdnRef element) { | ||
ids.add(element.getId()); | ||
} | ||
|
||
public SortedSet<BigInteger> referencedNoteIds() { | ||
return ids; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...on/officestamper/preset/postprocessors/cleanendnotes/RemoveOrphanedEndnotesProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package pro.verron.officestamper.preset.postprocessors.cleanendnotes; | ||
|
||
import org.docx4j.openpackaging.packages.WordprocessingMLPackage; | ||
import org.docx4j.openpackaging.parts.WordprocessingML.EndnotesPart; | ||
import org.docx4j.wml.CTEndnotes; | ||
import org.docx4j.wml.CTFtnEdn; | ||
import pro.verron.officestamper.api.PostProcessor; | ||
import pro.verron.officestamper.preset.postprocessors.NoteRefsVisitor; | ||
import pro.verron.officestamper.utils.WmlUtils; | ||
|
||
import java.util.Collection; | ||
import java.util.Optional; | ||
|
||
import static org.docx4j.wml.STFtnEdn.NORMAL; | ||
import static pro.verron.officestamper.api.OfficeStamperException.throwing; | ||
import static pro.verron.officestamper.core.DocumentUtil.visitDocument; | ||
|
||
public class RemoveOrphanedEndnotesProcessor | ||
implements PostProcessor { | ||
@Override | ||
public void process(WordprocessingMLPackage document) { | ||
var visitor = new NoteRefsVisitor(); | ||
visitDocument(document, visitor); | ||
var referencedNoteIds = visitor.referencedNoteIds(); | ||
var mainDocumentPart = document.getMainDocumentPart(); | ||
|
||
var ednPart = mainDocumentPart.getEndNotesPart(); | ||
Optional.ofNullable(ednPart) | ||
.stream() | ||
.map(throwing(EndnotesPart::getContents)) | ||
.map(CTEndnotes::getEndnote) | ||
.flatMap(Collection::stream) | ||
.filter(RemoveOrphanedEndnotesProcessor::normalNotes) | ||
.filter(note -> !referencedNoteIds.contains(note.getId())) | ||
.toList() | ||
.forEach(WmlUtils::remove); | ||
} | ||
|
||
private static boolean normalNotes(CTFtnEdn note) { | ||
return Optional.ofNullable(note.getType()) | ||
.orElse(NORMAL) | ||
.equals(NORMAL); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
.../officestamper/preset/postprocessors/cleanfootnotes/RemoveOrphanedFootnotesProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package pro.verron.officestamper.preset.postprocessors.cleanfootnotes; | ||
|
||
import org.docx4j.openpackaging.packages.WordprocessingMLPackage; | ||
import org.docx4j.openpackaging.parts.WordprocessingML.FootnotesPart; | ||
import org.docx4j.wml.CTFootnotes; | ||
import org.docx4j.wml.CTFtnEdn; | ||
import pro.verron.officestamper.api.PostProcessor; | ||
import pro.verron.officestamper.preset.postprocessors.NoteRefsVisitor; | ||
import pro.verron.officestamper.utils.WmlUtils; | ||
|
||
import java.util.Collection; | ||
import java.util.Optional; | ||
|
||
import static org.docx4j.wml.STFtnEdn.NORMAL; | ||
import static pro.verron.officestamper.api.OfficeStamperException.throwing; | ||
import static pro.verron.officestamper.core.DocumentUtil.visitDocument; | ||
|
||
public class RemoveOrphanedFootnotesProcessor | ||
implements PostProcessor { | ||
@Override | ||
public void process(WordprocessingMLPackage document) { | ||
var visitor = new NoteRefsVisitor(); | ||
visitDocument(document, visitor); | ||
var referencedNoteIds = visitor.referencedNoteIds(); | ||
var mainDocumentPart = document.getMainDocumentPart(); | ||
|
||
var ftnPart = mainDocumentPart.getFootnotesPart(); | ||
Optional.ofNullable(ftnPart) | ||
.stream() | ||
.map(throwing(FootnotesPart::getContents)) | ||
.map(CTFootnotes::getFootnote) | ||
.flatMap(Collection::stream) | ||
.filter(RemoveOrphanedFootnotesProcessor::normalNotes) | ||
.filter(note -> !referencedNoteIds.contains(note.getId())) | ||
.toList() | ||
.forEach(WmlUtils::remove); | ||
} | ||
|
||
private static boolean normalNotes(CTFtnEdn note) { | ||
return Optional.ofNullable(note.getType()) | ||
.orElse(NORMAL) | ||
.equals(NORMAL); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.