1 | package pro.verron.officestamper.core; | |
2 | ||
3 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; | |
4 | import org.docx4j.wml.*; | |
5 | import org.jvnet.jaxb2_commons.ppp.Child; | |
6 | import org.slf4j.Logger; | |
7 | import org.slf4j.LoggerFactory; | |
8 | import org.springframework.expression.spel.SpelEvaluationException; | |
9 | import org.springframework.expression.spel.SpelParseException; | |
10 | import pro.verron.officestamper.api.*; | |
11 | import pro.verron.officestamper.utils.WmlFactory; | |
12 | import pro.verron.officestamper.utils.WmlUtils; | |
13 | ||
14 | import java.math.BigInteger; | |
15 | import java.util.*; | |
16 | ||
17 | import static pro.verron.officestamper.core.Placeholders.findProcessors; | |
18 | ||
19 | /// Allows registration of [CommentProcessor] objects. Each registered | |
20 | /// ICommentProcessor must implement an interface which has to be specified at | |
21 | /// registration time. Provides several getter methods to access the registered | |
22 | /// [CommentProcessor]. | |
23 | /// | |
24 | /// @author Joseph Verron | |
25 | /// @author Tom Hombergs | |
26 | /// @version ${version} | |
27 | /// @since 1.0.0 | |
28 | public class CommentProcessorRegistry { | |
29 | ||
30 | private static final Logger logger = LoggerFactory.getLogger(CommentProcessorRegistry.class); | |
31 | private final DocxPart source; | |
32 | private final CommentProcessors commentProcessors; | |
33 | private final ExpressionResolver expressionResolver; | |
34 | private final ExceptionResolver exceptionResolver; | |
35 | ||
36 | /// Constructs a new CommentProcessorRegistry. | |
37 | /// | |
38 | /// @param source the source part of the Word document. | |
39 | /// @param expressionResolver the resolver for evaluating expressions. | |
40 | /// @param commentProcessors map of comment processor instances keyed by their respective class types. | |
41 | /// @param exceptionResolver the resolver for handling exceptions during processing. | |
42 | public CommentProcessorRegistry( | |
43 | DocxPart source, | |
44 | ExpressionResolver expressionResolver, | |
45 | CommentProcessors commentProcessors, | |
46 | ExceptionResolver exceptionResolver | |
47 | ) { | |
48 | this.source = source; | |
49 | this.expressionResolver = expressionResolver; | |
50 | this.commentProcessors = commentProcessors; | |
51 | this.exceptionResolver = exceptionResolver; | |
52 | } | |
53 | ||
54 | /// Processes comments and inline content in the document by evaluating them against all registered | |
55 | /// [CommentProcessor]s. This method processes runs, paragraphs, and inline content, and applies | |
56 | /// changes based on the evaluation results. | |
57 | /// | |
58 | /// @param <T> the type of the context root object | |
59 | /// @param expressionContext the context root object against which expressions within comments are evaluated | |
60 | public <T> void runProcessors(T expressionContext) { | |
61 | var proceedComments = new ArrayList<Comment>(); | |
62 | ||
63 | source.streamRun() | |
64 |
1
1. runProcessors : removed call to java/util/stream/Stream::forEach → KILLED |
.forEach(run -> { |
65 | var comments = collectComments(); | |
66 | var runParent = StandardParagraph.from(source, (P) run.getParent()); | |
67 | var optional = runProcessorsOnRunComment(comments, expressionContext, run, runParent); | |
68 |
1
1. lambda$runProcessors$0 : removed call to java/util/Optional::ifPresent → KILLED |
optional.ifPresent(proceedComments::add); |
69 | }); | |
70 |
1
1. runProcessors : removed call to pro/verron/officestamper/core/CommentProcessors::commitChanges → KILLED |
commentProcessors.commitChanges(source); |
71 | ||
72 | // we run the paragraph afterward so that the comments inside work before the whole paragraph comments | |
73 | source.streamParagraphs() | |
74 |
1
1. runProcessors : removed call to java/util/stream/Stream::forEach → KILLED |
.forEach(p -> { |
75 | var comments = collectComments(); | |
76 | var paragraphComment = p.getComment(); | |
77 |
1
1. lambda$runProcessors$1 : removed call to java/util/Collection::forEach → KILLED |
paragraphComment.forEach((pc -> { |
78 | var optional = runProcessorsOnParagraphComment(comments, expressionContext, p, pc.getId()); | |
79 |
1
1. lambda$runProcessors$2 : removed call to pro/verron/officestamper/core/CommentProcessors::commitChanges → KILLED |
commentProcessors.commitChanges(source); |
80 |
1
1. lambda$runProcessors$2 : removed call to java/util/Optional::ifPresent → KILLED |
optional.ifPresent(proceedComments::add); |
81 | })); | |
82 | }); | |
83 | ||
84 | source.streamParagraphs() | |
85 |
2
1. lambda$runProcessors$3 : removed call to pro/verron/officestamper/core/CommentProcessorRegistry::runProcessorsOnInlineContent → KILLED 2. runProcessors : removed call to java/util/stream/Stream::forEach → KILLED |
.forEach(paragraph -> runProcessorsOnInlineContent(expressionContext, paragraph)); |
86 | ||
87 |
1
1. runProcessors : removed call to java/util/ArrayList::forEach → KILLED |
proceedComments.forEach(CommentUtil::deleteComment); |
88 | } | |
89 | ||
90 | private Map<BigInteger, Comment> collectComments() { | |
91 | var rootComments = new HashMap<BigInteger, Comment>(); | |
92 | var allComments = new HashMap<BigInteger, Comment>(); | |
93 | var stack = Collections.asLifoQueue(new ArrayDeque<Comment>()); | |
94 | ||
95 | var list = WmlUtils.extractCommentElements(document()); | |
96 | for (Child commentElement : list) { | |
97 |
2
1. collectComments : removed call to pro/verron/officestamper/core/CommentProcessorRegistry::onRangeStart → KILLED 2. collectComments : negated conditional → KILLED |
if (commentElement instanceof CommentRangeStart crs) onRangeStart(crs, allComments, stack, rootComments); |
98 |
2
1. collectComments : removed call to pro/verron/officestamper/core/CommentProcessorRegistry::onRangeEnd → KILLED 2. collectComments : negated conditional → KILLED |
else if (commentElement instanceof CommentRangeEnd cre) onRangeEnd(cre, allComments, stack); |
99 |
2
1. collectComments : removed call to pro/verron/officestamper/core/CommentProcessorRegistry::onReference → KILLED 2. collectComments : negated conditional → KILLED |
else if (commentElement instanceof R.CommentReference cr) onReference(cr, allComments); |
100 | } | |
101 | CommentUtil.getCommentsPart(document().getParts()) | |
102 | .map(CommentUtil::extractContent) | |
103 | .map(Comments::getComment) | |
104 | .stream() | |
105 | .flatMap(Collection::stream) | |
106 |
2
1. lambda$collectComments$0 : replaced boolean return with true for pro/verron/officestamper/core/CommentProcessorRegistry::lambda$collectComments$0 → KILLED 2. lambda$collectComments$0 : replaced boolean return with false for pro/verron/officestamper/core/CommentProcessorRegistry::lambda$collectComments$0 → KILLED |
.filter(comment -> allComments.containsKey(comment.getId())) |
107 |
1
1. collectComments : removed call to java/util/stream/Stream::forEach → KILLED |
.forEach(comment -> allComments.get(comment.getId()) |
108 |
1
1. lambda$collectComments$1 : removed call to pro/verron/officestamper/api/Comment::setComment → KILLED |
.setComment(comment)); |
109 |
1
1. collectComments : replaced return value with Collections.emptyMap for pro/verron/officestamper/core/CommentProcessorRegistry::collectComments → KILLED |
return new HashMap<>(rootComments); |
110 | } | |
111 | ||
112 | private <T> Optional<Comment> runProcessorsOnRunComment( | |
113 | Map<BigInteger, Comment> comments, | |
114 | T expressionContext, | |
115 | R run, | |
116 | Paragraph paragraph | |
117 | ) { | |
118 |
1
1. runProcessorsOnRunComment : replaced return value with Optional.empty for pro/verron/officestamper/core/CommentProcessorRegistry::runProcessorsOnRunComment → KILLED |
return CommentUtil.getCommentAround(run, document()) |
119 |
1
1. lambda$runProcessorsOnRunComment$0 : replaced return value with Optional.empty for pro/verron/officestamper/core/CommentProcessorRegistry::lambda$runProcessorsOnRunComment$0 → SURVIVED |
.flatMap(c -> Optional.ofNullable(comments.get(c.getId()))) |
120 | .flatMap(c -> { | |
121 | var cPlaceholder = c.asPlaceholder(); | |
122 | var cComment = c.getComment(); | |
123 | comments.remove(cComment.getId()); | |
124 |
1
1. lambda$runProcessorsOnRunComment$1 : removed call to pro/verron/officestamper/core/CommentProcessors::setContext → KILLED |
commentProcessors.setContext(new ProcessorContext(paragraph, run, c, cPlaceholder)); |
125 |
1
1. lambda$runProcessorsOnRunComment$1 : negated conditional → KILLED |
return runCommentProcessors(expressionContext, cPlaceholder) |
126 | ? Optional.of(c) | |
127 | : Optional.empty(); | |
128 | }); | |
129 | } | |
130 | ||
131 | private <T> Optional<Comment> runProcessorsOnParagraphComment( | |
132 | Map<BigInteger, Comment> comments, | |
133 | T expressionContext, | |
134 | Paragraph paragraph, | |
135 | BigInteger paragraphCommentId | |
136 | ) { | |
137 |
1
1. runProcessorsOnParagraphComment : negated conditional → KILLED |
if (!comments.containsKey(paragraphCommentId)) return Optional.empty(); |
138 | ||
139 | var c = comments.get(paragraphCommentId); | |
140 | var cPlaceholder = c.asPlaceholder(); | |
141 | var cComment = c.getComment(); | |
142 | comments.remove(cComment.getId()); | |
143 |
1
1. runProcessorsOnParagraphComment : removed call to pro/verron/officestamper/core/CommentProcessors::setContext → KILLED |
commentProcessors.setContext(new ProcessorContext(paragraph, null, c, cPlaceholder)); |
144 |
1
1. runProcessorsOnParagraphComment : negated conditional → KILLED |
return runCommentProcessors(expressionContext, c.asPlaceholder()) ? Optional.of(c) : Optional.empty(); |
145 | } | |
146 | ||
147 | private <T> void runProcessorsOnInlineContent(T context, Paragraph paragraph) { | |
148 | var processorContexts = findProcessors(paragraph.asString()).stream() | |
149 | .map(paragraph::processorContext) | |
150 | .toList(); | |
151 | for (var processorContext : processorContexts) { | |
152 |
1
1. runProcessorsOnInlineContent : removed call to pro/verron/officestamper/core/CommentProcessors::setContext → KILLED |
commentProcessors.setContext(processorContext); |
153 | var placeholder = processorContext.placeholder(); | |
154 | try { | |
155 |
1
1. runProcessorsOnInlineContent : removed call to pro/verron/officestamper/core/ExpressionResolver::setContext → KILLED |
expressionResolver.setContext(context); |
156 | expressionResolver.resolve(placeholder); | |
157 |
1
1. runProcessorsOnInlineContent : removed call to pro/verron/officestamper/api/Paragraph::replace → KILLED |
paragraph.replace(placeholder, WmlFactory.newRun("")); |
158 | logger.debug("Placeholder '{}' successfully processed by a comment processor.", placeholder); | |
159 | } catch (SpelEvaluationException | SpelParseException e) { | |
160 | var message = "Placeholder '%s' failed to process.".formatted(placeholder); | |
161 | exceptionResolver.resolve(placeholder, message, e); | |
162 | } | |
163 |
1
1. runProcessorsOnInlineContent : removed call to pro/verron/officestamper/core/CommentProcessors::commitChanges → KILLED |
commentProcessors.commitChanges(source); |
164 | } | |
165 | } | |
166 | ||
167 | private WordprocessingMLPackage document() { | |
168 |
1
1. document : replaced return value with null for pro/verron/officestamper/core/CommentProcessorRegistry::document → KILLED |
return source.document(); |
169 | } | |
170 | ||
171 | private void onRangeStart( | |
172 | CommentRangeStart crs, | |
173 | HashMap<BigInteger, Comment> allComments, | |
174 | Queue<Comment> stack, | |
175 | HashMap<BigInteger, Comment> rootComments | |
176 | ) { | |
177 | Comment comment = allComments.get(crs.getId()); | |
178 |
1
1. onRangeStart : negated conditional → KILLED |
if (comment == null) { |
179 | comment = new StandardComment(document()); | |
180 | allComments.put(crs.getId(), comment); | |
181 |
1
1. onRangeStart : negated conditional → KILLED |
if (stack.isEmpty()) { |
182 | rootComments.put(crs.getId(), comment); | |
183 | } | |
184 | else { | |
185 | stack.peek() | |
186 | .getChildren() | |
187 | .add(comment); | |
188 | } | |
189 | } | |
190 |
1
1. onRangeStart : removed call to pro/verron/officestamper/api/Comment::setCommentRangeStart → KILLED |
comment.setCommentRangeStart(crs); |
191 | stack.add(comment); | |
192 | } | |
193 | ||
194 | private void onRangeEnd(CommentRangeEnd cre, HashMap<BigInteger, Comment> allComments, Queue<Comment> stack) { | |
195 | Comment comment = allComments.get(cre.getId()); | |
196 |
1
1. onRangeEnd : negated conditional → KILLED |
if (comment == null) |
197 | throw new OfficeStamperException("Found a comment range end before the comment range start !"); | |
198 | ||
199 |
1
1. onRangeEnd : removed call to pro/verron/officestamper/api/Comment::setCommentRangeEnd → KILLED |
comment.setCommentRangeEnd(cre); |
200 | ||
201 |
1
1. onRangeEnd : negated conditional → KILLED |
if (!stack.isEmpty()) { |
202 | var peek = stack.peek(); | |
203 |
1
1. onRangeEnd : negated conditional → KILLED |
if (peek.equals(comment)) stack.remove(); |
204 | else throw new OfficeStamperException("Cannot figure which comment contains the other !"); | |
205 | } | |
206 | } | |
207 | ||
208 | private void onReference(R.CommentReference cr, HashMap<BigInteger, Comment> allComments) { | |
209 | Comment comment = allComments.get(cr.getId()); | |
210 |
1
1. onReference : negated conditional → KILLED |
if (comment == null) { |
211 | comment = new StandardComment(document()); | |
212 | allComments.put(cr.getId(), comment); | |
213 | } | |
214 |
1
1. onReference : removed call to pro/verron/officestamper/api/Comment::setCommentReference → KILLED |
comment.setCommentReference(cr); |
215 | } | |
216 | ||
217 | private <T> boolean runCommentProcessors(T context, Placeholder commentPlaceholder) { | |
218 | try { | |
219 |
1
1. runCommentProcessors : removed call to pro/verron/officestamper/core/ExpressionResolver::setContext → KILLED |
expressionResolver.setContext(context); |
220 | expressionResolver.resolve(commentPlaceholder); | |
221 | logger.debug("Comment '{}' successfully processed by a comment processor.", commentPlaceholder); | |
222 |
1
1. runCommentProcessors : replaced boolean return with false for pro/verron/officestamper/core/CommentProcessorRegistry::runCommentProcessors → KILLED |
return true; |
223 | } catch (SpelEvaluationException | SpelParseException e) { | |
224 | var message = "Comment '%s' failed to process.".formatted(commentPlaceholder.expression()); | |
225 | exceptionResolver.resolve(commentPlaceholder, message, e); | |
226 |
1
1. runCommentProcessors : replaced boolean return with true for pro/verron/officestamper/core/CommentProcessorRegistry::runCommentProcessors → KILLED |
return false; |
227 | } | |
228 | } | |
229 | } | |
Mutations | ||
64 |
1.1 |
|
68 |
1.1 |
|
70 |
1.1 |
|
74 |
1.1 |
|
77 |
1.1 |
|
79 |
1.1 |
|
80 |
1.1 |
|
85 |
1.1 2.2 |
|
87 |
1.1 |
|
97 |
1.1 2.2 |
|
98 |
1.1 2.2 |
|
99 |
1.1 2.2 |
|
106 |
1.1 2.2 |
|
107 |
1.1 |
|
108 |
1.1 |
|
109 |
1.1 |
|
118 |
1.1 |
|
119 |
1.1 |
|
124 |
1.1 |
|
125 |
1.1 |
|
137 |
1.1 |
|
143 |
1.1 |
|
144 |
1.1 |
|
152 |
1.1 |
|
155 |
1.1 |
|
157 |
1.1 |
|
163 |
1.1 |
|
168 |
1.1 |
|
178 |
1.1 |
|
181 |
1.1 |
|
190 |
1.1 |
|
196 |
1.1 |
|
199 |
1.1 |
|
201 |
1.1 |
|
203 |
1.1 |
|
210 |
1.1 |
|
214 |
1.1 |
|
219 |
1.1 |
|
222 |
1.1 |
|
226 |
1.1 |