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 Tag.
27
    /// @param tag the CTSmartTagRun representing the smart tag element in the document.
28
    /// @return a new Tag instance initialized with the given DocxPart and CTSmartTagRun.
29
    public static Tag of(DocxPart docxPart, CTSmartTagRun tag) {
30 1 1. of : replaced return value with null for pro/verron/officestamper/core/Tag::of → NO_COVERAGE
        return new Tag(docxPart, tag);
31
    }
32
33
34
    /// Removes the current tag from its parent's content list.
35
    ///
36
    /// This method locates the parent content accessor of the tag, retrieves its sibling elements, and removes the tag
37
    /// from the sibling list, detaching it from its parent content.
38
    public void remove() {
39
        var parent = (ContentAccessor) tag.getParent();
40
        var siblings = parent.getContent();
41
        siblings.remove(tag);
42
    }
43
44
    /// Retrieves the paragraph associated with the smart tag's parent element.
45
    ///
46
    /// @return the Paragraph object representing the parent element of the smart tag
47
    public Paragraph getParagraph() {
48 1 1. getParagraph : replaced return value with null for pro/verron/officestamper/core/Tag::getParagraph → KILLED
        return StandardParagraph.from(docxPart, tag.getParent());
49
    }
50
51
    /// Converts the current tag entity into a Comment representation.
52
    ///
53
    /// This method creates a new Comment instance associated with the parent paragraph of the smart tag, using its
54
    /// placeholder representation, and a predefined position value.
55
    ///
56
    /// @return a Comment object representing the current tag
57
    public Comment asComment() {
58 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);
59
    }
60
61
    /// Retrieves the expression of the tag.
62
    ///
63
    /// @return the expression.
64
    public String expression() {
65 1 1. expression : replaced return value with "" for pro/verron/officestamper/core/Tag::expression → KILLED
        return asString(tag.getContent());
66
    }
67
68
    /// Replaces the current tag with the provided Insert object in the parent's content list. It sets the Run
69
    /// Properties [RPr] of the provided Insert object and then removes the current tag and inserts the elements from
70
    /// the Insert object at the appropriate position.
71
    ///
72
    /// @param insert the Insert object containing elements to replace the current tag. It also provides the
73
    ///         ability to set Run Properties [RPr] for styling purposes.
74
    public void replace(Insert insert) {
75
        var optionalRun = getFirst(tag, R.class);
76 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()));
77
        var siblings = tag.getContent();
78 1 1. replace : removed call to java/util/List::clear → KILLED
        siblings.clear();
79
        siblings.addAll(insert.elements());
80
    }
81
82
    private static <T> Optional<T> getFirst(CTSmartTagRun tagRun, Class<T> clazz) {
83 1 1. getFirst : replaced return value with Optional.empty for pro/verron/officestamper/core/Tag::getFirst → KILLED
        return tagRun.getContent()
84
                     .stream()
85
                     .filter(clazz::isInstance)
86
                     .map(clazz::cast)
87
                     .findFirst();
88
    }
89
90
    /// Retrieves the type of the tag.
91
    ///
92
    /// @return the type.
93
    public Optional<String> type() {
94 1 1. type : replaced return value with Optional.empty for pro/verron/officestamper/core/Tag::type → KILLED
        return tag.getSmartTagPr()
95
                  .getAttr()
96
                  .stream()
97 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()
98
                                .equals("type"))
99
                  .map(CTAttr::getVal)
100
                  .findFirst();
101
    }
102
103
    /// Retrieves the context key of the tag.
104
    ///
105
    /// @return the context key.
106
    public String getContextKey() {
107
        var smartTagPr = tag.getSmartTagPr();
108 1 1. getContextKey : negated conditional → KILLED
        if (smartTagPr == null) return String.valueOf(0);
109
        var smartTagPrAttr = smartTagPr.getAttr();
110 1 1. getContextKey : negated conditional → KILLED
        if (smartTagPrAttr == null) return String.valueOf(0);
111
        for (CTAttr attribute : smartTagPrAttr) {
112 1 1. getContextKey : negated conditional → KILLED
            if ("context".equals(attribute.getName())) try {
113 1 1. getContextKey : replaced return value with "" for pro/verron/officestamper/core/Tag::getContextKey → KILLED
                return String.valueOf(Integer.parseInt(attribute.getVal()));
114
            } catch (NumberFormatException _) {
115
                return String.valueOf(0);
116
            }
117
        }
118
        return String.valueOf(0);
119
    }
120
}

Mutations

30

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

48

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:#2]
replaced return value with null for pro/verron/officestamper/core/Tag::getParagraph → KILLED

58

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

65

1.1
Location : expression
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]
replaced return value with "" for pro/verron/officestamper/core/Tag::expression → KILLED

76

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:#22]
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:#22]
removed call to java/util/Optional::ifPresent → KILLED

78

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

83

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:#22]
replaced return value with Optional.empty for pro/verron/officestamper/core/Tag::getFirst → KILLED

94

1.1
Location : type
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]
replaced return value with Optional.empty for pro/verron/officestamper/core/Tag::type → KILLED

97

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.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]
replaced boolean return with false for pro/verron/officestamper/core/Tag::lambda$type$0 → KILLED

108

1.1
Location : getContextKey
Killed by : pro.verron.officestamper.test.RegressionTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.RegressionTests]/[test-template:test52(pro.verron.officestamper.test.RegressionTests$Conditions, java.lang.String)]/[test-template-invocation:#3]
negated conditional → KILLED

110

1.1
Location : getContextKey
Killed by : pro.verron.officestamper.test.RegressionTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.RegressionTests]/[test-template:test52(pro.verron.officestamper.test.RegressionTests$Conditions, java.lang.String)]/[test-template-invocation:#3]
negated conditional → KILLED

112

1.1
Location : getContextKey
Killed by : pro.verron.officestamper.test.RegressionTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.RegressionTests]/[test-template:test52(pro.verron.officestamper.test.RegressionTests$Conditions, java.lang.String)]/[test-template-invocation:#3]
negated conditional → KILLED

113

1.1
Location : getContextKey
Killed by : pro.verron.officestamper.test.RegressionTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.RegressionTests]/[test-template:test52(pro.verron.officestamper.test.RegressionTests$Conditions, java.lang.String)]/[test-template-invocation:#3]
replaced return value with "" for pro/verron/officestamper/core/Tag::getContextKey → KILLED

Active mutators

Tests examined


Report generated by PIT 1.25.5 support