OpenpackagingFactory.java

1
package pro.verron.officestamper.utils.openpackaging;
2
3
import org.docx4j.openpackaging.contenttype.ContentTypeManager;
4
import org.docx4j.openpackaging.exceptions.InvalidFormatException;
5
import org.docx4j.openpackaging.exceptions.PartUnrecognisedException;
6
import org.docx4j.openpackaging.parts.Part;
7
import org.docx4j.openpackaging.parts.PartName;
8
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage;
9
import org.docx4j.openpackaging.parts.relationships.RelationshipsPart;
10
import org.docx4j.relationships.Relationship;
11
import pro.verron.officestamper.utils.UtilsException;
12
13
import java.io.ByteArrayInputStream;
14
15
/// Utility class for creating Open Packaging objects.
16
///
17
/// This class provides helper methods to create instances of docx4j Open Packaging objects, wrapping checked exceptions
18
/// in runtime [UtilsException] for easier handling.
19
public class OpenpackagingFactory {
20
21
    private OpenpackagingFactory() {
22
        throw new UtilsException("Utility class shouldn't be instantiated");
23
    }
24
25
    /// Creates a new PartName instance from the given string representation.
26
    ///
27
    /// This method wraps the checked [InvalidFormatException] that can occur when creating a PartName in a runtime
28
    /// [UtilsException].
29
    ///
30
    /// @param partName the string representation of the part name
31
    /// @return a new PartName instance
32
    /// @throws UtilsException if the part name string is invalid
33
    public static PartName newPartName(String partName) {
34
        try {
35 1 1. newPartName : replaced return value with null for pro/verron/officestamper/utils/openpackaging/OpenpackagingFactory::newPartName → NO_COVERAGE
            return new PartName(partName);
36
        } catch (InvalidFormatException e) {
37
            throw new UtilsException(e);
38
        }
39
    }
40
41
    /// Creates an image part using the specified binary data, content type manager, MIME type, and part name.
42
    ///
43
    /// This method generates an image part for use in Open Packaging workflows. It wraps checked exceptions such as
44
    /// InvalidFormatException and PartUnrecognisedException in a runtime UtilsException for easier handling.
45
    ///
46
    /// @param ctm the content type manager to use for part creation
47
    /// @param bytes the binary data for the image
48
    /// @param mimeType the MIME type of the image (e.g., "image/png")
49
    /// @param partName the name of the part to be created
50
    /// @return the created image part
51
    /// @throws UtilsException if an error occurs while creating the part
52
    public static Part createImagePart(ContentTypeManager ctm, byte[] bytes, String mimeType, String partName) {
53
        try {
54
            var imagePart = (BinaryPartAbstractImage) ctm.newPartForContentType(mimeType, partName, null);
55 1 1. createImagePart : removed call to org/docx4j/openpackaging/parts/WordprocessingML/BinaryPartAbstractImage::setBinaryData → NO_COVERAGE
            imagePart.setBinaryData(new ByteArrayInputStream(bytes));
56 1 1. createImagePart : replaced return value with null for pro/verron/officestamper/utils/openpackaging/OpenpackagingFactory::createImagePart → NO_COVERAGE
            return imagePart;
57
        } catch (InvalidFormatException | PartUnrecognisedException e) {
58
            throw new UtilsException(e);
59
        }
60
    }
61
62
    /// Establishes a relationship between a source part and a target part using the specified relationship ID.
63
    ///
64
    /// @param sourcePart the source part from which the relationship originates
65
    /// @param targetPart the target part to which the relationship points
66
    /// @param relationshipId the unique identifier for the relationship
67
    /// @return the created relationship between the source and target parts
68
    /// @throws UtilsException if an error occurs while creating the relationship
69
    public static Relationship setupRelationship(Part sourcePart, Part targetPart, String relationshipId) {
70
        try {
71
            var reuseExisting = RelationshipsPart.AddPartBehaviour.REUSE_EXISTING;
72 1 1. setupRelationship : replaced return value with null for pro/verron/officestamper/utils/openpackaging/OpenpackagingFactory::setupRelationship → NO_COVERAGE
            return sourcePart.addTargetPart(targetPart, reuseExisting, relationshipId);
73
        } catch (InvalidFormatException e) {
74
            throw new UtilsException(e);
75
        }
76
    }
77
}

Mutations

35

1.1
Location : newPartName
Killed by : none
replaced return value with null for pro/verron/officestamper/utils/openpackaging/OpenpackagingFactory::newPartName → NO_COVERAGE

55

1.1
Location : createImagePart
Killed by : none
removed call to org/docx4j/openpackaging/parts/WordprocessingML/BinaryPartAbstractImage::setBinaryData → NO_COVERAGE

56

1.1
Location : createImagePart
Killed by : none
replaced return value with null for pro/verron/officestamper/utils/openpackaging/OpenpackagingFactory::createImagePart → NO_COVERAGE

72

1.1
Location : setupRelationship
Killed by : none
replaced return value with null for pro/verron/officestamper/utils/openpackaging/OpenpackagingFactory::setupRelationship → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.25.5 support