Package uk.ac.starlink.table.formats
Class AbstractTextTableWriter
- java.lang.Object
-
- uk.ac.starlink.table.StreamStarTableWriter
-
- uk.ac.starlink.table.formats.AbstractTextTableWriter
-
- All Implemented Interfaces:
StarTableWriter
- Direct Known Subclasses:
AsciiTableWriter
,IpacTableWriter
,TextTableWriter
public abstract class AbstractTextTableWriter 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.- Author:
- Mark Taylor (Starlink)
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractTextTableWriter(boolean writeParams)
Constructor.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract java.lang.String
formatValue(java.lang.Object val, ValueInfo vinfo, int width)
Formats a data value for output.protected byte[]
getBytes(java.lang.String str)
Returns a byte array corresponding to a given string.java.lang.String
getFormatName()
Returns "text";protected int
getMaximumParameterLength()
Returns the maximum length for the value of a parameter as passed toprintParam(java.io.OutputStream, java.lang.String, java.lang.String, java.lang.Class)
.abstract int
getMaxWidth()
Returns the maximum width for a given column.java.lang.String
getMimeType()
Returns a string suitable for use as the value of a MIME Content-Type header.int
getMinNameWidth(ColumnInfo info)
Returns the minimum width required to output the actual characters of the name for a given column.int
getSampledRows()
Returns the number of columns which will be sampled to work out the column width.boolean
getWriteParameters()
Finds out whether the output will include table parameters.protected abstract void
printColumnHeads(java.io.OutputStream strm, int[] colwidths, ColumnInfo[] cinfos)
Outputs headings for the table columns.protected abstract void
printLine(java.io.OutputStream strm, int[] colwidths, java.lang.String[] data)
Outputs a line of table data.protected abstract void
printParam(java.io.OutputStream strm, java.lang.String name, java.lang.String value, java.lang.Class clazz)
Outputs a parameter and its value.protected abstract void
printSeparator(java.io.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, java.io.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
-
Methods inherited from interface uk.ac.starlink.table.StarTableWriter
looksLikeFile
-
-
-
-
Method Detail
-
getFormatName
public java.lang.String getFormatName()
Returns "text";- Returns:
- "text"
-
getMimeType
public java.lang.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
-
writeStarTable
public void writeStarTable(StarTable startab, java.io.OutputStream strm) throws java.io.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 aBufferedOutputStream
), 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 writestrm
- the output stream to which startab should be written- Throws:
TableFormatException
- if this table cannot be written to a streamjava.io.IOException
- if there is some I/O error
-
getMaximumParameterLength
protected int getMaximumParameterLength()
Returns the maximum length for the value of a parameter as passed toprintParam(java.io.OutputStream, java.lang.String, java.lang.String, java.lang.Class)
. The default implementation currently returns 160.- Returns:
- maximum length for output string parameters
-
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
-
getMaxWidth
public abstract int getMaxWidth()
Returns the maximum width for a given column. Values longer than this may be truncated.- Returns:
- maximum permitted column width in characters
-
getMinNameWidth
public int getMinNameWidth(ColumnInfo info)
Returns the minimum width required to output the actual characters of the name for a given column. Padding applied subsequently by this object'sprintColumnHeads(java.io.OutputStream, int[], uk.ac.starlink.table.ColumnInfo[])
method does not need to be included.- Parameters:
info
- column metadata- Returns:
- minimum number of characters required for column title
-
getSampledRows
public int getSampledRows()
Returns the number of columns which will be sampled to work out the column width.- Returns:
- number of rows scanned
-
formatValue
protected abstract java.lang.String formatValue(java.lang.Object val, ValueInfo vinfo, int width)
Formats a data value for output.- Parameters:
val
- the valuevinfo
- the metadata object describing val's typewidth
- maximum preferred width into which the value should be formatted- Returns:
- formatted string meaning value, preferably no longer than width characters
-
printSeparator
protected abstract void printSeparator(java.io.OutputStream strm, int[] colwidths) throws java.io.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 intocolwidths
- column widths in characters- Throws:
java.io.IOException
-
printColumnHeads
protected abstract void printColumnHeads(java.io.OutputStream strm, int[] colwidths, ColumnInfo[] cinfos) throws java.io.IOException
Outputs headings for the table columns.- Parameters:
strm
- stream to write intocolwidths
- column widths in characterscinfos
- array of column headings- Throws:
java.io.IOException
-
printLine
protected abstract void printLine(java.io.OutputStream strm, int[] colwidths, java.lang.String[] data) throws java.io.IOException
Outputs a line of table data.- Parameters:
strm
- stream to write intocolwidths
- column widths in charactersdata
- array of strings to be output, one per column- Throws:
java.io.IOException
-
printParam
protected abstract void printParam(java.io.OutputStream strm, java.lang.String name, java.lang.String value, java.lang.Class clazz) throws java.io.IOException
Outputs a parameter and its value.- Parameters:
strm
- stream to write intoname
- parameter namevalue
- formatted parameter valueclazz
- type of value- Throws:
java.io.IOException
-
getBytes
protected byte[] getBytes(java.lang.String str)
Returns a byte array corresponding to a given string.- Parameters:
str
- string to decode
-
-