public abstract class DocumentWriter extends Object
DocumentWriter is an abstract class designed to help writing documents. It contains all the methods to define the structure of a document such as startDocument, openParagraph...
To write a document, you must call startDocument() first. Then build the structure of the document using the openXXX or addXXX methods. End the document with close()
The openXXX and closeXXX deals with the tree structure of the document and call the corresponding doXXX methods.
The doXXX methods are implemented in the DocumentWriter's subclass and must write the structure in the document.
For example, openHeader make sure we are at the "out" level in the document's tree then it calls doOpenHeader which must write the header itself
The protected instance variable documentProperties is a Map of properties (meta) that contains some informations about the document such as the title, the authors...
There is a list of class constants beginning with META_* for commonly used properties
They must be declared before the startDocument()
There is 3 methods to manipulate documentProperties :
DocumentExtra is a concept to allow to define content for a specific type of document.
A documentExtra is a StringBuffer containing the content.
documentExtras is a map of documentExtra with a key representing the id of the extra
When you implement a DocumentWriter subclass, you should register for the extra you are able to use
When you use a DocumentWriter subclass, if you want to add specific content, first test if the document support the extra, if it does get the documentExtra and append it your content.
There is 3 methods to manipulate documentExtras :
Allow you to set the style for the document.
They must be declared before the startDocument()
There is 2 methods to manipulate documentStyles :
To append a generic new line, use the newLine() function.
When you subclass DocumentWriter, you can overwrite newLine() or simply set a new value for the NEW_LINE variable.
DocumentStyle
Modifier and Type | Field and Description |
---|---|
protected String |
dirPrefix |
protected Map |
documentProperties |
protected DocumentStyle |
documentStyles |
static String |
META_AUTHOR |
static String |
META_DATE |
static String |
META_DESCRIPTION |
static String |
META_GENERATOR |
static String |
META_KEYWORDS |
static String |
META_TITLE |
String |
NEW_LINE |
protected OutputStream |
output |
protected File |
outputDir |
protected org.ginsim.common.document.DocumentPos |
pos |
static int |
POS_HEADER |
static int |
POS_LIST |
static int |
POS_LIST_ITEM |
static int |
POS_OUT |
static int |
POS_PARAGRAPH |
static int |
POS_TABLE |
static int |
POS_TABLE_CELL |
static int |
POS_TABLE_ROW |
static String |
PROP_SUBDOCUMENT |
Constructor and Description |
---|
DocumentWriter() |
Modifier and Type | Method and Description |
---|---|
void |
addAnchor(String name,
String content)
add a new anchor into the document.
|
void |
addImage(BufferedImage img,
String name) |
void |
addLink(String href,
String content)
add a new link into the document.
|
void |
addTableRow(String[] t_content)
Open a new table row and new table cells with the content of the array
|
void |
close()
Close every element open then close the document
|
void |
closeList()
Close a list.
|
void |
closeListItem()
Close a list item.
|
void |
closeParagraph()
Close a paragraph
|
void |
closeTable()
Close a table
|
void |
closeTableCell()
Close a table cell
|
void |
closeTableRow()
Close a table row
|
protected void |
closeUntil(int targetpos)
Close every opened element until we are in the correct position in the tree
|
protected abstract void |
doAddAnchor(String name,
String content) |
protected abstract void |
doAddImage(BufferedImage img,
String name) |
protected abstract void |
doAddLink(String href,
String content) |
protected abstract void |
doCloseDocument() |
protected abstract void |
doCloseList() |
protected abstract void |
doCloseListItem() |
protected abstract void |
doCloseParagraph() |
protected abstract void |
doCloseTable() |
protected abstract void |
doCloseTableCell() |
protected abstract void |
doCloseTableRow() |
boolean |
doesDocumentSupportExtra(String extra)
Indicate if the current document support a specific extra
|
protected abstract void |
doOpenHeader(int level,
String content,
String style) |
protected abstract void |
doOpenList(String style,
boolean isNumbered) |
protected abstract void |
doOpenListItem() |
protected abstract void |
doOpenParagraph(String style) |
protected abstract void |
doOpenTable(String name,
String style,
String[] t_colStyle) |
protected abstract void |
doOpenTableCell(int colspan,
int rowspan,
boolean header,
String style) |
protected abstract void |
doOpenTableRow(String style) |
protected abstract void |
doWriteText(String text,
boolean newLine) |
protected void |
ensureParagraph()
Open a new paragraph if we are in a paragraph.
|
StringBuffer |
getDocumentExtra(String extra)
Get a documentExtra
|
DocumentStyle |
getStyles()
Get the styles of the document, and initialize them if needed.
|
protected String |
newLine() |
void |
newParagraph()
Open a paragraph without a style
|
void |
openHeader(int level,
String content,
String style)
Open a new header.
|
void |
openList(String style)
Open a list element with a specific style
A list is made of ListItems
|
void |
openList(String style,
boolean isNumbered) |
void |
openListItem(String content)
Open a new list item and write the content in.
|
void |
openParagraph(String style)
Open a paragraph with a style
|
void |
openTable(String name,
String style,
String[] t_colStyle)
Open a new table.
|
void |
openTableCell(int colspan,
int rowspan,
String content)
Open a new cell in the current table row
|
void |
openTableCell(int colspan,
int rowspan,
String content,
boolean header)
Open a new cell in the current table row
|
void |
openTableCell(int colspan,
int rowspan,
String content,
String cl,
boolean header) |
void |
openTableCell(String content)
Open a new cell in the current table row with 1 colspan and rowspan
|
void |
openTableCell(String content,
boolean header) |
void |
openTableRow()
Open a new row in the current table.
|
void |
openTableRow(String style)
Open a new row in the current table.
|
protected void |
registerForDocumentExtra(String extra)
Register the current document for extra (initialize the StringBuffer)
|
void |
setDocumentProperties(Map map)
Add a map of properties to the document.
|
void |
setDocumentProperties(Object[] table)
Add an array of properties to the document
|
void |
setDocumentProperty(Object name,
Object value)
Set a property for the document.
|
void |
setOutput(File file) |
void |
setOutput(OutputStream output) |
DocumentStyle |
setStyles(DocumentStyle newStyle)
Define a new DocumentStyle for the document
|
abstract void |
startDocument() |
void |
writeText(String text)
Write the text into the current opened element.
|
void |
writeTextln(String text)
Write the text into the current opened element and append a new line at the end.
|
public static final int POS_OUT
public static final int POS_PARAGRAPH
public static final int POS_HEADER
public static final int POS_TABLE
public static final int POS_TABLE_ROW
public static final int POS_TABLE_CELL
public static final int POS_LIST
public static final int POS_LIST_ITEM
public static final String META_TITLE
public static final String META_AUTHOR
public static final String META_DATE
public static final String META_KEYWORDS
public static final String META_DESCRIPTION
public static final String META_GENERATOR
public static final String PROP_SUBDOCUMENT
public String NEW_LINE
protected org.ginsim.common.document.DocumentPos pos
protected Map documentProperties
protected DocumentStyle documentStyles
protected OutputStream output
protected File outputDir
protected String dirPrefix
public void setOutput(OutputStream output)
public void setOutput(File file) throws FileNotFoundException
FileNotFoundException
public abstract void startDocument() throws IOException
IOException
public void openParagraph(String style) throws IOException
style
- IOException
public void newParagraph() throws IOException
IOException
public void closeParagraph() throws IOException
IOException
public void openHeader(int level, String content, String style) throws IOException
level
- The header's level (1 = section, 2 = subsection, 3 = subsubsection...)content
- style
- IOException
public void addLink(String href, String content) throws IOException
href
- the link's location (URI)content
- the link's descriptionIOException
public void addAnchor(String name, String content) throws IOException
name
- the anchors location (URI)content
- the anchors descriptionIOException
public void openTable(String name, String style, String[] t_colStyle) throws IOException
name
- style
- t_colStyle
- a style for each column of the table. You must set the style, even if you do not use it. (use new String[] {"" , ""...} if you didn't want any style)IOException
public void openTableRow() throws IOException
IOException
public void openTableRow(String style) throws IOException
style
- TODOIOException
public void openTableCell(int colspan, int rowspan, String content) throws IOException
colspan
- the number of column to collapse together.rowspan
- the number of rows to collapse together.content
- the cell's contentIOException
public void openTableCell(int colspan, int rowspan, String content, boolean header) throws IOException
colspan
- the number of column to collapse together.rowspan
- the number of rows to collapse together.content
- the cell's contentheader
- if true the cell is an headerIOException
public void openTableCell(int colspan, int rowspan, String content, String cl, boolean header) throws IOException
IOException
public void openTableCell(String content) throws IOException
content
- the cell's contentIOException
public void addTableRow(String[] t_content) throws IOException
t_content
- the content for the cellsIOException
public void closeTableCell() throws IOException
IOException
public void closeTableRow() throws IOException
IOException
public void closeTable() throws IOException
IOException
public void openList(String style) throws IOException
style
- IOException
public void openList(String style, boolean isNumbered) throws IOException
IOException
public void openListItem(String content) throws IOException
content
- IOException
public void closeListItem() throws IOException
IOException
public void closeList() throws IOException
IOException
public void writeText(String text) throws IOException
text
- the text to writeIOException
public void writeTextln(String text) throws IOException
text
- the text to writeIOException
public void close() throws IOException
IOException
public void addImage(BufferedImage img, String name) throws IOException
IOException
protected abstract void doOpenParagraph(String style) throws IOException
IOException
protected abstract void doCloseParagraph() throws IOException
IOException
protected abstract void doWriteText(String text, boolean newLine) throws IOException
IOException
protected abstract void doOpenTable(String name, String style, String[] t_colStyle) throws IOException
IOException
protected abstract void doCloseTable() throws IOException
IOException
protected abstract void doCloseTableRow() throws IOException
IOException
protected abstract void doCloseTableCell() throws IOException
IOException
protected abstract void doOpenTableRow(String style) throws IOException
IOException
protected abstract void doOpenTableCell(int colspan, int rowspan, boolean header, String style) throws IOException
IOException
protected abstract void doCloseDocument() throws IOException
IOException
protected abstract void doOpenHeader(int level, String content, String style) throws IOException
IOException
protected abstract void doAddLink(String href, String content) throws IOException
IOException
protected abstract void doAddAnchor(String name, String content) throws IOException
IOException
protected abstract void doAddImage(BufferedImage img, String name) throws IOException
IOException
protected abstract void doOpenList(String style, boolean isNumbered) throws IOException
IOException
protected abstract void doOpenListItem() throws IOException
IOException
protected abstract void doCloseListItem() throws IOException
IOException
protected abstract void doCloseList() throws IOException
IOException
protected String newLine()
public void setDocumentProperty(Object name, Object value)
name
- the name of the property (use 'META' constant if possible)value
- the value of the propertypublic void setDocumentProperties(Map map)
map
- the map of propertiespublic void setDocumentProperties(Object[] table) throws ArrayIndexOutOfBoundsException
table
- A table that alternate the name then the value of propertiesArrayIndexOutOfBoundsException
- if there is not the same number of name and values.protected void registerForDocumentExtra(String extra)
extra
- The name of the extra to createpublic StringBuffer getDocumentExtra(String extra)
extra
- The name of the extra to getpublic boolean doesDocumentSupportExtra(String extra)
extra
- The name of the extrapublic DocumentStyle getStyles()
public DocumentStyle setStyles(DocumentStyle newStyle)
newStyle
- the new Styleprotected void ensureParagraph() throws IOException
IOException
protected void closeUntil(int targetpos) throws IOException
targetpos
- the positionIOException
public void openTableCell(String content, boolean header) throws IOException
IOException
Copyright © 2005–2020. All rights reserved.