org.geotools.io
Class TableWriter

java.lang.Object
  extended byjava.io.Writer
      extended byjava.io.FilterWriter
          extended byorg.geotools.io.TableWriter

public class TableWriter
extends java.io.FilterWriter

A character stream that can be used to format tables. Columns are separated by tabulations ('\t') and rows are separated by line terminators ('\r', '\n' or "\r\n"). Every table's cells are stored in memory until flush() is invoked. When invoked, flush() copy cell's contents to the underlying stream while replacing tabulations by some amount of spaces. The exact number of spaces is computed from cell's widths. produces correct output when displayed with a monospace font.

For example, the following code...

     TableWriter out = new TableWriter(new OutputStreamWriter(System.out), 3);
     out.write("Pr?nom\tNom\n");
     out.nextLine('-');
     out.write("Id?phonse\tLaporte\nSarah\tCoursi\nYvan\tDubois");
     out.flush();
 
...produces the following output:
      Pr?nom      Nom
      ---------   -------
      Id?phonse   Laporte
      Sarah       Coursi
      Yvan        Dubois
 

Since:
2.0
Version:
$Id: TableWriter.java 17672 2006-01-19 00:25:55Z desruisseaux $
Author:
Martin Desruisseaux

Field Summary
static int ALIGN_CENTER
          A possible value for cell alignment.
static int ALIGN_LEFT
          A possible value for cell alignment.
static int ALIGN_RIGHT
          A possible value for cell alignment.
 
Fields inherited from class java.io.FilterWriter
out
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
TableWriter(java.io.Writer out)
          Create a new table writer with a default column separator.
TableWriter(java.io.Writer out, int spaces)
          Create a new table writer with the specified amount of spaces as column separator.
TableWriter(java.io.Writer out, java.lang.String separator)
          Create a new table writer with the specified column separator.
 
Method Summary
 void close()
          Flush the table content and close the underlying stream.
 void flush()
          Flush the table content to the underlying stream.
 int getAlignment()
          Returns the alignment for current and next cells.
 boolean isMultiLinesCells()
          Tells if EOL characters are used for line feeds inside current cells.
 void nextColumn()
          Moves one column to the right.
 void nextColumn(char fill)
          Moves one column to the right.
 void nextLine()
          Moves to the first column on the next row.
 void nextLine(char fill)
          Moves to the first column on the next row.
 void setAlignment(int alignment)
          Set the alignment for current and next cells.
 void setColumnAlignment(int column, int alignment)
          Set the alignment for all cells in the specified column.
 void setMultiLinesCells(boolean multiLines)
          Set the desired behavior for EOL and tabulations characters.
 java.lang.String toString()
          Returns the table content as a string.
 void write(char[] cbuf)
          Write an array of characters.
 void write(char[] cbuf, int offset, int length)
          Write a portion of an array of characters.
 void write(int c)
          Write a single character.
 void write(java.lang.String string)
          Write a string.
 void write(java.lang.String string, int offset, int length)
          Write a portion of a string.
 void writeHorizontalSeparator()
          Write an horizontal separator.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ALIGN_LEFT

public static final int ALIGN_LEFT
A possible value for cell alignment. This specifies that the text is aligned to the left indent and extra whitespace should be placed on the right.

See Also:
Constant Field Values

ALIGN_RIGHT

public static final int ALIGN_RIGHT
A possible value for cell alignment. This specifies that the text is aligned to the right indent and extra whitespace should be placed on the left.

See Also:
Constant Field Values

ALIGN_CENTER

public static final int ALIGN_CENTER
A possible value for cell alignment. This specifies that the text is aligned to the center and extra whitespace should be placed equally on the left and right.

See Also:
Constant Field Values
Constructor Detail

TableWriter

public TableWriter(java.io.Writer out)
Create a new table writer with a default column separator. Note: this writer may produces bad output on Windows console, unless the underlying stream use the correct codepage (e.g. OutputStreamWriter(System.out, "Cp437")). To display the appropriate codepage for a Windows NT console, type on the command line.

Parameters:
out - Writer object to provide the underlying stream, or if there is no underlying stream. If is null, then the toString() method is the only way to get the table's content.

TableWriter

public TableWriter(java.io.Writer out,
                   int spaces)
Create a new table writer with the specified amount of spaces as column separator.

Parameters:
out - Writer object to provide the underlying stream, or if there is no underlying stream. If is null, then the toString() method is the only way to get the table's content.
spaces - Amount of white spaces to use as column separator.

TableWriter

public TableWriter(java.io.Writer out,
                   java.lang.String separator)
Create a new table writer with the specified column separator.

Parameters:
out - Writer object to provide the underlying stream, or if there is no underlying stream. If is null, then the toString() method is the only way to get the table's content.
separator - String to write between columns. Drawing box characters are treated specially. For example " \\u2502 " can be used for a single-line box.
Method Detail

setMultiLinesCells

public void setMultiLinesCells(boolean multiLines)
Set the desired behavior for EOL and tabulations characters. The default value is .

Parameters:
multiLines - true if EOL are used for line feeds inside current cells, or if EOL move to the next row.

isMultiLinesCells

public boolean isMultiLinesCells()
Tells if EOL characters are used for line feeds inside current cells.


setColumnAlignment

public void setColumnAlignment(int column,
                               int alignment)
Set the alignment for all cells in the specified column. This method overwrite the alignment for all previous cells in the specified column.

Parameters:
column - The 0-based column number.
alignment - Cell alignment. Must be ALIGN_LEFT ALIGN_RIGHT or ALIGN_CENTER.

setAlignment

public void setAlignment(int alignment)
Set the alignment for current and next cells. Change to the alignment doesn't affect the alignment of previous cells and previous rows. The default alignment is ALIGN_LEFT.

Parameters:
alignment - Cell alignment. Must be ALIGN_LEFT ALIGN_RIGHT or ALIGN_CENTER.

getAlignment

public int getAlignment()
Returns the alignment for current and next cells.

Returns:
Cell alignment: ALIGN_LEFT (the default), ALIGN_RIGHT or ALIGN_CENTER.

write

public void write(int c)
Write a single character. If isMultiLinesCells() is false (which is the default), then:

Parameters:
c - Character to write.

write

public void write(java.lang.String string)
Write a string. Tabulations and line separators are interpreted as by write(int).

Parameters:
string - String to write.

write

public void write(java.lang.String string,
                  int offset,
                  int length)
Write a portion of a string. Tabulations and line separators are interpreted as by write(int).

Parameters:
string - String to write.
offset - Offset from which to start writing characters.
length - Number of characters to write.

write

public void write(char[] cbuf)
Write an array of characters. Tabulations and line separators are interpreted as by write(int).

Parameters:
cbuf - Array of characters to be written.

write

public void write(char[] cbuf,
                  int offset,
                  int length)
Write a portion of an array of characters. Tabulations and line separators are interpreted as by write(int).

Parameters:
cbuf - Array of characters.
offset - Offset from which to start writing characters.
length - Number of characters to write.

writeHorizontalSeparator

public void writeHorizontalSeparator()
Write an horizontal separator.


nextColumn

public void nextColumn()
Moves one column to the right. Next write operations will occur in a new cell on the same row.


nextColumn

public void nextColumn(char fill)
Moves one column to the right. Next write operations will occur in a new cell on the same row. This method fill every remaining space in the current cell with the specified character. For example calling nextColumn('*') from the first character of a cell is a convenient way to put a pad value in this cell.

Parameters:
fill - Character filling the cell (default to whitespace).

nextLine

public void nextLine()
Moves to the first column on the next row. Next write operations will occur on a new row.


nextLine

public void nextLine(char fill)
Moves to the first column on the next row. Next write operations will occur on a new row. This method fill every remaining cell in the current row with the specified character. Calling nextLine('-') from the first column of a row is a convenient way to fill this row with a line separator.

Parameters:
fill - Character filling the rest of the line (default to whitespace). This caracter may be use as a row separator.

flush

public void flush()
           throws java.io.IOException
Flush the table content to the underlying stream. This method should not be called before the table is completed (otherwise, columns may have the wrong width).

Throws:
java.io.IOException - if an output operation failed.

close

public void close()
           throws java.io.IOException
Flush the table content and close the underlying stream.

Throws:
java.io.IOException - if an output operation failed.

toString

public java.lang.String toString()
Returns the table content as a string.



Copyright © GeoTools. All Rights Reserved.