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