Image.java

1
package pro.verron.officestamper.preset;
2
3
import org.apache.commons.io.IOUtils;
4
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
5
import org.docx4j.openpackaging.parts.Part;
6
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage;
7
import org.docx4j.wml.R;
8
import pro.verron.officestamper.api.DocxPart;
9
import pro.verron.officestamper.api.OfficeStamperException;
10
import pro.verron.officestamper.utils.WmlFactory;
11
12
import java.io.ByteArrayOutputStream;
13
import java.io.IOException;
14
import java.io.InputStream;
15
16
/// This class describes an image which will be inserted into a document.
17
///
18
/// @author Joseph Verron
19
/// @author Romster
20
/// @version ${version}
21
/// @since 1.0.0
22
public final class Image {
23
24
    private final byte[] imageBytes;
25
    private Integer maxWidth;
26
27
    /// Constructor for Image.
28
    ///
29
    /// @param in - content of the image as InputStream
30
    ///
31
    /// @throws IOException if any.
32
    public Image(InputStream in)
33
            throws IOException {
34
        ByteArrayOutputStream out = new ByteArrayOutputStream();
35
        IOUtils.copy(in, out);
36
        this.imageBytes = out.toByteArray();
37
    }
38
39
    /// Constructor for Image.
40
    ///
41
    /// @param in       - content of the image as InputStream
42
    /// @param maxWidth - max width of the image in twip
43
    ///
44
    /// @throws IOException if any.
45
    public Image(InputStream in, Integer maxWidth)
46
            throws IOException {
47
        ByteArrayOutputStream out = new ByteArrayOutputStream();
48
        IOUtils.copy(in, out);
49
        this.imageBytes = out.toByteArray();
50
        this.maxWidth = maxWidth;
51
    }
52
53
    /// Constructor for Image.
54
    ///
55
    /// @param imageBytes - content of the image as an array of the bytes
56
    public Image(byte[] imageBytes) {
57
        this.imageBytes = imageBytes;
58
    }
59
60
    /// Constructor for Image.
61
    ///
62
    /// @param imageBytes - content of the image as an array of the bytes
63
    /// @param maxWidth   - max width of the image in twip
64
    public Image(byte[] imageBytes, Integer maxWidth) {
65
        this.imageBytes = imageBytes;
66
        this.maxWidth = maxWidth;
67
    }
68
69
    /// Creates a new run with the provided image and associated metadata.
70
    ///
71
    /// TODO: adding the same image twice will put the image twice into the docx-zip file.
72
    ///  We should make the second addition of the same image a reference instead.
73
    ///
74
    /// @param document     The document part where the image will be inserted.
75
    /// @param filenameHint A hint for the filename to be used.
76
    /// @param altText      Alternative text for the image.
77
    ///
78
    /// @return The created run containing the image.
79
    ///
80
    /// @throws OfficeStamperException If there is an error creating the image part
81
    public R newRun(DocxPart document, String filenameHint, String altText) {
82
        WordprocessingMLPackage wordprocessingMLPackage = document.document();
83
        Part part = document.part();
84
        try {
85
            var image = BinaryPartAbstractImage.createImagePart(wordprocessingMLPackage, part, imageBytes);
86 1 1. newRun : replaced return value with null for pro/verron/officestamper/preset/Image::newRun → KILLED
            return WmlFactory.newRun(maxWidth, image, filenameHint, altText);
87
        } catch (Exception e) {
88
            throw new OfficeStamperException("Failed to create an ImagePart", e);
89
        }
90
    }
91
92
    /// Getter for the field <code>maxWidth</code>.
93
    ///
94
    /// @return a [Integer] object
95
    ///
96
    /// @deprecated use the [#newRun(DocxPart, String, String)] method directly to generate a Run with Inline
97
    /// Drawing
98
    @Deprecated(since = "2.6", forRemoval = true)
99
    public Integer getMaxWidth() {
100 1 1. getMaxWidth : replaced Integer return value with 0 for pro/verron/officestamper/preset/Image::getMaxWidth → NO_COVERAGE
        return maxWidth;
101
    }
102
103
    /// Getter for the field <code>imageBytes</code>.
104
    ///
105
    /// @return an array of <code>byte</code> object
106
    ///
107
    /// @deprecated use the [#newRun(DocxPart, String, String)] method directly to generate a Run with Inline
108
    /// Drawing
109
    @Deprecated(since = "2.6", forRemoval = true)
110
    public byte[] getImageBytes() {
111 1 1. getImageBytes : replaced return value with null for pro/verron/officestamper/preset/Image::getImageBytes → NO_COVERAGE
        return imageBytes;
112
    }
113
}

Mutations

86

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:#30]
replaced return value with null for pro/verron/officestamper/preset/Image::newRun → KILLED

100

1.1
Location : getMaxWidth
Killed by : none
replaced Integer return value with 0 for pro/verron/officestamper/preset/Image::getMaxWidth → NO_COVERAGE

111

1.1
Location : getImageBytes
Killed by : none
replaced return value with null for pro/verron/officestamper/preset/Image::getImageBytes → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.21.0