Package org.apache.poi.openxml4j.opc
Class OPCPackage
- java.lang.Object
-
- org.apache.poi.openxml4j.opc.OPCPackage
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,RelationshipSource
- Direct Known Subclasses:
ZipPackage
public abstract class OPCPackage extends java.lang.Object implements RelationshipSource, java.io.Closeable
Represents a container that can store multiple data objects.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description PackageRelationship
addExternalRelationship(java.lang.String target, java.lang.String relationshipType)
Adds an external relationship to a part (except relationships part).PackageRelationship
addExternalRelationship(java.lang.String target, java.lang.String relationshipType, java.lang.String id)
Adds an external relationship to a part (except relationships part).void
addMarshaller(java.lang.String contentType, PartMarshaller marshaller)
Add a marshaller.PackageRelationship
addRelationship(PackagePartName targetPartName, TargetMode targetMode, java.lang.String relationshipType)
Add a package relationship.PackageRelationship
addRelationship(PackagePartName targetPartName, TargetMode targetMode, java.lang.String relationshipType, java.lang.String relID)
Add a relationship to the package (except relationships part).void
addThumbnail(java.lang.String path)
Add a thumbnail to the package.void
addThumbnail(java.lang.String filename, java.io.InputStream data)
Add a thumbnail to the package.void
addUnmarshaller(java.lang.String contentType, PartUnmarshaller unmarshaller)
Add an unmarshaller.void
clearRelationships()
Clear package relationships.void
close()
Close the open, writable package and save its content.boolean
containPart(PackagePartName partName)
Check if a part already exists in this package from its name.static OPCPackage
create(java.io.File file)
Creates a new package.static OPCPackage
create(java.io.OutputStream output)
static OPCPackage
create(java.lang.String path)
Creates a new package.PackagePart
createPart(PackagePartName partName, java.lang.String contentType)
Create and add a part, with the specified name and content type, to the package.PackagePart
createPart(PackagePartName partName, java.lang.String contentType, java.io.ByteArrayOutputStream content)
Add a part to the package.void
deletePart(PackagePartName partName)
Delete the part with the specified name and its associated relationships part if one exists.void
deletePartRecursive(PackagePartName partName)
Delete the part with the specified name and all part listed in its associated relationships part if one exists.void
ensureRelationships()
Ensure that the relationships collection is not null.void
flush()
Flush the package : save all.PackageAccess
getPackageAccess()
Get the package access mode.PackageProperties
getPackageProperties()
Retrieves or creates if none exists, core package property part.PackagePart
getPart(PackagePartName partName)
Retrieve a part identified by its name.PackagePart
getPart(PackageRelationship partRel)
Get the target part from the specified relationship.java.util.ArrayList<PackagePart>
getParts()
Load the parts of the archive if it has not been done yet.java.util.ArrayList<PackagePart>
getPartsByContentType(java.lang.String contentType)
Retrieve parts by content type.java.util.List<PackagePart>
getPartsByName(java.util.regex.Pattern namePattern)
Retrieve parts by namejava.util.ArrayList<PackagePart>
getPartsByRelationshipType(java.lang.String relationshipType)
Retrieve parts by relationship type.PackageRelationship
getRelationship(java.lang.String id)
Retrieves a package relationship from its id.PackageRelationshipCollection
getRelationships()
Retrieves all package relationships.PackageRelationshipCollection
getRelationshipsByType(java.lang.String relationshipType)
Retrieves all relationships with the specified type.int
getUnusedPartIndex(java.lang.String nameTemplate)
Get an unused part index based on the namePattern, which doesn't exist yet and has the lowest positive indexboolean
hasRelationships()
Knows if the part have any relationships.abstract boolean
isClosed()
Has close been called already?boolean
isRelationshipExists(PackageRelationship rel)
Checks if the specified relationship is part of this package part.boolean
isStrictOoxmlFormat()
static OPCPackage
open(java.io.File file)
Open a package with read/write permission.static OPCPackage
open(java.io.File file, PackageAccess access)
Open a package.static OPCPackage
open(java.io.InputStream in)
Open a package.static OPCPackage
open(java.lang.String path)
Open a package with read/write permission.static OPCPackage
open(java.lang.String path, PackageAccess access)
Open a package.static OPCPackage
open(ZipEntrySource zipEntry)
Open an user providedZipEntrySource
with read-only permission.static OPCPackage
openOrCreate(java.io.File file)
Opens a package if it exists, else it creates one.void
registerPartAndContentType(PackagePart part)
Add the specified part, and register its content type with the content type manager.void
removeMarshaller(java.lang.String contentType)
Remove a marshaller by its content type.void
removePart(PackagePart part)
Remove the specified part in this package.void
removePart(PackagePartName partName)
Remove a part in this package.void
removePartRecursive(PackagePartName partName)
Remove a part from this package as well as its relationship part, if one exists, and all parts listed in the relationship part.void
removeRelationship(java.lang.String id)
Delete a relationship from this package.void
removeUnmarshaller(java.lang.String contentType)
Remove an unmarshaller by its content type.boolean
replaceContentType(java.lang.String oldContentType, java.lang.String newContentType)
Replace a content type in this package.void
revert()
Close the package WITHOUT saving its content.void
save(java.io.File targetFile)
Save the document in the specified file.void
save(java.io.OutputStream outputStream)
Save the document in the specified output stream.java.lang.String
toString()
void
unregisterPartAndContentType(PackagePartName partName)
Remove the specified part, and clear its content type from the content type manager.boolean
validatePackage(OPCPackage pkg)
Validates the package compliance with the OPC specifications.
-
-
-
Method Detail
-
open
public static OPCPackage open(java.lang.String path) throws InvalidFormatException
Open a package with read/write permission.- Parameters:
path
- The document path.- Returns:
- A Package object, else null.
- Throws:
InvalidFormatException
- If the specified file doesn't exist, and a parsing error occur.
-
open
public static OPCPackage open(java.io.File file) throws InvalidFormatException
Open a package with read/write permission.- Parameters:
file
- The file to open.- Returns:
- A Package object, else null.
- Throws:
InvalidFormatException
- If the specified file doesn't exist, and a parsing error occur.
-
open
public static OPCPackage open(ZipEntrySource zipEntry) throws InvalidFormatException
Open an user providedZipEntrySource
with read-only permission. This method can be used to stream data into POI. Opposed to other open variants, the data is read as-is, e.g. there aren't any zip-bomb protection put in place.- Parameters:
zipEntry
- the custom source- Returns:
- A Package object
- Throws:
InvalidFormatException
- if a parsing error occur.
-
open
public static OPCPackage open(java.lang.String path, PackageAccess access) throws InvalidFormatException, InvalidOperationException
Open a package.- Parameters:
path
- The document path.access
- PackageBase access.- Returns:
- A PackageBase object, else null.
- Throws:
InvalidFormatException
- If the specified file doesn't exist, and a parsing error occur.InvalidOperationException
- If the zip file cannot be opened.InvalidFormatException
- if the package is not valid.
-
open
public static OPCPackage open(java.io.File file, PackageAccess access) throws InvalidFormatException
Open a package.- Parameters:
file
- The file to open.access
- PackageBase access.- Returns:
- A PackageBase object, else null.
- Throws:
InvalidFormatException
- If the specified file doesn't exist, and a parsing error occur.
-
open
public static OPCPackage open(java.io.InputStream in) throws InvalidFormatException, java.io.IOException
Open a package. Note - uses quite a bit more memory thanopen(String)
, which doesn't need to hold the whole zip file in memory, and can take advantage of native methods- Parameters:
in
- The InputStream to read the package from- Returns:
- A PackageBase object
- Throws:
InvalidFormatException
- Throws if the specified file exist and is not valid.java.io.IOException
- If reading the stream fails
-
openOrCreate
public static OPCPackage openOrCreate(java.io.File file) throws InvalidFormatException
Opens a package if it exists, else it creates one.- Parameters:
file
- The file to open or to create.- Returns:
- A newly created package if the specified file does not exist, else the package extract from the file.
- Throws:
InvalidFormatException
- Throws if the specified file exist and is not valid.
-
create
public static OPCPackage create(java.lang.String path)
Creates a new package.- Parameters:
path
- Path of the document.- Returns:
- A newly created PackageBase ready to use.
-
create
public static OPCPackage create(java.io.File file)
Creates a new package.- Parameters:
file
- Path of the document.- Returns:
- A newly created PackageBase ready to use.
-
create
public static OPCPackage create(java.io.OutputStream output)
-
flush
public void flush()
Flush the package : save all.- See Also:
close()
-
close
public void close() throws java.io.IOException
Close the open, writable package and save its content. If your package is open read only, then you should callrevert()
when finished with the package. This method is not thread-safe.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Throws:
java.io.IOException
- If an IO exception occur during the saving process.
-
revert
public void revert()
Close the package WITHOUT saving its content. Reinitialize this package and cancel all changes done to it.
-
addThumbnail
public void addThumbnail(java.lang.String path) throws java.io.IOException
Add a thumbnail to the package. This method is provided to make easier the addition of a thumbnail in a package. You can do the same work by using the traditionnal relationship and part mechanism.- Parameters:
path
- The full path to the image file.- Throws:
java.io.IOException
-
addThumbnail
public void addThumbnail(java.lang.String filename, java.io.InputStream data) throws java.io.IOException
Add a thumbnail to the package. This method is provided to make easier the addition of a thumbnail in a package. You can do the same work by using the traditionnal relationship and part mechanism.- Parameters:
filename
- The full path to the image file.data
- the image data- Throws:
java.io.IOException
-
getPackageProperties
public PackageProperties getPackageProperties() throws InvalidFormatException
Retrieves or creates if none exists, core package property part.- Returns:
- The PackageProperties part of this package.
- Throws:
InvalidFormatException
-
getPart
public PackagePart getPart(PackagePartName partName)
Retrieve a part identified by its name.- Parameters:
partName
- Part name of the part to retrieve.- Returns:
- The part with the specified name, else
null
.
-
getPartsByContentType
public java.util.ArrayList<PackagePart> getPartsByContentType(java.lang.String contentType)
Retrieve parts by content type.- Parameters:
contentType
- The content type criteria.- Returns:
- All part associated to the specified content type.
-
getPartsByRelationshipType
public java.util.ArrayList<PackagePart> getPartsByRelationshipType(java.lang.String relationshipType)
Retrieve parts by relationship type.- Parameters:
relationshipType
- Relationship type. Must not benull
.- Returns:
- All parts which are the target of a relationship with the specified type. If no such parts are found, the list is empty.
- Throws:
InvalidOperationException
- If called on a write-only package.java.lang.IllegalArgumentException
- if relationshipType input param is null.
-
getPartsByName
public java.util.List<PackagePart> getPartsByName(java.util.regex.Pattern namePattern)
Retrieve parts by name- Parameters:
namePattern
- The pattern for matching the names- Returns:
- All parts associated to the specified content type, sorted in alphanumerically by the part-name
-
getPart
public PackagePart getPart(PackageRelationship partRel)
Get the target part from the specified relationship.- Parameters:
partRel
- The part relationship uses to retrieve the part.
-
getParts
public java.util.ArrayList<PackagePart> getParts() throws InvalidFormatException
Load the parts of the archive if it has not been done yet. The relationships of each part are not loaded. Note - Rule M4.1 states that there may only ever be one Core Properties Part, but Office produced files will sometimes have multiple! As Office ignores all but the first, we relax Compliance with Rule M4.1, and ignore all others silently too.- Returns:
- All this package's parts.
- Throws:
InvalidFormatException
- if the package is not valid.
-
createPart
public PackagePart createPart(PackagePartName partName, java.lang.String contentType)
Create and add a part, with the specified name and content type, to the package.- Parameters:
partName
- Part name.contentType
- Part content type.- Returns:
- The newly created part.
- Throws:
PartAlreadyExistsException
- If rule M1.12 is not verified : Packages shall not contain equivalent part names and package implementers shall neither create nor recognize packages with equivalent part names.- See Also:
createPartImpl(PackagePartName, String, boolean)
-
createPart
public PackagePart createPart(PackagePartName partName, java.lang.String contentType, java.io.ByteArrayOutputStream content)
Add a part to the package.- Parameters:
partName
- Part name of the part to create.contentType
- type associated with the filecontent
- the contents to add. In order to have faster operation in document merge, the data are stored in memory not on a hard disk- Returns:
- The new part.
- See Also:
createPart(PackagePartName, String)
-
removePart
public void removePart(PackagePart part)
Remove the specified part in this package. If this part is relationship part, then delete all relationships in the source part.- Parameters:
part
- The part to remove. Ifnull
, skip the action.- See Also:
removePart(PackagePartName)
-
removePart
public void removePart(PackagePartName partName)
Remove a part in this package. If this part is relationship part, then delete all relationships in the source part.- Parameters:
partName
- The part name of the part to remove.
-
removePartRecursive
public void removePartRecursive(PackagePartName partName) throws InvalidFormatException
Remove a part from this package as well as its relationship part, if one exists, and all parts listed in the relationship part. Be aware that this do not delete relationships which target the specified part.- Parameters:
partName
- The name of the part to delete.- Throws:
InvalidFormatException
- Throws if the associated relationship part of the specified part is not valid.
-
deletePart
public void deletePart(PackagePartName partName)
Delete the part with the specified name and its associated relationships part if one exists. Prefer the use of this method to delete a part in the package, compare to the remove() methods that don't remove associated relationships part.- Parameters:
partName
- Name of the part to delete
-
deletePartRecursive
public void deletePartRecursive(PackagePartName partName)
Delete the part with the specified name and all part listed in its associated relationships part if one exists. This process is recursively apply to all parts in the relationships part of the specified part. Prefer the use of this method to delete a part in the package, compare to the remove() methods that don't remove associated relationships part.- Parameters:
partName
- Name of the part to delete
-
containPart
public boolean containPart(PackagePartName partName)
Check if a part already exists in this package from its name.- Parameters:
partName
- Part name to check.- Returns:
- true if the part is logically added to this package, else false.
-
addRelationship
public PackageRelationship addRelationship(PackagePartName targetPartName, TargetMode targetMode, java.lang.String relationshipType, java.lang.String relID)
Add a relationship to the package (except relationships part). Check rule M4.1 : The format designer shall specify and the format producer shall create at most one core properties relationship for a package. A format consumer shall consider more than one core properties relationship for a package to be an error. If present, the relationship shall target the Core Properties 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 interfaceRelationshipSource
- Parameters:
targetPartName
- Target part name.targetMode
- Target mode, either Internal or External.relationshipType
- Relationship type.relID
- ID of the relationship.- Returns:
- The newly created and added relationship
- See Also:
PackageRelationshipTypes
-
addRelationship
public PackageRelationship addRelationship(PackagePartName targetPartName, TargetMode targetMode, java.lang.String relationshipType)
Add a package relationship.- Specified by:
addRelationship
in interfaceRelationshipSource
- Parameters:
targetPartName
- Target part name.targetMode
- Target mode, either Internal or External.relationshipType
- Relationship type.- Returns:
- The newly created and added relationship
- See Also:
PackageRelationshipTypes
-
addExternalRelationship
public PackageRelationship addExternalRelationship(java.lang.String target, java.lang.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 interfaceRelationshipSource
- Parameters:
target
- External target of the relationshiprelationshipType
- Type of relationship.- Returns:
- The newly created and added relationship
-
addExternalRelationship
public PackageRelationship addExternalRelationship(java.lang.String target, java.lang.String relationshipType, java.lang.String id)
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 interfaceRelationshipSource
- Parameters:
target
- External target of the relationshiprelationshipType
- Type of relationship.id
- Relationship unique id.- Returns:
- The newly created and added relationship
- See Also:
RelationshipSource.addExternalRelationship(String, String)
-
removeRelationship
public void removeRelationship(java.lang.String id)
Delete a relationship from this package.- Specified by:
removeRelationship
in interfaceRelationshipSource
- Parameters:
id
- Id of the relationship to delete.
-
getRelationships
public PackageRelationshipCollection getRelationships()
Retrieves all package relationships.- Specified by:
getRelationships
in interfaceRelationshipSource
- Returns:
- All package relationships of this package.
- Throws:
InvalidOperationException
- if a read operation is done on a write only package.- See Also:
getRelationshipsHelper(String)
-
getRelationshipsByType
public PackageRelationshipCollection getRelationshipsByType(java.lang.String relationshipType)
Retrieves all relationships with the specified type.- Specified by:
getRelationshipsByType
in interfaceRelationshipSource
- Parameters:
relationshipType
- The filter specifying the relationship type.- Returns:
- All relationships with the specified relationship type.
-
clearRelationships
public void clearRelationships()
Clear package relationships.- Specified by:
clearRelationships
in interfaceRelationshipSource
-
ensureRelationships
public void ensureRelationships()
Ensure that the relationships collection is not null.
-
getRelationship
public PackageRelationship getRelationship(java.lang.String id)
Description copied from interface:RelationshipSource
Retrieves a package relationship from its id.- Specified by:
getRelationship
in interfaceRelationshipSource
- Parameters:
id
- ID of the package relationship to retrieve.- Returns:
- The package relationship
-
hasRelationships
public boolean hasRelationships()
Description copied from interface:RelationshipSource
Knows if the part have any relationships.- Specified by:
hasRelationships
in interfaceRelationshipSource
- Returns:
- true if the part have at least one relationship else false.
-
isRelationshipExists
public boolean isRelationshipExists(PackageRelationship rel)
Description copied from interface:RelationshipSource
Checks if the specified relationship is part of this package part.- Specified by:
isRelationshipExists
in interfaceRelationshipSource
- Parameters:
rel
- The relationship to check.- Returns:
- true if the specified relationship exists in this part, else returns false
-
addMarshaller
public void addMarshaller(java.lang.String contentType, PartMarshaller marshaller)
Add a marshaller.- Parameters:
contentType
- The content type to bind to the specified marshaller.marshaller
- The marshaller to register with the specified content type.
-
addUnmarshaller
public void addUnmarshaller(java.lang.String contentType, PartUnmarshaller unmarshaller)
Add an unmarshaller.- Parameters:
contentType
- The content type to bind to the specified unmarshaller.unmarshaller
- The unmarshaller to register with the specified content type.
-
removeMarshaller
public void removeMarshaller(java.lang.String contentType)
Remove a marshaller by its content type.- Parameters:
contentType
- The content type associated with the marshaller to remove.
-
removeUnmarshaller
public void removeUnmarshaller(java.lang.String contentType)
Remove an unmarshaller by its content type.- Parameters:
contentType
- The content type associated with the unmarshaller to remove.
-
getPackageAccess
public PackageAccess getPackageAccess()
Get the package access mode.- Returns:
- the packageAccess The current package access.
-
validatePackage
@NotImplemented public boolean validatePackage(OPCPackage pkg) throws InvalidFormatException
Validates the package compliance with the OPC specifications.- Returns:
- true if the package is valid else false
- Throws:
InvalidFormatException
-
save
public void save(java.io.File targetFile) throws java.io.IOException
Save the document in the specified file.- Parameters:
targetFile
- Destination file.- Throws:
java.io.IOException
- Throws if an IO exception occur.- See Also:
save(OutputStream)
-
save
public void save(java.io.OutputStream outputStream) throws java.io.IOException
Save the document in the specified output stream.- Parameters:
outputStream
- The stream to save the package.- Throws:
java.io.IOException
- See Also:
saveImpl(OutputStream)
-
replaceContentType
public boolean replaceContentType(java.lang.String oldContentType, java.lang.String newContentType)
Replace a content type in this package.A typical scneario to call this method is to rename a template file to the main format, e.g.
- ".dotx" to ".docx"
- ".dotm" to ".docm"
- ".xltx" to ".xlsx"
- ".xltm" to ".xlsm"
- ".potx" to ".pptx"
- ".potm" to ".pptm"
OPCPackage pkg = OPCPackage.open(new FileInputStream("macro-workbook.xlsm")); pkg.replaceContentType( "application/vnd.ms-excel.sheet.macroEnabled.main+xml", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"); try (FileOutputStream out = new FileOutputStream("workbook.xlsx")) { pkg.save(out); }
- Parameters:
oldContentType
- the content type to be replacednewContentType
- the replacement- Returns:
- whether replacement was successful
- Since:
- POI-3.8
-
registerPartAndContentType
public void registerPartAndContentType(PackagePart part)
Add the specified part, and register its content type with the content type manager.- Parameters:
part
- The part to add.
-
unregisterPartAndContentType
public void unregisterPartAndContentType(PackagePartName partName)
Remove the specified part, and clear its content type from the content type manager.- Parameters:
partName
- The part name of the part to remove.
-
getUnusedPartIndex
public int getUnusedPartIndex(java.lang.String nameTemplate) throws InvalidFormatException
Get an unused part index based on the namePattern, which doesn't exist yet and has the lowest positive index- Parameters:
nameTemplate
- The template for new part names containing a'#'
for the index, e.g. "/ppt/slides/slide#.xml"- Returns:
- the next available part name index
- Throws:
InvalidFormatException
- if the nameTemplate is null or doesn't contain the index char (#) or results in an invalid part name
-
isStrictOoxmlFormat
public boolean isStrictOoxmlFormat()
- Returns:
- true if the package is in Strict OOXML format
- Since:
- POI 5.1.0
-
isClosed
public abstract boolean isClosed()
Has close been called already?
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-