Class ContentTypeManager

  • Direct Known Subclasses:
    ZipContentTypeManager

    public abstract class ContentTypeManager
    extends java.lang.Object
    Manage package content types ([Content_Types].xml part).
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      void addContentType​(PackagePartName partName, java.lang.String contentType)
      Build association extention-> content type (will be stored in [Content_Types].xml) for example ContentType="image/png" Extension="png"
      void clearAll()
      Clear all content types.
      void clearOverrideContentTypes()
      Clear all override content types.
      java.lang.String getContentType​(PackagePartName partName)
      Get the content type for the specified part, if any.
      boolean isContentTypeRegister​(java.lang.String contentType)
      Check if the specified content type is already register.
      void removeContentType​(PackagePartName partName)
      Delete a content type based on the specified part name.
      boolean save​(java.io.OutputStream outStream)
      Save the contents type part.
      abstract boolean saveImpl​(org.w3c.dom.Document content, java.io.OutputStream out)
      Specific implementation of the save method.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • CONTENT_TYPES_PART_NAME

        public static final java.lang.String CONTENT_TYPES_PART_NAME
        Content type part name.
        See Also:
        Constant Field Values
      • TYPES_NAMESPACE_URI

        public static final java.lang.String TYPES_NAMESPACE_URI
        Content type namespace
        See Also:
        Constant Field Values
    • Constructor Detail

      • ContentTypeManager

        public ContentTypeManager​(java.io.InputStream in,
                                  OPCPackage pkg)
                           throws InvalidFormatException
        Constructor. Parses the content of the specified input stream.
        Parameters:
        in - If different of null then the content types part is retrieve and parse.
        Throws:
        InvalidFormatException - If the content types part content is not valid.
    • Method Detail

      • addContentType

        public void addContentType​(PackagePartName partName,
                                   java.lang.String contentType)
        Build association extention-> content type (will be stored in [Content_Types].xml) for example ContentType="image/png" Extension="png"

        [M2.8]: When adding a new part to a package, the package implementer shall ensure that a content type for that part is specified in the Content Types stream; the package implementer shall perform the steps described in §9.1.2.3:

        1. Get the extension from the part name by taking the substring to the right of the rightmost occurrence of the dot character (.) from the rightmost segment.

        2. If a part name has no extension, a corresponding Override element shall be added to the Content Types stream.

        3. Compare the resulting extension with the values specified for the Extension attributes of the Default elements in the Content Types stream. The comparison shall be case-insensitive ASCII.

        4. If there is a Default element with a matching Extension attribute, then the content type of the new part shall be compared with the value of the ContentType attribute. The comparison might be case-sensitive and include every character regardless of the role it plays in the content-type grammar of RFC 2616, or it might follow the grammar of RFC 2616.

        a. If the content types match, no further action is required.

        b. If the content types do not match, a new Override element shall be added to the Content Types stream. .

        5. If there is no Default element with a matching Extension attribute, a new Default element or Override element shall be added to the Content Types stream.

      • removeContentType

        public void removeContentType​(PackagePartName partName)
                               throws InvalidOperationException

        Delete a content type based on the specified part name. If the specified part name is register with an override content type, then this content type is remove, else the content type is remove in the default content type list if it exists and if no part is associated with it yet.

        Check rule M2.4: The package implementer shall require that the Content Types stream contain one of the following for every part in the package: One matching Default element One matching Override element Both a matching Default element and a matching Override element, in which case the Override element takes precedence.

        Parameters:
        partName - The part URI associated with the override content type to delete.
        Throws:
        InvalidOperationException - Throws if
      • isContentTypeRegister

        public boolean isContentTypeRegister​(java.lang.String contentType)
        Check if the specified content type is already register.
        Parameters:
        contentType - The content type to check.
        Returns:
        true if the specified content type is already register, then false.
      • getContentType

        public java.lang.String getContentType​(PackagePartName partName)
        Get the content type for the specified part, if any.

        Rule [M2.9]: To get the content type of a part, the package implementer shall perform the steps described in §9.1.2.4:

        1. Compare the part name with the values specified for the PartName attribute of the Override elements. The comparison shall be case-insensitive ASCII.

        2. If there is an Override element with a matching PartName attribute, return the value of its ContentType attribute. No further action is required.

        3. If there is no Override element with a matching PartName attribute, then a. Get the extension from the part name by taking the substring to the right of the rightmost occurrence of the dot character (.) from the rightmost segment. b. Check the Default elements of the Content Types stream, comparing the extension with the value of the Extension attribute. The comparison shall be case-insensitive ASCII.

        4. If there is a Default element with a matching Extension attribute, return the value of its ContentType attribute. No further action is required.

        5. If neither Override nor Default elements with matching attributes are found for the specified part name, the implementation shall not map this part name to a part.

        Parameters:
        partName - The URI part to check.
        Returns:
        The content type associated with the URI (in case of an override content type) or the extension (in case of default content type), else null.
        Throws:
        OpenXML4JRuntimeException - Throws if the content type manager is not able to find the content from an existing part.
      • clearAll

        public void clearAll()
        Clear all content types.
      • clearOverrideContentTypes

        public void clearOverrideContentTypes()
        Clear all override content types.
      • save

        public boolean save​(java.io.OutputStream outStream)
        Save the contents type part.
        Parameters:
        outStream - The output stream use to save the XML content of the content types part.
        Returns:
        true if the operation success, else false.
      • saveImpl

        public abstract boolean saveImpl​(org.w3c.dom.Document content,
                                         java.io.OutputStream out)
        Specific implementation of the save method. Call by the save() method, call before exiting.
        Parameters:
        out - The output stream use to write the content type XML.