| 1 | package pro.verron.officestamper.api; | |
| 2 | ||
| 3 | import org.docx4j.TraversalUtil; | |
| 4 | import org.docx4j.finders.CommentFinder; | |
| 5 | import org.docx4j.openpackaging.exceptions.Docx4JException; | |
| 6 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; | |
| 7 | import org.docx4j.wml.Comments; | |
| 8 | import org.docx4j.wml.ContentAccessor; | |
| 9 | import org.jvnet.jaxb2_commons.ppp.Child; | |
| 10 | ||
| 11 | /// The CommentRemover class is a concrete implementation of the PostProcessor interface. | |
| 12 | /// This class is responsible for removing all comments and their corresponding elements | |
| 13 | /// from a WordprocessingMLPackage document. It traverses the document, identifies comment | |
| 14 | /// elements, removes them from their parent contents, and clears the comments stored in | |
| 15 | /// the document's comments part. | |
| 16 | /// | |
| 17 | /// Key responsibilities of this class include: | |
| 18 | /// 1. Identifying all comment-related elements within the document using a CommentFinder instance. | |
| 19 | /// 2. Removing those elements from their parent's content list. | |
| 20 | /// 3. Accessing and clearing the document's comments part to ensure no residual comments remain. | |
| 21 | /// | |
| 22 | /// This class is designed for scenarios where comments in a document need to be fully removed | |
| 23 | /// as part of preprocessing or cleanup operations. | |
| 24 | public class CommentRemover | |
| 25 | implements PostProcessor { | |
| 26 | /// Build a [CommentRemover] instance | |
| 27 | public CommentRemover() {} | |
| 28 | ||
| 29 | @Override | |
| 30 | public void process(WordprocessingMLPackage document) { | |
| 31 | var visitor = new CommentFinder(); | |
| 32 |
1
1. process : removed call to org/docx4j/TraversalUtil::visit → KILLED |
TraversalUtil.visit(document.getMainDocumentPart(), visitor); |
| 33 | ||
| 34 | // Replaces tags with their content in parent | |
| 35 | for (Child commentElement : visitor.getCommentElements()) { | |
| 36 | var parent = (ContentAccessor) commentElement.getParent(); | |
| 37 | var siblings = parent.getContent(); | |
| 38 | siblings.remove(commentElement); | |
| 39 | } | |
| 40 | ||
| 41 | var mainDocumentPart = document.getMainDocumentPart(); | |
| 42 |
1
1. process : negated conditional → SURVIVED |
if (mainDocumentPart == null) return; |
| 43 | ||
| 44 | var commentsPart = mainDocumentPart.getCommentsPart(); | |
| 45 |
1
1. process : negated conditional → KILLED |
if (commentsPart == null) return; |
| 46 | ||
| 47 | Comments commentsPartContents; | |
| 48 | try { | |
| 49 | commentsPartContents = commentsPart.getContents(); | |
| 50 | } catch (Docx4JException e) { | |
| 51 | throw new OfficeStamperException(e); | |
| 52 | } | |
| 53 | ||
| 54 |
1
1. process : negated conditional → SURVIVED |
if (commentsPartContents == null) return; |
| 55 | var comments = commentsPartContents.getComment(); | |
| 56 | ||
| 57 |
1
1. process : negated conditional → SURVIVED |
if (comments == null) return; |
| 58 |
1
1. process : removed call to java/util/List::clear → SURVIVED |
comments.clear(); |
| 59 | } | |
| 60 | } | |
Mutations | ||
| 32 |
1.1 |
|
| 42 |
1.1 |
|
| 45 |
1.1 |
|
| 54 |
1.1 |
|
| 57 |
1.1 |
|
| 58 |
1.1 |