Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public final class Document
- Object
- 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 types
enum Document.Format | The file formats read and write understand. |
Fields
public final List<Block> blocks | The body, in order. |
public final PageSpec page | The page geometry. |
public final String title | The title, which HTML writes as the page title. |
public final long nextId | The next free numeric id; see newId. |
public final List<Comment> comments | The review comments, in document order. |
public final boolean trackChanges | Whether edits made through an editor are recorded as revisions. |
public final List<Block> header | The blocks at the top of every page; empty for none. |
public final List<Block> footer | The blocks at the bottom of every page; empty for none. |
public final List<Block> firstHeader | The header of the first page, or null when the first page uses header. |
public final List<Block> firstFooter | The footer of the first page, or null when the first page uses footer. |
Constructors
Methods
Inherited methods
Field details
blocks
public final List<Block> blocksThe body, in order.
page
public final PageSpec pageThe page geometry.
title
public final String titleThe title, which
HTML writes as the page title.nextId
public final long nextIdThe next free numeric id; see
newId.comments
public final List<Comment> commentsThe review comments, in document order.
trackChanges
public final boolean trackChangesWhether edits made through an editor are recorded as revisions.
header
public final List<Block> headerThe blocks at the top of every page; empty for none.
firstHeader
public final List<Block> firstHeaderThe header of the first page, or null when the first page uses
header.Constructor details
Document
public Document()A document with one empty paragraph, on A4.
Document
public Document(List<Block> blocks, PageSpec page, String title, long nextId)Creates a document.
Parameters
blocksList<Block>- the body; copied. An empty body gets one empty paragraph, because an editor always needs somewhere to put the caret.
pagePageSpec- the page geometry, or null for A4
titleString- the title, or null
nextIdlong- the next free numeric id; it is raised past any
p/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
blocksList<Block>- the body; copied. An empty body gets one empty paragraph.
pagePageSpec- the page geometry, or null for A4
titleString- the title, or null
nextIdlong- the next free numeric id
commentsList<Comment>- the review comments, or null for none; copied
trackChangesboolean- 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
PAGE and
NUMPAGES fields, which show the number of the page they are drawn on.Parameters
pageHeaderList<Block>- the blocks at the top of every page, or null for none
pageFooterList<Block>- the blocks at the bottom of every page, or null for none
firstPageHeaderList<Block>- the first page’s header, or null to use
pageHeader; an empty list leaves the first page without one firstPageFooterList<Block>- the first page’s footer, or null to use
pageFooter
Returns
the copy
newId
public String newId(char prefix)A fresh id of the form
prefix + number that no block of this document uses. Use the
document withNextId(nextId + 1) returns afterwards, or DocumentModel.newId, which
does both.Parameters
prefixcharpfor a paragraph,tfor a table,ifor a picture
Returns
the id
size
public int size()The number of blocks.
Returns
the block count
get
public Block get(int index)One block.
Parameters
indexint- from zero
Returns
the block
indexOf
public int indexOf(String id)The index of the top-level block with an id.
Parameters
idString- the id
Returns
the index, or -1
withBlocks
public Document withBlocks(List<Block> value)A copy with a different body.
Parameters
valueList<Block>- the blocks
Returns
the copy
withBlock
public Document withBlock(int index, Block block)A copy with one block replaced.
Parameters
indexint- the block to replace
blockBlock- its replacement
Returns
the copy
insert
public Document insert(int index, Block block)A copy with a block inserted.
Parameters
indexint- where, 0 to
size() blockBlock- the new block
Returns
the copy
remove
public Document remove(int index)A copy without one block. Removing the last block leaves one empty paragraph.
Parameters
indexint- the block to remove
Returns
the copy
withPage
public Document withPage(PageSpec value)A copy on a different page.
Parameters
valuePageSpec- the page geometry
Returns
the copy
withTitle
public Document withTitle(String value)A copy with a different title.
Parameters
valueString- the title
Returns
the copy
withNextId
public Document withNextId(long value)A copy with a different next id.
Parameters
valuelong- the next free numeric id
Returns
the copy
withComments
public Document withComments(List<Comment> value)A copy with different review comments.
Parameters
valueList<Comment>- the comments
Returns
the new document
withTrackChanges
public Document withTrackChanges(boolean value)A copy with change tracking turned on or off.
Parameters
valueboolean- whether edits are tracked
Returns
the new document
comment
public Comment comment(String id)The comment with an id.
Parameters
idString- the id
Returns
the comment, or null
plainText
public String plainText()The whole text without formatting: paragraphs separated by newlines, table cells by tabs.
Returns
the characters
read
public static Document read(InputStream in, Document.Format format)
throws IOExceptionReads a document.
Parameters
inInputStream- the stream; it is read to the end but not closed
formatDocument.Format- what the stream holds
Returns
the document
Throws
IOException- when the stream fails or does not hold a document of that format
write
public void write(OutputStream out, Document.Format format)
throws IOExceptionWrites this document.
Parameters
outOutputStream- the stream; it is flushed but not closed
formatDocument.Format- what to write
Throws
IOException- when the stream fails
toString
public String toString(Document.Format format)This document in a text format, for clipboards and previews.
Parameters
formatDocument.FormatTEXT,HTMLorMARKDOWN
Returns
the text
equals
public boolean equals(Object o)Two documents are equal when their blocks, page and title are.
Parameters
oObject- the other object
Returns
true when equal
hashCode
public int hashCode()A hash consistent with
equals.Returns
the hash