Class Document
An immutable word-processing document: a list of Blocks (paragraphs, tables and pictures),
the page they are laid out on and a title.
A document never changes. Editing produces a new one, which shares every unchanged block
with the old, so keeping old versions for undo costs little. DocumentModel holds the
current version, notifies listeners when it is replaced and keeps the undo history; it is to
a document what a com.codename1.ui.table.TableModel is to its rows.
Reading and writing:
try {
Document d = Document.read(new ByteArrayInputStream("# Notes\n\nSome *text*.".getBytes()),
Document.Format.MARKDOWN);
ByteArrayOutputStream out = new ByteArrayOutputStream();
d.write(out, Document.Format.DOCX);
Log.p("DOCX bytes: " + out.size());
} catch (IOException e) {
Log.e(e);
}
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumThe file formatsreadandwriteunderstand. -
Field Summary
FieldsModifier and TypeFieldDescriptionThe body, in order.The review comments, in document order.The footer of the first page, or null when the first page usesfooter.The header of the first page, or null when the first page usesheader.The blocks at the bottom of every page; empty for none.The blocks at the top of every page; empty for none.final longThe next free numeric id; seenewId.final PageSpecThe page geometry.final StringThe title, whichHTMLwrites as the page title.final booleanWhether edits made through an editor are recorded as revisions. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionThe comment with an id.booleanTwo documents are equal when their blocks, page and title are.get(int index) One block.inthashCode()A hash consistent withequals.intThe index of the top-level block with an id.A copy with a block inserted.newId(char prefix) A fresh id of the formprefix+ number that no block of this document uses.The whole text without formatting: paragraphs separated by newlines, table cells by tabs.static Documentread(InputStream in, Document.Format format) Reads a document.remove(int index) A copy without one block.intsize()The number of blocks.toString(Document.Format format) This document in a text format, for clipboards and previews.A copy with one block replaced.withBlocks(List<Block> value) A copy with a different body.withComments(List<Comment> value) A copy with different review comments.withHeaders(List<Block> pageHeader, List<Block> pageFooter, List<Block> firstPageHeader, List<Block> firstPageFooter) A copy with different page headers and footers.withNextId(long value) A copy with a different next id.A copy on a different page.A copy with a different title.withTrackChanges(boolean value) A copy with change tracking turned on or off.voidwrite(OutputStream out, Document.Format format) Writes this document.
-
Field Details
-
blocks
-
page
The page geometry. -
title
The title, whichHTMLwrites as the page title. -
nextId
public final long nextIdThe next free numeric id; seenewId. -
comments
-
trackChanges
public final boolean trackChangesWhether edits made through an editor are recorded as revisions. -
header
-
firstHeader
-
-
Constructor Details
-
Document
public Document()A document with one empty paragraph, on A4. -
Document
Creates a document.
Parameters
-
blocks: the body; copied. An empty body gets one empty paragraph, because an editor always needs somewhere to put the caret. -
page: the page geometry, or null for A4 -
title: the title, or null -
nextId: the next free numeric id; it is raised past anyp/t/i+ digits id in the body sonewIdnever collides
-
-
Document
public Document(List<Block> blocks, PageSpec page, String title, long nextId, List<Comment> comments, boolean trackChanges) Creates a document with review comments.
Parameters
-
blocks: the body; copied. An empty body gets one empty paragraph. -
page: the page geometry, or null for A4 -
title: the title, or null -
nextId: the next free numeric id -
comments: the review comments, or null for none; copied -
trackChanges: whether edits are recorded as revisions
-
-
-
Method Details
-
withHeaders
public Document withHeaders(List<Block> pageHeader, List<Block> pageFooter, List<Block> firstPageHeader, List<Block> firstPageFooter) A copy with different page headers and footers. Their paragraphs may hold
PAGEandNUMPAGESfields, which show the number of the page they are drawn on.Parameters
-
pageHeader: the blocks at the top of every page, or null for none -
pageFooter: the blocks at the bottom of every page, or null for none -
firstPageHeader: the first page's header, or null to usepageHeader; an empty list leaves the first page without one -
firstPageFooter: the first page's footer, or null to usepageFooter
Returns
the copy
-
-
newId
A fresh id of the form
prefix+ number that no block of this document uses. Use the documentwithNextId(nextId + 1)returns afterwards, orDocumentModel.newId, which does both.Parameters
prefix:pfor a paragraph,tfor a table,ifor a picture
Returns
the id
-
size
public int size()The number of blocks.
Returns
the block count
-
get
One block.
Parameters
index: from zero
Returns
the block
-
indexOf
The index of the top-level block with an id.
Parameters
id: the id
Returns
the index, or -1
-
withBlocks
-
withBlock
-
insert
-
remove
A copy without one block. Removing the last block leaves one empty paragraph.
Parameters
index: the block to remove
Returns
the copy
-
withPage
-
withTitle
-
withNextId
A copy with a different next id.
Parameters
value: the next free numeric id
Returns
the copy
-
withComments
-
withTrackChanges
A copy with change tracking turned on or off.
Parameters
value: whether edits are tracked
Returns
the new document
-
comment
-
plainText
The whole text without formatting: paragraphs separated by newlines, table cells by tabs.
Returns
the characters
-
read
Reads a document.
Parameters
-
in: the stream; it is read to the end but not closed -
format: what the stream holds
Returns
the document
Throws
IOException: when the stream fails or does not hold a document of that format
- Throws:
IOException
-
-
write
Writes this document.
Parameters
-
out: the stream; it is flushed but not closed -
format: what to write
Throws
IOException: when the stream fails
- Throws:
IOException
-
-
toString
This document in a text format, for clipboards and previews.
Parameters
format:TEXT,HTMLorMARKDOWN
Returns
the text
-
equals
-
hashCode
-