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"
(建立关联扩展-> 内容类型(将存储在 [Content_Types].xml 中)例如 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.
(save方法的具体实现。)
|
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.
(in - 如果与 null 不同,则内容类型部分将被检索和解析。)
InvalidFormatException
- If the content types part content is not valid.
(InvalidFormatException - 如果内容类型部分内容无效。)
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.
(建立关联extension->内容类型(将存储在[Content_Types].xml中)例如ContentType="image/png" Extension="png" [M2.8]:当向包中添加新部分时,包实现者应确保该部分的内容类型在内容类型流中指定;包实现者应执行第 9.1.2.3 节中描述的步骤: 1. 通过从最右边的段中获取点字符 (.) 最右边出现的子字符串,从部件名称中获取扩展名。 2. 如果部分名称没有扩展名,则应将相应的 Override 元素添加到 Content Types 流中。 3. 将生成的扩展与为内容类型流中默认元素的扩展属性指定的值进行比较。比较应为不区分大小写的 ASCII。 4. 如果有一个 Default 元素具有匹配的 Extension 属性,则新部分的内容类型应与 ContentType 属性的值进行比较。比较可能区分大小写并包括每个字符,无论它在 RFC 2616 的内容类型语法中扮演什么角色,或者它可能遵循 RFC 2616 的语法。如果内容类型匹配,则无需进一步操作。湾。如果内容类型不匹配,则应将新的 Override 元素添加到 Content Types 流中。 . 5. 如果没有具有匹配扩展属性的默认元素,则应将新的默认元素或覆盖元素添加到内容类型流中。)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.
(根据指定的部件名称删除内容类型。如果指定的部分名称注册了覆盖内容类型,则此内容类型为删除,否则,如果该内容类型存在并且没有任何部分与之关联,则该内容类型在默认内容类型列表中被删除。检查规则 M2.4:包实现者应要求内容类型流包含包中每个部分的以下内容之一: 一个匹配的默认元素 一个匹配的覆盖元素 匹配的默认元素和匹配的覆盖元素,在这种情况下Override 元素优先。)partName
- The part URI associated with the override content type to delete.
(partName - 与要删除的覆盖内容类型关联的部分 URI。)
InvalidOperationException
- Throws if
(InvalidOperationException - 如果发生则抛出)
public boolean isContentTypeRegister(java.lang.String contentType)
contentType
- The content type to check.
(contentType - 要检查的内容类型。)
true
if the specified content type is already register, then
false
.
(如果指定的内容类型已经注册,则为 true,然后为 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.
(获取指定部分的内容类型(如果有)。规则 [M2.9]:要获取部件的内容类型,包实现者应执行第 9.1.2.4 节中描述的步骤: 1. 将部件名称与为 Override 元素的 PartName 属性指定的值进行比较。比较应为不区分大小写的 ASCII。 2. 如果存在具有匹配 PartName 属性的 Override 元素,则返回其 ContentType 属性的值。不需要采取进一步行动。 3. 如果不存在具有匹配 PartName 属性的 Override 元素,则 a。通过从最右边的段中获取最右边的点字符 (.) 的右侧的子字符串,从部件名称中获取扩展名。湾。检查 Content Types 流的 Default 元素,将扩展名与 Extension 属性的值进行比较。比较应为不区分大小写的 ASCII。 4. 如果存在具有匹配 Extension 属性的 Default 元素,则返回其 ContentType 属性的值。不需要采取进一步行动。 5. 如果没有为指定的部件名称找到具有匹配属性的 Override 和 Default 元素,则实现不应将此部件名称映射到部件。)partName
- The URI part to check.
(partName - 要检查的 URI 部分。)
null
.
(与 URI 关联的内容类型(在覆盖内容类型的情况下)或扩展名(在默认内容类型的情况下),否则为 null。)
OpenXML4JRuntimeException
- Throws if the content type manager is not able to find the content from an existing part.
(OpenXML4JRuntimeException - 如果内容类型管理器无法从现有部件中找到内容,则引发。)
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.
(outStream - 用于保存内容类型部分的 XML 内容的输出流。)
public abstract boolean saveImpl(org.w3c.dom.Document content, java.io.OutputStream out)
out
- The output stream use to write the content type XML.
(out - 用于写入内容类型 XML 的输出流。)
Copyright 2021 The Apache Software Foundation or its licensors, as applicable.