org.odftoolkit.odfdom.doc
Class OdfDocument

java.lang.Object
  extended by org.odftoolkit.odfdom.doc.OdfDocument
Direct Known Subclasses:
OdfChartDocument, OdfGraphicsDocument, OdfImageDocument, OdfPresentationDocument, OdfSpreadsheetDocument, OdfTextDocument

public abstract class OdfDocument
extends java.lang.Object

This abstract class is representing one of the possible ODF documents


Nested Class Summary
static class OdfDocument.OdfMediaType
          This enum contains all possible media types of OpenDocument documents.
static class OdfDocument.OdfXMLFile
          This enum contains all possible standardized XML ODF files of the OpenDocument document.
protected static class OdfDocument.Resource
           
 
Constructor Summary
OdfDocument()
           
 
Method Summary
 void close()
          Close the OdfPackage and release all temporary created data.
 void embedDocument(java.lang.String pkgPathToChildDocument, OdfDocument newOdfDocument)
          Add an OdfDocument as an embedded OdfDocument to the current OdfDocument
 java.lang.String getBaseURI()
          Get the URI, where this ODF document is stored.
 OdfFileDom getContentDom()
          Return the ODF type-based content DOM of the current OdfDocument
 java.io.InputStream getContentStream()
          Gets the ODF content.xml file as stream.
 java.lang.String getDocumentPackagePath()
          Get the relative path for an embedded ODF document.
 OdfOfficeStyles getDocumentStyles()
           
 OdfDocument getEmbeddedDocument(java.lang.String pathToObject)
          Returns an embedded OdfDocument of the current OdfDocument matching to the internal package path given as an parameter.
 java.util.List<OdfDocument> getEmbeddedDocuments()
          Method returns all embedded OdfDocuments, which match a valid OdfMediaType, of the current OdfDocument.
 java.util.List<OdfDocument> getEmbeddedDocuments(OdfDocument.OdfMediaType mediaType)
          Method returns all embedded OdfDocuments of the current OdfDocument matching the according MediaType.
 java.lang.String getMediaType()
          Get the media type from document
 OdfFileDom getMetaDom()
          Return the ODF type-based content DOM of the meta.xml
 java.io.InputStream getMetaStream()
          Gets the ODF metadata.xml file as stream.
 OdfOfficeBody getOfficeBody()
           
 OdfOfficeMasterStyles getOfficeMasterStyles()
          return the office:master-styles element of this document.
 OdfOfficeMeta getOfficeMetadata()
          Get the meta data feature instance of the current document
 OdfOfficeStyles getOrCreateDocumentStyles()
           
 OdfPackage getPackage()
          Retreives the OdfPackage for this OdfDocument.
protected  OdfDocument getRootDocument()
          Retreives the root OdfDocument that determines the mediatype of the package.
 java.io.InputStream getSettingsStream()
          Gets the ODF settings.xml file as stream.
 OdfFileDom getStylesDom()
          Return the ODF type-based content DOM of the current OdfDocument
 java.io.InputStream getStylesStream()
          Gets the ODF style.xml file as stream.
 OdfTable getTableByName(java.lang.String name)
          Return an instance of table feature with the specific table name.
 java.util.List<OdfTable> getTableList()
          Return a list of table features in this document.
protected  java.lang.String getXMLFilePath(OdfDocument.OdfXMLFile file)
          Get the relative path for an embedded ODF document including its file name.
 javax.xml.xpath.XPath getXPath()
          Create an XPath instance to select one or more nodes from an ODF document.
static OdfDocument loadDocument(java.io.File file)
          Creates an OdfDocument from the OpenDocument provided by a File.
static OdfDocument loadDocument(java.io.InputStream inStream)
          Creates an OdfDocument from the OpenDocument provided by a resource Stream.
static OdfDocument loadDocument(OdfPackage odfPackage)
          Creates an OdfDocument from the OpenDocument provided by an ODF package.
static OdfDocument loadDocument(java.lang.String path)
          Loads an OdfDocument from the provided path.
protected static OdfDocument loadTemplate(OdfDocument.Resource res)
          Loads an OpenDocument from the given resource
 java.lang.String newImage(java.net.URI imageUri)
          Insert an Image from the specified uri to the end of the OdfDocument.
 void RemoveEmbedDocument(java.lang.String pathToObject)
          remove an embedded Document from the current OdfDocument
 void save(java.io.File file)
          Save the document to given file.
 void save(java.io.OutputStream out)
          Save the document to an OutputStream.
 void save(java.lang.String path)
          Save the document to given path.Delegate to the root document and save possible embedded OdfDocuments.
protected  void setMediaType(OdfDocument.OdfMediaType odfMediaType)
          Sets the media type of the OdfDocument
protected  void setPackage(OdfPackage pkg)
          Sets the OdfPackage that contains this OdfDocument.
protected  void setRootDocument(OdfDocument root)
          Sets the root OdfDocument that determines the mediatype of the package.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

OdfDocument

public OdfDocument()
Method Detail

loadTemplate

protected static OdfDocument loadTemplate(OdfDocument.Resource res)
                                   throws java.lang.Exception
Loads an OpenDocument from the given resource

Parameters:
res - a resource containing the document
Returns:
the OpenDocument document or NULL if the media type is not supported by ODFDOM.
Throws:
java.lang.Exception - - if the document could not be created.

loadDocument

public static OdfDocument loadDocument(java.lang.String path)
                                throws java.lang.Exception
Loads an OdfDocument from the provided path.

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

Parameters:
path - - the path from where the document can be loaded
Returns:
the OpenDocument from the given path or NULL if the media type is not supported by ODFDOM.
Throws:
java.lang.Exception - - if the document could not be created.

loadDocument

public static OdfDocument loadDocument(java.io.InputStream inStream)
                                throws java.lang.Exception
Creates an OdfDocument from the OpenDocument provided by a resource Stream.

Since an InputStream does not provide the arbitrary (non sequentiell) read access needed by OdfDocument, the InputStream is cached. This usually takes more time compared to the other loadDocument methods. An advantage of caching is that there are no problems overwriting an input file.

Parameters:
inStream - - the InputStream of the ODF document.
Returns:
the document created from the given InputStream
Throws:
java.lang.Exception - - if the document could not be created.

loadDocument

public static OdfDocument loadDocument(java.io.File file)
                                throws java.lang.Exception
Creates an OdfDocument from the OpenDocument provided by a File.

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

Parameters:
file - - a file representing the ODF document.
Returns:
the document created from the given File
Throws:
java.lang.Exception - - if the document could not be created.

loadDocument

public static OdfDocument loadDocument(OdfPackage odfPackage)
                                throws java.lang.Exception
Creates an OdfDocument from the OpenDocument provided by an ODF package.

Parameters:
odfPackage - - the ODF package containing the ODF document.
Returns:
the root document of the given OdfPackage
Throws:
java.lang.Exception - - if the ODF document could not be created.

setRootDocument

protected void setRootDocument(OdfDocument root)
Sets the root OdfDocument that determines the mediatype of the package.

Parameters:
root - the OdfDocument that has its file on the root level of the package

getRootDocument

protected OdfDocument getRootDocument()
Retreives the root OdfDocument that determines the mediatype of the package.

Returns:
the OdfDocument that has its file on the root level of the package

setPackage

protected void setPackage(OdfPackage pkg)
Sets the OdfPackage that contains this OdfDocument.

Parameters:
pkg - the OdfPackage that contains this OdfDocument

getPackage

public OdfPackage getPackage()
Retreives the OdfPackage for this OdfDocument.

Returns:
the OdfPackage that contains this OdfDocument.

setMediaType

protected void setMediaType(OdfDocument.OdfMediaType odfMediaType)
Sets the media type of the OdfDocument

Parameters:
odfMediaType - media type to be set

getDocumentPackagePath

public java.lang.String getDocumentPackagePath()
Get the relative path for an embedded ODF document.

Returns:
path to the directory of the embedded ODF document (relative to ODF package root).

getXMLFilePath

protected java.lang.String getXMLFilePath(OdfDocument.OdfXMLFile file)
Get the relative path for an embedded ODF document including its file name.

Parameters:
file - represents one of the standardized XML ODF files.
Returns:
path to embedded ODF XML file relative to ODF package root.

embedDocument

public void embedDocument(java.lang.String pkgPathToChildDocument,
                          OdfDocument newOdfDocument)
Add an OdfDocument as an embedded OdfDocument to the current OdfDocument

Parameters:
pkgPathToChildDocument - path to the directory of the embedded ODF document (always relative to ODF package root).
newOdfDocument - the OdfDocument to be embedded

getContentStream

public java.io.InputStream getContentStream()
                                     throws java.lang.Exception
Gets the ODF content.xml file as stream.

Returns:
- a stream of the ODF content 'content.xml' file
Throws:
java.lang.Exception - - if the stream can not be extracted

getStylesStream

public java.io.InputStream getStylesStream()
                                    throws java.lang.Exception
Gets the ODF style.xml file as stream.

Returns:
- a stream of the ODF style 'styles.xml' file
Throws:
java.lang.Exception - - if the stream can not be extracted

getSettingsStream

public java.io.InputStream getSettingsStream()
                                      throws java.lang.Exception
Gets the ODF settings.xml file as stream.

Returns:
- a stream of the ODF metadata 'setting.xml' file
Throws:
java.lang.Exception - - if the stream can not be extracted

getMetaStream

public java.io.InputStream getMetaStream()
                                  throws java.lang.Exception
Gets the ODF metadata.xml file as stream.

Returns:
- a stream of the ODF metadata 'meta.xml' file
Throws:
java.lang.Exception - - if the stream can not be extracted

getBaseURI

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

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

getEmbeddedDocument

public OdfDocument getEmbeddedDocument(java.lang.String pathToObject)
Returns an embedded OdfDocument of the current OdfDocument matching to the internal package path given as an parameter. Once loaded embedded OdfDocuments are cached for later use.

Parameters:
pathToObject - path to the directory of the embedded ODF document (relative to ODF package root).
Returns:
an embedded OdfDocument

getEmbeddedDocuments

public java.util.List<OdfDocument> getEmbeddedDocuments(OdfDocument.OdfMediaType mediaType)
Method returns all embedded OdfDocuments of the current OdfDocument matching the according MediaType. This is done by matching the subfolder entries of the manifest file with the given OdfMediaType.

Parameters:
mediaType - media type which is used as a filter
Returns:
embedded documents of the current OdfDocument matching the given media type

getEmbeddedDocuments

public java.util.List<OdfDocument> getEmbeddedDocuments()
Method returns all embedded OdfDocuments, which match a valid OdfMediaType, of the current OdfDocument.

Returns:
a list with all embedded documents of the current odfDocument

getDocumentStyles

public OdfOfficeStyles getDocumentStyles()
Returns:
the office:styles element from the styles dom or null if there is no such element.

getOrCreateDocumentStyles

public OdfOfficeStyles getOrCreateDocumentStyles()
Returns:
the office:styles element from the styles dom. If there is not yet such an element, it is created.

getXPath

public javax.xml.xpath.XPath getXPath()
Create an XPath instance to select one or more nodes from an ODF document. Therefore the namespace context is set to the OdfNamespace

Returns:
an XPath instance with namespace context set to include the standard ODFDOM prefixes.

getContentDom

public OdfFileDom getContentDom()
                         throws java.lang.Exception
Return the ODF type-based content DOM of the current OdfDocument

Returns:
ODF type-based content DOM
Throws:
java.lang.Exception - if content DOM could not be initialized

getStylesDom

public OdfFileDom getStylesDom()
                        throws java.lang.Exception
Return the ODF type-based content DOM of the current OdfDocument

Returns:
ODF type-based styles DOM
Throws:
java.lang.Exception - if styles DOM could not be initialized

getMetaDom

public OdfFileDom getMetaDom()
                      throws java.lang.Exception
Return the ODF type-based content DOM of the meta.xml

Returns:
ODF type-based meta DOM
Throws:
java.lang.Exception - if meta DOM could not be initialized

getOfficeMetadata

public OdfOfficeMeta getOfficeMetadata()
                                throws java.lang.Exception
Get the meta data feature instance of the current document

Returns:
the meta data feature instance which represent office:meta in the meta.xml
Throws:
java.lang.Exception - if the file meta DOM could not be created.

getMediaType

public java.lang.String getMediaType()
Get the media type from document

Returns:
the mMediaType string of this package

save

public void save(java.lang.String path)
          throws java.lang.Exception
Save the document to given path.Delegate to the root document and save possible embedded OdfDocuments.

Parameters:
path - - the path to the file
Throws:
java.lang.Exception - if the document could not be saved

save

public void save(java.io.File file)
          throws java.lang.Exception
Save the document to given file. Delegate to the root document and save possible embedded OdfDocuments.

If the input file has been cached (this is the case when loading from an InputStream), the input file can be overwritten.

Otherwise it's allowed to overwrite the input file as long as the same path name is used that was used for loading (no symbolic link foo2.odt pointing to the loaded file foo1.odt, no network path X:\foo.odt pointing to the loaded file D:\foo.odt).

Parameters:
file - - the file to save the document
Throws:
java.lang.Exception - if the document could not be saved

save

public void save(java.io.OutputStream out)
          throws java.lang.Exception
Save the document to an OutputStream. Delegate to the root document and save possible embedded OdfDocuments.

If the input file has been cached (this is the case when loading from an InputStream), the input file can be overwritten.

If not, the OutputStream may not point to the input file! Otherwise this will result in unwanted behaviour and broken files.

Parameters:
out - - the OutputStream to write the file to
Throws:
java.lang.Exception - if the document could not be saved

close

public void close()
Close the OdfPackage and release all temporary created data. Acter execution of this method, this class is no longer usable. Do this as the last action to free resources. Closing an already closed document has no effect. Note that this will not close any cached documents.


getOfficeBody

public OdfOfficeBody getOfficeBody()
Returns:
the office:body element of this document

getOfficeMasterStyles

public OdfOfficeMasterStyles getOfficeMasterStyles()
return the office:master-styles element of this document.

Returns:
the office:master-styles element

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

newImage

public java.lang.String newImage(java.net.URI imageUri)
Insert an Image from the specified uri to the end of the OdfDocument.

Parameters:
imageUri - The URI of the image that will be added to the document, add image stream to the package, in the 'Pictures/' graphic directory with the same image file name as in the URI. If the imageURI is relative first the user.dir is taken to make it absolute.
Returns:
Returns the internal package path of the image, which was created based on the given URI.

RemoveEmbedDocument

public void RemoveEmbedDocument(java.lang.String pathToObject)
remove an embedded Document from the current OdfDocument

Parameters:
pathToObject - path to the directory of the embedded ODF document (always relative to ODF package root).

getTableByName

public OdfTable getTableByName(java.lang.String name)
Return an instance of table feature with the specific table name.

Parameters:
name - of the table beeing searched for.
Returns:
an instance of table feature with the specific table name.

getTableList

public java.util.List<OdfTable> getTableList()
Return a list of table features in this document.

Returns:
a list of table features in this document.


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