1 | package pro.verron.officestamper.core; | |
2 | ||
3 | import org.docx4j.TextUtils; | |
4 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; | |
5 | import org.docx4j.wml.*; | |
6 | import org.docx4j.wml.R.CommentReference; | |
7 | import pro.verron.officestamper.api.Comment; | |
8 | import pro.verron.officestamper.api.Placeholder; | |
9 | ||
10 | import java.math.BigInteger; | |
11 | import java.util.ArrayList; | |
12 | import java.util.HashSet; | |
13 | import java.util.List; | |
14 | import java.util.Set; | |
15 | import java.util.stream.Collectors; | |
16 | ||
17 | import static java.util.stream.Collectors.joining; | |
18 | import static pro.verron.officestamper.utils.WmlFactory.*; | |
19 | ||
20 | /// CommentWrapper class. | |
21 | /// | |
22 | /// @author Joseph Verron | |
23 | /// @author Tom Hombergs | |
24 | /// @version ${version} | |
25 | /// @since 1.0.2 | |
26 | public class StandardComment | |
27 | implements Comment { | |
28 | private final Set<Comment> children = new HashSet<>(); | |
29 | private final WordprocessingMLPackage document; | |
30 | private Comments.Comment comment; | |
31 | private CommentRangeStart commentRangeStart; | |
32 | private CommentRangeEnd commentRangeEnd; | |
33 | private CommentReference commentReference; | |
34 | ||
35 | /// Constructs a new StandardComment object. | |
36 | /// | |
37 | /// @param document the WordprocessingMLPackage document instance | |
38 | public StandardComment(WordprocessingMLPackage document) { | |
39 | this.document = document; | |
40 | } | |
41 | ||
42 | /// Creates a new instance of a StandardComment and initializes its properties | |
43 | /// including the comment, comment range start, comment range end, and comment reference. | |
44 | /// | |
45 | /// @param document the WordprocessingMLPackage document where the comment will be created | |
46 | /// @param parent the parent element (P) to which the comment belongs | |
47 | /// @param placeholder the placeholder containing the content for the comment | |
48 | /// @param id the unique identifier for the comment | |
49 | /// @return a fully initialized StandardComment object | |
50 | public static StandardComment create( | |
51 | WordprocessingMLPackage document, | |
52 | P parent, | |
53 | Placeholder placeholder, | |
54 | BigInteger id | |
55 | ) { | |
56 | var commentWrapper = new StandardComment(document); | |
57 |
1
1. create : removed call to pro/verron/officestamper/core/StandardComment::setComment → SURVIVED |
commentWrapper.setComment(newComment(id, placeholder.content())); |
58 |
1
1. create : removed call to pro/verron/officestamper/core/StandardComment::setCommentRangeStart → KILLED |
commentWrapper.setCommentRangeStart(newCommentRangeStart(id, parent)); |
59 |
1
1. create : removed call to pro/verron/officestamper/core/StandardComment::setCommentRangeEnd → KILLED |
commentWrapper.setCommentRangeEnd(newCommentRangeEnd(id, parent)); |
60 |
1
1. create : removed call to pro/verron/officestamper/core/StandardComment::setCommentReference → SURVIVED |
commentWrapper.setCommentReference(newCommentReference(id, parent)); |
61 |
1
1. create : replaced return value with null for pro/verron/officestamper/core/StandardComment::create → KILLED |
return commentWrapper; |
62 | } | |
63 | ||
64 | /// Generates a string representation of the StandardComment object, including its ID, | |
65 | /// content, and the number of children comments. | |
66 | /// | |
67 | /// @return a formatted string describing the StandardComment's properties, | |
68 | /// including its ID, content, and the size of its children. | |
69 | @Override public String toString() { | |
70 |
1
1. toString : replaced return value with "" for pro/verron/officestamper/core/StandardComment::toString → NO_COVERAGE |
return "StandardComment{comment={id=%s, content=%s, children=%s}}}".formatted(comment.getId(), |
71 | comment.getContent() | |
72 | .stream() | |
73 | .map(TextUtils::getText) | |
74 | .collect(Collectors.joining(",")), | |
75 | children.size()); | |
76 | } | |
77 | ||
78 | /// Converts the current comment into a Placeholder representation. | |
79 | /// This method processes the content of the associated comment, extracts specific elements | |
80 | /// that are instances of the `P` class, transforms them into strings, and combines them | |
81 | /// to create a raw placeholder using `Placeholders.raw`. | |
82 | /// | |
83 | /// @return a [Placeholder] instance containing the combined string representation of the comment content | |
84 | @Override public Placeholder asPlaceholder() { | |
85 | String string = this.getComment() | |
86 | .getContent() | |
87 | .stream() | |
88 | .filter(P.class::isInstance) | |
89 | .map(P.class::cast) | |
90 |
1
1. lambda$asPlaceholder$0 : replaced return value with null for pro/verron/officestamper/core/StandardComment::lambda$asPlaceholder$0 → KILLED |
.map(p -> StandardParagraph.from(new TextualDocxPart(document), p)) |
91 | .map(StandardParagraph::asString) | |
92 | .collect(joining()); | |
93 |
1
1. asPlaceholder : replaced return value with null for pro/verron/officestamper/core/StandardComment::asPlaceholder → KILLED |
return Placeholders.raw(string); |
94 | } | |
95 | ||
96 | /// Returns the smallest common parent of the elements defined by the start | |
97 | /// and end of the comment range. | |
98 | /// | |
99 | /// @return the ContentAccessor representing the smallest common parent of | |
100 | /// the comment range start and end, or null if no common parent exists | |
101 | @Override public ContentAccessor getParent() { | |
102 |
1
1. getParent : replaced return value with null for pro/verron/officestamper/core/StandardComment::getParent → KILLED |
return DocumentUtil.findSmallestCommonParent(getCommentRangeStart(), getCommentRangeEnd()); |
103 | } | |
104 | ||
105 | /// Retrieves a list of elements that exist within the comment's range, | |
106 | /// bounded by the start and end of the comment range. | |
107 | /// The method iterates through the siblings of the comment's parent content, | |
108 | /// collecting elements starting from the range start to the range end. | |
109 | /// | |
110 | /// @return a list of elements between the comment range start and comment range end | |
111 | @Override public List<Object> getElements() { | |
112 | List<Object> elements = new ArrayList<>(); | |
113 | boolean startFound = false; | |
114 | boolean endFound = false; | |
115 | var siblings = getParent().getContent(); | |
116 | for (Object element : siblings) { | |
117 |
2
1. getElements : negated conditional → KILLED 2. getElements : negated conditional → KILLED |
startFound = startFound || DocumentUtil.depthElementSearch(getCommentRangeStart(), element); |
118 |
2
1. getElements : negated conditional → KILLED 2. getElements : negated conditional → KILLED |
if (startFound && !endFound) elements.add(element); |
119 |
2
1. getElements : negated conditional → KILLED 2. getElements : negated conditional → KILLED |
endFound = endFound || DocumentUtil.depthElementSearch(getCommentRangeEnd(), element); |
120 | } | |
121 |
1
1. getElements : replaced return value with Collections.emptyList for pro/verron/officestamper/core/StandardComment::getElements → KILLED |
return elements; |
122 | } | |
123 | ||
124 | /// Retrieves the CommentRangeEnd object associated with this comment. | |
125 | /// | |
126 | /// @return the CommentRangeEnd object representing the end of the comment range | |
127 | @Override 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 | /// Sets the comment range end for the current comment. | |
132 | /// | |
133 | /// @param commentRangeEnd the [CommentRangeEnd] object representing the end of the comment range | |
134 | public void setCommentRangeEnd(CommentRangeEnd commentRangeEnd) { | |
135 | this.commentRangeEnd = commentRangeEnd; | |
136 | } | |
137 | ||
138 | /// Getter for the field <code>commentRangeStart</code>. | |
139 | /// | |
140 | /// @return a [CommentRangeStart] object | |
141 | @Override public CommentRangeStart getCommentRangeStart() { | |
142 |
1
1. getCommentRangeStart : replaced return value with null for pro/verron/officestamper/core/StandardComment::getCommentRangeStart → KILLED |
return commentRangeStart; |
143 | } | |
144 | ||
145 | /// Sets the starting point of the comment range for the current comment. | |
146 | /// | |
147 | /// @param commentRangeStart the [CommentRangeStart] object representing the beginning of the comment range | |
148 | public void setCommentRangeStart(CommentRangeStart commentRangeStart) { | |
149 | this.commentRangeStart = commentRangeStart; | |
150 | } | |
151 | ||
152 | /// Retrieves the comment reference associated with this comment. | |
153 | /// | |
154 | /// @return the CommentReference object linked to this comment | |
155 | @Override public CommentReference getCommentReference() { | |
156 |
1
1. getCommentReference : replaced return value with null for pro/verron/officestamper/core/StandardComment::getCommentReference → KILLED |
return commentReference; |
157 | } | |
158 | ||
159 | /// Sets the comment reference for the current comment. | |
160 | /// | |
161 | /// @param commentReference the [CommentReference] object to associate with this comment | |
162 | public void setCommentReference(CommentReference commentReference) { | |
163 | this.commentReference = commentReference; | |
164 | } | |
165 | ||
166 | /// Retrieves the set of child comments associated with this comment. | |
167 | /// | |
168 | /// @return a set containing the child comments of the current comment | |
169 | @Override public Set<Comment> getChildren() { | |
170 |
1
1. getChildren : replaced return value with Collections.emptySet for pro/verron/officestamper/core/StandardComment::getChildren → KILLED |
return children; |
171 | } | |
172 | ||
173 | /// Sets the children of the comment by adding all elements from the provided set | |
174 | /// to the existing children set. | |
175 | /// | |
176 | /// @param children the set of [Comment] objects to be added as children | |
177 | public void setChildren(Set<Comment> children) { | |
178 | this.children.addAll(children); | |
179 | } | |
180 | ||
181 | /// Retrieves the comment associated with this StandardComment. | |
182 | /// | |
183 | /// @return the Comments.Comment object representing the associated comment | |
184 | @Override public Comments.Comment getComment() { | |
185 |
1
1. getComment : replaced return value with null for pro/verron/officestamper/core/StandardComment::getComment → KILLED |
return comment; |
186 | } | |
187 | ||
188 | /// Sets the comment for the current StandardComment. | |
189 | /// | |
190 | /// @param comment the [Comments.Comment] object to associate with this StandardComment | |
191 | public void setComment(Comments.Comment comment) { | |
192 | this.comment = comment; | |
193 | } | |
194 | ||
195 | /// Retrieves the WordprocessingMLPackage document associated with this StandardComment instance. | |
196 | /// | |
197 | /// @return the WordprocessingMLPackage document associated with this StandardComment instance | |
198 | @Override public WordprocessingMLPackage getDocument() { | |
199 |
1
1. getDocument : replaced return value with null for pro/verron/officestamper/core/StandardComment::getDocument → KILLED |
return document; |
200 | } | |
201 | } | |
Mutations | ||
57 |
1.1 |
|
58 |
1.1 |
|
59 |
1.1 |
|
60 |
1.1 |
|
61 |
1.1 |
|
70 |
1.1 |
|
90 |
1.1 |
|
93 |
1.1 |
|
102 |
1.1 |
|
117 |
1.1 2.2 |
|
118 |
1.1 2.2 |
|
119 |
1.1 2.2 |
|
121 |
1.1 |
|
128 |
1.1 |
|
142 |
1.1 |
|
156 |
1.1 |
|
170 |
1.1 |
|
185 |
1.1 |
|
199 |
1.1 |