| 1 | package pro.verron.officestamper.core; | |
| 2 | ||
| 3 | import org.docx4j.wml.CTSmartTagRun; | |
| 4 | import org.docx4j.wml.CommentRangeStart; | |
| 5 | import org.docx4j.wml.ContentAccessor; | |
| 6 | import pro.verron.officestamper.api.DocxPart; | |
| 7 | import pro.verron.officestamper.api.Hook; | |
| 8 | import pro.verron.officestamper.utils.iterator.ResetableIterator; | |
| 9 | import pro.verron.officestamper.utils.wml.DocxIterator; | |
| 10 | ||
| 11 | import static pro.verron.officestamper.utils.wml.WmlUtils.isTagElement; | |
| 12 | ||
| 13 | /// Interface for hooks that process specific parts of a DOCX document. | |
| 14 | public interface DocxHook | |
| 15 | extends Hook { | |
| 16 | ||
| 17 | /// Creates an iterator over the hooks in the given content accessor. | |
| 18 | /// | |
| 19 | /// @param contentAccessor the content accessor to search for hooks. | |
| 20 | /// @param part the document part. | |
| 21 | /// | |
| 22 | /// @return an iterator over the found hooks. | |
| 23 | static ResetableIterator<DocxHook> ofHooks(ContentAccessor contentAccessor, DocxPart part) { | |
| 24 |
1
1. ofHooks : replaced return value with null for pro/verron/officestamper/core/DocxHook::ofHooks → KILLED |
return new DocxIterator(contentAccessor).filter(DocxHook::isPotentialHook) |
| 25 |
1
1. lambda$ofHooks$0 : replaced return value with null for pro/verron/officestamper/core/DocxHook::lambda$ofHooks$0 → KILLED |
.map(o -> asHook(part, o)); |
| 26 | } | |
| 27 | ||
| 28 | /// Checks if the given object is a potential hook. | |
| 29 | /// | |
| 30 | /// @param o the object to check. | |
| 31 | /// | |
| 32 | /// @return `true` if it is a potential hook. | |
| 33 | static boolean isPotentialHook(Object o) { | |
| 34 |
3
1. isPotentialHook : replaced boolean return with true for pro/verron/officestamper/core/DocxHook::isPotentialHook → KILLED 2. isPotentialHook : negated conditional → KILLED 3. isPotentialHook : negated conditional → KILLED |
return o instanceof CTSmartTagRun tag && isTagElement(tag, "officestamper"); |
| 35 | } | |
| 36 | ||
| 37 | /// Converts an object to a hook. | |
| 38 | /// | |
| 39 | /// @param part the document part. | |
| 40 | /// @param o the object to convert. | |
| 41 | /// | |
| 42 | /// @return the hook. | |
| 43 | static DocxHook asHook(DocxPart part, Object o) { | |
| 44 |
1
1. asHook : replaced return value with null for pro/verron/officestamper/core/DocxHook::asHook → KILLED |
return switch (o) { |
| 45 |
1
1. asHook : negated conditional → KILLED |
case CTSmartTagRun tag when isType(tag, "processor", "type") -> newCommentHook(part, tag); |
| 46 | case CTSmartTagRun tag -> new TagHook(part, new Tag(part, tag)); | |
| 47 | default -> throw new IllegalArgumentException("Unexpected value: " + o); | |
| 48 | }; | |
| 49 | } | |
| 50 | ||
| 51 | /// Checks if the given tag is of the specified type. | |
| 52 | /// | |
| 53 | /// @param tag the tag to check. | |
| 54 | /// @param type the expected type value. | |
| 55 | /// @param typeKey the attribute name for the type. | |
| 56 | /// | |
| 57 | /// @return `true` if the tag matches the type. | |
| 58 | static boolean isType(CTSmartTagRun tag, String type, String typeKey) { | |
| 59 |
2
1. isType : replaced boolean return with true for pro/verron/officestamper/core/DocxHook::isType → KILLED 2. isType : replaced boolean return with false for pro/verron/officestamper/core/DocxHook::isType → KILLED |
return tag.getSmartTagPr() |
| 60 | .getAttr() | |
| 61 | .stream() | |
| 62 |
3
1. lambda$isType$0 : negated conditional → KILLED 2. lambda$isType$0 : negated conditional → KILLED 3. lambda$isType$0 : replaced boolean return with true for pro/verron/officestamper/core/DocxHook::lambda$isType$0 → KILLED |
.anyMatch(attr -> typeKey.equals(attr.getName()) && type.equals(attr.getVal())); |
| 63 | } | |
| 64 | ||
| 65 | /// Creates a new comment hook. | |
| 66 | /// | |
| 67 | /// @param part the document part. | |
| 68 | /// @param tag the tag. | |
| 69 | /// | |
| 70 | /// @return the comment hook. | |
| 71 | static DocxHook newCommentHook(DocxPart part, CTSmartTagRun tag) { | |
| 72 | var tagContent = tag.getContent(); | |
| 73 | var commentRangeStart = (CommentRangeStart) tagContent.getFirst(); | |
| 74 | var myTag = new Tag(part, tag); | |
| 75 | var comment = CommentUtil.comment(part, commentRangeStart, part.document(), part::content); | |
| 76 |
1
1. newCommentHook : replaced return value with null for pro/verron/officestamper/core/DocxHook::newCommentHook → KILLED |
return new CommentHook(part, myTag, comment); |
| 77 | } | |
| 78 | ||
| 79 | /// Executes the hook's logic within the context of a document processing flow. | |
| 80 | /// | |
| 81 | /// @param engineFactory a factory responsible for creating instances of the `Engine` class, which may be | |
| 82 | /// used during the execution of the hook's logic | |
| 83 | /// @param contextTree the root of the context tree, representing the hierarchical structure of context | |
| 84 | /// branches available during document processing | |
| 85 | /// @param officeStamperContextFactory a factory for creating evaluation contexts, which are used to | |
| 86 | /// evaluate expressions and handle dynamic behavior during the document processing flow | |
| 87 | /// | |
| 88 | /// @return `true` if the execution of the hook was successful, otherwise `false` | |
| 89 | boolean run( | |
| 90 | EngineFactory engineFactory, | |
| 91 | ContextRoot contextTree, | |
| 92 | OfficeStamperEvaluationContextFactory officeStamperContextFactory | |
| 93 | ); | |
| 94 | } | |
Mutations | ||
| 24 |
1.1 |
|
| 25 |
1.1 |
|
| 34 |
1.1 2.2 3.3 |
|
| 44 |
1.1 |
|
| 45 |
1.1 |
|
| 59 |
1.1 2.2 |
|
| 62 |
1.1 2.2 3.3 |
|
| 76 |
1.1 |