uk.ac.starlink.table.formats
Class TextTableWriter

java.lang.Object
  |
  +--uk.ac.starlink.table.StreamStarTableWriter
        |
        +--uk.ac.starlink.table.formats.TextTableWriter
All Implemented Interfaces:
StarTableWriter
Direct Known Subclasses:
AsciiTableWriter

public class TextTableWriter
extends StreamStarTableWriter

A StarTableWriter which outputs text to a human-readable text file. Table parameters (per-table metadata) can optionally be output as well as the table data themselves.


Constructor Summary
TextTableWriter()
           
 
Method Summary
protected  String formatValue(Object val, ValueInfo vinfo, int width)
          Formats a data value for output.
protected static byte[] getBytes(String str)
          Returns a byte array corresponding to a given string.
 String getFormatName()
          Returns "text";
 String getMimeType()
          Returns a string suitable for use as the value of a MIME Content-Type header.
 boolean getWriteParameters()
          Finds out whether the output will include table parameters.
 boolean looksLikeFile(String location)
          Returns true for location argument which ends in ".txt" or is equal to "-", indicating standard output.
protected  void printColumnHeads(OutputStream strm, int[] colwidths, ColumnInfo[] cinfos)
          Outputs headings for the table columns.
protected  void printLine(OutputStream strm, int[] colwidths, String[] data)
          Outputs a line of table data.
protected  void printParam(OutputStream strm, String name, String value)
          Outputs a parameter and its value.
protected  void printSeparator(OutputStream strm, int[] colwidths)
          Outputs a decorative separator line, of the sort you might find between the column headings and the table data.
 void setWriteParameters(boolean writeParams)
          Set whether the output should include table parameters.
 void writeStarTable(StarTable startab, OutputStream strm)
          Writes a StarTable object to a given output stream.
 
Methods inherited from class uk.ac.starlink.table.StreamStarTableWriter
writeStarTable
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TextTableWriter

public TextTableWriter()
Method Detail

getFormatName

public String getFormatName()
Returns "text";

Returns:
"text"

getMimeType

public String getMimeType()
Description copied from interface: StarTableWriter
Returns a string suitable for use as the value of a MIME Content-Type header. If no suitable MIME type is available or known, one of "application/octet-stream" (for binary formats) or "text/plain" for ASCII ones) is recommended.

Returns:
MIME content type

looksLikeFile

public boolean looksLikeFile(String location)
Returns true for location argument which ends in ".txt" or is equal to "-", indicating standard output.

Parameters:
location - the intended destination of the output
Returns:
whether it looks suitable for this class

writeStarTable

public void writeStarTable(StarTable startab,
                           OutputStream strm)
                    throws IOException
Description copied from interface: StarTableWriter
Writes a StarTable object to a given output stream. The implementation can assume that out is suitable for direct writing (for instance it should not normally wrap it in a BufferedOutputStream), and should not close it at the end of the call.

Not all table writers are capable of writing to a stream; an implementation may throw a TableFormatException to indicate that it cannot do so.

Parameters:
startab - the table to write
strm - the output stream to which startab should be written
Throws:
IOException - if there is some I/O error

setWriteParameters

public void setWriteParameters(boolean writeParams)
Set whether the output should include table parameters. If so they are written as name:value pairs one per line before the start of the table proper.

Parameters:
writeParams - true iff you want table parameters to be output as well as the table data

getWriteParameters

public boolean getWriteParameters()
Finds out whether the output will include table parameters.

Returns:
true iff the table parameters will be output as well as the table data

formatValue

protected String formatValue(Object val,
                             ValueInfo vinfo,
                             int width)
Formats a data value for output.

Parameters:
val - the value
vinfo - the metadata object describing val's type
width - maximum preferred width into which the value should be formatted
Returns:
formatted string meaning value, preferably no longer than width characters

printSeparator

protected void printSeparator(OutputStream strm,
                              int[] colwidths)
                       throws IOException
Outputs a decorative separator line, of the sort you might find between the column headings and the table data.

Parameters:
strm - stream to write into
colwidths - column widths in characters
IOException

printColumnHeads

protected void printColumnHeads(OutputStream strm,
                                int[] colwidths,
                                ColumnInfo[] cinfos)
                         throws IOException
Outputs headings for the table columns.

Parameters:
strm - stream to write into
colwidths - column widths in characters
IOException

printLine

protected void printLine(OutputStream strm,
                         int[] colwidths,
                         String[] data)
                  throws IOException
Outputs a line of table data.

Parameters:
strm - stream to write into
colwidths - column widths in characters
data - array of strings to be output, one per column
IOException

printParam

protected void printParam(OutputStream strm,
                          String name,
                          String value)
                   throws IOException
Outputs a parameter and its value.

Parameters:
strm - stream to write into
name - parameter name
value - formatted parameter value
IOException

getBytes

protected static byte[] getBytes(String str)
Returns a byte array corresponding to a given string.

Parameters:
str - string to decode

Copyright © 2004 CLRC: Central Laboratory of the Research Councils. All rights reserved.