uk.ac.starlink.table.formats

## Class CsvTableWriter

- java.lang.Object
- - uk.ac.starlink.table.formats.DocumentedStreamStarTableWriter
  - - uk.ac.starlink.table.formats.CsvTableWriter

- All Implemented Interfaces:
    Documented, DocumentedIOHandler, StarTableWriter

      public class CsvTableWriter
      extends DocumentedStreamStarTableWriter

  A StarTableWriter which outputs to Comma-Separated Value format. This format is readable by `CsvTableBuilder`.
  Since:
    21 Sep 2004

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `CsvTableWriter()` Constructs a default CSV table writer.
    `CsvTableWriter(boolean writeHeader)` Constructs a CSV table writer which optionally writes headers.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `boolean` | `docIncludesExample()` Indicates whether the serialization of some (short) example table should be added to the user documentation for this handler.
    `char` | `getDelimiter()` Returns the delimiter character.
    `java.nio.charset.Charset` | `getEncoding()` Returns the encoding used for CSV output.
    `java.lang.String` | `getFormatName()` Returns "CSV" or "CSV-noheader".
    `int` | `getMaxFieldChars()` Returns the limit on the number of characters that will be written in a single field.
    `java.lang.String` | `getMimeType()` Returns a string suitable for use as the value of a MIME Content-Type header.
    `boolean` | `getWriteHeader()` Indicates whether an initial row containing column names will be written.
    `java.lang.String` | `getXmlDescription()` Returns user-directed documentation in XML format.
    `void` | `setDelimiter(char delimiter)` Sets the delimiter character.
    `void` | `setEncoding(java.nio.charset.Charset encoding)` Sets the character encoding used for the output CSV content.
    `void` | `setMaxFieldChars(int maxFieldChars)` Sets a limit on the number of characters that will be written in a single field.
    `void` | `setWriteHeader(boolean writeHeader)` Indicate whether an initial row containing column names should be written.
    `void` | `writeStarTable(StarTable table, java.io.OutputStream ostrm)` Writes a `StarTable` object to a given output stream.

    - ### Methods inherited from class uk.ac.starlink.table.formats.DocumentedStreamStarTableWriter

      `getExtensions, looksLikeFile, 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.formats.DocumentedIOHandler

      `matchesExtension, readText, toLink`

- - ### Constructor Detail

    - #### CsvTableWriter

          public CsvTableWriter()

      Constructs a default CSV table writer. This doesn't write a header.

    - #### CsvTableWriter

          public CsvTableWriter(boolean writeHeader)

      Constructs a CSV table writer which optionally writes headers.
      Parameters:
        `writeHeader` - true iff you want the first output line to contain column names

  - ### Method Detail

    - #### setWriteHeader

          @ConfigMethod(property="header",
                        doc="<p>If true, the first line of the CSV output will be a header containing the column names; if false, no header line is written and all lines represent data rows.</p>",
                        sequence=1)
          public void setWriteHeader(boolean writeHeader)

      Indicate whether an initial row containing column names should be written.
      Parameters:
        `writeHeader` - true iff you want the first output line to contain column names

    - #### getWriteHeader

          public boolean getWriteHeader()

      Indicates whether an initial row containing column names will be written.
      Returns:
        whether the first output line will contain column names

    - #### setDelimiter

          @ConfigMethod(property="delimiter",
                        doc="<p>Field delimiter character, by default a comma. Permitted values are a single character like \"<code>|</code>\", a hexadecimal character code like \"<code>0x7C</code>\", or one of the names \"<code>comma</code>\", \"<code>space</code>\" or \"<code>tab</code>\". Some choices of delimiter, for instance whitespace characters, might not work well or might behave in surprising ways.</p>",
                        example="|",
                        sequence=2)
          public void setDelimiter(char delimiter)

      Sets the delimiter character. Non-comma delimiters are not guaranteed to work.
      Parameters:
        `delimiter` - delimiter character

    - #### getDelimiter

          public char getDelimiter()

      Returns the delimiter character.
      Returns:
        delimiter

    - #### setMaxFieldChars

          @ConfigMethod(property="maxCell",
                        doc="<p>Maximum width in characters of an output table cell. Cells longer than this will be truncated.</p>",
                        example="160",
                        sequence=3)
          public void setMaxFieldChars(int maxFieldChars)

      Sets a limit on the number of characters that will be written in a single field. Fields beyond this length will be truncated.
      Parameters:
        `maxFieldChars` - new limit

    - #### getMaxFieldChars

          public int getMaxFieldChars()

      Returns the limit on the number of characters that will be written in a single field. Fields beyond this length will be truncated.
      Returns:
        current limit

    - #### setEncoding

          @ConfigMethod(property="encoding",
                        usage="ASCII|UTF-8|UTF-16|...",
                        example="UTF-16",
                        doc="<p>Specifies the character encoding used in the output CSV file.\n</p>")
          public void setEncoding(java.nio.charset.Charset encoding)

      Sets the character encoding used for the output CSV content. The default value is UTF-8.
      Parameters:
        `encoding` - character encoding

    - #### getEncoding

          public java.nio.charset.Charset getEncoding()

      Returns the encoding used for CSV output.
      Returns:
        character encoding

    - #### getFormatName

          public java.lang.String getFormatName()

      Returns "CSV" or "CSV-noheader".
      Returns:
        a short string identifying the output format of this writer

    - #### 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

    - #### docIncludesExample

          public boolean docIncludesExample()

      Description copied from interface: `DocumentedIOHandler`
      Indicates whether the serialization of some (short) example table should be added to the user documentation for this handler. Binary formats, or instances for which the `Documented.getXmlDescription()` method already includes some example output, should return false.
      Returns:
        true if the user documentation would benefit from the addition of an example serialization

    - #### getXmlDescription

          public java.lang.String getXmlDescription()

      Description copied from interface: `Documented`
      Returns user-directed documentation in XML format.

      The output should be a sequence of one or more <P> elements, using XHTML-like XML. Since rendering may be done in a number of contexts however, use of the full range of XHTML elements is discouraged. Where possible, the content should stick to simple markup such as the elements P, A, UL, OL, LI, DL, DT, DD EM, STRONG, I, B, CODE, TT, PRE.

      Returns:
        XML description of this object

    - #### writeStarTable

          public void writeStarTable(StarTable table,
                                     java.io.OutputStream ostrm)
                              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 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:
        `table` - the table to write
        `ostrm` - the output stream to which `startab` should be written
      Throws:
        `TableFormatException` - if this table cannot be written to a stream
        `java.io.IOException` - if there is some I/O error