olg.csv.base.ods
Class ODSWriter

Package class diagram package ODSWriter
java.lang.Object
  extended by olg.csv.base.AbstractSheetWriter
      extended by olg.csv.base.ods.ODSWriter
All Implemented Interfaces:
Closeable, IWriter

public class ODSWriter
extends AbstractSheetWriter

ODS Writer based on ODFDOM API.


Nested Class Summary
static class ODSWriter.ODSParser
          This parser returns lines extracted from a string by interpreting \r, \n or \r\n as line breaks found in it.
 
Field Summary
private  AbstractParser<List<String>> cellParser
          The cell parser.
private  int currentLine
          the current row index to add.
private  boolean emptyingCell
          Indicates if document cells must be emptied.
private  boolean emtpyingRow
          Indicates if document rows must be emptied. useful when the rows to add do not follow.
private static org.slf4j.Logger LOGGER
          Class logger.
private  OutputStream out
          the out stream.
private  File outFile
          the out file.
private  org.odftoolkit.odfdom.doc.OdfSpreadsheetDocument outputDocument
          the ODS Document.
private  boolean overridding
          Indicates if the document must be overriding. true if the table was not created but opened in an existing document.
private  int size
          the size of the rows to add in the document.
private  org.odftoolkit.odfdom.doc.table.OdfTable table
          the table on which write rows.
 
Fields inherited from class olg.csv.base.AbstractSheetWriter
beginAtColumn, beginAtRow, sheetName, withHeaders
 
Constructor Summary
ODSWriter(File outFile, ODSSettings settings)
           
ODSWriter(OutputStream out, ODSSettings settings)
          Constructs ODSWriter with outputStream and Settings.
 
Method Summary
 void addLine(String[] values)
          Adds a row from the given String array.
 void addRow(Row row)
          Adds a row.
 void close()
          Close this stream and save the ODS Document to the file/stream and catch and log Exception if necessary. if the parent stream is provided and passed to its constructor by user, user must close it (close what you open!).
private  void createNewDocument(String sheetName)
          Create a new ODS Document with a table.
private  org.odftoolkit.odfdom.doc.table.OdfTable createSheet(String sheetName)
          creates a new empty ODF table in the ODS document.
private  void emptyCells(org.odftoolkit.odfdom.doc.table.OdfTableRow row, int begin, int end, boolean force)
          empties cells of a row if overriding and emptyingRow are true.
private  void emptyRows(int num)
          empties rows.
private  void insertCell(org.odftoolkit.odfdom.doc.table.OdfTableCell odfCell, String value)
          set a value on a cell.
private  void insertCell(org.odftoolkit.odfdom.doc.table.OdfTableRow odfRow, Cell cell)
          copy a cell in a row.
private  void insertCellAtNum(org.odftoolkit.odfdom.doc.table.OdfTableRow odfRow, int num, String value)
          Insert a cell in a row.
private  void loadDocument(String sheetName)
          Opens the outFile as an ODS Document and opens the table or create the table if not existe in the document.
 
Methods inherited from class olg.csv.base.AbstractSheetWriter
getSheetName, isWithHeaders
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOGGER

private static final org.slf4j.Logger LOGGER
Class logger.


outputDocument

private org.odftoolkit.odfdom.doc.OdfSpreadsheetDocument outputDocument
the ODS Document.


table

private org.odftoolkit.odfdom.doc.table.OdfTable table
the table on which write rows.


outFile

private File outFile
the out file.


out

private OutputStream out
the out stream.


cellParser

private final AbstractParser<List<String>> cellParser
The cell parser.


size

private int size
the size of the rows to add in the document.


currentLine

private int currentLine
the current row index to add.


emptyingCell

private final boolean emptyingCell
Indicates if document cells must be emptied. Useful when cells of a row to add do not follow.


emtpyingRow

private final boolean emtpyingRow
Indicates if document rows must be emptied. useful when the rows to add do not follow.


overridding

private boolean overridding
Indicates if the document must be overriding. true if the table was not created but opened in an existing document.

Constructor Detail

ODSWriter

public ODSWriter(File outFile,
                 ODSSettings settings)
          throws IOException
Parameters:
outFile - if outFile exists and contains sheet with specified sheetName, rewrites rows and cells in it. if sheetname sheet doesn't exists, creates it and writes on it. If outFile not exists, creates a new ODS File.
settings - the ODS Settings
Throws:
IOException - if an errors occurs on file opening.

ODSWriter

public ODSWriter(OutputStream out,
                 ODSSettings settings)
          throws IOException
Constructs ODSWriter with outputStream and Settings. Try to create a new ODS Document which will be saved

Parameters:
out - writes a new ODSDocument on it.
settings - ODS Settings
Throws:
IOException - if unable to create a new ODS Document
Method Detail

close

public void close()
Close this stream and save the ODS Document to the file/stream and catch and log Exception if necessary. if the parent stream is provided and passed to its constructor by user, user must close it (close what you open!).


addRow

public void addRow(Row row)
Adds a row.

The first row allows to define the size used along writing unless IWriter.addLine(String[]) had been used first.

Throws a WriterException when row size differs from initial size or row num is lesser than the expected next number line. Or if I/O error occurs during writing.

Parameters:
row - the row. It's possible to use a row with no cells. In this case, an empty line(a line wich all fields are null) is added. It's possible to use a row with a num greater than the expected number. In this case, empty lines are wroten as much as necessary. It's possible to use row which all cells are not specified. In this case the missing cells are added as empty cells. Must be not null with cells correctly ordered and size>0.

addLine

public void addLine(String[] values)
Adds a row from the given String array. The first time this method define the size ( as the given string array length) used along writing unless IWriter.addRow(Row) had been used first.

Throws a WriterException when line size differs from initial size.

Parameters:
values - the row. Must be not null with length >0. .

createSheet

private org.odftoolkit.odfdom.doc.table.OdfTable createSheet(String sheetName)
creates a new empty ODF table in the ODS document.

Parameters:
sheetName - the table name.
Returns:
the table

createNewDocument

private void createNewDocument(String sheetName)
                        throws IOException
Create a new ODS Document with a table.

Parameters:
sheetName - the name of the table
Throws:
IOException - if unable to create new ODS Document

loadDocument

private void loadDocument(String sheetName)
                   throws IOException
Opens the outFile as an ODS Document and opens the table or create the table if not existe in the document.

Parameters:
sheetName - the name of the table to open.
Throws:
IOException - if unable to load the document.

insertCell

private void insertCell(org.odftoolkit.odfdom.doc.table.OdfTableRow odfRow,
                        Cell cell)
copy a cell in a row.

Parameters:
odfRow - the row
cell - the cell to insert

insertCellAtNum

private void insertCellAtNum(org.odftoolkit.odfdom.doc.table.OdfTableRow odfRow,
                             int num,
                             String value)
Insert a cell in a row.

Parameters:
odfRow - the row
num - the column num after beginAtColumn
value - the cell value

insertCell

private void insertCell(org.odftoolkit.odfdom.doc.table.OdfTableCell odfCell,
                        String value)
set a value on a cell. Because line breaks are not correctly interpreted, insert a paragraph for each line identified by the cellParser.

Parameters:
odfCell - the cell
value - the value.

emptyRows

private void emptyRows(int num)
empties rows. Only if overriding and emptyingRow are true.

Parameters:
num - the number of rows to empty. begins at currentLine after beginAtRow value.

emptyCells

private void emptyCells(org.odftoolkit.odfdom.doc.table.OdfTableRow row,
                        int begin,
                        int end,
                        boolean force)
empties cells of a row if overriding and emptyingRow are true.

Parameters:
row - the row
begin - first column (include) after beginAtColumn value.
end - last column (exclude) after beginAtColumn value.
force - if true bypass overriding and emptyingCell condition.


Copyright © 2014. All Rights Reserved.