public abstract class ContentTypeManager
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
protected OPCPackage |
container
Reference to the package using this content type manager.
|
static java.lang.String |
CONTENT_TYPES_PART_NAME
Content type part name.
|
static java.lang.String |
TYPES_NAMESPACE_URI
Content type namespace
|
Constructor and Description |
---|
ContentTypeManager(java.io.InputStream in,
OPCPackage pkg)
Constructor.
|
Modifier and Type | Method and 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.
|
public static final java.lang.String CONTENT_TYPES_PART_NAME
public static final java.lang.String TYPES_NAMESPACE_URI
protected OPCPackage container
public ContentTypeManager(java.io.InputStream in, OPCPackage pkg) throws InvalidFormatException
in
- If different of null then the content types part is
retrieve and parse.InvalidFormatException
- If the content types part content is not valid.public void addContentType(PackagePartName partName, java.lang.String contentType)
[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.
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.
partName
- The part URI associated with the override content type to
delete.InvalidOperationException
- Throws ifpublic boolean isContentTypeRegister(java.lang.String contentType)
contentType
- The content type to check.true
if the specified content type is already
register, then false
.public java.lang.String getContentType(PackagePartName partName)
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.
partName
- The URI part to check.null
.OpenXML4JRuntimeException
- Throws if the content type manager is not able to find the
content from an existing part.public void clearAll()
public void clearOverrideContentTypes()
public boolean save(java.io.OutputStream outStream)
outStream
- The output stream use to save the XML content of the content
types part.public abstract boolean saveImpl(org.w3c.dom.Document content, java.io.OutputStream out)
out
- The output stream use to write the content type XML.Copyright 2021 The Apache Software Foundation or its licensors, as applicable.