WmlFactory.java

1
package pro.verron.officestamper.utils;
2
3
import org.docx4j.dml.wordprocessingDrawing.Inline;
4
import org.docx4j.openpackaging.exceptions.InvalidFormatException;
5
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage;
6
import org.docx4j.openpackaging.parts.WordprocessingML.CommentsPart;
7
import org.docx4j.wml.*;
8
import org.springframework.lang.Nullable;
9
import pro.verron.officestamper.api.OfficeStamperException;
10
11
import java.math.BigInteger;
12
import java.util.Arrays;
13
import java.util.List;
14
import java.util.Random;
15
16
/**
17
 * WmlFactory is a utility class providing methods to create and manipulate WordML objects.
18
 * It includes methods for creating paragraphs, runs, text elements, comments, bodies and drawings.
19
 * This factory encapsulates the complexity of WordML elements and simplifies the process of working with them.
20
 */
21
public class WmlFactory {
22
    private static final Random RANDOM = new Random();
23
24
    private WmlFactory() {
25
        throw new OfficeStamperException("Utility class");
26
    }
27
28
    /**
29
     * Creates a new paragraph containing a single drawing.
30
     *
31
     * @param drawing The Drawing object to be included in the new paragraph.
32
     *
33
     * @return A new paragraph encapsulating the provided drawing.
34
     */
35
    public static P newParagraph(Drawing drawing) {
36 1 1. newParagraph : replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newParagraph → NO_COVERAGE
        return newParagraph(List.of(newRun(drawing)));
37
    }
38
39
    /**
40
     * Creates a new paragraph containing the provided values.
41
     *
42
     * @param values A list of objects to be added to the new paragraph.
43
     *
44
     * @return A new paragraph containing the provided values.
45
     */
46
    public static P newParagraph(List<?> values) {
47
        var paragraph = new P();
48
        var paragraphContent = paragraph.getContent();
49
        paragraphContent.addAll(values);
50 1 1. newParagraph : replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newParagraph → KILLED
        return paragraph;
51
    }
52
53
    /**
54
     * Creates a new run containing a single drawing.
55
     *
56
     * @param value The Drawing object to be included in the new run.
57
     *
58
     * @return A new run encapsulating the provided drawing.
59
     */
60
    public static R newRun(Drawing value) {
61 1 1. newRun : replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newRun → KILLED
        return newRun(List.of(value));
62
    }
63
64
    private static R newRun(List<Object> values) {
65
        var run = new R();
66
        var runContent = run.getContent();
67
        runContent.addAll(values);
68 1 1. newRun : replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newRun → KILLED
        return run;
69
    }
70
71
    /**
72
     * Creates a new comment with the provided value.
73
     *
74
     * @param value The string value to be included in the comment.
75
     *
76
     * @return A new Comments.Comment object containing the provided value.
77
     */
78
    public static Comments.Comment newComment(BigInteger id, String value) {
79
        var comment = new Comments.Comment();
80 1 1. newComment : removed call to org/docx4j/wml/Comments$Comment::setId → SURVIVED
        comment.setId(id);
81
        var commentContent = comment.getContent();
82
        commentContent.add(newParagraph(value));
83 1 1. newComment : replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newComment → SURVIVED
        return comment;
84
    }
85
86
    /**
87
     * Creates a new paragraph containing the provided string value.
88
     *
89
     * @param value The string value to be added to the new paragraph.
90
     *
91
     * @return A new paragraph containing the provided string value.
92
     */
93
    public static P newParagraph(String value) {
94 1 1. newParagraph : replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newParagraph → SURVIVED
        return newParagraph(newRun(value));
95
    }
96
97
    /**
98
     * Creates a new paragraph containing the provided run.
99
     *
100
     * @param run The R object (run) to be included in the new paragraph.
101
     *
102
     * @return A new paragraph containing the provided run.
103
     */
104
    public static P newParagraph(R run) {
105 1 1. newParagraph : replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newParagraph → SURVIVED
        return newParagraph(List.of(run));
106
    }
107
108
    /**
109
     * Creates a new run containing the provided string value.
110
     *
111
     * @param value The string value to be included in the new run.
112
     *
113
     * @return A new run containing the provided string value.
114
     */
115
    public static R newRun(String value) {
116 1 1. newRun : replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newRun → KILLED
        return newRun(newText(value));
117
    }
118
119
    /**
120
     * Creates a new run containing a single text object.
121
     *
122
     * @param value The Text object to be included in the new run.
123
     *
124
     * @return A new run encapsulating the provided text object.
125
     */
126
    public static R newRun(Text value) {
127 1 1. newRun : replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newRun → KILLED
        return newRun(List.of(value));
128
    }
129
130
    /**
131
     * Creates a new Text object with the specified value, preserving spaces.
132
     *
133
     * @param value The string value to be set in the new Text object.
134
     *
135
     * @return A new Text object containing the provided value with space preserved.
136
     */
137
    public static Text newText(String value) {
138
        var text = new Text();
139 1 1. newText : removed call to org/docx4j/wml/Text::setValue → KILLED
        text.setValue(value);
140 1 1. newText : removed call to org/docx4j/wml/Text::setSpace → KILLED
        text.setSpace("preserve");
141 1 1. newText : replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newText → KILLED
        return text;
142
    }
143
144
    /**
145
     * Creates a new Body object containing the provided elements.
146
     *
147
     * @param elements A list of objects to be added to the new Body.
148
     *
149
     * @return A new Body containing the provided elements.
150
     */
151
    public static Body newBody(List<Object> elements) {
152
        Body body = new Body();
153
        var bodyContent = body.getContent();
154
        bodyContent.addAll(elements);
155 1 1. newBody : replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newBody → KILLED
        return body;
156
    }
157
158
    /**
159
     * Creates a new paragraph containing the provided text values.
160
     *
161
     * @param texts The array of string values to be included in the new paragraph.
162
     *
163
     * @return A new paragraph containing the provided text values.
164
     */
165
    public static P newParagraph(String... texts) {
166 1 1. newParagraph : replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newParagraph → KILLED
        return newParagraph(Arrays.stream(texts)
167
                                  .map(WmlFactory::newRun)
168
                                  .toList());
169
    }
170
171
    /**
172
     * Creates a new PPr (paragraph properties) object.
173
     *
174
     * @return A new PPr object.
175
     */
176
    public static PPr newPPr() {
177 1 1. newPPr : replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newPPr → KILLED
        return new PPr();
178
    }
179
180
    /**
181
     * Creates a new Comments object and populates it with a list of Comment objects.
182
     *
183
     * @param list A list of Comments.Comment objects to be added to the new Comments object.
184
     *
185
     * @return A new Comments object containing the provided Comment objects.
186
     */
187
    public static Comments newComments(List<Comments.Comment> list) {
188
        Comments comments = new Comments();
189
        List<Comments.Comment> commentList = comments.getComment();
190
        commentList.addAll(list);
191 1 1. newComments : replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newComments → KILLED
        return comments;
192
    }
193
194
    /**
195
     * Creates a new CommentsPart object.
196
     * This method attempts to create a new instance of CommentsPart.
197
     * If an InvalidFormatException occurs during the creation process, it wraps the exception in an
198
     * OfficeStamperException and throws it.
199
     *
200
     * @return A new instance of CommentsPart.
201
     */
202
    public static CommentsPart newCommentsPart() {
203
        try {
204 1 1. newCommentsPart : replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newCommentsPart → NO_COVERAGE
            return new CommentsPart();
205
        } catch (InvalidFormatException e) {
206
            throw new OfficeStamperException(e);
207
        }
208
    }
209
210
    /**
211
     * Creates a new run containing an image with the specified attributes.
212
     *
213
     * @param maxWidth      the maximum width of the image, it can be null
214
     * @param abstractImage the binary part abstract image to be included in the run
215
     * @param filenameHint  the filename hint for the image
216
     * @param altText       the alternative text for the image
217
     *
218
     * @return a new run element containing the image
219
     */
220
    public static R newRun(
221
            @Nullable Integer maxWidth, BinaryPartAbstractImage abstractImage, String filenameHint, String altText
222
    ) {
223
        var inline = newInline(abstractImage, filenameHint, altText, maxWidth);
224 1 1. newRun : replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newRun → KILLED
        return newRun(newDrawing(inline));
225
    }
226
227
    /**
228
     * Creates a new Inline object for the given image part, filename hint, and alt text.
229
     *
230
     * @param imagePart    The binary part abstract image to be used.
231
     * @param filenameHint A hint for the filename of the image.
232
     * @param altText      Alternative text for the image.
233
     *
234
     * @return A new Inline object containing the specified image information.
235
     *
236
     * @throws OfficeStamperException If there is an error creating the image inline.
237
     */
238
    public static Inline newInline(
239
            BinaryPartAbstractImage imagePart, String filenameHint, String altText, @Nullable Integer maxWidth
240
    ) {
241
        // creating random ids assuming they are unique,
242
        // id must not be too large
243
        // otherwise Word cannot open the document
244
        var id1 = RANDOM.nextLong(100_000L);
245
        var id2 = RANDOM.nextInt(100_000);
246
        try {
247 2 1. newInline : negated conditional → KILLED
2. newInline : replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newInline → KILLED
            return maxWidth == null
248
                    ? imagePart.createImageInline(filenameHint, altText, id1, id2, false)
249
                    : imagePart.createImageInline(filenameHint, altText, id1, id2, false, maxWidth);
250
        } catch (Exception e) {
251
            throw new OfficeStamperException(e);
252
        }
253
    }
254
255
    /**
256
     * Creates a new Drawing object containing the provided Inline object.
257
     *
258
     * @param inline The Inline object to be contained within the new Drawing.
259
     *
260
     * @return A new Drawing object encapsulating the provided inline object.
261
     */
262
    public static Drawing newDrawing(Inline inline) {
263
        var drawing = new Drawing();
264
        var anchorOrInline = drawing.getAnchorOrInline();
265
        anchorOrInline.add(inline);
266 1 1. newDrawing : replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newDrawing → KILLED
        return drawing;
267
    }
268
269
    public static CommentRangeStart newCommentRangeStart(BigInteger id, P parent) {
270
        var commentRangeStart = new CommentRangeStart();
271 1 1. newCommentRangeStart : removed call to org/docx4j/wml/CommentRangeStart::setId → SURVIVED
        commentRangeStart.setId(id);
272 1 1. newCommentRangeStart : removed call to org/docx4j/wml/CommentRangeStart::setParent → SURVIVED
        commentRangeStart.setParent(parent);
273 1 1. newCommentRangeStart : replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newCommentRangeStart → KILLED
        return commentRangeStart;
274
    }
275
276
    public static CommentRangeEnd newCommentRangeEnd(BigInteger id, P parent) {
277
        var commentRangeEnd = new CommentRangeEnd();
278 1 1. newCommentRangeEnd : removed call to org/docx4j/wml/CommentRangeEnd::setId → SURVIVED
        commentRangeEnd.setId(id);
279 1 1. newCommentRangeEnd : removed call to org/docx4j/wml/CommentRangeEnd::setParent → SURVIVED
        commentRangeEnd.setParent(parent);
280 1 1. newCommentRangeEnd : replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newCommentRangeEnd → KILLED
        return commentRangeEnd;
281
    }
282
283
    public static R.CommentReference newCommentReference(BigInteger id, P parent) {
284
        var commentReference = new R.CommentReference();
285 1 1. newCommentReference : removed call to org/docx4j/wml/R$CommentReference::setId → SURVIVED
        commentReference.setId(id);
286 1 1. newCommentReference : removed call to org/docx4j/wml/R$CommentReference::setParent → SURVIVED
        commentReference.setParent(parent);
287 1 1. newCommentReference : replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newCommentReference → SURVIVED
        return commentReference;
288
    }
289
}

Mutations

36

1.1
Location : newParagraph
Killed by : none
replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newParagraph → NO_COVERAGE

50

1.1
Location : newParagraph
Killed by : pro.verron.officestamper.test.DefaultTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.DefaultTests]/[test-template:features(java.lang.String, pro.verron.officestamper.api.OfficeStamperConfiguration, java.lang.Object, java.io.InputStream, java.lang.String)]/[test-template-invocation:#39]
replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newParagraph → KILLED

61

1.1
Location : newRun
Killed by : pro.verron.officestamper.test.DefaultTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.DefaultTests]/[test-template:features(java.lang.String, pro.verron.officestamper.api.OfficeStamperConfiguration, java.lang.Object, java.io.InputStream, java.lang.String)]/[test-template-invocation:#31]
replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newRun → KILLED

68

1.1
Location : newRun
Killed by : pro.verron.officestamper.test.ResolutionTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ResolutionTest]/[test-template:testCustomResolution(java.lang.String, boolean, boolean, boolean, boolean, boolean, java.lang.String, boolean, java.lang.String)]/[test-template-invocation:#28]
replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newRun → KILLED

80

1.1
Location : newComment
Killed by : none
removed call to org/docx4j/wml/Comments$Comment::setId → SURVIVED
Covering tests

83

1.1
Location : newComment
Killed by : none
replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newComment → SURVIVED
Covering tests

94

1.1
Location : newParagraph
Killed by : none
replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newParagraph → SURVIVED
Covering tests

105

1.1
Location : newParagraph
Killed by : none
replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newParagraph → SURVIVED
Covering tests

116

1.1
Location : newRun
Killed by : pro.verron.officestamper.test.ResolutionTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ResolutionTest]/[test-template:testCustomResolution(java.lang.String, boolean, boolean, boolean, boolean, boolean, java.lang.String, boolean, java.lang.String)]/[test-template-invocation:#28]
replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newRun → KILLED

127

1.1
Location : newRun
Killed by : pro.verron.officestamper.test.ResolutionTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ResolutionTest]/[test-template:testCustomResolution(java.lang.String, boolean, boolean, boolean, boolean, boolean, java.lang.String, boolean, java.lang.String)]/[test-template-invocation:#28]
replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newRun → KILLED

139

1.1
Location : newText
Killed by : pro.verron.officestamper.test.ResolutionTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ResolutionTest]/[test-template:testCustomResolution(java.lang.String, boolean, boolean, boolean, boolean, boolean, java.lang.String, boolean, java.lang.String)]/[test-template-invocation:#28]
removed call to org/docx4j/wml/Text::setValue → KILLED

140

1.1
Location : newText
Killed by : pro.verron.officestamper.test.DefaultTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.DefaultTests]/[test-template:features(java.lang.String, pro.verron.officestamper.api.OfficeStamperConfiguration, java.lang.Object, java.io.InputStream, java.lang.String)]/[test-template-invocation:#6]
removed call to org/docx4j/wml/Text::setSpace → KILLED

141

1.1
Location : newText
Killed by : pro.verron.officestamper.test.ResolutionTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ResolutionTest]/[test-template:testCustomResolution(java.lang.String, boolean, boolean, boolean, boolean, boolean, java.lang.String, boolean, java.lang.String)]/[test-template-invocation:#28]
replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newText → KILLED

155

1.1
Location : newBody
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:#1]
replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newBody → KILLED

166

1.1
Location : newParagraph
Killed by : pro.verron.officestamper.test.StampTableTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.StampTableTest]/[method:stampTableTest()]
replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newParagraph → KILLED

177

1.1
Location : newPPr
Killed by : pro.verron.officestamper.test.DefaultTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.DefaultTests]/[test-template:features(java.lang.String, pro.verron.officestamper.api.OfficeStamperConfiguration, java.lang.Object, java.io.InputStream, java.lang.String)]/[test-template-invocation:#14]
replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newPPr → KILLED

191

1.1
Location : newComments
Killed by : pro.verron.officestamper.test.RepeatDocPartBadPlaceholderTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.RepeatDocPartBadPlaceholderTest]/[method:testBadExpressionShouldNotBlockCallerThread()]
replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newComments → KILLED

204

1.1
Location : newCommentsPart
Killed by : none
replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newCommentsPart → NO_COVERAGE

224

1.1
Location : newRun
Killed by : pro.verron.officestamper.test.DefaultTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.DefaultTests]/[test-template:features(java.lang.String, pro.verron.officestamper.api.OfficeStamperConfiguration, java.lang.Object, java.io.InputStream, java.lang.String)]/[test-template-invocation:#31]
replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newRun → KILLED

247

1.1
Location : newInline
Killed by : pro.verron.officestamper.test.DefaultTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.DefaultTests]/[test-template:features(java.lang.String, pro.verron.officestamper.api.OfficeStamperConfiguration, java.lang.Object, java.io.InputStream, java.lang.String)]/[test-template-invocation:#31]
negated conditional → KILLED

2.2
Location : newInline
Killed by : pro.verron.officestamper.test.DefaultTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.DefaultTests]/[test-template:features(java.lang.String, pro.verron.officestamper.api.OfficeStamperConfiguration, java.lang.Object, java.io.InputStream, java.lang.String)]/[test-template-invocation:#31]
replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newInline → KILLED

266

1.1
Location : newDrawing
Killed by : pro.verron.officestamper.test.DefaultTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.DefaultTests]/[test-template:features(java.lang.String, pro.verron.officestamper.api.OfficeStamperConfiguration, java.lang.Object, java.io.InputStream, java.lang.String)]/[test-template-invocation:#31]
replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newDrawing → KILLED

271

1.1
Location : newCommentRangeStart
Killed by : none
removed call to org/docx4j/wml/CommentRangeStart::setId → SURVIVED
Covering tests

272

1.1
Location : newCommentRangeStart
Killed by : none
removed call to org/docx4j/wml/CommentRangeStart::setParent → SURVIVED
Covering tests

273

1.1
Location : newCommentRangeStart
Killed by : pro.verron.officestamper.test.SpelInjectionTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.SpelInjectionTest]/[method:spelInjectionTest()]
replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newCommentRangeStart → KILLED

278

1.1
Location : newCommentRangeEnd
Killed by : none
removed call to org/docx4j/wml/CommentRangeEnd::setId → SURVIVED
Covering tests

279

1.1
Location : newCommentRangeEnd
Killed by : none
removed call to org/docx4j/wml/CommentRangeEnd::setParent → SURVIVED
Covering tests

280

1.1
Location : newCommentRangeEnd
Killed by : pro.verron.officestamper.test.SpelInjectionTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.SpelInjectionTest]/[method:spelInjectionTest()]
replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newCommentRangeEnd → KILLED

285

1.1
Location : newCommentReference
Killed by : none
removed call to org/docx4j/wml/R$CommentReference::setId → SURVIVED
Covering tests

286

1.1
Location : newCommentReference
Killed by : none
removed call to org/docx4j/wml/R$CommentReference::setParent → SURVIVED
Covering tests

287

1.1
Location : newCommentReference
Killed by : none
replaced return value with null for pro/verron/officestamper/utils/WmlFactory::newCommentReference → SURVIVED
Covering tests

Active mutators

Tests examined


Report generated by PIT 1.17.1