org.odftoolkit.simple
Class TextDocument

java.lang.Object
  extended by org.odftoolkit.odfdom.pkg.OdfPackageDocument
      extended by org.odftoolkit.odfdom.dom.OdfSchemaDocument
          extended by org.odftoolkit.simple.Document
              extended by org.odftoolkit.simple.TextDocument
All Implemented Interfaces:
Closeable, ChartContainer, VariableContainer, TableContainer, ListContainer, ParagraphContainer

public class TextDocument
extends Document
implements ListContainer, ParagraphContainer, VariableContainer, ChartContainer

This class represents an empty ODF text document.


Nested Class Summary
static class TextDocument.OdfMediaType
          This enum contains all possible media types of SpreadsheetDocument documents.
 
Nested classes/interfaces inherited from class org.odftoolkit.simple.Document
Document.ScriptType
 
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 TextDocument(OdfPackage pkg, String internalPath, TextDocument.OdfMediaType odfMediaType)
          To avoid data duplication a new document is only created, if not already opened.
 
Method Summary
 void addColumnBreak()
          Appends a new column break to this document.
 void addColumnBreak(Paragraph refParagraph)
          Appends a new column break to this document after the reference paragraph.
 List addList()
          Add a new List to this container.
 List addList(ListDecorator decorator)
          Add a List with specified ListDecorator to this container.
 void addPageBreak()
          Appends a new page break to this document.
 void addPageBreak(Paragraph refParagraph)
          Appends a new page break to this document after the reference paragraph.
 Paragraph addParagraph(String text)
          Creates a new paragraph and append text.
 OdfTextParagraph addText(String text)
          Deprecated. As of Simple version 0.5, replaced by Paragraph.appendTextContent(String content)
 Section appendSection(Section section, boolean isResourceCopied)
          Copy a section and append it at the end of the text document, whether the section is in this document or in a different document.
 void changeMode(TextDocument.OdfMediaType mediaType)
          Changes the document to the given mediatype.
 void clearList()
          Remove all Lists from this container.
 Chart createChart(String title, DataSet dataset, Rectangle rect)
          Creates a new Chart for this container.
 Chart createChart(String title, SpreadsheetDocument document, CellRangeAddressList cellRangeAddr, boolean firstRowAsLabel, boolean firstColumnAsLabel, boolean rowAsDataSeries, Rectangle rect)
          Creates a new Chart for this container.
 Chart createChart(String title, String[] labels, String[] legends, double[][] data, Rectangle rect)
          Creates a new Chart for this container.
 VariableField declareVariable(String name, VariableField.VariableType type)
          Declare a new variable field to this container.
 void deleteChartById(String chartId)
          Deletes chart by chart id.
 void deleteChartByTitle(String title)
          Deletes chart(s) by chart title.
 Chart getChartById(String chartId)
          Gets chart with specified id.
 List<Chart> getChartByTitle(String title)
          Gets chart list with specified title.
 int getChartCount()
          Returns the chart count of this container
 OfficeTextElement getContentRoot()
          Get the content root of a text document.
 Footer getFooter()
          Get the Standard Page footer of this text document.
 Footer getFooter(boolean isFirstPage)
          Get the footer of this text document.
 Header getHeader()
          Get the Standard Page header of this text document.
 Header getHeader(boolean isFirstPage)
          Get the header of this text document.
 OdfElement getListContainerElement()
          Get the ODF element which can have as child element directly according to ODF specification.
 Iterator<List> getListIterator()
          Return an Iterator of the Lists in this ListContainer.
 Paragraph getParagraphByIndex(int index, boolean isEmptyParagraphSkipped)
          Return a paragraph with a given index.
 Paragraph getParagraphByReverseIndex(int reverseIndex, boolean isEmptyParagraphSkipped)
          Return a paragraph with a given index.
 OdfElement getParagraphContainerElement()
          Get the ODF element which can have as child element directly.
 Iterator<Paragraph> getParagraphIterator()
          Return an Iterator of the paragraph in this container.
 OdfElement getTableContainerElement()
          Get the ODF element which can have as child element directly according to ODF specification.
 OdfElement getVariableContainerElement()
          Get the ODF element which can have variable declare section as child element directly according to ODF specification.
 VariableField getVariableFieldByName(String name)
          Return the declared variable by name.
static TextDocument loadDocument(File file)
          Creates an TextDocument from the OpenDocument provided by a File.
static TextDocument loadDocument(InputStream inputStream)
          Creates an TextDocument from the OpenDocument provided by a resource Stream.
static TextDocument loadDocument(String documentPath)
          Loads an TextDocument from the provided path.
 OdfTextParagraph newParagraph()
          Deprecated. As of Simple version 0.5, replaced by Paragraph.newParagraph(ParagraphContainer)
 OdfTextParagraph newParagraph(String text)
          Deprecated. As of Simple version 0.5, replaced by addParagraph(String text)
static TextDocument newTextDocument()
          Creates an empty text document.
static TextDocument newTextDocument(TextDocument.OdfMediaType mimeType)
          Creates an empty text document.
static TextDocument newTextMasterDocument()
          Creates an empty text master document.
static TextDocument newTextTemplateDocument()
          Creates an empty text template.
static TextDocument newTextWebDocument()
          Creates an empty text web.
 boolean removeList(List list)
          Remove the existing List from this container.
 boolean removeParagraph(Paragraph para)
          Remove paragraph from this document
 void setPageColumns(int columnsNumber, double spacing)
          Defines several columns to the page whose style is specified.
 
Methods inherited from class org.odftoolkit.simple.Document
addTable, addTable, close, getComponentMap, getContentRoot, getEmbeddedDocument, getEmbeddedDocuments, getEmbeddedDocuments, getLocale, getOdfMediaType, getOfficeMetadata, getScriptType, getSectionByName, getSectionIterator, getTableBuilder, getTableByName, getTableContainerImpl, getTableList, insertDocument, loadDocument, loadDocument, loadTemplate, newImage, removeElementLinkedResource, save, save, setLocale, setLocale, setOdfMediaType, toString
 
Methods inherited from class org.odftoolkit.odfdom.dom.OdfSchemaDocument
getBaseURI, getContentDom, getContentStream, getDocumentStyles, getFileDom, getMasterPages, getMetaDom, getMetaStream, getOfficeMasterStyles, getOrCreateDocumentStyles, getSettingsDom, getSettingsStream, getStylesDom, getStylesStream, getTables, getXMLFilePath
 
Methods inherited from class org.odftoolkit.odfdom.pkg.OdfPackageDocument
flushDoms, getDocumentPath, getFileDom, getMediaTypeString, getPackage, isExternalReference, isRootDocument, loadSubDocument, normalizeDocumentPath, removeDocument, save, setMediaTypeString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TextDocument

protected TextDocument(OdfPackage pkg,
                       String internalPath,
                       TextDocument.OdfMediaType odfMediaType)
To avoid data duplication a new document is only created, if not already opened. A document is cached by this constructor using the internalpath as key.

Method Detail

newTextDocument

public static TextDocument newTextDocument()
                                    throws Exception
Creates an empty text document.

Returns:
ODF text document based on a default template
Throws:
Exception - - if the document could not be created

newTextDocument

public static TextDocument newTextDocument(TextDocument.OdfMediaType mimeType)
                                    throws Exception
Creates an empty text document.

Returns:
ODF text document based on a default template
Throws:
Exception - - if the document could not be created

newTextTemplateDocument

public static TextDocument newTextTemplateDocument()
                                            throws Exception
Creates an empty text template.

Returns:
ODF text template based on a default
Throws:
Exception - - if the template could not be created

newTextMasterDocument

public static TextDocument newTextMasterDocument()
                                          throws Exception
Creates an empty text master document.

Returns:
ODF text master based on a default
Throws:
Exception - - if the document could not be created

newTextWebDocument

public static TextDocument newTextWebDocument()
                                       throws Exception
Creates an empty text web.

Returns:
ODF text web based on a default
Throws:
Exception - - if the document could not be created

loadDocument

public static TextDocument loadDocument(InputStream inputStream)
                                 throws Exception
Creates an TextDocument from the OpenDocument provided by a resource Stream.

Since an InputStream does not provide the arbitrary (non sequentiell) read access needed by TextDocument, 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.

If the resource stream is not a ODF text document, ClassCastException might be thrown.

Parameters:
inputStream - - the InputStream of the ODF text document.
Returns:
the text document created from the given InputStream
Throws:
Exception - - if the document could not be created.

loadDocument

public static TextDocument loadDocument(String documentPath)
                                 throws Exception
Loads an TextDocument from the provided path.

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

If the resource stream is not a ODF text document, ClassCastException might be thrown.

Parameters:
documentPath - - the path from where the document can be loaded
Returns:
the text 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 TextDocument loadDocument(File file)
                                 throws Exception
Creates an TextDocument from the OpenDocument provided by a File.

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

If the resource stream is not a ODF text document, ClassCastException might be thrown.

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

getContentRoot

public OfficeTextElement getContentRoot()
                                 throws Exception
Get the content root of a text document. Start here to get or create new elements of a text document like paragraphs, headings, tables or lists.

Overrides:
getContentRoot in class Document
Returns:
content root, representing the office:text tag
Throws:
Exception - if the file DOM could not be created.

newParagraph

public OdfTextParagraph newParagraph(String text)
                              throws Exception
Deprecated. As of Simple version 0.5, replaced by addParagraph(String text)

Creates a new paragraph and append text

Parameters:
text -
Returns:
the new paragraph
Throws:
Exception - if the file DOM could not be created.
See Also:
addParagraph(String)

newParagraph

public OdfTextParagraph newParagraph()
                              throws Exception
Deprecated. As of Simple version 0.5, replaced by Paragraph.newParagraph(ParagraphContainer)

Creates a new paragraph

Returns:
The new paragraph
Throws:
Exception - if the file DOM could not be created.
See Also:
Paragraph.newParagraph(ParagraphContainer), addParagraph(String)

addText

public OdfTextParagraph addText(String text)
                         throws Exception
Deprecated. As of Simple version 0.5, replaced by Paragraph.appendTextContent(String content)

Append text to the end of a text document. If there is no paragraph at the end of a document, a new one will be created.

Parameters:
text - initial text for the paragraph.
Returns:
The paragraph at the end of the text document, where the text has been added to.
Throws:
Exception - if the file DOM could not be created.
See Also:
Paragraph.appendTextContent(String), Paragraph.appendTextContentNotCollapsed(String), getParagraphByIndex(int, boolean), getParagraphByReverseIndex(int, boolean)

changeMode

public void changeMode(TextDocument.OdfMediaType mediaType)
Changes the document to the given mediatype. This method can only be used to convert a document to a related mediatype, e.g. template.

Parameters:
mediaType - the related ODF mimetype

appendSection

public Section appendSection(Section section,
                             boolean isResourceCopied)
Copy a section and append it at the end of the text document, whether the section is in this document or in a different document.

The IDs and names in this section would be changed to ensure unique.

If the section contains a linked resource, isResourceCopied would specify whether the linked resource would be copied or not, when the copy and append happens within a same document.

Parameters:
section - - the section object
isResourceCopied - - whether the linked resource is copied or not.

getHeader

public Header getHeader()
Get the Standard Page header of this text document.

Returns:
the Standard Page header of this text document.
Since:
0.4.5

getHeader

public Header getHeader(boolean isFirstPage)
Get the header of this text document.

Parameters:
isFirstPage - if isFirstPage is true, return the First Page header, otherwise return Standard Page header.
Returns:
the header of this text document.
Since:
0.5

getFooter

public Footer getFooter()
Get the Standard Page footer of this text document.

Returns:
the Standard Page footer of this text document.
Since:
0.4.5

getFooter

public Footer getFooter(boolean isFirstPage)
Get the footer of this text document.

Parameters:
isFirstPage - if isFirstPage is true, return the First Page footer, otherwise return Standard Page footer.
Returns:
the footer of this text document.
Since:
0.5

getTableContainerElement

public OdfElement getTableContainerElement()
Description copied from interface: TableContainer
Get the ODF element which can have as child element directly according to ODF specification. This Element will help to find the position to insert a new Table. For example, element is usable with element, so TextDocument will return OfficeTextElement. While Presentation Notes is an indirectly TableContainer, which holds Table with the help of its grand-child element , so for Notes, DrawTextBoxElement should be return.

Specified by:
getTableContainerElement in interface TableContainer
Returns:
container element which can hold .

getListContainerElement

public OdfElement getListContainerElement()
Description copied from interface: ListContainer
Get the ODF element which can have as child element directly according to ODF specification. This Element will help to find the position to insert a new List. For example, element is usable with element, so TextDocument will return OfficeTextElement. While Presentation Notes is an indirectly ListContainer, which holds List with the help of its grand-child element , so for Notes, DrawTextBoxElement should be return.

Specified by:
getListContainerElement in interface ListContainer
Returns:
container element which can hold .

addList

public List addList()
Description copied from interface: ListContainer
Add a new List to this container.

Specified by:
addList in interface ListContainer
Returns:
added list.

addList

public List addList(ListDecorator decorator)
Description copied from interface: ListContainer
Add a List with specified ListDecorator to this container.

Specified by:
addList in interface ListContainer
Parameters:
decorator - the specified ListDecorator
Returns:
added list.

clearList

public void clearList()
Description copied from interface: ListContainer
Remove all Lists from this container.

Specified by:
clearList in interface ListContainer

getListIterator

public Iterator<List> getListIterator()
Description copied from interface: ListContainer
Return an Iterator of the Lists in this ListContainer. The Lists are iterated in the same order that they occur in the ListContainer.

Specified by:
getListIterator in interface ListContainer
Returns:
an Iterator of the Lists in this ListContainer
See Also:
Iterator

removeList

public boolean removeList(List list)
Description copied from interface: ListContainer
Remove the existing List from this container.

Specified by:
removeList in interface ListContainer
Returns:
true, if the container contains this List.

addPageBreak

public void addPageBreak()
Appends a new page break to this document.

Since:
0.6.5

addPageBreak

public void addPageBreak(Paragraph refParagraph)
Appends a new page break to this document after the reference paragraph.

Parameters:
refParagraph - the reference paragraph after where the page break inserted.
Since:
0.6.5

setPageColumns

public void setPageColumns(int columnsNumber,
                           double spacing)
Defines several columns to the page whose style is specified.

Parameters:
columnsNumber - the number of columns (are of width identical)
spacing - column spacing in cm (ex. 2.40 for 2,4 cm)
Since:
0.6.6

addColumnBreak

public void addColumnBreak()
Appends a new column break to this document.

Since:
0.6.6

addColumnBreak

public void addColumnBreak(Paragraph refParagraph)
Appends a new column break to this document after the reference paragraph.

Parameters:
refParagraph - the reference paragraph after where the column break inserted.
Since:
0.6.6

addParagraph

public Paragraph addParagraph(String text)
Creates a new paragraph and append text.

Specified by:
addParagraph in interface ParagraphContainer
Parameters:
text - the text content of this paragraph
Returns:
the new paragraph

removeParagraph

public boolean removeParagraph(Paragraph para)
Remove paragraph from this document

Specified by:
removeParagraph in interface ParagraphContainer
Parameters:
para - the instance of paragraph
Returns:
true if the paragraph is removed successfully, false if errors happen.

getParagraphContainerElement

public OdfElement getParagraphContainerElement()
Description copied from interface: ParagraphContainer
Get the ODF element which can have as child element directly.

Specified by:
getParagraphContainerElement in interface ParagraphContainer
Returns:
- an ODF element which can have paragraph as child

getParagraphByIndex

public Paragraph getParagraphByIndex(int index,
                                     boolean isEmptyParagraphSkipped)
Description copied from interface: ParagraphContainer
Return a paragraph with a given index.

An index of zero represents the first paragraph.

If empty paragraph is skipped, the empty paragraph won't be counted.

Specified by:
getParagraphByIndex in interface ParagraphContainer
Parameters:
index - - the index started from 0.
isEmptyParagraphSkipped - - whether the empty paragraph is skipped or not
Returns:
the paragraph with a given index

getParagraphByReverseIndex

public Paragraph getParagraphByReverseIndex(int reverseIndex,
                                            boolean isEmptyParagraphSkipped)
Description copied from interface: ParagraphContainer
Return a paragraph with a given index. The index is in reverse order.

An index of zero represents the last paragraph.

If empty paragraph is skipped, the empty paragraph won't be counted.

Specified by:
getParagraphByReverseIndex in interface ParagraphContainer
Parameters:
reverseIndex - - the index started from 0 in reverse order.
isEmptyParagraphSkipped - - whether the empty paragraph is skipped or not
Returns:
the paragraph with a given index

getParagraphIterator

public Iterator<Paragraph> getParagraphIterator()
Description copied from interface: ParagraphContainer
Return an Iterator of the paragraph in this container.

Specified by:
getParagraphIterator in interface ParagraphContainer
Returns:
an Iterator of the paragraph in this container

declareVariable

public VariableField declareVariable(String name,
                                     VariableField.VariableType type)
Description copied from interface: VariableContainer
Declare a new variable field to this container.

Specified by:
declareVariable in interface VariableContainer
Parameters:
name - the unique name of this variable field.
type - the type of this variable field.
Returns:
declared variable field.

getVariableFieldByName

public VariableField getVariableFieldByName(String name)
Description copied from interface: VariableContainer
Return the declared variable by name.

Specified by:
getVariableFieldByName in interface VariableContainer
Parameters:
name - the unique name of this variable field.
Returns:
the declared variable.

getVariableContainerElement

public OdfElement getVariableContainerElement()
Description copied from interface: VariableContainer
Get the ODF element which can have variable declare section as child element directly according to ODF specification. This element will help to find the position to declare a new variable.

The variable declare section may be , or .

Specified by:
getVariableContainerElement in interface VariableContainer
Returns:
container element which can hold variable declare section.

createChart

public Chart createChart(String title,
                         DataSet dataset,
                         Rectangle rect)
Description copied from interface: ChartContainer
Creates a new Chart for this container.

Specified by:
createChart in interface ChartContainer
Parameters:
title - chart title.
dataset - chart data set.
rect - chart rectangle.
Returns:
the created chart.

createChart

public Chart createChart(String title,
                         SpreadsheetDocument document,
                         CellRangeAddressList cellRangeAddr,
                         boolean firstRowAsLabel,
                         boolean firstColumnAsLabel,
                         boolean rowAsDataSeries,
                         Rectangle rect)
Description copied from interface: ChartContainer
Creates a new Chart for this container.

Specified by:
createChart in interface ChartContainer
Parameters:
title - chart title.
document - the data source spreadsheet document.
cellRangeAddr - the cell range address list which is used as chart data set.
firstRowAsLabel - whether uses first row as label.
firstColumnAsLabel - whether uses first column as label.
rowAsDataSeries - whether uses data as series.
rect - chart rectangle.
Returns:
the created chart.

createChart

public Chart createChart(String title,
                         String[] labels,
                         String[] legends,
                         double[][] data,
                         Rectangle rect)
Description copied from interface: ChartContainer
Creates a new Chart for this container.

Specified by:
createChart in interface ChartContainer
Parameters:
title - chart rectangle.
labels - label strings
legends - legend strings
data - chart data set.
rect - chart rectangle.
Returns:
the created chart.

deleteChartById

public void deleteChartById(String chartId)
Description copied from interface: ChartContainer
Deletes chart by chart id.

Specified by:
deleteChartById in interface ChartContainer
Parameters:
chartId - the id of specified chart.

deleteChartByTitle

public void deleteChartByTitle(String title)
Description copied from interface: ChartContainer
Deletes chart(s) by chart title.

Specified by:
deleteChartByTitle in interface ChartContainer
Parameters:
title - the title of specified chart(s).

getChartById

public Chart getChartById(String chartId)
Description copied from interface: ChartContainer
Gets chart with specified id.

Specified by:
getChartById in interface ChartContainer
Parameters:
chartId - the id of this chart.
Returns:
the chart with specified id.

getChartByTitle

public List<Chart> getChartByTitle(String title)
Description copied from interface: ChartContainer
Gets chart list with specified title.

Specified by:
getChartByTitle in interface ChartContainer
Parameters:
title - the title of specified chart(s).
Returns:
the chart(s) with specified title.

getChartCount

public int getChartCount()
Description copied from interface: ChartContainer
Returns the chart count of this container

Specified by:
getChartCount in interface ChartContainer
Returns:
the chart count of this container


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