org.odftoolkit.simple
Class Document

java.lang.Object
  extended by org.odftoolkit.odfdom.pkg.OdfPackageDocument
      extended by org.odftoolkit.odfdom.dom.OdfSchemaDocument
          extended by org.odftoolkit.simple.Document
All Implemented Interfaces:
Closeable, TableContainer
Direct Known Subclasses:
ChartDocument, GraphicsDocument, PresentationDocument, SpreadsheetDocument, TextDocument

public abstract class Document
extends OdfSchemaDocument
implements TableContainer

This abstract class is representing one of the possible ODF documents


Nested Class Summary
static class Document.OdfMediaType
          This enum contains all possible media types of Document documents.
static class Document.ScriptType
           Unicode characters are in general divided by office applications into three different types:
 
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
 
Fields inherited from class org.odftoolkit.odfdom.dom.OdfSchemaDocument
mContentDom, mDocumentStyles, mMetaDom, mSettingsDom, mStylesDom
 
Fields inherited from class org.odftoolkit.odfdom.pkg.OdfPackageDocument
mDocumentMediaType, mDocumentPathInPackage, mPackage, ROOT_DOCUMENT_PATH
 
Constructor Summary
protected Document(OdfPackage pkg, String internalPath, Document.OdfMediaType mediaType)
           
 
Method Summary
 Table addTable()
          Add a new Table to this container.
 Table addTable(int numRows, int numCols)
          Add a new Table to this container with a specified row number and column number.
 void close()
          Close the OdfPackage and release all temporary created data.
protected  IdentityHashMap<OdfElement,Component> getComponentMap()
          Return the component repository of this document.
 OdfElement getContentRoot()
          Get the content root of a document.
protected
<T extends OdfElement>
T
getContentRoot(Class<T> clazz)
          Get the content root of a document.
 Document getEmbeddedDocument(String documentPath)
          Returns an embedded OdfPackageDocument from the given package path.
 List<Document> getEmbeddedDocuments()
          Method returns all embedded OdfPackageDocuments, which match a valid OdfMediaType, of the root OdfPackageDocument.
 List<Document> getEmbeddedDocuments(Document.OdfMediaType mediaType)
          Method returns all embedded OdfPackageDocuments of the root OdfPackageDocument matching the according MediaType.
 Locale getLocale(Document.ScriptType scriptType)
           Get a locale information of a specific script type.
protected  Document.OdfMediaType getOdfMediaType()
          Gets the media type of the Document
 Meta getOfficeMetadata()
          Get the meta data feature instance of the current document
static Document.ScriptType getScriptType(Locale locale)
           
 Section getSectionByName(String name)
          This method will search both the document content and header/footer, return a section with a specific name.
 Iterator<Section> getSectionIterator()
          This method will search both the document content and header/footer, return an iterator of section objects.
 Table.TableBuilder getTableBuilder()
          Return the table builder of this document.
 Table getTableByName(String name)
          Return an instance of table feature with the specific table name.
protected  TableContainer getTableContainerImpl()
           
 List<Table> getTableList()
          Return a list of table features in this document.
 void insertDocument(OdfPackageDocument sourceDocument, String documentPath)
          Embed an OdfPackageDocument to the current OdfPackageDocument.
static Document loadDocument(File file)
          Creates a Document from the Document provided by a File.
static Document loadDocument(File file, String password)
          Creates a Document from the Document provided by a File.
static Document loadDocument(InputStream inStream)
          Creates a Document from the Document provided by a resource Stream.
static Document loadDocument(OdfPackage odfPackage)
          Creates a Document from the Document provided by an ODF package.
static Document loadDocument(OdfPackage odfPackage, String internalPath)
          Creates a Document from the Document provided by an ODF package.
static Document loadDocument(String documentPath)
          Loads a Document from the provided path.
static Document loadDocument(String documentPath, String password)
          Loads a Document from the provided path.
 TableTemplate LoadTableTemplateFromForeignTable(InputStream templateFileInputStream, String tableName)
          Construct a TableTemplate feature by extracting style template from an pre-defined table in a foreign document.
protected static Document loadTemplate(OdfPackageDocument.Resource res, Document.OdfMediaType odfMediaType)
          Loads an Document from the given resource.
 String newImage(URI imageUri)
          Insert an Image from the specified uri to the end of the Document.
 boolean removeElementLinkedResource(OdfElement odfElement)
          Remove an ODF element from the document.
 void save(File file)
          Save the document to a given file.
 void save(File file, String password)
          Save the document to a given file with given password.
 void save(OutputStream out)
          Save the document to an OutputStream.
 void setLocale(Locale locale)
           Set a locale information.
 void setLocale(Locale locale, Document.ScriptType scriptType)
           Set a locale of a specific script type.
protected  void setOdfMediaType(Document.OdfMediaType odfMediaType)
          Sets the media type of the Document
 void setPassword(String password)
          Sets password of this document.
 String toString()
           
 
Methods inherited from class org.odftoolkit.odfdom.dom.OdfSchemaDocument
getBaseURI, getBookmarkRDFMetadata, getContentDom, getContentStream, getDocumentStyles, getFileDom, getInContentMetadata, getInContentMetadataFromCache, getManifestRDFMetadata, getMasterPages, getMetaDom, getMetaStream, getOfficeMasterStyles, getOrCreateDocumentStyles, getRDFMetadata, getSettingsDom, getSettingsStream, getStylesDom, getStylesStream, getTables, getXMLFilePath
 
Methods inherited from class org.odftoolkit.odfdom.pkg.OdfPackageDocument
flushDoms, getDocumentPath, getFileDom, getMediaTypeString, getPackage, getXMLFileMetadata, isExternalReference, isRootDocument, loadSubDocument, normalizeDocumentPath, removeDocument, save, setMediaTypeString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.odftoolkit.simple.table.TableContainer
getTableContainerElement
 

Constructor Detail

Document

protected Document(OdfPackage pkg,
                   String internalPath,
                   Document.OdfMediaType mediaType)
Method Detail

loadTemplate

protected static Document loadTemplate(OdfPackageDocument.Resource res,
                                       Document.OdfMediaType odfMediaType)
                                throws Exception
Loads an Document from the given resource. NOTE: Initial meta data will be added in this method.

Parameters:
res - a resource containing a package with a root document
odfMediaType - the media type of the root document
Returns:
the Document document or NULL if the media type is not supported by SIMPLE.
Throws:
Exception - - if the document could not be created.

loadDocument

public static Document loadDocument(String documentPath,
                                    String password)
                             throws Exception
Loads a Document from the provided path.

Document relies on the file being available for read access over the whole life cycle of Document.

Parameters:
documentPath - - the path from where the document can be loaded
password - - file password.
Returns:
the Document from the given path or NULL if the media type is not supported by SIMPLE.
Throws:
Exception - - if the document could not be created.
Since:
0.8

loadDocument

public static Document loadDocument(String documentPath)
                             throws Exception
Loads a Document from the provided path.

Document relies on the file being available for read access over the whole life cycle of Document.

Parameters:
documentPath - - the path from where the document can be loaded
Returns:
the Document from the given path or NULL if the media type is not supported by SIMPLE.
Throws:
Exception - - if the document could not be created.

loadDocument

public static Document loadDocument(InputStream inStream)
                             throws Exception
Creates a Document from the Document provided by a resource Stream.

Since an InputStream does not provide the arbitrary (non sequentiell) read access needed by Document, 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 Document loadDocument(File file)
                             throws Exception
Creates a Document from the Document provided by a File.

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

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

public static Document loadDocument(File file,
                                    String password)
                             throws Exception
Creates a Document from the Document provided by a File.

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

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

loadDocument

public static Document loadDocument(OdfPackage odfPackage)
                             throws Exception
Creates a Document from the Document provided by an 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 Document loadDocument(OdfPackage odfPackage,
                                    String internalPath)
                             throws Exception
Creates a Document from the Document 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.
Returns:
the root document of the given OdfPackage
Throws:
Exception - - if the ODF document could not be created.

setPassword

public void setPassword(String password)
Sets password of this document.

Parameters:
password - the password of this document.
Since:
0.8

getEmbeddedDocument

public Document getEmbeddedDocument(String documentPath)
Returns an embedded OdfPackageDocument from the given package path.

Parameters:
documentPath - path to the ODF document within the package. The path is relative to the current document.
Returns:
an embedded Document

getEmbeddedDocuments

public List<Document> getEmbeddedDocuments()
Method returns all embedded OdfPackageDocuments, which match a valid OdfMediaType, of the root OdfPackageDocument.

Returns:
a list with all embedded documents of the root OdfPackageDocument

getEmbeddedDocuments

public List<Document> getEmbeddedDocuments(Document.OdfMediaType mediaType)
Method returns all embedded OdfPackageDocuments of the root OdfPackageDocument 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 root OdfPackageDocument matching the given media type

insertDocument

public void insertDocument(OdfPackageDocument sourceDocument,
                           String documentPath)
Embed an OdfPackageDocument to the current OdfPackageDocument. All the file entries of child document will be embedded as well to the current document package.

Overrides:
insertDocument in class OdfPackageDocument
Parameters:
documentPath - to the directory the ODF document should be inserted (relative to the current document).
sourceDocument - the OdfPackageDocument to be embedded.

setOdfMediaType

protected void setOdfMediaType(Document.OdfMediaType odfMediaType)
Sets the media type of the Document

Parameters:
odfMediaType - media type to be set

getOdfMediaType

protected Document.OdfMediaType getOdfMediaType()
Gets the media type of the Document


getOfficeMetadata

public Meta 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

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

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

public void save(File file)
          throws Exception
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 class OdfPackageDocument
Parameters:
file - - the file to save the document
Throws:
Exception - if the document could not be saved

save

public void save(File file,
                 String password)
          throws Exception
Save the document to a given file with given password.

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.

Parameters:
file - the file to save the document.
file - the password of this document.
Throws:
Exception - if the document could not be saved
Since:
0.8

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.

Specified by:
close in interface Closeable
Overrides:
close in class OdfSchemaDocument

getContentRoot

protected <T extends OdfElement> T getContentRoot(Class<T> clazz)
                                       throws Exception
Get the content root of a document. You may prefer to use the getContentRoot methods of subclasses of Document. Their return parameters are already casted to respective subclasses of OdfElement.

Parameters:
clazz - the type of the content root, depend on the document type
Returns:
the child element of office:body, e.g. office:text for text docs
Throws:
Exception - if the file DOM could not be created.

getContentRoot

public OdfElement getContentRoot()
                          throws Exception
Get the content root of a document. You may prefer to use the getContentRoot methods of subclasses of Document.

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

public String toString()
Overrides:
toString in class Object

newImage

public String newImage(URI imageUri)
Insert an Image from the specified uri to the end of the Document.

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.

setLocale

public void setLocale(Locale locale)

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

getScriptType

public static Document.ScriptType getScriptType(Locale locale)

setLocale

public void setLocale(Locale locale,
                      Document.ScriptType scriptType)

Set a locale of a specific script type.

If the locale is not belone to the script type, nothing will happen.

Parameters:
locale - - Locale information
scriptType - - The script type

getLocale

public Locale getLocale(Document.ScriptType scriptType)

Get a locale information of a specific script type.

Parameters:
scriptType - - The script type
Returns:
the Locale information

getSectionIterator

public Iterator<Section> getSectionIterator()
This method will search both the document content and header/footer, return an iterator of section objects.

The sections defined in embed document won't be covered.

Returns:
an iterator of Section objects

getSectionByName

public Section getSectionByName(String name)
This method will search both the document content and header/footer, return a section with a specific name.

This method won't search in the embed document.

Null will be returned if there is no section found.

Parameters:
name - - the name of a section
Returns:
a section object with a specific name

removeElementLinkedResource

public boolean removeElementLinkedResource(OdfElement odfElement)
Remove an ODF element from the document. All the resources that are only related with this element will be removed at the same time.

Parameters:
odfElement - - the odf element that would be moved.

addTable

public Table addTable()
Description copied from interface: TableContainer
Add a new Table to this container.

Specified by:
addTable in interface TableContainer
Returns:
added table.

addTable

public Table addTable(int numRows,
                      int numCols)
Description copied from interface: TableContainer
Add a new Table to this container with a specified row number and column number.

The table will be inserted at the end of the tableContainer. An unique table name will be given, you may set a custom table name using the setTableName method.

Specified by:
addTable in interface TableContainer
Parameters:
numRows - the row number
numCols - the column number
Returns:
a new instance of Table

getTableByName

public Table getTableByName(String name)
Description copied from interface: TableContainer
Return an instance of table feature with the specific table name.

Specified by:
getTableByName in interface TableContainer
Parameters:
name - of the table being searched for.
Returns:
an instance of table feature with the specific table name.

getTableList

public List<Table> getTableList()
Description copied from interface: TableContainer
Return a list of table features in this document.

Specified by:
getTableList in interface TableContainer
Returns:
a list of table features in this document.

getTableBuilder

public Table.TableBuilder getTableBuilder()
Description copied from interface: TableContainer
Return the table builder of this document. Every document has a table builder, which supplies all of the table creation realization, for example newTable().

Specified by:
getTableBuilder in interface TableContainer
Returns:
the table builder of this document.

getTableContainerImpl

protected TableContainer getTableContainerImpl()

getComponentMap

protected IdentityHashMap<OdfElement,Component> getComponentMap()
Return the component repository of this document.

Returns:
the component repository of this document.

LoadTableTemplateFromForeignTable

public TableTemplate LoadTableTemplateFromForeignTable(InputStream templateFileInputStream,
                                                       String tableName)
                                                throws Exception
Construct a TableTemplate feature by extracting style template from an pre-defined table in a foreign document. The styles loaded by the template will be copied into the document as well and can be referenced by table directly.

The imported table need to be at least a 5*5 table (e.g. A1E5). Each type of style in the template will be set according to the style reference in a specific table cell, as following:
first column - A2
last column - E2
first row - A2
last row - E2
even rows - B3
odd rows - B2
even columns - C2
odd columns - B2
body - B2
first-row-start-column -A1
first-row-end-column -E1
last-row-start-column -A5
last-row-end-column -E5

Parameters:
templateFileInputStream - - the InputStream of the ODF document.
tableName - - the table name which will be used to load styles as template
Throws:
Exception - - if content DOM could not be initialized


Copyright © 2010-2017 The Apache Software Foundation. All Rights Reserved.