org.nocrala.tools.texttablefmt
Class StreamingTable

java.lang.Object
  extended by org.nocrala.tools.texttablefmt.StreamingTable

public class StreamingTable
extends java.lang.Object

Text table generator with reduced memory usage. This class will generate text tables like:

 
  +---------+-----------+----------+--------+
  |Country  | Population|Area (km2)| Density|
  +---------+-----------+----------+--------+
  |Chile    | 17 000 000| 1 250 000|   13.60|
  |Argentina| 50 000 000| 3 000 000|   16.67|
  |Brasil   | 80 000 000| 5 000 000|   16.00|
  +---------+-----------+----------+--------+
  |Total    |147 000 000| 9 250 000|   15.89|
  +---------+-----------+----------+--------+
  

where the border styles, shown borders/separators, column widths, alignments and other are configurable.

Cells are added using the addCell() method and the table is finished using the finishTable() method.

This class uses a reduced memory footprint, since it writes rows to the Appendable provided object each time a row is completed. Therefore it's suitable for rendering large tables.

All column widths default to 10 characters wide. Change this widths as desired BEFORE adding any cell, using the method setColumnWidth().

After adding all cells use the method finishTable() to flush any remaining characters to the Appendable object.

As an example, the following code

 
  CellStyle cs = new CellStyle(HorizontalAlign.left, AbbreviationStyle.crop,
      NullStyle.emptyString);
  StringBuffer sb = new StringBuffer();
  StreamingTable t = new StreamingTable(sb, 2, BorderStyle.CLASSIC,
      ShownBorders.ALL, false, "");
  t.addCell("abcdef", cs);
  t.addCell("123456", cs);
  t.addCell("mno", cs);
  t.addCell("45689", cs);
  t.addCell("xyztuvw", cs);
  t.addCell("01234567", cs);
  t.finishTable();
  System.out.println(sb.toString());
  

will generate the table:

  
  +-------+--------+
  |abcdef |123456  | 
  +-------+--------+ 
  |mno    |45689   | 
  +-------+--------+ 
  |xyztuvw|01234567| 
  +-------+--------+
  

The generated table can be customized using a BorderStyle, ShownBorders and cell widths. Besides, cell rendering can be customized on a cell basis using CellStyles.

Author:
valarcon

Constructor Summary
StreamingTable(java.lang.Appendable appendable, int totalColumns)
          Creates a streaming table that will write to an Appendable object using BorderStyle.CLASSIC and ShownBorders.SURROUND_HEADER_AND_COLUMNS, no XML escaping and no left margin.
StreamingTable(java.lang.Appendable appendable, int totalColumns, BorderStyle borderStyle)
          Creates a streaming table that will write to an Appendable object using a specific border style, showing ShownBorders.SURROUND_HEADER_AND_COLUMNS separators, no XML escaping and no left margin.
StreamingTable(java.lang.Appendable appendable, int totalColumns, BorderStyle borderStyle, ShownBorders shownBorders)
          Creates a streaming table that will write to an Appendable object using specific border style and shown borders, no XML escaping and no left margin.
StreamingTable(java.lang.Appendable appendable, int totalColumns, BorderStyle borderStyle, ShownBorders shownBorders, boolean escapeXml)
          Creates a streaming table that will write to an Appendable object using specific border style, shown borders and XML escaping and without left margin.
StreamingTable(java.lang.Appendable appendable, int totalColumns, BorderStyle borderStyle, ShownBorders shownBorders, boolean escapeXml, int leftMargin)
          Creates a streaming table that will write to an Appendable object using specific border style, shown borders, XML escaping and left margin.
StreamingTable(java.lang.Appendable appendable, int totalColumns, BorderStyle borderStyle, ShownBorders shownBorders, boolean escapeXml, java.lang.String prompt)
          Creates a streaming table that will write to an Appendable object using specific border style, shown borders, XML escaping and left margin.
 
Method Summary
 void addCell(java.lang.String content)
          Adds a cell with the default CellStyle.
 void addCell(java.lang.String content, CellStyle style)
          Adds a cell with a specific cell style.
 void addCell(java.lang.String content, CellStyle style, int colSpan)
          Adds a cell with a specific cell style and colspan.
 void addCell(java.lang.String content, int colSpan)
          Adds a cell with a colspan and the default CellStyle.
 void finishTable()
          Finishes the table rendering and flushes any remaining characters to the Appendable object.
 void setColumnWidth(int col, int width)
          Sets the width of a specific column.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StreamingTable

public StreamingTable(java.lang.Appendable appendable,
                      int totalColumns)
Creates a streaming table that will write to an Appendable object using BorderStyle.CLASSIC and ShownBorders.SURROUND_HEADER_AND_COLUMNS, no XML escaping and no left margin.

Parameters:
appendable - Character stream where to write the rendered table.
totalColumns - Total columns of this table.

StreamingTable

public StreamingTable(java.lang.Appendable appendable,
                      int totalColumns,
                      BorderStyle borderStyle)
Creates a streaming table that will write to an Appendable object using a specific border style, showing ShownBorders.SURROUND_HEADER_AND_COLUMNS separators, no XML escaping and no left margin.

Parameters:
appendable - Character stream where to write the rendered table.
totalColumns - Total columns of this table.
borderStyle - The border style to use when rendering the table.

StreamingTable

public StreamingTable(java.lang.Appendable appendable,
                      int totalColumns,
                      BorderStyle borderStyle,
                      ShownBorders shownBorders)
Creates a streaming table that will write to an Appendable object using specific border style and shown borders, no XML escaping and no left margin.

Parameters:
appendable - Character stream where to write the rendered table.
totalColumns - Total columns of this table.
borderStyle - The border style to use when rendering the table.
shownBorders - Specifies which borders will be rendered.

StreamingTable

public StreamingTable(java.lang.Appendable appendable,
                      int totalColumns,
                      BorderStyle borderStyle,
                      ShownBorders shownBorders,
                      boolean escapeXml)
Creates a streaming table that will write to an Appendable object using specific border style, shown borders and XML escaping and without left margin.

Parameters:
appendable - Character stream where to write the rendered table.
totalColumns - Total columns of this table.
borderStyle - The border style to use when rendering the table.
shownBorders - Specifies which borders will be rendered.
escapeXml - Specifies if the rendered text should be escaped using XML entities.

StreamingTable

public StreamingTable(java.lang.Appendable appendable,
                      int totalColumns,
                      BorderStyle borderStyle,
                      ShownBorders shownBorders,
                      boolean escapeXml,
                      int leftMargin)
Creates a streaming table that will write to an Appendable object using specific border style, shown borders, XML escaping and left margin.

Parameters:
appendable - Character stream where to write the rendered table.
totalColumns - Total columns of this table.
borderStyle - The border style to use when rendering the table.
shownBorders - Specifies which borders will be rendered.
escapeXml - Specifies if the rendered text should be escaped using XML entities.
leftMargin - Specifies how many blank spaces will be used as a left margin for the table.

StreamingTable

public StreamingTable(java.lang.Appendable appendable,
                      int totalColumns,
                      BorderStyle borderStyle,
                      ShownBorders shownBorders,
                      boolean escapeXml,
                      java.lang.String prompt)
Creates a streaming table that will write to an Appendable object using specific border style, shown borders, XML escaping and left margin.

Parameters:
appendable - Character stream where to write the rendered table.
totalColumns - Total columns of this table.
borderStyle - The border style to use when rendering the table.
shownBorders - Specifies which borders will be rendered.
escapeXml - Specifies if the rendered text should be escaped using XML entities.
prompt - Text to use as left margin for the table.
Method Detail

setColumnWidth

public void setColumnWidth(int col,
                           int width)
Sets the width of a specific column.

Parameters:
col - Column whose width will be set. First column is 0 (zero).
width - width of the column.

addCell

public void addCell(java.lang.String content)
             throws java.io.IOException
Adds a cell with the default CellStyle.

Parameters:
content - Cell text.
Throws:
java.io.IOException - if it is not possible to output to the Appendable object.

addCell

public void addCell(java.lang.String content,
                    int colSpan)
             throws java.io.IOException
Adds a cell with a colspan and the default CellStyle.

Parameters:
content - Cell text.
colSpan - Columns this cell will span through.
Throws:
java.io.IOException - if it is not possible to output to the Appendable object.

addCell

public void addCell(java.lang.String content,
                    CellStyle style)
             throws java.io.IOException
Adds a cell with a specific cell style.

Parameters:
content - Cell text.
style - Cell style to use when rendering the cell content.
Throws:
java.io.IOException - if it is not possible to output to the Appendable object.

addCell

public void addCell(java.lang.String content,
                    CellStyle style,
                    int colSpan)
             throws java.io.IOException
Adds a cell with a specific cell style and colspan.

Parameters:
content - Cell text.
style - Cell style to use when rendering the cell content.
colSpan - Columns this cell will span through.
Throws:
java.io.IOException - if it is not possible to output to the Appendable object.

finishTable

public void finishTable()
                 throws java.io.IOException
Finishes the table rendering and flushes any remaining characters to the Appendable object.

Throws:
java.io.IOException


Nocrala tools.