Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

public final class Document

  1. Object
  2. 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.FormatThe file formats read and write understand.

Fields

public final List<Block> blocksThe body, in order.
public final PageSpec pageThe page geometry.
public final String titleThe title, which HTML writes as the page title.
public final long nextIdThe next free numeric id; see newId.
public final List<Comment> commentsThe review comments, in document order.
public final boolean trackChangesWhether edits made through an editor are recorded as revisions.
public final List<Block> headerThe blocks at the top of every page; empty for none.
public final List<Block> footerThe blocks at the bottom of every page; empty for none.
public final List<Block> firstHeaderThe header of the first page, or null when the first page uses header.
public final List<Block> firstFooterThe footer of the first page, or null when the first page uses footer.

Constructors

public Document()A document with one empty paragraph, on A4.
public Document(List<Block> blocks, PageSpec page, String title, long nextId)Creates a document.
public Document(List<Block> blocks, PageSpec page, String title, long nextId, List<Comment> comments, boolean trackChanges)Creates a document with review comments.

Methods

public Document withHeaders(List<Block> pageHeader, List<Block> pageFooter, List<Block> firstPageHeader, List<Block> firstPageFooter)A copy with different page headers and footers.
public String newId(char prefix)A fresh id of the form prefix + number that no block of this document uses.
public int size()The number of blocks.
public Block get(int index)One block.
public int indexOf(String id)The index of the top-level block with an id.
public Document withBlocks(List<Block> value)A copy with a different body.
public Document withBlock(int index, Block block)A copy with one block replaced.
public Document insert(int index, Block block)A copy with a block inserted.
public Document remove(int index)A copy without one block.
public Document withPage(PageSpec value)A copy on a different page.
public Document withTitle(String value)A copy with a different title.
public Document withNextId(long value)A copy with a different next id.
public Document withComments(List<Comment> value)A copy with different review comments.
public Document withTrackChanges(boolean value)A copy with change tracking turned on or off.
public Comment comment(String id)The comment with an id.
public String plainText()The whole text without formatting: paragraphs separated by newlines, table cells by tabs.
public static Document read(InputStream in, Document.Format format) throws IOExceptionReads a document.
public void write(OutputStream out, Document.Format format) throws IOExceptionWrites this document.
public String toString(Document.Format format)This document in a text format, for clipboards and previews.
public boolean equals(Object o)Two documents are equal when their blocks, page and title are.
public int hashCode()A hash consistent with equals.

Inherited methods

Field details

blocks

public final List<Block> blocks
The body, in order.

page

public final PageSpec page
The page geometry.

title

public final String title
The title, which HTML writes as the page title.

nextId

public final long nextId
The next free numeric id; see newId.

comments

public final List<Comment> comments
The review comments, in document order.

trackChanges

public final boolean trackChanges
Whether edits made through an editor are recorded as revisions.

firstHeader

public final List<Block> firstHeader
The header of the first page, or null when the first page uses header.

firstFooter

public final List<Block> firstFooter
The footer of the first page, or null when the first page uses footer.

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

blocks List<Block>
the body; copied. An empty body gets one empty paragraph, because an editor always needs somewhere to put the caret.
page PageSpec
the page geometry, or null for A4
title String
the title, or null
nextId long
the next free numeric id; it is raised past any p/t/i + digits id in the body so newId never 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 List<Block>
the body; copied. An empty body gets one empty paragraph.
page PageSpec
the page geometry, or null for A4
title String
the title, or null
nextId long
the next free numeric id
comments List<Comment>
the review comments, or null for none; copied
trackChanges boolean
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

pageHeader List<Block>
the blocks at the top of every page, or null for none
pageFooter List<Block>
the blocks at the bottom of every page, or null for none
firstPageHeader List<Block>
the first page’s header, or null to use pageHeader; an empty list leaves the first page without one
firstPageFooter List<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

prefix char
p for a paragraph, t for a table, i for 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

index int
from zero

Returns

the block

indexOf

public int indexOf(String id)
The index of the top-level block with an id.

Parameters

id String
the id

Returns

the index, or -1

withBlocks

public Document withBlocks(List<Block> value)
A copy with a different body.

Parameters

value List<Block>
the blocks

Returns

the copy

withBlock

public Document withBlock(int index, Block block)
A copy with one block replaced.

Parameters

index int
the block to replace
block Block
its replacement

Returns

the copy

insert

public Document insert(int index, Block block)
A copy with a block inserted.

Parameters

index int
where, 0 to size()
block Block
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

index int
the block to remove

Returns

the copy

withPage

public Document withPage(PageSpec value)
A copy on a different page.

Parameters

value PageSpec
the page geometry

Returns

the copy

withTitle

public Document withTitle(String value)
A copy with a different title.

Parameters

value String
the title

Returns

the copy

withNextId

public Document withNextId(long value)
A copy with a different next id.

Parameters

value long
the next free numeric id

Returns

the copy

withComments

public Document withComments(List<Comment> value)
A copy with different review comments.

Parameters

value List<Comment>
the comments

Returns

the new document

withTrackChanges

public Document withTrackChanges(boolean value)
A copy with change tracking turned on or off.

Parameters

value boolean
whether edits are tracked

Returns

the new document

comment

public Comment comment(String id)
The comment with an id.

Parameters

id String
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 IOException
Reads a document.

Parameters

in InputStream
the stream; it is read to the end but not closed
format Document.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 IOException
Writes this document.

Parameters

out OutputStream
the stream; it is flushed but not closed
format Document.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

format Document.Format
TEXT, HTML or MARKDOWN

Returns

the text

equals

public boolean equals(Object o)
Two documents are equal when their blocks, page and title are.

Parameters

o Object
the other object

Returns

true when equal

hashCode

public int hashCode()
A hash consistent with equals.

Returns

the hash