org.odftoolkit.odfdom.pkg
Class OdfPackage

java.lang.Object
  extended by org.odftoolkit.odfdom.pkg.OdfPackage

public class OdfPackage
extends java.lang.Object

OdfPackage represents the package view to an OpenDocument document. The OdfPackage will be created from an ODF document and represents a copy of the loaded document, where files can be inserted and deleted. The changes take effect, when the OdfPackage is being made persistend by save().


Nested Class Summary
static class OdfPackage.OdfFile
           
 
Method Summary
 void close()
          Close the OdfPackage after it is no longer needed.
 boolean contains(java.lang.String packagePath)
          Check existence of a file in the package.
 java.lang.String getBaseURI()
          Get the URI, where this ODF package is stored.
 byte[] getBytes(java.lang.String packagePath)
          Get package (sub-) content as byte array
 org.w3c.dom.Document getDom(java.lang.String packagePath)
          Gets org.w3c.dom.Document for XML file contained in package.
 org.xml.sax.EntityResolver getEntityResolver()
          Get EntityResolver to be used in XML Parsers which can resolve content inside the OdfPackage
 java.util.Set<java.lang.String> getFileEntries()
          Get a OdfFileEntries from the manifest file (i.e.
 OdfFileEntry getFileEntry(java.lang.String packagePath)
          Get an OdfFileEntry for the packagePath NOTE: This method should be better moved to a DOM inherited Manifest class
 java.io.InputStream getInputStream()
          Gets the InputStream containing whole OdfPackage.
 java.io.InputStream getInputStream(java.lang.String packagePath)
          Get subcontent as InputStream
 java.lang.String getManifestAsString()
          Get Manifest as String NOTE: This functionality should better be moved to a DOM based Manifest class
 java.util.HashMap<java.lang.String,OdfFileEntry> getManifestEntries()
           
 java.lang.String getMediaType()
          Get the media type of the ODF package (equal to media type of ODF root document)
 javax.xml.transform.URIResolver getURIResolver()
          Get URIResolver to be used in XSL Transformations which can resolve content inside the OdfPackage
 boolean hasDom(java.lang.String packagePath)
          returns true if a DOM tree has been requested for given sub-content of OdfPackage
 void insert(byte[] fileBytes, java.lang.String packagePath, java.lang.String mediaType)
          Insert byte array into OdfPackage.
 void insert(org.w3c.dom.Document fileDOM, java.lang.String packagePath, java.lang.String mediaType)
          Insert DOM tree into OdfPackage.
 void insert(java.io.InputStream fileStream, java.lang.String packagePath, java.lang.String mediaType)
          Inserts InputStream into an OdfPackage.
 void insert(java.net.URI sourceURI, java.lang.String packagePath, java.lang.String mediaType)
          Inserts InputStream into an OdfPackage.
 java.io.OutputStream insertOutputStream(java.lang.String packagePath)
          Insert the OutputStream for into OdfPackage.
 java.io.OutputStream insertOutputStream(java.lang.String packagePath, java.lang.String mediaType)
          Insert the OutputStream - to be filled after method - when stream is closed into OdfPackage.
static boolean isExternalReference(java.lang.String fileRef)
          Checks if the given reference is a reference, which points outside the ODF package
static OdfPackage loadPackage(java.io.File odfFile)
          Loads an OdfPackage from the OpenDocument provided by a File.
static OdfPackage loadPackage(java.io.InputStream odfStream)
          Creates an OdfPackage from the OpenDocument provided by a InputStream.
static OdfPackage loadPackage(java.lang.String odfPath)
          Loads an OdfPackage from the given filePath.
 void remove(java.lang.String packagePath)
           
 void save(java.io.File odfFile)
          Save package to a given File.
 void save(java.io.OutputStream odfStream)
           
 void save(java.lang.String odfPath)
          Save the package to given filePath.
 void setMediaType(java.lang.String mediaType)
          Set the media type of the ODF package (equal to media type of ODF root document)
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

loadPackage

public static OdfPackage loadPackage(java.lang.String odfPath)
                              throws java.lang.Exception
Loads an OdfPackage from the given filePath.

OdfPackage relies on the file being available for read access over the whole lifecycle of OdfPackage.

Parameters:
odfPath - - the filePath to the ODF package
Returns:
the OpenDocument document represented as an OdfPackage
Throws:
java.lang.Exception - - if the package could not be loaded

loadPackage

public static OdfPackage loadPackage(java.io.File odfFile)
                              throws java.lang.Exception
Loads an OdfPackage from the OpenDocument provided by a File.

OdfPackage relies on the file being available for read access over the whole lifecycle of OdfPackage.

Parameters:
odfFile - - a File to loadPackage content from
Returns:
the OpenDocument document represented as an OdfPackage
Throws:
java.lang.Exception - - if the package could not be loaded

loadPackage

public static OdfPackage loadPackage(java.io.InputStream odfStream)
                              throws java.lang.Exception
Creates an OdfPackage from the OpenDocument provided by a InputStream.

Since an InputStream does not provide the arbitrary (non sequentiell) read access needed by OdfPackage, the InputStream is cached. This usually takes more time compared to the other loadPackage methods.

Parameters:
odfStream - - an inputStream representing the ODF package
Returns:
the OpenDocument document represented as an OdfPackage
Throws:
java.lang.Exception - - if the package could not be loaded

getBaseURI

public java.lang.String getBaseURI()
Get the URI, where this ODF package is stored.

Returns:
the URI to the ODF package. Returns null if package is not stored yet.

getMediaType

public java.lang.String getMediaType()
Get the media type of the ODF package (equal to media type of ODF root document)

Returns:
the mediaType string of this ODF package

setMediaType

public void setMediaType(java.lang.String mediaType)
Set the media type of the ODF package (equal to media type of ODF root document)

Parameters:
mediaType - string of this ODF package

getFileEntry

public OdfFileEntry getFileEntry(java.lang.String packagePath)
Get an OdfFileEntry for the packagePath NOTE: This method should be better moved to a DOM inherited Manifest class

Parameters:
packagePath - The relative package path within the ODF package
Returns:
The manifest file entry will be returned.

getFileEntries

public java.util.Set<java.lang.String> getFileEntries()
Get a OdfFileEntries from the manifest file (i.e. /META/manifest.xml")

Returns:
The manifest file entries will be returned.

contains

public boolean contains(java.lang.String packagePath)
Check existence of a file in the package.

Parameters:
packagePath - The relative package filePath within the ODF package
Returns:
True if there is an entry and a file for the given filePath

save

public void save(java.lang.String odfPath)
          throws java.lang.Exception
Save the package to given filePath.

Parameters:
odfPath - - the path to the ODF package destination
Throws:
java.lang.Exception - - if the package could not be saved

save

public void save(java.io.File odfFile)
          throws java.lang.Exception
Save package to a given File. After saving it is still necessary to close the package to have again full access about the file.

Parameters:
odfFile - - the File to save the ODF package to
Throws:
java.lang.Exception - - if the package could not be saved

save

public void save(java.io.OutputStream odfStream)
          throws java.lang.Exception
Throws:
java.lang.Exception

close

public void close()
Close the OdfPackage after it is no longer needed. Even after saving it is still necessary to close the package to have again full access about the file. Closing the OdfPackage will release all temporary created data. Do this as the last action to free resources. Closing an already closed document has no effect.


insert

public void insert(org.w3c.dom.Document fileDOM,
                   java.lang.String packagePath,
                   java.lang.String mediaType)
            throws java.lang.Exception
Insert DOM tree into OdfPackage. An existing file will be replaced.

Parameters:
fileDOM - - XML DOM tree to be inserted as file
packagePath - - relative filePath where the DOM tree should be inserted as XML file
mediaType - - media type of stream. Set to null if unknown
Throws:
java.lang.Exception - when the DOM tree could not be inserted

hasDom

public boolean hasDom(java.lang.String packagePath)
returns true if a DOM tree has been requested for given sub-content of OdfPackage

Parameters:
packagePath - - a path inside the OdfPackage eg to a content.xml stream
Returns:
- wether the package class internally has a DOM representation for the given path

getDom

public org.w3c.dom.Document getDom(java.lang.String packagePath)
                            throws org.xml.sax.SAXException,
                                   javax.xml.parsers.ParserConfigurationException,
                                   java.lang.Exception,
                                   java.lang.IllegalArgumentException,
                                   javax.xml.transform.TransformerConfigurationException,
                                   javax.xml.transform.TransformerException
Gets org.w3c.dom.Document for XML file contained in package.

Parameters:
packagePath - - a path inside the OdfPackage eg to a content.xml stream
Returns:
an org.w3c.dom.Document
Throws:
org.xml.sax.SAXException
javax.xml.parsers.ParserConfigurationException
java.lang.Exception
java.lang.IllegalArgumentException
javax.xml.transform.TransformerConfigurationException
javax.xml.transform.TransformerException

insert

public void insert(java.net.URI sourceURI,
                   java.lang.String packagePath,
                   java.lang.String mediaType)
            throws java.lang.Exception
Inserts InputStream into an OdfPackage. An existing file will be replaced.

Parameters:
sourceURI - - the source URI to the file to be inserted into the package.
mediaType - - media type of stream. Set to null if unknown
packagePath - - relative filePath where the tree should be inserted as XML file
Throws:
java.lang.Exception - In case the file could not be saved

insert

public void insert(java.io.InputStream fileStream,
                   java.lang.String packagePath,
                   java.lang.String mediaType)
            throws java.lang.Exception
Inserts InputStream into an OdfPackage. An existing file will be replaced.

Parameters:
fileStream - - the stream of the file to be inserted into the ODF package.
mediaType - - media type of stream. Set to null if unknown
packagePath - - relative filePath where the tree should be inserted as XML file
Throws:
java.lang.Exception - In case the file could not be saved

insert

public void insert(byte[] fileBytes,
                   java.lang.String packagePath,
                   java.lang.String mediaType)
            throws java.lang.Exception
Insert byte array into OdfPackage. An existing file will be replaced.

Parameters:
fileBytes - - data of the file stream to be stored in package
mediaType - - media type of stream. Set to null if unknown
packagePath - - relative filePath where the DOM tree should be inserted as XML file
Throws:
java.lang.Exception - when the DOM tree could not be inserted

getManifestEntries

public java.util.HashMap<java.lang.String,OdfFileEntry> getManifestEntries()

getManifestAsString

public java.lang.String getManifestAsString()
Get Manifest as String NOTE: This functionality should better be moved to a DOM based Manifest class

Returns:
the /META-INF/manifest.xml as a String

getBytes

public byte[] getBytes(java.lang.String packagePath)
                throws java.lang.Exception
Get package (sub-) content as byte array

Parameters:
packagePath - relative filePath to the package content
Returns:
the unzipped package content as byte array
Throws:
java.lang.Exception

getInputStream

public java.io.InputStream getInputStream(java.lang.String packagePath)
                                   throws java.lang.Exception
Get subcontent as InputStream

Parameters:
packagePath - of the desired stream.
Returns:
Inputstream of the ODF file within the package for the given path.
Throws:
java.lang.Exception

getInputStream

public java.io.InputStream getInputStream()
                                   throws java.lang.Exception
Gets the InputStream containing whole OdfPackage.

Returns:
the ODF package as input stream
Throws:
java.lang.Exception - - if the package could not be read

insertOutputStream

public java.io.OutputStream insertOutputStream(java.lang.String packagePath)
                                        throws java.lang.Exception
Insert the OutputStream for into OdfPackage. An existing file will be replaced.

Parameters:
packagePath - - relative filePath where the DOM tree should be inserted as XML file
Returns:
outputstream for the data of the file to be stored in package
Throws:
java.lang.Exception - when the DOM tree could not be inserted

insertOutputStream

public java.io.OutputStream insertOutputStream(java.lang.String packagePath,
                                               java.lang.String mediaType)
                                        throws java.lang.Exception
Insert the OutputStream - to be filled after method - when stream is closed into OdfPackage. An existing file will be replaced.

Parameters:
packagePath - - relative filePath where the DOM tree should be inserted as XML file
mediaType - - media type of stream
Returns:
outputstream for the data of the file to be stored in package
Throws:
java.lang.Exception - when the DOM tree could not be inserted

remove

public void remove(java.lang.String packagePath)

isExternalReference

public static boolean isExternalReference(java.lang.String fileRef)
Checks if the given reference is a reference, which points outside the ODF package

Parameters:
fileRef - the file reference to be checked
Returns:
true if the reference is an package external reference

getEntityResolver

public org.xml.sax.EntityResolver getEntityResolver()
Get EntityResolver to be used in XML Parsers which can resolve content inside the OdfPackage

Returns:
a SAX EntityResolver

getURIResolver

public javax.xml.transform.URIResolver getURIResolver()
Get URIResolver to be used in XSL Transformations which can resolve content inside the OdfPackage

Returns:
a TraX Resolver


Copyright © 2008-2010 ODF Toolkit Project. All Rights Reserved.