com.univocity.parsers.common
Class AbstractWriter<S extends CommonWriterSettings<?>>

java.lang.Object
  extended by com.univocity.parsers.common.AbstractWriter<S>
Type Parameters:
S - The specific writer settings configuration class, which can potentially provide additional configuration options supported by the writer implementation.
Direct Known Subclasses:
CsvWriter, FixedWidthWriter

public abstract class AbstractWriter<S extends CommonWriterSettings<?>>
extends Object

The AbstractWriter class provides a common ground for all writers in uniVocity-parsers. It handles all settings defined by CommonWriterSettings, and delegates the writing algorithm implementation to its subclasses through the abstract method processRow(Object[])

The following (absolutely required) attributes are exposed to subclasses:

Author:
uniVocity Software Pty Ltd - [email protected]
See Also:
CsvWriter, CsvWriterSettings, FixedWidthWriter, FixedWidthWriterSettings, WriterCharAppender, RowWriterProcessor

Field Summary
protected  CharAppender appender
           
protected  String emptyValue
           
protected  String nullValue
           
 
Constructor Summary
AbstractWriter(Writer writer, S settings)
          All writers must support, at the very least, the settings provided by CommonWriterSettings.
 
Method Summary
protected  void appendToRow(char ch)
          Appends the given character to the output row.
protected  void appendValueToRow()
          Appends the processed sequence of characters in appender to the output row.
 void close()
          Closes the Writer given in this class construtor.
 void commentRow(String comment)
          Writes a comment row to the output.
 void flush()
          Flushes the Writer given in this class constructor.
protected  String getStringValue(Object element)
          Converts a given object to its String representation for writing to the output.
 void processRecord(Object... record)
          Processes the data given for an individual record with the RowWriterProcessor provided by CommonWriterSettings.getRowWriterProcessor(), then writes it.
 void processRecord(Object record)
          Processes the data given for an individual record with the RowWriterProcessor provided by CommonWriterSettings.getRowWriterProcessor(), then writes it.
 void processRecords(Iterable<?> records)
          Iterates over all records, processes each one with the RowWriterProcessor provided by CommonWriterSettings.getRowWriterProcessor(), and writes them.
 void processRecords(Object[] records)
          Iterates over all records, processes each one with the RowWriterProcessor provided by CommonWriterSettings.getRowWriterProcessor(), and writes them.
 void processRecordsAndClose(Iterable<?> allRecords)
          Iterates over all records, processes each one with the RowWriterProcessor provided by CommonWriterSettings.getRowWriterProcessor(), and writes them, then finally and closes the output
 void processRecordsAndClose(Object[] allRecords)
          Iterates over all records, processes each one with the RowWriterProcessor provided by CommonWriterSettings.getRowWriterProcessor(), and writes them, then finally and closes the output
protected abstract  void processRow(Object[] row)
          Format-specific implementation for writing a single record into the output.
protected  int skipLeadingWhitespace(String element)
          Identifies the starting character index of a value being written if leading whitespaces are to be discarded.
 void writeEmptyRow()
          Writes an empty line to the output.
 void writeHeaders()
          Writes the headers defined in CommonSettings.getHeaders()
 void writeHeaders(Collection<String> headers)
          Writes the given collection of headers to the output.
 void writeHeaders(String... headers)
          Writes the given collection of headers to the output.
 void writeRow(Collection<Object> row)
          Writes the data given for an individual record.
 void writeRow(Object... row)
          Writes the data given for an individual record.
 void writeRow(String row)
          Writes a plain (potentially free-text) String as a line to the output.
 void writeRows(Collection<Object[]> rows)
          Iterates over all records and writes them to the output.
<C extends Collection<Object[]>>
void
writeRows(Iterable<C> rows)
          Iterates over all records and writes them to the output.
 void writeRows(Object[][] rows)
          Iterates over all records and writes them to the output.
 void writeRowsAndClose(Collection<Object[]> allRows)
          Iterates over all records, writes them and closes the output.
<C extends Collection<Object[]>>
void
writeRowsAndClose(Iterable<C> allRows)
          Iterates over all records, writes them and closes the output.
 void writeRowsAndClose(Object[][] allRows)
          Iterates over all records, writes them and closes the output.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

nullValue

protected final String nullValue

emptyValue

protected final String emptyValue

appender

protected final CharAppender appender
Constructor Detail

AbstractWriter

public AbstractWriter(Writer writer,
                      S settings)
All writers must support, at the very least, the settings provided by CommonWriterSettings. The AbstractWriter requires its configuration to be properly initialized.

Parameters:
writer - the output resource that will receive the format-specific records as defined by subclasses of AbstractWriter.
settings - the parser configuration
Method Detail

processRow

protected abstract void processRow(Object[] row)
Format-specific implementation for writing a single record into the output.

The AbstractWriter handles the initialization and processing of the output until it is ready to be written (generally, reorganizing it and passing it on to a RowWriterProcessor).

It then delegates the record to the writer-specific implementation defined by processRow(Object[]). In general, an implementation of processRow(Object[]) will perform the following steps:

Once the processRow(Object[]) method returns, a row will be written to the output with the processed information, and a newline will be automatically written after the given contents. The newline character sequence will conform to what is specified in Format.getLineSeparator()

This cycle repeats until the writing process is stopped by the user or an error happens.

In case of errors, the unchecked exception TextWritingException will be thrown and all resources in use will be closed automatically. The exception should contain the cause and more information about the output state when the error happened.

Parameters:
row - the data to be written to the output in the expected format.
See Also:
CharAppender, CommonWriterSettings

appendValueToRow

protected final void appendValueToRow()
Appends the processed sequence of characters in appender to the output row.


appendToRow

protected final void appendToRow(char ch)
Appends the given character to the output row.

Parameters:
ch - the character to append to the output row

writeHeaders

public final void writeHeaders()
Writes the headers defined in CommonSettings.getHeaders()

A TextWritingException will be thrown if no headers were defined or if records were already written to the output.


writeHeaders

public final void writeHeaders(Collection<String> headers)
Writes the given collection of headers to the output.

A TextWritingException will be thrown if no headers were defined or if records were already written to the output.

Parameters:
headers - the headers to write to the output.

writeHeaders

public final void writeHeaders(String... headers)
Writes the given collection of headers to the output.

A TextWritingException will be thrown if no headers were defined or if records were already written to the output.

Parameters:
headers - the headers to write to the output.

processRecordsAndClose

public final void processRecordsAndClose(Iterable<?> allRecords)
Iterates over all records, processes each one with the RowWriterProcessor provided by CommonWriterSettings.getRowWriterProcessor(), and writes them, then finally and closes the output

An IllegalStateException will be thrown if no RowWriterProcessor is provided by CommonWriterSettings.getRowWriterProcessor().

Parameters:
allRecords - the records to be transformed by a RowWriterProcessor and then written to the output

processRecordsAndClose

public final void processRecordsAndClose(Object[] allRecords)
Iterates over all records, processes each one with the RowWriterProcessor provided by CommonWriterSettings.getRowWriterProcessor(), and writes them, then finally and closes the output

An IllegalStateException will be thrown if no RowWriterProcessor is provided by CommonWriterSettings.getRowWriterProcessor().

Parameters:
allRecords - the records to be transformed by a RowWriterProcessor and then written to the output

processRecords

public final void processRecords(Iterable<?> records)
Iterates over all records, processes each one with the RowWriterProcessor provided by CommonWriterSettings.getRowWriterProcessor(), and writes them.

The output will remain open for further writing.

An IllegalStateException will be thrown if no RowWriterProcessor is provided by CommonWriterSettings.getRowWriterProcessor().

Parameters:
records - the records to be transformed by a RowWriterProcessor and then written to the output

processRecords

public final void processRecords(Object[] records)
Iterates over all records, processes each one with the RowWriterProcessor provided by CommonWriterSettings.getRowWriterProcessor(), and writes them.

The output will remain open for further writing.

An IllegalStateException will be thrown if no RowWriterProcessor was provided by CommonWriterSettings.getRowWriterProcessor().

Parameters:
records - the records to transformed by a RowWriterProcessor and then written to the output

processRecord

public final void processRecord(Object... record)
Processes the data given for an individual record with the RowWriterProcessor provided by CommonWriterSettings.getRowWriterProcessor(), then writes it.

The output will remain open for further writing.

An IllegalStateException will be thrown if no RowWriterProcessor is provided by CommonWriterSettings.getRowWriterProcessor().

Parameters:
record - the information of a single record to be transformed by a RowWriterProcessor and then written to the output

processRecord

public final void processRecord(Object record)
Processes the data given for an individual record with the RowWriterProcessor provided by CommonWriterSettings.getRowWriterProcessor(), then writes it.

The output will remain open for further writing.

An IllegalStateException will be thrown if no RowWriterProcessor is provided by CommonWriterSettings.getRowWriterProcessor().

Parameters:
record - the information of a single record to be transformed by a RowWriterProcessor and then written to the output

writeRowsAndClose

public final <C extends Collection<Object[]>> void writeRowsAndClose(Iterable<C> allRows)
Iterates over all records, writes them and closes the output.

Note this method will not use the RowWriterProcessor. Use processRecordsAndClose(Iterable) for that.

Parameters:
allRows - the rows to be written to the output

writeRowsAndClose

public final void writeRowsAndClose(Collection<Object[]> allRows)
Iterates over all records, writes them and closes the output.

Note this method will not use the RowWriterProcessor. Use processRecordsAndClose(Object[]) for that.

Parameters:
allRows - the rows to be written to the output

writeRowsAndClose

public final void writeRowsAndClose(Object[][] allRows)
Iterates over all records, writes them and closes the output.

Note this method will not use the RowWriterProcessor. Use processRecordsAndClose(Object[]) for that.

Parameters:
allRows - the rows to be written to the output

writeRows

public final void writeRows(Object[][] rows)
Iterates over all records and writes them to the output.

The output will remain open for further writing.

Note this method will not use the RowWriterProcessor. Use processRecords(Object[]) for that.

Parameters:
rows - the rows to be written to the output

writeRows

public final <C extends Collection<Object[]>> void writeRows(Iterable<C> rows)
Iterates over all records and writes them to the output.

The output will remain open for further writing.

Note this method will not use the RowWriterProcessor. Use processRecords(Iterable) for that.

Parameters:
rows - the rows to be written to the output

writeRows

public final void writeRows(Collection<Object[]> rows)
Iterates over all records and writes them to the output.

The output will remain open for further writing.

Note this method will not use the RowWriterProcessor. Use processRecords(Iterable) for that.

Parameters:
rows - the rows to be written to the output

writeRow

public final void writeRow(Collection<Object> row)
Writes the data given for an individual record.

The output will remain open for further writing.

Note this method will not use the RowWriterProcessor. Use processRecord(Object) for that.

Parameters:
row - the information of a single record to be written to the output

writeRow

public final void writeRow(Object... row)
Writes the data given for an individual record.

The output will remain open for further writing.

If the given data is null or empty, and CommonSettings.getSkipEmptyLines() is true, the input will be just ignored.

If CommonSettings.getSkipEmptyLines() is false, then an empty row will be written to the output (as specified by writeEmptyRow()).

In case of any errors, a TextWritingException will be thrown and the Writer given in the constructor will be closed.

Note this method will not use the RowWriterProcessor. Use processRecord(Object) for that.

Parameters:
row - the information of a single record to be written to the output

writeRow

public final void writeRow(String row)
Writes a plain (potentially free-text) String as a line to the output.

A newline will automatically written after the given contents. The newline character sequence will conform to what is specified in Format.getLineSeparator()

The writer implementation has no control over the format of this content.

The output will remain open for further writing.

Parameters:
row - the line to be written to the output

writeEmptyRow

public final void writeEmptyRow()
Writes an empty line to the output.

The newline character sequence will conform to what is specified in Format.getLineSeparator()

The output will remain open for further writing.


commentRow

public final void commentRow(String comment)
Writes a comment row to the output.

A newline will automatically written after the given contents. The newline character sequence will conform to what is specified in Format.getLineSeparator()

The output will remain open for further writing.

Parameters:
comment - the content to be written as a comment to the output

skipLeadingWhitespace

protected final int skipLeadingWhitespace(String element)
Identifies the starting character index of a value being written if leading whitespaces are to be discarded.

Implementation note whitespaces are considered all characters where ch <= ' ' evaluates to true

Parameters:
element - the String to be scanned for leading whitespaces.
Returns:
the index of the first non-whitespace character in the given element.

flush

public final void flush()
Flushes the Writer given in this class constructor.

An IllegalStateException will be thrown in case of any errors, and the writer will be closed.


close

public final void close()
Closes the Writer given in this class construtor.

An IllegalStateException will be thrown in case of any errors.


getStringValue

protected String getStringValue(Object element)
Converts a given object to its String representation for writing to the output.

Parameters:
element - the object to be converted into a String.
Returns:
the String representation of the given object


Copyright © 2014 uniVocity Software Pty Ltd. All rights reserved.