uk.ac.starlink.ecsv

## Class EcsvTableWriter

- java.lang.Object
- - uk.ac.starlink.table.formats.DocumentedStreamStarTableWriter
  - - uk.ac.starlink.ecsv.EcsvTableWriter

- All Implemented Interfaces:
    Documented, DocumentedIOHandler, StarTableWriter

      public class EcsvTableWriter
      extends DocumentedStreamStarTableWriter

  TableWriter for ECSV output format. The format currently supported is ECSV 1.0, as documented at Astropy APE6.

  The current implementation avoids use of any YAML serialization library, it just uses print statements. This may facilitate its use in some contexts, and doing it like this seems straightforward enough. However if it turns out that the serialization is more complicated or error-prone than I thought it was, it might be worth revisiting this decision and using for instance the serialization facilities of the YAML parser library that is in any case a dependency of this package.

  Since:
    29 Apr 2020

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static EcsvTableWriter` | `COMMA_WRITER` Instance using commas for delimiters.
    `static EcsvTableWriter` | `SPACE_WRITER` Instance using spaces for delimiters.
    `static java.lang.String` | `TABLENAME_METAKEY` Meta map key for table name string value.
    `static java.lang.String` | `UCD_METAKEY` Meta map key for UCD string value.
    `static java.lang.String` | `UTYPE_METAKEY` Meta map key for Utype string value.
    `static java.lang.String` | `XTYPE_METAKEY` Meta map key for Xtype string value.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `EcsvTableWriter()` Constructs a writer with default characteristics.
    `EcsvTableWriter(char delimiter, java.lang.String nameSuffix)` Constructs a writer with a given delimiter character.

  - ### 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, either a space or a comma.
    `java.nio.charset.Charset` | `getEncoding()` Returns the character encoding for the input stream.
    `java.lang.String` | `getFormatName()` Gives the name of the format which is written by this writer.
    `java.lang.String` | `getMimeType()` Returns "text/plain".
    `java.lang.String` | `getXmlDescription()` Returns user-directed documentation in XML format.
    `void` | `setDelimiter(java.lang.String delimiter)` Sets the delimiter.
    `void` | `setEncoding(java.nio.charset.Charset encoding)` Sets the character encoding for the output stream.
    `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`

- - ### Field Detail

    - #### TABLENAME_METAKEY

          public static final java.lang.String TABLENAME_METAKEY

      Meta map key for table name string value.
      See Also:
        Constant Field Values

    - #### UCD_METAKEY

          public static final java.lang.String UCD_METAKEY

      Meta map key for UCD string value.
      See Also:
        Constant Field Values

    - #### UTYPE_METAKEY

          public static final java.lang.String UTYPE_METAKEY

      Meta map key for Utype string value.
      See Also:
        Constant Field Values

    - #### XTYPE_METAKEY

          public static final java.lang.String XTYPE_METAKEY

      Meta map key for Xtype string value.
      See Also:
        Constant Field Values

    - #### SPACE_WRITER

          public static final EcsvTableWriter SPACE_WRITER

      Instance using spaces for delimiters.

    - #### COMMA_WRITER

          public static final EcsvTableWriter COMMA_WRITER

      Instance using commas for delimiters.

  - ### Constructor Detail

    - #### EcsvTableWriter

          public EcsvTableWriter()

      Constructs a writer with default characteristics.

    - #### EcsvTableWriter

          public EcsvTableWriter(char delimiter,
                                 java.lang.String nameSuffix)

      Constructs a writer with a given delimiter character.
      Parameters:
        `delimiter` - field delimiter character; should be a space or comma
        `nameSuffix` - string to append to "ECSV" to provide the format name

  - ### Method Detail

    - #### getFormatName

          public java.lang.String getFormatName()

      Description copied from interface: `StarTableWriter`
      Gives the name of the format which is written by this writer. Matching against this string may be used by callers to identify or select this writer from a list.
      Returns:
        a short string identifying the output format of this writer

    - #### getMimeType

          public java.lang.String getMimeType()

      Returns "text/plain".
      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

    - #### setDelimiter

          @ConfigMethod(property="delimiter",
                        doc="<p>Delimiter character, which for ECSV may be either a space or a comma. Permitted values are \"<code>space</code>\" or \"<code>comma</code>\".</p>",
                        usage="comma|space",
                        example="comma",
                        sequence=1)
          public void setDelimiter(java.lang.String delimiter)

      Sets the delimiter. ECSV only permits the space or comma.
      Parameters:
        `delimiter` - delimiter character; may be "space", "comma", " " or ","
      Throws:
        `java.lang.IllegalArgumentException` - if not one of the permitted values

    - #### getDelimiter

          public char getDelimiter()

      Returns the delimiter character, either a space or a comma.
      Returns:
        delimiter character

    - #### setEncoding

          @ConfigMethod(property="encoding",
                        usage="ASCII|UTF-8|UTF-16|...",
                        example="UTF-8",
                        doc="<p>Specifies the character encoding used to write the output.\nECSV 1.0 is defined to use ASCII-encoded header and data sections,\nso setting this encoding to a non-ASCII value such as UTF-8\nwill write output that is not legal ECSV 1.0.\nHowever, such files may be readable by ECSV readers\nthat do not enforce the ASCII restriction.\n</p>",
                        sequence=2)
          public void setEncoding(java.nio.charset.Charset encoding)

      Sets the character encoding for the output stream. ECSV 1.0 by the book supports only ASCII, so that's the default, but this allows you to choose a different encoding such as UTF-8, which works without problems.
      Parameters:
        `encoding` - character encoding

    - #### getEncoding

          public java.nio.charset.Charset getEncoding()

      Returns the character encoding for the input stream.
      Returns:
        character encoding

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