| 1 | package pro.verron.officestamper.core; | |
| 2 | ||
| 3 | import org.docx4j.TextUtils; | |
| 4 | import org.docx4j.wml.*; | |
| 5 | import org.docx4j.wml.R.CommentReference; | |
| 6 | import org.jspecify.annotations.Nullable; | |
| 7 | import pro.verron.officestamper.api.Comment; | |
| 8 | import pro.verron.officestamper.api.DocxPart; | |
| 9 | import pro.verron.officestamper.api.Paragraph; | |
| 10 | ||
| 11 | import java.math.BigInteger; | |
| 12 | import java.util.ArrayList; | |
| 13 | import java.util.List; | |
| 14 | ||
| 15 | import static java.util.stream.Collectors.joining; | |
| 16 | import static pro.verron.officestamper.utils.wml.WmlFactory.*; | |
| 17 | ||
| 18 | /// Standard implementation of the [Comment] interface. Represents a comment in a DOCX document with its associated | |
| 19 | /// range markers and content. | |
| 20 | /// | |
| 21 | /// @author Joseph Verron | |
| 22 | /// @author Tom Hombergs | |
| 23 | /// @version ${version} | |
| 24 | /// @since 1.0.2 | |
| 25 | public class StandardComment | |
| 26 | implements Comment { | |
| 27 | private final DocxPart part; | |
| 28 | private final Comments.Comment comment; | |
| 29 | private final CommentRangeStart commentRangeStart; | |
| 30 | private final CommentRangeEnd commentRangeEnd; | |
| 31 | private final @Nullable CommentReference commentReference; | |
| 32 | private final CTSmartTagRun startTagRun; | |
| 33 | ||
| 34 | /// Constructs a new `StandardComment` object. | |
| 35 | /// | |
| 36 | /// @param part the [DocxPart] representing the document section this comment belongs to. | |
| 37 | /// @param startTagRun the start tag run. | |
| 38 | /// @param commentRangeStart the comment range start. | |
| 39 | /// @param commentRangeEnd the comment range end. | |
| 40 | /// @param comment the comment. | |
| 41 | /// @param commentReference the comment reference. | |
| 42 | public StandardComment( | |
| 43 | DocxPart part, | |
| 44 | CTSmartTagRun startTagRun, | |
| 45 | CommentRangeStart commentRangeStart, | |
| 46 | CommentRangeEnd commentRangeEnd, | |
| 47 | Comments.Comment comment, | |
| 48 | @Nullable CommentReference commentReference | |
| 49 | ) { | |
| 50 | this.part = part; | |
| 51 | this.startTagRun = startTagRun; | |
| 52 | this.commentRangeStart = commentRangeStart; | |
| 53 | this.commentRangeEnd = commentRangeEnd; | |
| 54 | this.comment = comment; | |
| 55 | this.commentReference = commentReference; | |
| 56 | } | |
| 57 | ||
| 58 | /// Creates a new instance of [StandardComment] and initializes it with the given parameters, including a comment, | |
| 59 | /// comment range start, comment range end, and a comment reference. | |
| 60 | /// | |
| 61 | /// @param document the [DocxPart] representing the document to which this comment belongs | |
| 62 | /// @param parent the [ContentAccessor] representing the parent content of the comment range | |
| 63 | /// @param expression the [String] content to be included in the comment | |
| 64 | /// @param id the unique [BigInteger] identifier for the comment | |
| 65 | /// | |
| 66 | /// @return a [StandardComment] instance initialized with the specified parameters | |
| 67 | public static StandardComment create(DocxPart document, ContentAccessor parent, String expression, BigInteger id) { | |
| 68 | var start = newCommentRangeStart(id, parent); | |
| 69 |
1
1. create : replaced return value with null for pro/verron/officestamper/core/StandardComment::create → TIMED_OUT |
return new StandardComment(document, |
| 70 | newSmartTag("officestamper", newCtAttr("type", "processor"), start), | |
| 71 | start, | |
| 72 | newCommentRangeEnd(id, parent), | |
| 73 | newComment(id, expression), | |
| 74 | newCommentReference(id, parent)); | |
| 75 | } | |
| 76 | ||
| 77 | /// Generates a string representation of the [StandardComment] object, including its ID, content, and the amount | |
| 78 | /// children comment. | |
| 79 | /// | |
| 80 | /// @return a formatted string describing the [StandardComment]'s properties, including its ID, content, and the | |
| 81 | /// size of its children. | |
| 82 | @Override | |
| 83 | public String toString() { | |
| 84 |
1
1. toString : replaced return value with "" for pro/verron/officestamper/core/StandardComment::toString → NO_COVERAGE |
return "StandardComment{comment={id=%s, content=%s}}}".formatted(comment.getId(), |
| 85 | comment.getContent() | |
| 86 | .stream() | |
| 87 | .map(TextUtils::getText) | |
| 88 | .collect(joining(","))); | |
| 89 | } | |
| 90 | ||
| 91 | @Override | |
| 92 | public Paragraph getParagraph() { | |
| 93 | var parent = commentRangeStart.getParent(); | |
| 94 |
1
1. getParagraph : replaced return value with null for pro/verron/officestamper/core/StandardComment::getParagraph → NO_COVERAGE |
return StandardParagraph.from(part, parent); |
| 95 | } | |
| 96 | ||
| 97 | @Override | |
| 98 | public CTSmartTagRun getStartTagRun() { | |
| 99 |
1
1. getStartTagRun : replaced return value with null for pro/verron/officestamper/core/StandardComment::getStartTagRun → KILLED |
return startTagRun; |
| 100 | } | |
| 101 | ||
| 102 | @Override | |
| 103 | public CommentRangeStart getCommentRangeStart() { | |
| 104 |
1
1. getCommentRangeStart : replaced return value with null for pro/verron/officestamper/core/StandardComment::getCommentRangeStart → KILLED |
return commentRangeStart; |
| 105 | } | |
| 106 | ||
| 107 | @Override | |
| 108 | public ContentAccessor getParent() { | |
| 109 |
1
1. getParent : replaced return value with null for pro/verron/officestamper/core/StandardComment::getParent → KILLED |
return DocumentUtil.findSmallestCommonParent(commentRangeStart, commentRangeEnd); |
| 110 | } | |
| 111 | ||
| 112 | @Override | |
| 113 | public List<Object> getElements() { | |
| 114 | List<Object> elements = new ArrayList<>(); | |
| 115 | boolean startFound = false; | |
| 116 | boolean endFound = false; | |
| 117 | var siblings = getParent().getContent(); | |
| 118 | for (Object element : siblings) { | |
| 119 |
2
1. getElements : negated conditional → KILLED 2. getElements : negated conditional → KILLED |
startFound = startFound || DocumentUtil.depthElementSearch(commentRangeStart, element); |
| 120 |
2
1. getElements : negated conditional → KILLED 2. getElements : negated conditional → KILLED |
if (startFound && !endFound) elements.add(element); |
| 121 |
2
1. getElements : negated conditional → KILLED 2. getElements : negated conditional → KILLED |
endFound = endFound || DocumentUtil.depthElementSearch(commentRangeEnd, element); |
| 122 | } | |
| 123 |
1
1. getElements : replaced return value with Collections.emptyList for pro/verron/officestamper/core/StandardComment::getElements → KILLED |
return elements; |
| 124 | } | |
| 125 | ||
| 126 | @Override | |
| 127 | public CommentRangeEnd getCommentRangeEnd() { | |
| 128 |
1
1. getCommentRangeEnd : replaced return value with null for pro/verron/officestamper/core/StandardComment::getCommentRangeEnd → KILLED |
return commentRangeEnd; |
| 129 | } | |
| 130 | ||
| 131 | @Override | |
| 132 | public @Nullable CommentReference getCommentReference() { | |
| 133 |
1
1. getCommentReference : replaced return value with null for pro/verron/officestamper/core/StandardComment::getCommentReference → KILLED |
return commentReference; |
| 134 | } | |
| 135 | ||
| 136 | @Override | |
| 137 | public Comments.Comment getComment() { | |
| 138 |
1
1. getComment : replaced return value with null for pro/verron/officestamper/core/StandardComment::getComment → KILLED |
return comment; |
| 139 | } | |
| 140 | ||
| 141 | @Override | |
| 142 | public String expression() { | |
| 143 |
1
1. expression : replaced return value with "" for pro/verron/officestamper/core/StandardComment::expression → KILLED |
return this.getComment() |
| 144 | .getContent() | |
| 145 | .stream() | |
| 146 | .filter(P.class::isInstance) | |
| 147 | .map(P.class::cast) | |
| 148 |
1
1. lambda$expression$0 : replaced return value with null for pro/verron/officestamper/core/StandardComment::lambda$expression$0 → KILLED |
.map(p -> StandardParagraph.from(new TextualDocxPart(part.document()), p)) |
| 149 | .map(StandardParagraph::asString) | |
| 150 | .collect(joining()); | |
| 151 | } | |
| 152 | ||
| 153 | @Override | |
| 154 | public BigInteger getId() { | |
| 155 |
1
1. getId : replaced return value with null for pro/verron/officestamper/core/StandardComment::getId → TIMED_OUT |
return comment.getId(); |
| 156 | } | |
| 157 | } | |
Mutations | ||
| 69 |
1.1 |
|
| 84 |
1.1 |
|
| 94 |
1.1 |
|
| 99 |
1.1 |
|
| 104 |
1.1 |
|
| 109 |
1.1 |
|
| 119 |
1.1 2.2 |
|
| 120 |
1.1 2.2 |
|
| 121 |
1.1 2.2 |
|
| 123 |
1.1 |
|
| 128 |
1.1 |
|
| 133 |
1.1 |
|
| 138 |
1.1 |
|
| 143 |
1.1 |
|
| 148 |
1.1 |
|
| 155 |
1.1 |