Class Document

java.lang.Object
com.codename1.wordprocessing.Document

public final class Document extends Object

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);
}
  • 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: 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 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: 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 PAGE and NUMPAGES fields, 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 use pageHeader; an empty list leaves the first page without one

      • firstPageFooter: 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: 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: from zero
      Returns

      the block

    • indexOf

      public int indexOf(String id)

      The index of the top-level block with an id.

      Parameters
      • id: the id
      Returns

      the index, or -1

    • withBlocks

      public Document withBlocks(List<Block> value)

      A copy with a different body.

      Parameters
      • value: the blocks
      Returns

      the copy

    • withBlock

      public Document withBlock(int index, Block block)

      A copy with one block replaced.

      Parameters
      • index: the block to replace

      • block: its replacement

      Returns

      the copy

    • insert

      public Document insert(int index, Block block)

      A copy with a block inserted.

      Parameters
      • index: where, 0 to size()

      • 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: the block to remove
      Returns

      the copy

    • withPage

      public Document withPage(PageSpec value)

      A copy on a different page.

      Parameters
      • value: the page geometry
      Returns

      the copy

    • withTitle

      public Document withTitle(String value)

      A copy with a different title.

      Parameters
      • value: the title
      Returns

      the copy

    • withNextId

      public Document withNextId(long value)

      A copy with a different next id.

      Parameters
      • value: the next free numeric id
      Returns

      the copy

    • withComments

      public Document withComments(List<Comment> value)

      A copy with different review comments.

      Parameters
      • value: the comments
      Returns

      the new document

    • withTrackChanges

      public Document withTrackChanges(boolean value)

      A copy with change tracking turned on or off.

      Parameters
      • value: whether edits are tracked
      Returns

      the new document

    • comment

      public Comment comment(String id)

      The comment with an id.

      Parameters
      • id: 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: 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

      public void write(OutputStream out, Document.Format format) throws IOException

      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

      public String toString(Document.Format format)

      This document in a text format, for clipboards and previews.

      Parameters
      • 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: the other object
      Returns

      true when equal

      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()

      A hash consistent with equals.

      Returns

      the hash

      Overrides:
      hashCode in class Object