Tag.java

1
package pro.verron.officestamper.core;
2
3
import org.docx4j.wml.*;
4
import pro.verron.officestamper.api.Comment;
5
import pro.verron.officestamper.api.DocxPart;
6
import pro.verron.officestamper.api.Insert;
7
import pro.verron.officestamper.api.Paragraph;
8
9
import java.math.BigInteger;
10
import java.util.Optional;
11
12
import static pro.verron.officestamper.utils.wml.WmlUtils.asString;
13
14
/// Represents a Tag entity consisting of a DocxPart and a CTSmartTagRun. A Tag provides functionality to manipulate and
15
/// retrieve information related to smart tags embedded within a WordprocessingML-based document. This class offers
16
/// methods to create a new Tag instance, remove the tag from its parent content, and retrieve associated elements such
17
/// as Paragraph and Comment objects. Additionally, a placeholder representation of the tag can be accessed through the
18
/// appropriate method.
19
///
20
/// @param docxPart the DocxPart instance representing the part of the document associated with the tag.
21
/// @param tag the CTSmartTagRun representing the smart tag element in the document.
22
public record Tag(DocxPart docxPart, CTSmartTagRun tag) {
23
24
    /// Creates a new Tag instance using the provided DocxPart and CTSmartTagRun.
25
    ///
26
    /// @param docxPart the DocxPart instance representing the part of the document associated with the new
27
    ///         Tag.
28
    /// @param tag the CTSmartTagRun representing the smart tag element in the document.
29
    ///
30
    /// @return a new Tag instance initialized with the given DocxPart and CTSmartTagRun.
31
    public static Tag of(DocxPart docxPart, CTSmartTagRun tag) {
32 1 1. of : replaced return value with null for pro/verron/officestamper/core/Tag::of → NO_COVERAGE
        return new Tag(docxPart, tag);
33
    }
34
35
36
    /// Removes the current tag from its parent's content list.
37
    ///
38
    /// This method locates the parent content accessor of the tag, retrieves its sibling elements, and removes the tag
39
    /// from the sibling list, detaching it from its parent content.
40
    public void remove() {
41
        var parent = (ContentAccessor) tag.getParent();
42
        var siblings = parent.getContent();
43
        siblings.remove(tag);
44
    }
45
46
    /// Retrieves the paragraph associated with the smart tag's parent element.
47
    ///
48
    /// @return the Paragraph object representing the parent element of the smart tag
49
    public Paragraph getParagraph() {
50 1 1. getParagraph : replaced return value with null for pro/verron/officestamper/core/Tag::getParagraph → KILLED
        return StandardParagraph.from(docxPart, tag.getParent());
51
    }
52
53
    /// Converts the current tag entity into a Comment representation.
54
    ///
55
    /// This method creates a new Comment instance associated with the parent paragraph of the smart tag, using its
56
    /// placeholder representation, and a predefined position value.
57
    ///
58
    /// @return a Comment object representing the current tag
59
    public Comment asComment() {
60 1 1. asComment : replaced return value with null for pro/verron/officestamper/core/Tag::asComment → SURVIVED
        return StandardComment.create(docxPart, (ContentAccessor) tag.getParent(), expression(), BigInteger.ZERO);
61
    }
62
63
    /// Retrieves the expression of the tag.
64
    ///
65
    /// @return the expression.
66
    public String expression() {
67 1 1. expression : replaced return value with "" for pro/verron/officestamper/core/Tag::expression → KILLED
        return asString(tag.getContent());
68
    }
69
70
    /// Replaces the current tag with the provided Insert object in the parent's content list. It sets the Run
71
    /// Properties [RPr] of the provided Insert object and then removes the current tag and inserts the elements from
72
    /// the Insert object at the appropriate position.
73
    ///
74
    /// @param insert the Insert object containing elements to replace the current tag. It also provides the
75
    ///         ability to set Run Properties [RPr] for styling purposes.
76
    public void replace(Insert insert) {
77
        var optionalRun = getFirst(tag, R.class);
78 2 1. lambda$replace$0 : removed call to pro/verron/officestamper/api/Insert::setRPr → KILLED
2. replace : removed call to java/util/Optional::ifPresent → KILLED
        optionalRun.ifPresent(firstRun -> insert.setRPr(firstRun.getRPr()));
79
        var siblings = tag.getContent();
80 1 1. replace : removed call to java/util/List::clear → KILLED
        siblings.clear();
81
        siblings.addAll(insert.elements());
82
    }
83
84
    private static <T> Optional<T> getFirst(CTSmartTagRun tagRun, Class<T> clazz) {
85 1 1. getFirst : replaced return value with Optional.empty for pro/verron/officestamper/core/Tag::getFirst → KILLED
        return tagRun.getContent()
86
                     .stream()
87
                     .filter(clazz::isInstance)
88
                     .map(clazz::cast)
89
                     .findFirst();
90
    }
91
92
    /// Retrieves the type of the tag.
93
    ///
94
    /// @return the type.
95
    public Optional<String> type() {
96 1 1. type : replaced return value with Optional.empty for pro/verron/officestamper/core/Tag::type → KILLED
        return tag.getSmartTagPr()
97
                  .getAttr()
98
                  .stream()
99 2 1. lambda$type$0 : replaced boolean return with true for pro/verron/officestamper/core/Tag::lambda$type$0 → SURVIVED
2. lambda$type$0 : replaced boolean return with false for pro/verron/officestamper/core/Tag::lambda$type$0 → KILLED
                  .filter(a -> a.getName()
100
                                .equals("type"))
101
                  .map(CTAttr::getVal)
102
                  .findFirst();
103
    }
104
105
    /// Retrieves the context key of the tag.
106
    ///
107
    /// @return the context key.
108
    public String getContextKey() {
109
        var smartTagPr = tag.getSmartTagPr();
110 1 1. getContextKey : negated conditional → KILLED
        if (smartTagPr == null) return String.valueOf(0);
111
        var smartTagPrAttr = smartTagPr.getAttr();
112 1 1. getContextKey : negated conditional → KILLED
        if (smartTagPrAttr == null) return String.valueOf(0);
113
        for (CTAttr attribute : smartTagPrAttr) {
114 1 1. getContextKey : negated conditional → KILLED
            if ("context".equals(attribute.getName())) try {
115 1 1. getContextKey : replaced return value with "" for pro/verron/officestamper/core/Tag::getContextKey → KILLED
                return String.valueOf(Integer.parseInt(attribute.getVal()));
116
            } catch (NumberFormatException _) {
117
                return String.valueOf(0);
118
            }
119
        }
120
        return String.valueOf(0);
121
    }
122
}

Mutations

32

1.1
Location : of
Killed by : none
replaced return value with null for pro/verron/officestamper/core/Tag::of → NO_COVERAGE

50

1.1
Location : getParagraph
Killed by : pro.verron.officestamper.test.CustomProcessorTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.CustomProcessorTests]/[test-template:should_allow_custom_processors_injection(pro.verron.officestamper.test.utils.ContextFactory)]/[test-template-invocation:#1]
replaced return value with null for pro/verron/officestamper/core/Tag::getParagraph → KILLED

60

1.1
Location : asComment
Killed by : none
replaced return value with null for pro/verron/officestamper/core/Tag::asComment → SURVIVED
Covering tests

67

1.1
Location : expression
Killed by : pro.verron.officestamper.test.SpelInjectionTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.SpelInjectionTest]/[test-template:spelInjectionTest(pro.verron.officestamper.test.utils.ContextFactory)]/[test-template-invocation:#2]
replaced return value with "" for pro/verron/officestamper/core/Tag::expression → KILLED

78

1.1
Location : lambda$replace$0
Killed by : pro.verron.officestamper.test.DefaultTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.DefaultTests]/[test-template:features(pro.verron.officestamper.api.OfficeStamperConfiguration, java.lang.Object, org.docx4j.openpackaging.packages.WordprocessingMLPackage, java.lang.String)]/[test-template-invocation:#8]
removed call to pro/verron/officestamper/api/Insert::setRPr → KILLED

2.2
Location : replace
Killed by : pro.verron.officestamper.test.DefaultTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.DefaultTests]/[test-template:features(pro.verron.officestamper.api.OfficeStamperConfiguration, java.lang.Object, org.docx4j.openpackaging.packages.WordprocessingMLPackage, java.lang.String)]/[test-template-invocation:#8]
removed call to java/util/Optional::ifPresent → KILLED

80

1.1
Location : replace
Killed by : pro.verron.officestamper.test.CustomFunctionTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.CustomFunctionTests]/[test-template:trifunctions(pro.verron.officestamper.test.utils.ContextFactory, java.lang.String, java.lang.String)]/[test-template-invocation:#12]
removed call to java/util/List::clear → KILLED

85

1.1
Location : getFirst
Killed by : pro.verron.officestamper.test.DefaultTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.DefaultTests]/[test-template:features(pro.verron.officestamper.api.OfficeStamperConfiguration, java.lang.Object, org.docx4j.openpackaging.packages.WordprocessingMLPackage, java.lang.String)]/[test-template-invocation:#8]
replaced return value with Optional.empty for pro/verron/officestamper/core/Tag::getFirst → KILLED

96

1.1
Location : type
Killed by : pro.verron.officestamper.test.SpelInjectionTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.SpelInjectionTest]/[test-template:spelInjectionTest(pro.verron.officestamper.test.utils.ContextFactory)]/[test-template-invocation:#2]
replaced return value with Optional.empty for pro/verron/officestamper/core/Tag::type → KILLED

99

1.1
Location : lambda$type$0
Killed by : none
replaced boolean return with true for pro/verron/officestamper/core/Tag::lambda$type$0 → SURVIVED
Covering tests

2.2
Location : lambda$type$0
Killed by : pro.verron.officestamper.test.SpelInjectionTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.SpelInjectionTest]/[test-template:spelInjectionTest(pro.verron.officestamper.test.utils.ContextFactory)]/[test-template-invocation:#2]
replaced boolean return with false for pro/verron/officestamper/core/Tag::lambda$type$0 → KILLED

110

1.1
Location : getContextKey
Killed by : pro.verron.officestamper.test.ProcessorRepeatDocPart_BadPlaceholderTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ProcessorRepeatDocPart_BadPlaceholderTest]/[test-template:testBadExpressionShouldNotBlockCallerThread(pro.verron.officestamper.test.utils.ContextFactory)]/[test-template-invocation:#2]
negated conditional → KILLED

112

1.1
Location : getContextKey
Killed by : pro.verron.officestamper.test.ProcessorRepeatDocPart_BadPlaceholderTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ProcessorRepeatDocPart_BadPlaceholderTest]/[test-template:testBadExpressionShouldNotBlockCallerThread(pro.verron.officestamper.test.utils.ContextFactory)]/[test-template-invocation:#2]
negated conditional → KILLED

114

1.1
Location : getContextKey
Killed by : pro.verron.officestamper.test.ProcessorRepeatDocPart_BadPlaceholderTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ProcessorRepeatDocPart_BadPlaceholderTest]/[test-template:testBadExpressionShouldNotBlockCallerThread(pro.verron.officestamper.test.utils.ContextFactory)]/[test-template-invocation:#2]
negated conditional → KILLED

115

1.1
Location : getContextKey
Killed by : pro.verron.officestamper.test.ProcessorRepeatDocPart_BadPlaceholderTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ProcessorRepeatDocPart_BadPlaceholderTest]/[test-template:testBadExpressionShouldNotBlockCallerThread(pro.verron.officestamper.test.utils.ContextFactory)]/[test-template-invocation:#2]
replaced return value with "" for pro/verron/officestamper/core/Tag::getContextKey → KILLED

Active mutators

Tests examined


Report generated by PIT 1.23.1 support