Class CsvSpreadSheetWriter

  • All Implemented Interfaces:
    AdditionalInformationHandler, Destroyable, GlobalInfoSupporter, EncodingSupporter, LoggingLevelHandler, LoggingSupporter, LocaleSupporter, OptionHandlingLocaleSupporter, OptionHandler, SizeOfHandler, Stoppable, StoppableWithFeedback, AppendableSpreadSheetWriter, IncrementalSpreadSheetWriter, NoHeaderSpreadSheetWriter, SpreadSheetWriter, SpreadSheetWriterWithFormulaSupport, Serializable
    Direct Known Subclasses:
    TsvSpreadSheetWriter

    public class CsvSpreadSheetWriter
    extends AbstractFormattedSpreadSheetWriter
    implements AppendableSpreadSheetWriter, SpreadSheetWriterWithFormulaSupport, IncrementalSpreadSheetWriter, NoHeaderSpreadSheetWriter
    Writes CSV files.

    -logging-level <OFF|SEVERE|WARNING|INFO|CONFIG|FINE|FINER|FINEST> (property: loggingLevel)
        The logging level for outputting errors and debugging output.
        default: WARNING
     
    -missing <java.lang.String> (property: missingValue)
        The placeholder for missing values.
        default: 
     
    -locale <java.util.Locale> (property: locale)
        The locale to use for formatting the numbers.
        default: Default
     
    -number-format <java.lang.String> (property: numberFormat)
        The format for the numbers (see java.text.DecimalFormat), use empty string 
        for default 'double' output.
        default: 
     
    -encoding <adams.core.base.BaseCharset> (property: encoding)
        The type of encoding to use when writing using a writer, use empty string 
        for default.
        default: Default
     
    -comment <java.lang.String> (property: comment)
        The string denoting the start of a line comment (comments can only precede 
        header row).
        default: #
     
    -output-comments <boolean> (property: outputComments)
        If enabled, any available comments are output before the actual data (using 
        the 'comment' prefix).
        default: true
     
    -output-as-displayed <boolean> (property: outputAsDisplayed)
        If enabled, cells are output as displayed, ie, results of formulas instead 
        of the formulas.
        default: false
     
    -no-header <boolean> (property: noHeader)
        If enabled, no header is output.
        default: false
     
    -check-file-exists <boolean> (property: checkFileExists)
        If enabled, it is checked each time whether the file exists; expensive test 
        if processing only one row at a time.
        default: false
     
    -appending <boolean> (property: appending)
        If enabled, multiple spreadsheets with the same structure can be written 
        to the same file.
        default: false
     
    -keep-existing <boolean> (property: keepExisting)
        If enabled, any output file that exists when the writer is executed for 
        the first time won't get replaced with the current header; useful when outputting 
        data in multiple locations in the flow, but one needs to be cautious as 
        to not stored mixed content (eg varying number of columns, etc).
        default: false
     
    -quote-char <java.lang.String> (property: quoteCharacter)
        The character to use for surrounding text cells.
        default: \"
     
    -separator <java.lang.String> (property: separator)
        The separator to use for the columns; use '\t' for tab.
        default: ,
     
    -new-line <java.lang.String> (property: newLine)
        The newline character(s) to use for the columns; use '\r' for carriage return 
        and '\n' for line feed; Linux/Unix use '\n', Windows uses '\r\n' and old 
        Macs use '\r'.
        default: \\n
     
    -always-quote-text <boolean> (property: alwaysQuoteText)
        If enabled, text/formula cells always get surrounded by double quotes.
        default: false
     
    -escape-special-chars <boolean> (property: escapeSpecialChars)
        If enabled, special characters get escaped with a backslash: \\, \', \t, 
        \n, \r, \"
        default: false
     
    -date-format <adams.data.DateFormatString> (property: dateFormat)
        The format for dates.
        default: yyyy-MM-dd
        more: http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
     
    -datetime-format <adams.data.DateFormatString> (property: dateTimeFormat)
        The format for date/times.
        default: yyyy-MM-dd HH:mm:ss
        more: http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
     
    -datetimemsec-format <adams.data.DateFormatString> (property: dateTimeMsecFormat)
        The format for date/time msecs.
        default: yyyy-MM-dd HH:mm:ss.SSS
        more: http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
     
    -time-format <adams.data.DateFormatString> (property: timeFormat)
        The format for times.
        default: HH:mm:ss
        more: http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
     
    Version:
    $Revision$
    Author:
    fracpete (fracpete at waikato dot ac dot nz)
    See Also:
    Serialized Form
    • Field Detail

      • m_Comment

        protected String m_Comment
        the line comment.
      • m_OutputComments

        protected boolean m_OutputComments
        whether to output the comments.
      • m_NoHeader

        protected boolean m_NoHeader
        whether to skip outputting the header.
      • m_Appending

        protected boolean m_Appending
        whether to append spreadsheets.
      • m_IsAppending

        protected boolean m_IsAppending
        whether the internal appending flag is one.
      • m_Header

        protected SpreadSheet m_Header
        the header of the first spreadsheet written to file, if appending is active.
      • m_KeepExisting

        protected boolean m_KeepExisting
        whether to keep existing files the first time the writer is called.
      • m_CheckFileExists

        protected boolean m_CheckFileExists
        whether to check whether file exists with each data item being processed.
      • m_FileExists

        protected boolean m_FileExists
        whether the file already exists.
      • m_OutputAsDisplayed

        protected boolean m_OutputAsDisplayed
        whether to output the cells as displayed (disable to output formulas).
      • m_QuoteCharacter

        protected String m_QuoteCharacter
        the quote character.
      • m_Separator

        protected String m_Separator
        the column separator.
      • m_NewLine

        protected String m_NewLine
        the new line.
      • m_AlwaysQuoteText

        protected boolean m_AlwaysQuoteText
        whether to always quote text cells.
      • m_EscapeSpecialChars

        protected boolean m_EscapeSpecialChars
        whether to escape special characters like \t, \n and \r.
      • m_DateFormat

        protected DateFormatString m_DateFormat
        the format string for the dates.
      • m_DateTimeFormat

        protected DateFormatString m_DateTimeFormat
        the format string for the date/times.
      • m_DateTimeMsecFormat

        protected DateFormatString m_DateTimeMsecFormat
        the format string for the date/time msecs.
      • m_TimeFormat

        protected DateFormatString m_TimeFormat
        the format string for the times.
      • m_TimeMsecFormat

        protected DateFormatString m_TimeMsecFormat
        the format string for the times with msec.
      • m_DateFormatter

        protected transient DateFormat m_DateFormatter
        the date formatter.
      • m_DateTimeFormatter

        protected transient DateFormat m_DateTimeFormatter
        the date/time formatter.
      • m_DateTimeMsecFormatter

        protected transient DateFormat m_DateTimeMsecFormatter
        the date/time msec formatter.
      • m_TimeFormatter

        protected transient DateFormat m_TimeFormatter
        the time formatter.
      • m_TimeMsecFormatter

        protected transient DateFormat m_TimeMsecFormatter
        the time msec formatter.
      • m_TimeZone

        protected TimeZone m_TimeZone
        the timezone to use.
    • Constructor Detail

      • CsvSpreadSheetWriter

        public CsvSpreadSheetWriter()
    • Method Detail

      • setComment

        public void setComment​(String value)
        Sets the string denoting the start of a line comment.
        Parameters:
        value - the comment start
      • getComment

        public String getComment()
        Returns the string denoting the start of a line comment.
        Returns:
        the comment start
      • commentTipText

        public String commentTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • setOutputComments

        public void setOutputComments​(boolean value)
        Sets whether to output the comments before the data.
        Parameters:
        value - true if to output comments
      • getOutputComments

        public boolean getOutputComments()
        Returns whether to output comments before the data.
        Returns:
        true if to output comments
      • outputCommentsTipText

        public String outputCommentsTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the explorer/experimenter gui
      • setOutputAsDisplayed

        public void setOutputAsDisplayed​(boolean value)
        Sets whether to output the cell content as displayed, ie, no formulas but the result of formulas.
        Specified by:
        setOutputAsDisplayed in interface SpreadSheetWriterWithFormulaSupport
        Parameters:
        value - true if to output as displayed
      • getOutputAsDisplayed

        public boolean getOutputAsDisplayed()
        Returns whether to output the cell content as displayed, ie, no formulas but the result of formulas.
        Specified by:
        getOutputAsDisplayed in interface SpreadSheetWriterWithFormulaSupport
        Returns:
        true if to output as displayed
      • canAppend

        public boolean canAppend​(SpreadSheet sheet)
        Checks whether we can append the specified spreadsheet to the existing file.
        Specified by:
        canAppend in interface AppendableSpreadSheetWriter
        Parameters:
        sheet - the spreadsheet to append to the existing one
        Returns:
        true if appending is possible
      • setAppending

        public void setAppending​(boolean value)
        Sets whether the next write call is to append the data to the existing file.
        Specified by:
        setAppending in interface AppendableSpreadSheetWriter
        Parameters:
        value - true if to append
      • isAppending

        public boolean isAppending()
        Returns whether the next spreadsheet will get appended.
        Specified by:
        isAppending in interface AppendableSpreadSheetWriter
        Returns:
        true if append is active
      • appendingTipText

        public String appendingTipText()
        Returns the tip text for this property.
        Specified by:
        appendingTipText in interface AppendableSpreadSheetWriter
        Returns:
        tip text for this property suitable for displaying in the explorer/experimenter gui
      • setNoHeader

        public void setNoHeader​(boolean value)
        Sets whether to use a header or not. file.
        Specified by:
        setNoHeader in interface NoHeaderSpreadSheetWriter
        Parameters:
        value - true if to skip header
      • noHeaderTipText

        public String noHeaderTipText()
        Returns the tip text for this property.
        Specified by:
        noHeaderTipText in interface NoHeaderSpreadSheetWriter
        Returns:
        tip text for this property suitable for displaying in the explorer/experimenter gui
      • setKeepExisting

        public void setKeepExisting​(boolean value)
        Sets whether to keep any existing file on first execution.
        Specified by:
        setKeepExisting in interface AppendableSpreadSheetWriter
        Parameters:
        value - if true then existing file is kept
      • getKeepExisting

        public boolean getKeepExisting()
        Returns whether any existing file is kept on first execution.
        Specified by:
        getKeepExisting in interface AppendableSpreadSheetWriter
        Returns:
        true if existing file is kept
      • keepExistingTipText

        public String keepExistingTipText()
        Returns the tip text for this property.
        Specified by:
        keepExistingTipText in interface AppendableSpreadSheetWriter
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • setCheckFileExists

        public void setCheckFileExists​(boolean value)
        Sets whether to check whether file exists with each data item being proceossed.
        Parameters:
        value - true if to check
      • getCheckFileExists

        public boolean getCheckFileExists()
        Returns whether check whether file exists with each data item being processed.
        Returns:
        true if to check
      • checkFileExistsTipText

        public String checkFileExistsTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • setQuoteCharacter

        public void setQuoteCharacter​(String value)
        Sets the character used for surrounding text.
        Parameters:
        value - the quote character
      • getQuoteCharacter

        public String getQuoteCharacter()
        Returns the string used as separator for the columns, '\t' for tab.
        Returns:
        the separator
      • quoteCharacterTipText

        public String quoteCharacterTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • setSeparator

        public void setSeparator​(String value)
        Sets the string to use as separator for the columns, use '\t' for tab.
        Parameters:
        value - the separator
      • getDefaultSeparator

        protected String getDefaultSeparator()
        Returns the default separator.
        Returns:
        the default
      • getSeparator

        public String getSeparator()
        Returns the string used as separator for the columns, '\t' for tab.
        Returns:
        the separator
      • separatorTipText

        public String separatorTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • setNewLine

        public void setNewLine​(String value)
        Sets the string to use as newline.
        Parameters:
        value - the character(s)
      • getNewLine

        public String getNewLine()
        Returns the string used as newline.
        Returns:
        the character(s)
      • newLineTipText

        public String newLineTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • setAlwaysQuoteText

        public void setAlwaysQuoteText​(boolean value)
        Sets whether to always surround text/formula cells by double quotes.
        Parameters:
        value - true if to always quote
      • getAlwaysQuoteText

        public boolean getAlwaysQuoteText()
        Returns whether to always surround text/formula cells by double quotes.
        Returns:
        true if append to always quote
      • alwaysQuoteTextTipText

        public String alwaysQuoteTextTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the explorer/experimenter gui
      • setEscapeSpecialChars

        public void setEscapeSpecialChars​(boolean value)
        Sets whether to escape special chars like \\t, \\r and \\n with a backslash.
        Parameters:
        value - true if to escape special chars
      • getEscapeSpecialChars

        public boolean getEscapeSpecialChars()
        Returns whether to escape special chars like \\t, \\r and \\n with a backslash.
        Returns:
        true if to escape special chars
      • escapeSpecialCharsTipText

        public String escapeSpecialCharsTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the explorer/experimenter gui
      • setDateFormat

        public void setDateFormat​(DateFormatString value)
        Sets the format for date columns.
        Parameters:
        value - the format
      • getDateFormat

        public DateFormatString getDateFormat()
        Returns the format for date columns.
        Returns:
        the format
      • dateFormatTipText

        public String dateFormatTipText()
        Returns the tip date for this property.
        Returns:
        tip date for this property suitable for displaying in the gui
      • setDateTimeFormat

        public void setDateTimeFormat​(DateFormatString value)
        Sets the format for date/time columns.
        Parameters:
        value - the format
      • getDateTimeFormat

        public DateFormatString getDateTimeFormat()
        Returns the format for date/time columns.
        Returns:
        the format
      • dateTimeFormatTipText

        public String dateTimeFormatTipText()
        Returns the tip date/time for this property.
        Returns:
        tip date for this property suitable for displaying in the gui
      • setDateTimeMsecFormat

        public void setDateTimeMsecFormat​(DateFormatString value)
        Sets the format for date/time msec columns.
        Parameters:
        value - the format
      • getDateTimeMsecFormat

        public DateFormatString getDateTimeMsecFormat()
        Returns the format for date/time msec columns.
        Returns:
        the format
      • dateTimeMsecFormatTipText

        public String dateTimeMsecFormatTipText()
        Returns the tip date/time for this property.
        Returns:
        tip date for this property suitable for displaying in the gui
      • setTimeFormat

        public void setTimeFormat​(DateFormatString value)
        Sets the format for time columns.
        Parameters:
        value - the format
      • getTimeFormat

        public DateFormatString getTimeFormat()
        Returns the format for time columns.
        Returns:
        the format
      • timeFormatTipText

        public String timeFormatTipText()
        Returns the tip time for this property.
        Returns:
        tip time for this property suitable for displaying in the gui
      • setTimeMsecFormat

        public void setTimeMsecFormat​(DateFormatString value)
        Sets the format for time msec columns.
        Parameters:
        value - the format
      • getTimeMsecFormat

        public DateFormatString getTimeMsecFormat()
        Returns the format for time msec columns.
        Returns:
        the format
      • timeMsecFormatTipText

        public String timeMsecFormatTipText()
        Returns the tip time for this property.
        Returns:
        tip time for this property suitable for displaying in the gui
      • setTimeZone

        public void setTimeZone​(TimeZone value)
        Sets the time zone to use.
        Parameters:
        value - the time zone
      • getTimeZone

        public TimeZone getTimeZone()
        Returns the time zone in use.
        Returns:
        the time zone
      • timeZoneTipText

        public String timeZoneTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the gui
      • setFileExists

        public void setFileExists​(boolean value)
        Sets whether the output file already exists.
        Specified by:
        setFileExists in interface AppendableSpreadSheetWriter
        Parameters:
        value - true if the output file already exists
      • getFileExists

        public boolean getFileExists()
        Returns whether the output file already exists.
        Specified by:
        getFileExists in interface AppendableSpreadSheetWriter
        Returns:
        true if the output file already exists
      • getDateFormatter

        protected DateFormat getDateFormatter()
        Returns the formatter for dates.
        Returns:
        the formatter
      • getDateTimeFormatter

        protected DateFormat getDateTimeFormatter()
        Returns the formatter for date/times.
        Returns:
        the formatter
      • getDateTimeMsecFormatter

        protected DateFormat getDateTimeMsecFormatter()
        Returns the formatter for date/time msecs.
        Returns:
        the formatter
      • getTimeFormatter

        protected DateFormat getTimeFormatter()
        Returns the formatter for times.
        Returns:
        the formatter
      • getTimeMsecFormatter

        protected DateFormat getTimeMsecFormatter()
        Returns the formatter for times with milli-seconds.
        Returns:
        the formatter
      • quoteString

        protected String quoteString​(String s)
        Quotes the string if necessary.
        Parameters:
        s - the string to quote, if necessary
        Returns:
        the potentially quoted string
      • quoteNumber

        protected String quoteNumber​(String s)
        Quotes the (numeric) string if necessary.
        Parameters:
        s - the (numeric) string to quote, if necessary
        Returns:
        the potentially quoted string
      • doWriteHeader

        protected boolean doWriteHeader​(Row header,
                                        Writer writer)
        Writes the header.
        Parameters:
        header - the header row to write
        writer - the writer to write the header to
      • writeHeader

        protected boolean writeHeader​(Row header,
                                      Writer writer)
        Writes the header, if necessary.
        Parameters:
        header - the header row to write
        writer - the writer to write the header to
      • doWrite

        protected boolean doWrite​(Row content,
                                  Writer writer)
        Performs the actual writing. The caller must ensure that the writer gets closed.
        Parameters:
        content - the row to write
        writer - the writer to write the spreadsheet to
        Returns:
        true if successfully written
      • doWrite

        protected boolean doWrite​(SpreadSheet content,
                                  Writer writer)
        Performs the actual writing. The caller must ensure that the writer gets closed.
        Overrides:
        doWrite in class AbstractSpreadSheetWriter
        Parameters:
        content - the spreadsheet to write
        writer - the writer to write the spreadsheet to
        Returns:
        true if successfully written
      • isIncremental

        public boolean isIncremental()
        Returns whether the writer can write data incrementally.
        Specified by:
        isIncremental in interface IncrementalSpreadSheetWriter
        Returns:
        true if data can be written incrementally
      • write

        public boolean write​(Row content,
                             File file)
        Writes the given content to the specified file.
        Specified by:
        write in interface IncrementalSpreadSheetWriter
        Parameters:
        content - the content to write
        file - the file to write to
        Returns:
        true if successfully written
      • checkFileExists

        protected void checkFileExists​(String filename)
        Resets the header if the m_CheckFileExists flag is set and the file no longer exists.
        Parameters:
        filename - the file to check
      • write

        public boolean write​(Row content,
                             String filename)
        Writes the spreadsheet to the given file.
        Specified by:
        write in interface IncrementalSpreadSheetWriter
        Parameters:
        content - the spreadsheet to write
        filename - the file to write the spreadsheet to
        Returns:
        true if successfully written
      • write

        public boolean write​(Row content,
                             OutputStream stream)
        Writes the spreadsheet to the given output stream. The caller must ensure that the stream gets closed.
        Specified by:
        write in interface IncrementalSpreadSheetWriter
        Parameters:
        content - the spreadsheet to write
        stream - the output stream to write the spreadsheet to
        Returns:
        true if successfully written
      • write

        public boolean write​(Row content,
                             Writer writer)
        Writes the spreadsheet to the given writer. The caller must ensure that the writer gets closed.
        Specified by:
        write in interface IncrementalSpreadSheetWriter
        Parameters:
        content - the spreadsheet to write
        writer - the writer to write the spreadsheet to
        Returns:
        true if successfully written