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 | public <T> void runProcessors(T expressionContext) { | |
59 | var proceedComments = new ArrayList<Comment>(); | |
60 | ||
61 | source.streamRun() | |
62 |
1
1. runProcessors : removed call to java/util/stream/Stream::forEach → KILLED |
.forEach(run -> { |
63 | var comments = collectComments(); | |
64 | var runParent = StandardParagraph.from(source, (P) run.getParent()); | |
65 | var optional = runProcessorsOnRunComment(comments, expressionContext, run, runParent); | |
66 |
1
1. lambda$runProcessors$0 : removed call to pro/verron/officestamper/core/CommentProcessors::commitChanges → SURVIVED |
commentProcessors.commitChanges(source); |
67 |
1
1. lambda$runProcessors$0 : removed call to java/util/Optional::ifPresent → SURVIVED |
optional.ifPresent(proceedComments::add); |
68 | }); | |
69 | ||
70 | // we run the paragraph afterward so that the comments inside work before the whole paragraph comments | |
71 | source.streamParagraphs() | |
72 |
1
1. runProcessors : removed call to java/util/stream/Stream::forEach → KILLED |
.forEach(p -> { |
73 | var comments = collectComments(); | |
74 | var paragraphComment = p.getComment(); | |
75 |
1
1. lambda$runProcessors$2 : removed call to java/util/Optional::ifPresent → KILLED |
paragraphComment.ifPresent((pc -> { |
76 | var optional = runProcessorsOnParagraphComment(comments, expressionContext, p, pc.getId()); | |
77 |
1
1. lambda$runProcessors$1 : removed call to pro/verron/officestamper/core/CommentProcessors::commitChanges → KILLED |
commentProcessors.commitChanges(source); |
78 |
1
1. lambda$runProcessors$1 : removed call to java/util/Optional::ifPresent → KILLED |
optional.ifPresent(proceedComments::add); |
79 | })); | |
80 | }); | |
81 | ||
82 | source.streamParagraphs() | |
83 |
2
1. runProcessors : removed call to java/util/stream/Stream::forEach → KILLED 2. lambda$runProcessors$3 : removed call to pro/verron/officestamper/core/CommentProcessorRegistry::runProcessorsOnInlineContent → KILLED |
.forEach(paragraph -> runProcessorsOnInlineContent(expressionContext, paragraph)); |
84 | ||
85 |
1
1. runProcessors : removed call to java/util/ArrayList::forEach → KILLED |
proceedComments.forEach(CommentUtil::deleteComment); |
86 | } | |
87 | ||
88 | private Map<BigInteger, Comment> collectComments() { | |
89 | var rootComments = new HashMap<BigInteger, Comment>(); | |
90 | var allComments = new HashMap<BigInteger, Comment>(); | |
91 | var stack = Collections.asLifoQueue(new ArrayDeque<Comment>()); | |
92 | ||
93 | var list = WmlUtils.extractCommentElements(document()); | |
94 | for (Child commentElement : list) { | |
95 |
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); |
96 |
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); |
97 |
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); |
98 | } | |
99 | CommentUtil.getCommentsPart(document().getParts()) | |
100 | .map(CommentUtil::extractContent) | |
101 | .map(Comments::getComment) | |
102 | .stream() | |
103 | .flatMap(Collection::stream) | |
104 |
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())) |
105 |
1
1. collectComments : removed call to java/util/stream/Stream::forEach → KILLED |
.forEach(comment -> allComments.get(comment.getId()) |
106 |
1
1. lambda$collectComments$5 : removed call to pro/verron/officestamper/api/Comment::setComment → KILLED |
.setComment(comment)); |
107 |
1
1. collectComments : replaced return value with Collections.emptyMap for pro/verron/officestamper/core/CommentProcessorRegistry::collectComments → KILLED |
return new HashMap<>(rootComments); |
108 | } | |
109 | ||
110 | private <T> Optional<Comment> runProcessorsOnRunComment( | |
111 | Map<BigInteger, Comment> comments, T expressionContext, R run, Paragraph paragraph | |
112 | ) { | |
113 |
1
1. runProcessorsOnRunComment : replaced return value with Optional.empty for pro/verron/officestamper/core/CommentProcessorRegistry::runProcessorsOnRunComment → SURVIVED |
return CommentUtil.getCommentAround(run, document()) |
114 |
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()))) |
115 | .flatMap(c -> { | |
116 | var cPlaceholder = c.asPlaceholder(); | |
117 | var cComment = c.getComment(); | |
118 | comments.remove(cComment.getId()); | |
119 |
1
1. lambda$runProcessorsOnRunComment$7 : removed call to pro/verron/officestamper/core/CommentProcessors::setContext → KILLED |
commentProcessors.setContext(new ProcessorContext(paragraph, run, c, cPlaceholder)); |
120 |
1
1. lambda$runProcessorsOnRunComment$7 : negated conditional → KILLED |
return runCommentProcessors(expressionContext, cPlaceholder) |
121 | ? Optional.of(c) | |
122 | : Optional.empty(); | |
123 | ||
124 | }); | |
125 | } | |
126 | ||
127 | /** | |
128 | * Takes the first comment on the specified paragraph and tries to evaluate | |
129 | * the string within the comment against all registered | |
130 | * {@link CommentProcessor}s. | |
131 | * | |
132 | * @param comments the comments within the document. | |
133 | * @param expressionContext the context root object | |
134 | * @param <T> the type of the context root object. | |
135 | */ | |
136 | private <T> Optional<Comment> runProcessorsOnParagraphComment( | |
137 | Map<BigInteger, Comment> comments, T expressionContext, Paragraph paragraph, BigInteger paragraphCommentId | |
138 | ) { | |
139 |
1
1. runProcessorsOnParagraphComment : negated conditional → KILLED |
if (!comments.containsKey(paragraphCommentId)) return Optional.empty(); |
140 | ||
141 | var c = comments.get(paragraphCommentId); | |
142 | var cPlaceholder = c.asPlaceholder(); | |
143 | var cComment = c.getComment(); | |
144 | comments.remove(cComment.getId()); | |
145 |
1
1. runProcessorsOnParagraphComment : removed call to pro/verron/officestamper/core/CommentProcessors::setContext → KILLED |
commentProcessors.setContext(new ProcessorContext(paragraph, null, c, cPlaceholder)); |
146 |
1
1. runProcessorsOnParagraphComment : negated conditional → KILLED |
return runCommentProcessors(expressionContext, c.asPlaceholder()) ? Optional.of(c) : Optional.empty(); |
147 | } | |
148 | ||
149 | /** | |
150 | * Finds all processor expressions within the specified paragraph and tries | |
151 | * to evaluate it against all registered {@link CommentProcessor}s. | |
152 | * | |
153 | * @param context the context root object against which evaluation is done | |
154 | * @param paragraph the paragraph to process. | |
155 | * @param <T> type of the context root object | |
156 | */ | |
157 | private <T> void runProcessorsOnInlineContent(T context, Paragraph paragraph) { | |
158 | var processorContexts = findProcessors(paragraph.asString()).stream() | |
159 | .map(paragraph::processorContext) | |
160 | .toList(); | |
161 | for (var processorContext : processorContexts) { | |
162 |
1
1. runProcessorsOnInlineContent : removed call to pro/verron/officestamper/core/CommentProcessors::setContext → KILLED |
commentProcessors.setContext(processorContext); |
163 | var placeholder = processorContext.placeholder(); | |
164 | try { | |
165 |
1
1. runProcessorsOnInlineContent : removed call to pro/verron/officestamper/core/ExpressionResolver::setContext → KILLED |
expressionResolver.setContext(context); |
166 | expressionResolver.resolve(placeholder); | |
167 |
1
1. runProcessorsOnInlineContent : removed call to pro/verron/officestamper/api/Paragraph::replace → KILLED |
paragraph.replace(placeholder, WmlFactory.newRun("")); |
168 | logger.debug("Placeholder '{}' successfully processed by a comment processor.", placeholder); | |
169 | } catch (SpelEvaluationException | SpelParseException e) { | |
170 | var message = "Placeholder '%s' failed to process.".formatted(placeholder); | |
171 | exceptionResolver.resolve(placeholder, message, e); | |
172 | } | |
173 |
1
1. runProcessorsOnInlineContent : removed call to pro/verron/officestamper/core/CommentProcessors::commitChanges → KILLED |
commentProcessors.commitChanges(source); |
174 | } | |
175 | } | |
176 | ||
177 | private WordprocessingMLPackage document() { | |
178 |
1
1. document : replaced return value with null for pro/verron/officestamper/core/CommentProcessorRegistry::document → KILLED |
return source.document(); |
179 | } | |
180 | ||
181 | private void onRangeStart( | |
182 | CommentRangeStart crs, | |
183 | HashMap<BigInteger, Comment> allComments, | |
184 | Queue<Comment> stack, | |
185 | HashMap<BigInteger, Comment> rootComments | |
186 | ) { | |
187 | Comment comment = allComments.get(crs.getId()); | |
188 |
1
1. onRangeStart : negated conditional → KILLED |
if (comment == null) { |
189 | comment = new StandardComment(document()); | |
190 | allComments.put(crs.getId(), comment); | |
191 |
1
1. onRangeStart : negated conditional → KILLED |
if (stack.isEmpty()) { |
192 | rootComments.put(crs.getId(), comment); | |
193 | } | |
194 | else { | |
195 | stack.peek() | |
196 | .getChildren() | |
197 | .add(comment); | |
198 | } | |
199 | } | |
200 |
1
1. onRangeStart : removed call to pro/verron/officestamper/api/Comment::setCommentRangeStart → KILLED |
comment.setCommentRangeStart(crs); |
201 | stack.add(comment); | |
202 | } | |
203 | ||
204 | private void onRangeEnd( | |
205 | CommentRangeEnd cre, HashMap<BigInteger, Comment> allComments, Queue<Comment> stack | |
206 | ) { | |
207 | Comment comment = allComments.get(cre.getId()); | |
208 |
1
1. onRangeEnd : negated conditional → KILLED |
if (comment == null) |
209 | throw new OfficeStamperException("Found a comment range end before the comment range start !"); | |
210 | ||
211 |
1
1. onRangeEnd : removed call to pro/verron/officestamper/api/Comment::setCommentRangeEnd → KILLED |
comment.setCommentRangeEnd(cre); |
212 | ||
213 |
1
1. onRangeEnd : negated conditional → KILLED |
if (!stack.isEmpty()) { |
214 | var peek = stack.peek(); | |
215 |
1
1. onRangeEnd : negated conditional → KILLED |
if (peek.equals(comment)) stack.remove(); |
216 | else throw new OfficeStamperException("Cannot figure which comment contains the other !"); | |
217 | } | |
218 | } | |
219 | ||
220 | private void onReference(R.CommentReference cr, HashMap<BigInteger, Comment> allComments) { | |
221 | Comment comment = allComments.get(cr.getId()); | |
222 |
1
1. onReference : negated conditional → KILLED |
if (comment == null) { |
223 | comment = new StandardComment(document()); | |
224 | allComments.put(cr.getId(), comment); | |
225 | } | |
226 |
1
1. onReference : removed call to pro/verron/officestamper/api/Comment::setCommentReference → KILLED |
comment.setCommentReference(cr); |
227 | } | |
228 | ||
229 | private <T> boolean runCommentProcessors(T context, Placeholder commentPlaceholder) { | |
230 | try { | |
231 |
1
1. runCommentProcessors : removed call to pro/verron/officestamper/core/ExpressionResolver::setContext → KILLED |
expressionResolver.setContext(context); |
232 | expressionResolver.resolve(commentPlaceholder); | |
233 | logger.debug("Comment '{}' successfully processed by a comment processor.", commentPlaceholder); | |
234 |
1
1. runCommentProcessors : replaced boolean return with false for pro/verron/officestamper/core/CommentProcessorRegistry::runCommentProcessors → KILLED |
return true; |
235 | } catch (SpelEvaluationException | SpelParseException e) { | |
236 | var message = "Comment '%s' failed to process.".formatted(commentPlaceholder.expression()); | |
237 | exceptionResolver.resolve(commentPlaceholder, message, e); | |
238 |
1
1. runCommentProcessors : replaced boolean return with true for pro/verron/officestamper/core/CommentProcessorRegistry::runCommentProcessors → KILLED |
return false; |
239 | } | |
240 | } | |
241 | ||
242 | } | |
Mutations | ||
62 |
1.1 |
|
66 |
1.1 |
|
67 |
1.1 |
|
72 |
1.1 |
|
75 |
1.1 |
|
77 |
1.1 |
|
78 |
1.1 |
|
83 |
1.1 2.2 |
|
85 |
1.1 |
|
95 |
1.1 2.2 |
|
96 |
1.1 2.2 |
|
97 |
1.1 2.2 |
|
104 |
1.1 2.2 |
|
105 |
1.1 |
|
106 |
1.1 |
|
107 |
1.1 |
|
113 |
1.1 |
|
114 |
1.1 |
|
119 |
1.1 |
|
120 |
1.1 |
|
139 |
1.1 |
|
145 |
1.1 |
|
146 |
1.1 |
|
162 |
1.1 |
|
165 |
1.1 |
|
167 |
1.1 |
|
173 |
1.1 |
|
178 |
1.1 |
|
188 |
1.1 |
|
191 |
1.1 |
|
200 |
1.1 |
|
208 |
1.1 |
|
211 |
1.1 |
|
213 |
1.1 |
|
215 |
1.1 |
|
222 |
1.1 |
|
226 |
1.1 |
|
231 |
1.1 |
|
234 |
1.1 |
|
238 |
1.1 |