Class PackagePart

    • Field Detail

      • _container

        protected OPCPackage _container
        This part's container.
      • _partName

        protected PackagePartName _partName
        The part name. (required by the specification [M1.1])
      • _contentType

        protected ContentType _contentType
        The type of content of this part. (required by the specification [M1.2])
    • Constructor Detail

      • PackagePart

        protected PackagePart​(OPCPackage pack,
                              PackagePartName partName,
                              ContentType contentType,
                              boolean loadRelationships)
                       throws InvalidFormatException
        Constructor.
        Parameters:
        pack - Parent package.
        partName - The part name, relative to the parent Package root.
        contentType - The content type.
        loadRelationships - Specify if the relationships will be loaded
        Throws:
        InvalidFormatException - If the specified URI is not valid.
      • PackagePart

        public PackagePart​(OPCPackage pack,
                           PackagePartName partName,
                           String contentType)
                    throws InvalidFormatException
        Constructor.
        Parameters:
        pack - Parent package.
        partName - The part name, relative to the parent Package root.
        contentType - The Multipurpose Internet Mail Extensions (MIME) content type of the part's data stream.
        Throws:
        InvalidFormatException - If the specified URI is not valid.
    • Method Detail

      • findExistingRelation

        public PackageRelationship findExistingRelation​(PackagePart packagePart)
        Check if the new part was already added before via PackagePart.addRelationship()
        Parameters:
        packagePart - to find the relationship for
        Returns:
        The existing relationship, or null if there isn't yet one
      • addExternalRelationship

        public PackageRelationship addExternalRelationship​(String target,
                                                           String relationshipType)
        Adds an external relationship to a part (except relationships part). The targets of external relationships are not subject to the same validity checks that internal ones are, as the contents is potentially any file, URL or similar.
        Specified by:
        addExternalRelationship in interface RelationshipSource
        Parameters:
        target - External target of the relationship
        relationshipType - Type of relationship.
        Returns:
        The newly created and added relationship
      • addRelationship

        public PackageRelationship addRelationship​(PackagePartName targetPartName,
                                                   TargetMode targetMode,
                                                   String relationshipType)
        Add a relationship to a part (except relationships part).
        Specified by:
        addRelationship in interface RelationshipSource
        Parameters:
        targetPartName - Name of the target part. This one must be relative to the source root directory of the part.
        targetMode - Mode [Internal|External].
        relationshipType - Type of relationship.
        Returns:
        The newly created and added relationship
      • addRelationship

        public PackageRelationship addRelationship​(PackagePartName targetPartName,
                                                   TargetMode targetMode,
                                                   String relationshipType,
                                                   String id)
        Add a relationship to a part (except relationships part).

        Check rule M1.25: The Relationships part shall not have relationships to any other part. Package implementers shall enforce this requirement upon the attempt to create such a relationship and shall treat any such relationship as invalid.

        Specified by:
        addRelationship in interface RelationshipSource
        Parameters:
        targetPartName - Name of the target part. This one must be relative to the source root directory of the part.
        targetMode - Mode [Internal|External].
        relationshipType - Type of relationship.
        id - Relationship unique id.
        Returns:
        The newly created and added relationship
        Throws:
        InvalidOperationException - If a writing operation is done on a read only package or invalid nested relations are created.
        IllegalArgumentException - if targetPartName, targetMode or relationshipType are passed as null
      • removeRelationship

        public void removeRelationship​(String id)
        Delete the relationship specified by its id.
        Specified by:
        removeRelationship in interface RelationshipSource
        Parameters:
        id - The ID identified the part to delete.
      • hasRelationships

        public boolean hasRelationships()
        Knows if the part have any relationships.
        Specified by:
        hasRelationships in interface RelationshipSource
        Returns:
        true if the part have at least one relationship else false.
      • isRelationshipExists

        public boolean isRelationshipExists​(PackageRelationship rel)
        Checks if the specified relationship is part of this package part.
        Specified by:
        isRelationshipExists in interface RelationshipSource
        Parameters:
        rel - The relationship to check.
        Returns:
        true if the specified relationship exists in this part, else returns false
      • getInputStream

        public InputStream getInputStream()
                                   throws IOException
        Get the input stream of this part to read its content.
        Returns:
        The input stream of the content of this part, else null.
        Throws:
        IOException - If creating the input-stream fails.
      • getOutputStream

        public OutputStream getOutputStream()
                                     throws IOException
        Get the output stream of this part. If the part is originally embedded in Zip package, it'll be transform into a MemoryPackagePart in order to write inside (the standard Java API doesn't allow to write in the file)
        Returns:
        output stream for this part
        Throws:
        IOException
        See Also:
        MemoryPackagePart
      • getContentType

        public String getContentType()
        Returns:
        The Content Type of the part
      • getContentTypeDetails

        public ContentType getContentTypeDetails()
        Returns:
        The Content Type, including parameters, of the part
      • isRelationshipPart

        public boolean isRelationshipPart()
        Returns:
        true if this part is a relationship
      • isDeleted

        public boolean isDeleted()
        Returns:
        true if this part has been logically deleted
      • setDeleted

        public void setDeleted​(boolean isDeleted)
        Parameters:
        isDeleted - the isDeleted to set
      • getSize

        public long getSize()
        Returns:
        The length of the part in bytes, or -1 if not known
      • getInputStreamImpl

        protected abstract InputStream getInputStreamImpl()
                                                   throws IOException
        Method that gets the input stream for this part.
        Returns:
        input stream for this part
        Throws:
        IOException - Throws if an IO Exception occur in the implementation method.
      • getOutputStreamImpl

        protected abstract OutputStream getOutputStreamImpl()
                                                     throws IOException
        Method that gets the output stream for this part.
        Returns:
        output stream for this part
        Throws:
        IOException - Throws if an IO Exception occur in the implementation method.
      • save

        public abstract boolean save​(OutputStream zos)
                              throws OpenXML4JException
        Save the content of this part and the associated relationships part (if this part own at least one relationship) into the specified output stream.
        Parameters:
        zos - Output stream to save this part.
        Returns:
        true if the content has been successfully stored, false otherwise. More information about errors may be logged via Log4j 2.
        Throws:
        OpenXML4JException - If any exception occur.
      • load

        public abstract boolean load​(InputStream ios)
                              throws InvalidFormatException
        Load the content of this part.
        Parameters:
        ios - The input stream of the content to load.
        Returns:
        true if the content has been successfully loaded, false otherwise. More information about errors may be logged via Log4j 2.
        Throws:
        InvalidFormatException - Throws if the content format is invalid.
      • close

        public abstract void close()
        Close this part : flush this part, close the input stream and output stream. After this method call, the part must be available for packaging.
      • flush

        public abstract void flush()
        Flush the content of this part. If the input stream and/or output stream as in a waiting state to read or write, the must to empty their respective buffer.
      • clear

        public void clear()
        Allows sub-classes to clean up before new data is added.