Class OdfDocument
- All Implemented Interfaces:
Closeable
,AutoCloseable
- Direct Known Subclasses:
OdfChartDocument
,OdfGraphicsDocument
,OdfImageDocument
,OdfPresentationDocument
,OdfSpreadsheetDocument
,OdfTextDocument
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
This enum contains all possible media types of OpenDocument documents.static enum
Unicode characters are in general divided by office applications into three different groups.Nested classes/interfaces inherited from class org.odftoolkit.odfdom.dom.OdfSchemaDocument
OdfSchemaDocument.OdfXMLFile
Nested classes/interfaces inherited from class org.odftoolkit.odfdom.pkg.OdfPackageDocument
OdfPackageDocument.Resource
-
Field Summary
FieldsFields inherited from class org.odftoolkit.odfdom.dom.OdfSchemaDocument
mContentDom, mJsonOperationQueue, mMetaDom, mSettingsDom, mStylesDom
Fields inherited from class org.odftoolkit.odfdom.pkg.OdfPackageDocument
mDocumentMediaType, mDocumentPathInPackage, mPackage, ROOT_DOCUMENT_PATH
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
OdfDocument
(OdfPackage pkg, String internalPath, OdfDocument.OdfMediaType mediaType) -
Method Summary
Modifier and TypeMethodDescriptionvoid
addAnnotation
(String name, OfficeAnnotationElement element) void
close()
Close the document and release all temporary created data.getAnnotation
(String name) Get the content root of a document.This is a temporary solution to know about the fonts within the documents.getLocale
(OdfDocument.UnicodeGroup unicodeGroup) This method will return Locale, which presents the default language and country information settings in this documentprotected OdfDocument.OdfMediaType
Gets the media type of the OdfDocumentGet the meta data feature instance of the current document* All text selections within this document are cached and have to be updated when the document is changed.TheSelectionManager
takes care of those updates across eachTextNavigator
getTableByName
(String name) Return an instance of table feature with the specific table name.Deprecated.getTableList
(boolean doRecursiveSearch) Return a list of table features in this document.static OdfDocument.UnicodeGroup
getUnicodeGroup
(Locale locale) Similar to OpenOffice.org, ODFDOM assumes that every Locale is related to one of the three Unicodes Groups, either CJK, CTL or Western.static OdfDocument
loadDocument
(File file) Loads the ODF root document from the ODF package provided as a File.static OdfDocument
loadDocument
(InputStream inStream) Loads the ODF root document from the ODF package provided by a Stream.static OdfDocument
loadDocument
(InputStream inStream, Boolean enableCollaboration) Loads the ODF root document from the ODF package provided by a Stream.static OdfDocument
loadDocument
(InputStream inStream, Map<String, Object> configuration) Loads the ODF root document from the ODF package provided by a Stream.static OdfDocument
loadDocument
(InputStream inStream, Map<String, Object> configuration, Boolean enableCollaboration) Loads the ODF root document from the ODF package provided by a Stream.static OdfDocument
loadDocument
(String documentPath) Loads the ODF root document from the ODF package provided by its path.static OdfDocument
loadDocument
(OdfPackage odfPackage) Loads the ODF root document from the ODF package.static OdfDocument
loadDocument
(OdfPackage odfPackage, Boolean enableCollaboration) Loads the ODF root document from the ODF package.static OdfDocument
loadDocument
(OdfPackage odfPackage, String internalPath) Creates an OdfDocument from the OpenDocument provided by an ODF package.static OdfDocument
loadDocument
(OdfPackage odfPackage, String internalPath, Boolean enableCollaboration) Creates an OdfDocument from the OpenDocument provided by an ODF package.loadSubDocument
(String documentPath) Returns an embedded OdfPackageDocument from the given package path.Method returns all embedded OdfPackageDocuments, which match a valid OdfMediaType, of the current OdfPackageDocument.loadSubDocuments
(OdfDocument.OdfMediaType desiredMediaType) Method returns all embedded OdfPackageDocuments of sthe current OdfPackageDocument matching the according MediaType.protected static OdfDocument
loadTemplate
(OdfPackageDocument.Resource res, OdfDocument.OdfMediaType odfMediaType) Loads the ODF root document from the given Resource.protected static OdfDocument
loadTemplate
(OdfPackageDocument.Resource res, OdfDocument.OdfMediaType odfMediaType, Boolean enableCollaboration) Loads the ODF root document from the given Resource.Insert an Image from the specified uri to the end of the OdfDocument.void
removeAnnotation
(String name) protected void
void
Save the document to a given file.void
save
(OutputStream out) Save the document to an OutputStream.void
Set a locale information.protected void
setOdfMediaType
(OdfDocument.OdfMediaType odfMediaType) Sets the media type of the OdfDocumenttoString()
void
Update document meta data in the ODF document.Methods inherited from class org.odftoolkit.odfdom.dom.OdfSchemaDocument
getBaseURI, getBookmarkRDFMetadata, getContentDom, getContentStream, getDocumentStyles, getFileDom, getInContentMetadata, getInContentMetadataFromCache, getJsonOperationQueue, getManifestRDFMetadata, getMasterPages, getMetaDom, getMetaStream, getOfficeMasterStyles, getOperations, getOrCreateDocumentStyles, getRDFMetadata, getRootComponent, getRootComponentElement, getSettingsDom, getSettingsStream, getStyleByDisplayName, getStyleByName, getStylesDom, getStylesStream, getTables, getTables, getXMLFilePath, setContentDom, setJsonOperationQueue, setMetaDom, setRootComponent, setSettingsDom, setStylesDom
Methods inherited from class org.odftoolkit.odfdom.pkg.OdfPackageDocument
flushDoms, getAbsoluteFilePath, getCachedDom, getDocumentPath, getFileDom, getMediaTypeString, getPackage, getXMLFileMetadata, insertDocument, isExternalReference, isRootDocument, normalizeDocumentPath, removeDocument, save, setMediaTypeString
-
Field Details
-
mHasCollaboration
-
-
Constructor Details
-
OdfDocument
protected OdfDocument(OdfPackage pkg, String internalPath, OdfDocument.OdfMediaType mediaType) throws SAXException - Throws:
SAXException
-
-
Method Details
-
loadTemplate
protected static OdfDocument loadTemplate(OdfPackageDocument.Resource res, OdfDocument.OdfMediaType odfMediaType) throws Exception Loads the ODF root document from the given Resource.NOTE: Initial meta data (like the document creation time) will be added in this method.
- Parameters:
res
- a resource containing a package with a root documentodfMediaType
- the media type of the root document- Returns:
- the OpenDocument document or NULL if the media type is not supported by ODFDOM.
- Throws:
Exception
- - if the document could not be created.
-
loadTemplate
protected static OdfDocument loadTemplate(OdfPackageDocument.Resource res, OdfDocument.OdfMediaType odfMediaType, Boolean enableCollaboration) throws Exception Loads the ODF root document from the given Resource.NOTE: Initial meta data (like the document creation time) will be added in this method.
- Parameters:
res
- a resource containing a package with a root documentodfMediaType
- the media type of the root documentenableCollaboration
- - user changes equivalent for creating this document are gathered- Returns:
- the OpenDocument document or NULL if the media type is not supported by ODFDOM.
- Throws:
Exception
- - if the document could not be created.
-
loadDocument
Loads the ODF root document from the ODF package provided by its path.OdfDocument relies on the file being available for read access over the whole life-cycle of OdfDocument.
- Parameters:
documentPath
- - 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:
Exception
- - if the document could not be created.
-
loadDocument
public static OdfDocument loadDocument(InputStream inStream, Map<String, Object> configuration) throws ExceptionLoads the ODF root document from the ODF package provided by a Stream.Since an InputStream does not provide the arbitrary (non sequential) read access needed by OdfDocument, the InputStream is cached. This usually takes more time compared to the other createInternalDocument methods. An advantage of caching is that there are no problems overwriting an input file.
- Parameters:
inStream
- - the InputStream of the ODF document.configuration
- - key/value pairs of user given run-time settings (configuration)- Returns:
- the document created from the given InputStream
- Throws:
Exception
- - if the document could not be created.
-
loadDocument
public static OdfDocument loadDocument(InputStream inStream, Map<String, Object> configuration, Boolean enableCollaboration) throws ExceptionLoads the ODF root document from the ODF package provided by a Stream.Since an InputStream does not provide the arbitrary (non sequential) read access needed by OdfDocument, the InputStream is cached. This usually takes more time compared to the other createInternalDocument methods. An advantage of caching is that there are no problems overwriting an input file.
- Parameters:
inStream
- - the InputStream of the ODF document.configuration
- - key/value pairs of user given run-time settings (configuration)enableCollaboration
- - user changes equivalent for creating this document are gathered- Returns:
- the document created from the given InputStream
- Throws:
Exception
- - if the document could not be created.
-
loadDocument
Loads the ODF root document from the ODF package provided by a Stream.Since an InputStream does not provide the arbitrary (non sequential) read access needed by OdfDocument, the InputStream is cached. This usually takes more time compared to the other createInternalDocument 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:
Exception
- - if the document could not be created.
-
loadDocument
public static OdfDocument loadDocument(InputStream inStream, Boolean enableCollaboration) throws Exception Loads the ODF root document from the ODF package provided by a Stream.Since an InputStream does not provide the arbitrary (non sequential) read access needed by OdfDocument, the InputStream is cached. This usually takes more time compared to the other createInternalDocument methods. An advantage of caching is that there are no problems overwriting an input file.
- Parameters:
inStream
- - the InputStream of the ODF document.enableCollaboration
- - user changes equivalent for creating this document are gathered- Returns:
- the document created from the given InputStream
- Throws:
Exception
- - if the document could not be created.
-
loadDocument
Loads the ODF root document from the ODF package provided as a File.- Parameters:
file
- - a file representing the ODF document.- Returns:
- the document created from the given File
- Throws:
Exception
- - if the document could not be created.
-
loadDocument
Loads the ODF root document from the ODF package.- Parameters:
odfPackage
- - the ODF package containing the ODF document.- Returns:
- the root document of the given OdfPackage
- Throws:
Exception
- - if the ODF document could not be created.
-
loadDocument
public static OdfDocument loadDocument(OdfPackage odfPackage, Boolean enableCollaboration) throws Exception Loads the ODF root document from the ODF package.- Parameters:
odfPackage
- - the ODF package containing the ODF document.enableCollaboration
- - user changes equivalent for creating this document are gathered- Returns:
- the root document of the given OdfPackage
- Throws:
Exception
- - if the ODF document could not be created.
-
loadDocument
Creates an OdfDocument from the OpenDocument provided by an ODF package.- Parameters:
odfPackage
- - the ODF package containing the ODF document.internalPath
- - the path to the ODF document relative to the package root, or an empty String for the root document.- Returns:
- the root document of the given OdfPackage
- Throws:
Exception
- - if the ODF document could not be created.
-
loadDocument
public static OdfDocument loadDocument(OdfPackage odfPackage, String internalPath, Boolean enableCollaboration) throws Exception Creates an OdfDocument from the OpenDocument provided by an ODF package.- Parameters:
odfPackage
- - the ODF package containing the ODF document.internalPath
- - the path to the ODF document relative to the package root, or an empty String for the root document.enableCollaboration
- - user changes equivalent for creating this document are gathered- Returns:
- the root document of the given OdfPackage
- Throws:
Exception
- - if the ODF document could not be created.
-
loadSubDocument
Returns an embedded OdfPackageDocument from the given package path.- Overrides:
loadSubDocument
in classOdfPackageDocument
- Parameters:
documentPath
- to the ODF document within the package. The path is relative the current ODF document path.- Returns:
- an embedded OdfPackageDocument
-
loadSubDocuments
Method returns all embedded OdfPackageDocuments, which match a valid OdfMediaType, of the current OdfPackageDocument. Note: The root document is not part of the returned collection.- Returns:
- a map with all embedded documents and their paths of the current OdfPackageDocument
-
loadSubDocuments
Method returns all embedded OdfPackageDocuments of sthe current OdfPackageDocument matching the according MediaType. This is done by matching the subfolder entries of the manifest file with the given OdfMediaType.- Parameters:
desiredMediaType
- media type of the documents to be returned (used as a filter).- Returns:
- embedded documents of the current OdfPackageDocument matching the given media type
-
setOdfMediaType
Sets the media type of the OdfDocument- Parameters:
odfMediaType
- media type to be set
-
getOdfMediaType
Gets the media type of the OdfDocument -
getOfficeMetadata
Get the meta data feature instance of the current document- Returns:
- the meta data feature instance which represent
office:meta
in the meta.xml
-
save
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.
When save the embedded document to a stand alone document, all the file entries of the embedded document will be copied to a new document package. If the embedded document is outside of the current document directory, you have to embed it to the sub directory and refresh the link of the embedded document. you should reload it from the stream to get the saved embedded document.
- Parameters:
out
- - the OutputStream to write the file to- Throws:
Exception
- if the document could not be saved
-
save
Save the document to a given file.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).
When saving the embedded document to a stand alone document, all files of the embedded document will be copied to a new document package. If the embedded document is outside of the current document directory, you have to embed it to the sub directory and refresh the link of the embedded document. You should reload it from the given file to get the saved embedded document.
- Overrides:
save
in classOdfPackageDocument
- Parameters:
file
- - the file to save the document- Throws:
Exception
- if the document could not be saved
-
close
public void close()Close the document and release all temporary created data. After 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.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classOdfSchemaDocument
-
getContentRoot
Get the content root of a document.You may prefer to use the getContentRoot methods of subclasses of OdfDocument.
- Returns:
- the child element of office:body, e.g. office:text for text docs
- Throws:
Exception
- if the file DOM could not be created.
-
toString
-
newImage
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.
-
getTableByName
Return an instance of table feature with the specific table name.- Parameters:
name
- of the table being searched for.- Returns:
- an instance of table feature with the specific table name.
-
getTableList
@Deprecated(since="It was not clear that this is searching recursively, especialy in OdfSpreadsheetDocuments") public List<OdfTable> getTableList()Deprecated.Return a list of table features in this document. For general ODF documents it searches for them recursively through the document. For ODF documents, there is a getOdsTableList- Returns:
- a list of table features in this document.
-
getTableList
Return a list of table features in this document. For general ODF documents it searches for tables recursively through the document.- Parameters:
doRecursiveSearch
- In spreadsheet documents you do not need a recursive search.- Returns:
- a list of table features in this document.
-
updateMetaData
public void updateMetaData()Update document meta data in the ODF document. Following metadata data is being updated:- The name of the person who last modified this document will be the Java user.name System property
- The date and time when the document was last modified using current data
- The number of times this document has been edited is incremented by 1
- The total time spent editing this document
-
setLocale
Set a locale information.The locale information will affect the language and country setting of the document. Thus the font settings, the spell checkings and etc will be affected.
- Parameters:
locale
- - an instance of Locale
-
getUnicodeGroup
Similar to OpenOffice.org, ODFDOM assumes that every Locale is related to one of the three Unicodes Groups, either CJK, CTL or Western.- Parameters:
locale
- the UnicodeGroup is requested for- Returns:
- the related UnicodeGroup
-
getLocale
This method will return Locale, which presents the default language and country information settings in this documentODF allows to set a Locale for each of the three UnicodeGroups. Therefore there might be three different Locale for the document.
- Parameters:
unicodeGroup
- - One of the three (CJK, CTL or Western).- Returns:
- the Locale for the given UnicodeGroup
-
getFontNames
This is a temporary solution to know about the fonts within the documents. Project client does not provide font information, therefore the 16 fonts are hard coded -
addAnnotation
-
getAnnotation
-
removeAnnotation
-
getUniqueAnnotationName
-
removeCachedView
protected void removeCachedView() -
hasCollaboration
- Returns:
- TRUE if the document was created by CollabTextDocument and thereby supports user changes, otherwise FALSE
-
getSelectionManager
* All text selections within this document are cached and have to be updated when the document is changed.TheSelectionManager
takes care of those updates across eachTextNavigator
- Returns:
- the single selection manager for an OdfDocument instance
-