Class OpcPackage

java.lang.Object
org.docx4j.openpackaging.Base
org.docx4j.openpackaging.packages.OpcPackage
All Implemented Interfaces:
PackageIdentifier
Direct Known Subclasses:
DefaultPackage, GloxPackage, PresentationMLPackage, SpreadsheetMLPackage, WordprocessingMLPackage

public abstract class OpcPackage extends Base implements PackageIdentifier
Represent a Package as defined in the Open Packaging Specification.
Author:
Jason Harrop
  • Field Details

    • handled

      public HashMap<String,String> handled
      This HashMap is intended to prevent loops during the loading of this package. TODO This doesn't really tell us anything that the contents of Parts couldn't also tell us (except that that doesn't contain the rels parts), so consider removing. At least replace it with a method, so this implementation detail is hidden!
    • parts

      protected Parts parts
      Package parts collection. This is a collection of _all_ parts in the package (_except_ relationship parts), not just those referred to by the package-level relationships. It doesn't include external resources.
    • externalResources

      protected HashMap<ExternalTarget,Part> externalResources
    • customXmlDataStorageParts

      protected HashMap<String,CustomXmlPart> customXmlDataStorageParts
    • contentTypeManager

      protected ContentTypeManager contentTypeManager
    • docPropsCorePart

      protected DocPropsCorePart docPropsCorePart
    • docPropsExtendedPart

      protected DocPropsExtendedPart docPropsExtendedPart
    • docPropsCustomPart

      protected DocPropsCustomPart docPropsCustomPart
  • Constructor Details

    • OpcPackage

      public OpcPackage()
      Constructor. Also creates a new content type manager
    • OpcPackage

      public OpcPackage(ContentTypeManager contentTypeManager)
      Constructor.
      Parameters:
      contentTypeManager - The content type manager to use
  • Method Details

    • getParts

      public Parts getParts()
      Retrieve the Parts object.
    • getDrawingPropsIdTracker

      public DrawingPropsIdTracker getDrawingPropsIdTracker()
    • getExternalResources

      public HashMap<ExternalTarget,Part> getExternalResources()
    • getCustomXmlDataStorageParts

      public HashMap<String,CustomXmlPart> getCustomXmlDataStorageParts()
      keyed by item id (in lower case)
      Returns:
    • getContentTypeManager

      public ContentTypeManager getContentTypeManager()
    • setContentTypeManager

      public void setContentTypeManager(ContentTypeManager contentTypeManager)
    • getSourcePartStore

      public PartStore getSourcePartStore()
      Returns:
      the partStore
      Since:
      3.0.
    • setSourcePartStore

      public void setSourcePartStore(PartStore partStore)
      Parameters:
      partStore - the partStore to set
      Since:
      3.0.
    • getTargetPartStore

      public PartStore getTargetPartStore()
      Returns:
      the partStore
      Since:
      3.0.
    • setTargetPartStore

      public void setTargetPartStore(PartStore partStore)
      Parameters:
      partStore - the partStore to set
      Since:
      3.0.
    • getPackage

      public OpcPackage getPackage()
      Specified by:
      getPackage in class Base
    • isNew

      public boolean isNew()
      Was this pkg created from scratch in docx4j, or loaded?
      Returns:
    • setNew

      public void setNew(boolean isNew)
    • load

      public static OpcPackage load(PackageIdentifier pkgIdentifier, File docxFile) throws Docx4JException
      Convenience method to create a WordprocessingMLPackage or PresentationMLPackage from an existing File (.docx/.docxm, .ppxtx or Flat OPC .xml).
      Parameters:
      docxFile - The docx file
      Throws:
      Docx4JException
      Since:
      3.1.0
    • load

      public static OpcPackage load(File docxFile) throws Docx4JException
      Convenience method to create a WordprocessingMLPackage or PresentationMLPackage from an existing File (.docx/.docxm, .ppxtx or Flat OPC .xml).
      Parameters:
      docxFile - The docx file
      Throws:
      Docx4JException
    • load

      public static OpcPackage load(File docxFile, String password) throws Docx4JException
      Convenience method to create a WordprocessingMLPackage or PresentationMLPackage from an existing File (.docx/.docxm, .ppxtx or Flat OPC .xml).
      Parameters:
      docxFile - The docx file
      password - The password, if the file is password protected (compound)
      Throws:
      Docx4JException
    • load

      public static OpcPackage load(PackageIdentifier pkgIdentifier, File docxFile, String password) throws Docx4JException
      Convenience method to create a WordprocessingMLPackage or PresentationMLPackage from an existing File (.docx/.docxm, .ppxtx or Flat OPC .xml).
      Parameters:
      docxFile - The docx file
      password - The password, if the file is password protected (compound)
      Throws:
      Docx4JException
    • load

      public static OpcPackage load(InputStream inputStream) throws Docx4JException
      Convenience method to create a WordprocessingMLPackage or PresentationMLPackage from an inputstream (.docx/.docxm, .ppxtx or Flat OPC .xml). It detects the convenient format inspecting two first bytes of stream (magic bytes). For office 2007 'x' formats, these two bytes are 'PK' (same as zip file)
      Parameters:
      inputStream - The docx file
      Throws:
      Docx4JException
    • load

      public static OpcPackage load(PackageIdentifier pkgIdentifier, InputStream inputStream) throws Docx4JException
      Convenience method to create a WordprocessingMLPackage or PresentationMLPackage from an inputstream (.docx/.docxm, .ppxtx or Flat OPC .xml). It detects the convenient format inspecting two first bytes of stream (magic bytes). For office 2007 'x' formats, these two bytes are 'PK' (same as zip file)
      Parameters:
      inputStream - The docx file
      Throws:
      Docx4JException
      Since:
      3.1.0
    • load

      public static OpcPackage load(InputStream inputStream, String password) throws Docx4JException
      Convenience method to create a WordprocessingMLPackage or PresentationMLPackage from an inputstream (.docx/.docxm, .ppxtx or Flat OPC .xml). It detects the convenient format inspecting two first bytes of stream (magic bytes). For office 2007 'x' formats, these two bytes are 'PK' (same as zip file)
      Parameters:
      inputStream - The docx file
      password - The password, if the file is password protected (compound)
      Throws:
      Docx4JException
    • load

      @Deprecated public static OpcPackage load(InputStream is, boolean docxFormat) throws Docx4JException
      Deprecated.
      convenience method to load a word2007 document from an existing inputstream (.docx/.docxm, .ppxtx or Flat OPC .xml). Included for backwards compatibility
      Parameters:
      is -
      docxFormat -
      Returns:
      Throws:
      Docx4JException
    • load

      public static OpcPackage load(InputStream is, Filetype type) throws Docx4JException
      convenience method to load a word2007 document from an existing inputstream (.docx/.docxm, .ppxtx or Flat OPC .xml).
      Parameters:
      is -
      docxFormat -
      Returns:
      Throws:
      Docx4JException
    • load

      public static OpcPackage load(InputStream is, Filetype type, String password) throws Docx4JException
      convenience method to load a word2007 document from an existing inputstream (.docx/.docxm, .ppxtx or Flat OPC .xml).
      Parameters:
      is -
      docxFormat -
      Returns:
      Throws:
      Docx4JException
    • load

      public static OpcPackage load(File file, Filetype type) throws Docx4JException
      convenience method to load from a file, where you know the Filetype (if it is Filetype.ZippedPackage, ZipFile will be used instead of ZipArchiveInputStream)
      Parameters:
      is -
      docxFormat -
      Returns:
      Throws:
      Docx4JException
    • load

      public static OpcPackage load(File file, Filetype type, String password) throws Docx4JException
      convenience method to load from a file, where you know the Filetype (if it is Filetype.ZippedPackage, ZipFile will be used instead of ZipArchiveInputStream)
      Parameters:
      is -
      docxFormat -
      Returns:
      Throws:
      Docx4JException
    • save

      public void save(File file) throws Docx4JException
      Convenience method to save a WordprocessingMLPackage or PresentationMLPackage to a File. If the file ends with .xml, use Flat OPC XML format; otherwise zip it up.
      Throws:
      Docx4JException
    • save

      public void save(File outFile, int flags) throws Docx4JException
      Save this pkg to a File. The flag is typically Docx4J.FLAG_SAVE_ZIP_FILE or Docx4J.FLAG_SAVE_FLAT_XML
      Throws:
      Docx4JException
      Since:
      3.1.0
    • save

      public void save(File outFile, int flags, String password) throws Docx4JException
      Save this pkg to a file. The flag is typically Docx4J.FLAG_SAVE_ZIP_FILE or Docx4J.FLAG_SAVE_FLAT_XML or one of the Docx4J.FLAG_SAVE_ENCRYPTED_ variants (recommend FLAG_SAVE_ENCRYPTED_AGILE) For the FLAG_SAVE_ENCRYPTED_ variants, you need to provide a password. *
      Throws:
      Docx4JException
      Since:
      3.3.0
    • save

      public void save(OutputStream outStream) throws Docx4JException
      Save this pkg to an OutputStream in the usual zipped up format (Docx4J.FLAG_SAVE_ZIP_FILE)
      Throws:
      Docx4JException
      Since:
      3.1.0
    • save

      public void save(OutputStream outStream, int flags) throws Docx4JException
      Save this pkg to an OutputStream. The flag is typically Docx4J.FLAG_SAVE_ZIP_FILE or Docx4J.FLAG_SAVE_FLAT_XML
      Throws:
      Docx4JException
      Since:
      3.1.0
    • save

      public void save(OutputStream outStream, int flags, String password) throws Docx4JException
      Save this pkg to an OutputStream. The flag is typically Docx4J.FLAG_SAVE_ZIP_FILE or Docx4J.FLAG_SAVE_FLAT_XML or one of the Docx4J.FLAG_SAVE_ENCRYPTED_ variants (recommend FLAG_SAVE_ENCRYPTED_AGILE) For the FLAG_SAVE_ENCRYPTED_ variants, you need to provide a password.
      Throws:
      Docx4JException
      Since:
      3.3.0
    • setPartShortcut

      public boolean setPartShortcut(Part part, String relationshipType)
      Description copied from class: Base
      Tell the source Part about this target Part, so the source can use this part via a convenience method.
      Specified by:
      setPartShortcut in class Base
      Returns:
      true if the source Part does use the target Part in a convenience method.
    • getDocPropsCorePart

      public DocPropsCorePart getDocPropsCorePart()
      Get the DocPropsCorePart, if any.
      Returns:
    • addDocPropsCorePart

      public void addDocPropsCorePart()
    • getDocPropsExtendedPart

      public DocPropsExtendedPart getDocPropsExtendedPart()
      Get the DocPropsExtendedPart, if any.
      Returns:
    • addDocPropsExtendedPart

      public void addDocPropsExtendedPart()
    • getDocPropsCustomPart

      public DocPropsCustomPart getDocPropsCustomPart()
      Get DocPropsCustomPart, if any.
      Returns:
    • addDocPropsCustomPart

      public void addDocPropsCustomPart()
    • setTitle

      public void setTitle(String title)
      Since:
      3.0.0
    • getTitle

      public String getTitle()
      Since:
      3.0.0
    • clone

      public OpcPackage clone()
      Overrides:
      clone in class Object
      Since:
      2.7.2
    • cloneAs

      public OpcPackage cloneAs(String targetContentType) throws Docx4JException
      Returns:
      Throws:
      Docx4JException
      Since:
      6.1.0
    • name

      public String name()
      Specified by:
      name in interface PackageIdentifier
    • setName

      public void setName(String name)
      Allocate a name to this package, for the purposes of Docx4jEvent, and logging.
    • reset

      public void reset()
      Reinit fields so this pkg object can be re-used.
      Overrides:
      reset in class Base
      Since:
      3.3.7