Class AbstractRoutines<P extends CommonParserSettings<?>,W extends CommonWriterSettings<?>>
java.lang.Object
com.univocity.parsers.common.routine.AbstractRoutines<P,W>
- Type Parameters:
P
- parser configuration classW
- writer configuration class
- Direct Known Subclasses:
CsvRoutines
,FixedWidthRoutines
,TsvRoutines
public abstract class AbstractRoutines<P extends CommonParserSettings<?>,W extends CommonWriterSettings<?>>
extends Object
Basic implementation of commonly used routines around parsing/writing of data that can be reused and extended
by parsers/writers of any supported format.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate ColumnMapping
private boolean
private P
private Writer
private final String
private W
-
Constructor Summary
ConstructorsConstructorDescriptionAbstractRoutines
(String routineDescription) Creates a new instance of this routine class.AbstractRoutines
(String routineDescription, P parserSettings) Creates a new instance of this routine class.AbstractRoutines
(String routineDescription, P parserSettings, W writerSettings) Creates a new instance of this routine class.AbstractRoutines
(String routineDescription, W writerSettings) Creates a new instance of this routine class. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
adjustColumnLengths
(String[] headers, int[] lengths) Allows writers of any given format to adjust its settings to take into account column headers and lengths prior to writing data in any routine.private void
close
(AbstractWriter writer) protected abstract P
Creates a default parser settings configurationprotected abstract W
Creates a default writer settings configurationprotected abstract AbstractParser<P>
createParser
(P parserSettings) Creates a new parser implementation using the given parser configurationprotected abstract AbstractWriter<W>
createWriter
(Writer output, W writerSettings) Creates a new writer implementation using the given writer configurationprivate RowProcessor
createWritingRowProcessor
(Writer output) Returns a mapper that allows users to manually define mappings from attributes/methods of a given class to columns to be parsed or written in routines that manipulate java beans.getInputDimension
(File input) Calculates the dimensions of a file (row and column count).getInputDimension
(File input, String encoding) Calculates the dimensions of a file (row and column count).getInputDimension
(InputStream input) Calculates the dimensions of a given input (row and column count).getInputDimension
(InputStream input, String encoding) Calculates the dimensions of a given input (row and column count).getInputDimension
(Reader input) Calculates the dimensions of a given input (row and column count).boolean
Returns a flag indicating whether resources used for writing should be kept open after being used by the routines available from this object, when applicable.final P
Returns the parser configuration (if any) used by the routines of this utility class.final W
Returns the writer configuration (if any) used by the routines of this utility class.<T> IterableResult<T,
ParsingContext> Iterates over a file to produce instances of annotated java beans on demand.<T> IterableResult<T,
ParsingContext> Iterates over a file to produce instances of annotated java beans on demand.<T> IterableResult<T,
ParsingContext> Iterates over a file to produce instances of annotated java beans on demand.<T> IterableResult<T,
ParsingContext> iterate
(Class<T> beanType, InputStream input) Iterates over an input stream to produce instances of annotated java beans on demand.<T> IterableResult<T,
ParsingContext> iterate
(Class<T> beanType, InputStream input, String encoding) Iterates over an input stream to produce instances of annotated java beans on demand.<T> IterableResult<T,
ParsingContext> iterate
(Class<T> beanType, InputStream input, Charset encoding) Iterates over an input stream to produce instances of annotated java beans on demand.<T> IterableResult<T,
ParsingContext> Iterates over an input to produce instances of annotated java beans on demand.<T> List<T>
Parses a file into a list of annotated java beans<T> List<T>
Parses a file into a list of annotated java beans<T> List<T>
Parses a file into a list of annotated java beans<T> List<T>
Parses a file into a list of annotated java beans<T> List<T>
Parses a file into a list of annotated java beans<T> List<T>
Parses a file into a list of annotated java beans<T> List<T>
parseAll
(Class<T> beanType, InputStream input) Parses an input stream into a list of annotated java beans<T> List<T>
parseAll
(Class<T> beanType, InputStream input, int expectedBeanCount) Parses an input stream into a list of annotated java beans<T> List<T>
parseAll
(Class<T> beanType, InputStream input, String encoding) Parses an input stream into a list of annotated java beans<T> List<T>
parseAll
(Class<T> beanType, InputStream input, String encoding, int expectedBeanCount) Parses an input stream into a list of annotated java beans<T> List<T>
parseAll
(Class<T> beanType, InputStream input, Charset encoding) Parses an input stream into a list of annotated java beans<T> List<T>
parseAll
(Class<T> beanType, InputStream input, Charset encoding, int expectedBeanCount) Parses an input stream into a list of annotated java beans<T> List<T>
Parses the input into a list of annotated java beans<T> List<T>
Parses the input into a list of annotated java beansfinal void
parseAndWrite
(Reader input, Writer output) Reads all data from a given input and writes it to an output.void
setColumnMapper
(ColumnMapper columnMapper) Copies the given column mappings over to this processor.void
setKeepResourcesOpen
(boolean keepResourcesOpen) Allows preventing resources used for writing from being closed after being used by the routines available from this object, when applicable.final void
setParserSettings
(P parserSettings) Defines the parser configuration to be used by the routines of this utility class.private void
setRowProcessor
(RowProcessor rowProcessor) private void
setRowWriterProcessor
(RowWriterProcessor rowWriterProcessor) final void
setWriterSettings
(W writerSettings) Defines the writer configuration to be used by the routines of this utility class.toString()
private void
private void
final void
Dumps the content of aResultSet
into a file.final void
Dumps the content of aResultSet
into a file.final void
Dumps the content of aResultSet
into a file.final void
write
(ResultSet rs, OutputStream output) Dumps the content of aResultSet
into an output stream.final void
write
(ResultSet rs, OutputStream output, String encoding) Dumps the content of aResultSet
into an output stream.final void
write
(ResultSet rs, OutputStream output, Charset encoding) Dumps the content of aResultSet
into an output stream.final void
Dumps the content of aResultSet
.<T> void
Writes a collection of annotated java beans to a given output.<T> void
Writes a collection of annotated java beans to a given output.<T> void
Writes a collection of annotated java beans to a given output.<T> void
writeAll
(Iterable<T> elements, Class<T> beanType, OutputStream output, String... headers) Writes a collection of annotated java beans to a given output.<T> void
writeAll
(Iterable<T> elements, Class<T> beanType, OutputStream output, String encoding, String[] headers) Writes a collection of annotated java beans to a given output.<T> void
writeAll
(Iterable<T> elements, Class<T> beanType, OutputStream output, Charset encoding, String... headers) Writes a collection of annotated java beans to a given output.<T> void
Writes a collection of annotated java beans to a given output.
-
Field Details
-
keepResourcesOpen
private boolean keepResourcesOpen -
previousOutput
-
columnMapper
-
routineDescription
-
parserSettings
-
writerSettings
-
-
Constructor Details
-
AbstractRoutines
Creates a new instance of this routine class.- Parameters:
routineDescription
- description of the routines for a given format
-
AbstractRoutines
Creates a new instance of this routine class.- Parameters:
routineDescription
- description of the routines for a given formatparserSettings
- configuration to use for parsing
-
AbstractRoutines
Creates a new instance of this routine class.- Parameters:
routineDescription
- description of the routines for a given formatwriterSettings
- configuration to use for writing
-
AbstractRoutines
Creates a new instance of this routine class.- Parameters:
routineDescription
- description of the routines for a given formatparserSettings
- configuration to use for parsingwriterSettings
- configuration to use for writing
-
-
Method Details
-
createParser
Creates a new parser implementation using the given parser configuration- Parameters:
parserSettings
- the configuration for new parser- Returns:
- a parser implementation configured according to the given settings object.
-
createWriter
Creates a new writer implementation using the given writer configuration- Parameters:
output
- target output of the routine.writerSettings
- the configuration for new writer- Returns:
- a writer implementation configured according to the given settings object.
-
createDefaultParserSettings
Creates a default parser settings configuration- Returns:
- a new instance of a usable parser configuration.
-
createDefaultWriterSettings
Creates a default writer settings configuration- Returns:
- a new instance of a usable writer configuration.
-
validateWriterSettings
private void validateWriterSettings() -
validateParserSettings
private void validateParserSettings() -
getParserSettings
Returns the parser configuration (if any) used by the routines of this utility class.- Returns:
- the parser configuration.
-
setParserSettings
Defines the parser configuration to be used by the routines of this utility class.- Parameters:
parserSettings
- the parser configuration.
-
getWriterSettings
Returns the writer configuration (if any) used by the routines of this utility class.- Returns:
- the writer configuration.
-
setWriterSettings
Defines the writer configuration to be used by the routines of this utility class.- Parameters:
writerSettings
- the parser configuration.
-
adjustColumnLengths
Allows writers of any given format to adjust its settings to take into account column headers and lengths prior to writing data in any routine.- Parameters:
headers
- headers to be writtenlengths
- the corresponding lengths of each header
-
write
Dumps the content of aResultSet
into a file. -
write
Dumps the content of aResultSet
into a file. -
write
Dumps the content of aResultSet
into a file. -
write
Dumps the content of aResultSet
into an output stream. -
write
Dumps the content of aResultSet
into an output stream. -
write
Dumps the content of aResultSet
into an output stream. -
write
Dumps the content of aResultSet
. -
parseAndWrite
Reads all data from a given input and writes it to an output.- Parameters:
input
- the input data to be parsed using the settings provided ingetParserSettings()
output
- the output into where the input data should be written, using the format provided ingetWriterSettings()
-
setRowWriterProcessor
-
setRowProcessor
-
createWritingRowProcessor
-
close
-
writeAll
Writes a collection of annotated java beans to a given output.- Type Parameters:
T
- the type of element in the given collection- Parameters:
elements
- the elements to write to the outputbeanType
- the type of element in the given collectionoutput
- the output into which the given elements will be writtenheaders
- headers to use in the first row of the written result.
-
writeAll
public <T> void writeAll(Iterable<T> elements, Class<T> beanType, File output, String encoding, String[] headers) Writes a collection of annotated java beans to a given output.- Type Parameters:
T
- the type of element in the given collection- Parameters:
elements
- the elements to write to the outputbeanType
- the type of element in the given collectionoutput
- the output into which the given elements will be writtenencoding
- the output encoding to use for writingheaders
- headers to use in the first row of the written result.
-
writeAll
public <T> void writeAll(Iterable<T> elements, Class<T> beanType, File output, Charset encoding, String... headers) Writes a collection of annotated java beans to a given output.- Type Parameters:
T
- the type of element in the given collection- Parameters:
elements
- the elements to write to the outputbeanType
- the type of element in the given collectionoutput
- the output into which the given elements will be writtenencoding
- the output encoding to use for writingheaders
- headers to use in the first row of the written result.
-
writeAll
public <T> void writeAll(Iterable<T> elements, Class<T> beanType, OutputStream output, String... headers) Writes a collection of annotated java beans to a given output.- Type Parameters:
T
- the type of element in the given collection- Parameters:
elements
- the elements to write to the outputbeanType
- the type of element in the given collectionoutput
- the output into which the given elements will be writtenheaders
- headers to use in the first row of the written result.
-
writeAll
public <T> void writeAll(Iterable<T> elements, Class<T> beanType, OutputStream output, String encoding, String[] headers) Writes a collection of annotated java beans to a given output.- Type Parameters:
T
- the type of element in the given collection- Parameters:
elements
- the elements to write to the outputbeanType
- the type of element in the given collectionoutput
- the output into which the given elements will be writtenencoding
- the output encoding to use for writingheaders
- headers to use in the first row of the written result.
-
writeAll
public <T> void writeAll(Iterable<T> elements, Class<T> beanType, OutputStream output, Charset encoding, String... headers) Writes a collection of annotated java beans to a given output.- Type Parameters:
T
- the type of element in the given collection- Parameters:
elements
- the elements to write to the outputbeanType
- the type of element in the given collectionoutput
- the output into which the given elements will be writtenencoding
- the output encoding to use for writingheaders
- headers to use in the first row of the written result.
-
writeAll
Writes a collection of annotated java beans to a given output.- Type Parameters:
T
- the type of element in the given collection- Parameters:
elements
- the elements to write to the outputbeanType
- the type of element in the given collectionoutput
- the output into which the given elements will be writtenheaders
- headers to use in the first row of the written result.
-
parseAll
Parses a file into a list of annotated java beans- Type Parameters:
T
- the type of java beans to be instantiated.- Parameters:
beanType
- the type of java beans to be instantiated.input
- the file to be parsedexpectedBeanCount
- expected number of rows to be parsed from the input which will be converted into java beans. Used to pre-allocate the size of the outputList
- Returns:
- an
Iterable
that allows iterating over the input and producing instances of java beans on demand.
-
parseAll
Parses a file into a list of annotated java beans- Type Parameters:
T
- the type of java beans to be instantiated.- Parameters:
beanType
- the type of java beans to be instantiated.input
- the file to be parsedencoding
- encoding of the given fileexpectedBeanCount
- expected number of rows to be parsed from the input which will be converted into java beans. Used to pre-allocate the size of the outputList
- Returns:
- an
Iterable
that allows iterating over the input and producing instances of java beans on demand.
-
parseAll
Parses a file into a list of annotated java beans- Type Parameters:
T
- the type of java beans to be instantiated.- Parameters:
beanType
- the type of java beans to be instantiated.input
- the file to be parsedencoding
- encoding of the given fileexpectedBeanCount
- expected number of rows to be parsed from the input which will be converted into java beans. Used to pre-allocate the size of the outputList
- Returns:
- a list containing all java beans read from the input.
-
parseAll
Parses an input stream into a list of annotated java beans- Type Parameters:
T
- the type of java beans to be instantiated.- Parameters:
beanType
- the type of java beans to be instantiated.input
- the input stream to be parsedexpectedBeanCount
- expected number of rows to be parsed from the input which will be converted into java beans. Used to pre-allocate the size of the outputList
- Returns:
- a list containing all java beans read from the input.
-
parseAll
public <T> List<T> parseAll(Class<T> beanType, InputStream input, String encoding, int expectedBeanCount) Parses an input stream into a list of annotated java beans- Type Parameters:
T
- the type of java beans to be instantiated.- Parameters:
beanType
- the type of java beans to be instantiated.input
- the input stream to be parsedencoding
- encoding of the given input streamexpectedBeanCount
- expected number of rows to be parsed from the input which will be converted into java beans. Used to pre-allocate the size of the outputList
- Returns:
- a list containing all java beans read from the input.
-
parseAll
public <T> List<T> parseAll(Class<T> beanType, InputStream input, Charset encoding, int expectedBeanCount) Parses an input stream into a list of annotated java beans- Type Parameters:
T
- the type of java beans to be instantiated.- Parameters:
beanType
- the type of java beans to be instantiated.input
- the input stream to be parsedencoding
- encoding of the given input streamexpectedBeanCount
- expected number of rows to be parsed from the input which will be converted into java beans. Used to pre-allocate the size of the outputList
- Returns:
- a list containing all java beans read from the input.
-
parseAll
Parses the input into a list of annotated java beans- Type Parameters:
T
- the type of java beans to be instantiated.- Parameters:
beanType
- the type of java beans to be instantiated.input
- the input to be parsedexpectedBeanCount
- expected number of rows to be parsed from the input which will be converted into java beans. Used to pre-allocate the size of the outputList
- Returns:
- a list containing all java beans read from the input.
-
parseAll
Parses a file into a list of annotated java beans- Type Parameters:
T
- the type of java beans to be instantiated.- Parameters:
beanType
- the type of java beans to be instantiated.input
- the file to be parsed- Returns:
- an
Iterable
that allows iterating over the input and producing instances of java beans on demand.
-
parseAll
Parses a file into a list of annotated java beans- Type Parameters:
T
- the type of java beans to be instantiated.- Parameters:
beanType
- the type of java beans to be instantiated.input
- the file to be parsedencoding
- encoding of the given file- Returns:
- an
Iterable
that allows iterating over the input and producing instances of java beans on demand.
-
parseAll
Parses a file into a list of annotated java beans- Type Parameters:
T
- the type of java beans to be instantiated.- Parameters:
beanType
- the type of java beans to be instantiated.input
- the file to be parsedencoding
- encoding of the given file- Returns:
- a list containing all java beans read from the input.
-
parseAll
Parses an input stream into a list of annotated java beans- Type Parameters:
T
- the type of java beans to be instantiated.- Parameters:
beanType
- the type of java beans to be instantiated.input
- the input stream to be parsed- Returns:
- a list containing all java beans read from the input.
-
parseAll
Parses an input stream into a list of annotated java beans- Type Parameters:
T
- the type of java beans to be instantiated.- Parameters:
beanType
- the type of java beans to be instantiated.input
- the input stream to be parsedencoding
- encoding of the given input stream- Returns:
- a list containing all java beans read from the input.
-
parseAll
Parses an input stream into a list of annotated java beans- Type Parameters:
T
- the type of java beans to be instantiated.- Parameters:
beanType
- the type of java beans to be instantiated.input
- the input stream to be parsedencoding
- encoding of the given input stream- Returns:
- a list containing all java beans read from the input.
-
parseAll
Parses the input into a list of annotated java beans- Type Parameters:
T
- the type of java beans to be instantiated.- Parameters:
beanType
- the type of java beans to be instantiated.input
- the input to be parsed- Returns:
- a list containing all java beans read from the input.
-
iterate
Iterates over a file to produce instances of annotated java beans on demand.- Type Parameters:
T
- the type of java beans to be instantiated.- Parameters:
beanType
- the type of java beans to be instantiated.input
- the file to be parsed- Returns:
- an
Iterable
that allows iterating over the input and producing instances of java beans on demand.
-
iterate
Iterates over a file to produce instances of annotated java beans on demand.- Type Parameters:
T
- the type of java beans to be instantiated.- Parameters:
beanType
- the type of java beans to be instantiated.input
- the file to be parsedencoding
- encoding of the given file- Returns:
- an
Iterable
that allows iterating over the input and producing instances of java beans on demand.
-
iterate
public <T> IterableResult<T,ParsingContext> iterate(Class<T> beanType, File input, Charset encoding) Iterates over a file to produce instances of annotated java beans on demand.- Type Parameters:
T
- the type of java beans to be instantiated.- Parameters:
beanType
- the type of java beans to be instantiated.input
- the file to be parsedencoding
- encoding of the given file- Returns:
- an
Iterable
that allows iterating over the input and producing instances of java beans on demand.
-
iterate
Iterates over an input stream to produce instances of annotated java beans on demand.- Type Parameters:
T
- the type of java beans to be instantiated.- Parameters:
beanType
- the type of java beans to be instantiated.input
- the input stream to be parsed- Returns:
- an
Iterable
that allows iterating over the input and producing instances of java beans on demand.
-
iterate
public <T> IterableResult<T,ParsingContext> iterate(Class<T> beanType, InputStream input, String encoding) Iterates over an input stream to produce instances of annotated java beans on demand.- Type Parameters:
T
- the type of java beans to be instantiated.- Parameters:
beanType
- the type of java beans to be instantiated.input
- the input stream to be parsedencoding
- encoding of the given input stream- Returns:
- an
Iterable
that allows iterating over the input and producing instances of java beans on demand.
-
iterate
public <T> IterableResult<T,ParsingContext> iterate(Class<T> beanType, InputStream input, Charset encoding) Iterates over an input stream to produce instances of annotated java beans on demand.- Type Parameters:
T
- the type of java beans to be instantiated.- Parameters:
beanType
- the type of java beans to be instantiated.input
- the input stream to be parsedencoding
- encoding of the given input stream- Returns:
- an
Iterable
that allows iterating over the input and producing instances of java beans on demand.
-
iterate
Iterates over an input to produce instances of annotated java beans on demand.- Type Parameters:
T
- the type of java beans to be instantiated.- Parameters:
beanType
- the type of java beans to be instantiated.input
- the input to be parsed- Returns:
- an
Iterable
that allows iterating over the input and producing instances of java beans on demand.
-
toString
-
getInputDimension
Calculates the dimensions of a file (row and column count).- Parameters:
input
- the file to be parsed- Returns:
- a
InputDimension
with information about the dimensions of the given input.
-
getInputDimension
Calculates the dimensions of a file (row and column count).- Parameters:
input
- the file to be parsedencoding
- encoding of the given file- Returns:
- a
InputDimension
with information about the dimensions of the given input.
-
getInputDimension
Calculates the dimensions of a given input (row and column count).- Parameters:
input
- the input to be parsed- Returns:
- a
InputDimension
with information about the dimensions of the given input.
-
getInputDimension
Calculates the dimensions of a given input (row and column count).- Parameters:
input
- the input to be parsedencoding
- encoding of the given input- Returns:
- a
InputDimension
with information about the dimensions of the given input.
-
getInputDimension
Calculates the dimensions of a given input (row and column count).- Parameters:
input
- the input to be parsed- Returns:
- a
InputDimension
with information about the dimensions of the given input.
-
getKeepResourcesOpen
public boolean getKeepResourcesOpen()Returns a flag indicating whether resources used for writing should be kept open after being used by the routines available from this object, when applicable.- Returns:
- flag indicating whether to call the
close()
(or any other cleanup method) after a routine executes.
-
setKeepResourcesOpen
public void setKeepResourcesOpen(boolean keepResourcesOpen) Allows preventing resources used for writing from being closed after being used by the routines available from this object, when applicable.- Parameters:
keepResourcesOpen
- flag indicating whether to call theclose()
(or any other cleanup method) after a routine executes.
-
getColumnMapper
Returns a mapper that allows users to manually define mappings from attributes/methods of a given class to columns to be parsed or written in routines that manipulate java beans. This allows users to use instances of classes that are not annotated withParsed
norNested
. Any mappings defined with the column mapper will take precedence over these annotations.- Returns:
- the column mapper
-
setColumnMapper
Copies the given column mappings over to this processor. Further changes to the given object won't be reflected on the copy stored internally.- Parameters:
columnMapper
- the column mappings to use
-