Package com.univocity.parsers.common
Class DefaultConversionProcessor
- java.lang.Object
-
- com.univocity.parsers.common.DefaultConversionProcessor
-
- All Implemented Interfaces:
ConversionProcessor
- Direct Known Subclasses:
AbstractObjectProcessor
,BeanConversionProcessor
,ObjectRowWriterProcessor
public abstract class DefaultConversionProcessor extends java.lang.Object implements ConversionProcessor
The base class forRowProcessor
andRowWriterProcessor
implementations that support value conversions provided byConversion
instances.
-
-
Field Summary
Fields Modifier and Type Field Description (package private) Context
context
protected FieldConversionMapping
conversions
private java.util.Map<java.lang.Class<?>,Conversion[]>
conversionsByType
private boolean
conversionsInitialized
(package private) ProcessorErrorHandler
errorHandler
private int[]
fieldIndexes
private boolean
fieldsReordered
private int[]
reverseFieldIndexes
-
Constructor Summary
Constructors Constructor Description DefaultConversionProcessor()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Object[]
applyConversions(java.lang.String[] row, Context context)
Executes the sequences of conversions defined usingconvertFields(Conversion...)
,convertIndexes(Conversion...)
andconvertAll(Conversion...)
, for every field in the given row.private boolean
applyConversionsByType(boolean reverse, java.lang.Object[] row, boolean[] convertedFlags)
private java.lang.Object
applyTypeConversion(boolean revert, java.lang.Object input)
void
convertAll(Conversion... conversions)
Applies a set ofConversion
objects over all elements of a recordFieldSet<java.lang.String>
convertFields(Conversion... conversions)
Applies a set ofConversion
objects over fields of a record by name.FieldSet<java.lang.Integer>
convertIndexes(Conversion... conversions)
Applies a set ofConversion
objects over indexes of a record.void
convertType(java.lang.Class<?> type, Conversion... conversions)
Applies a sequence of conversions over values of a given type.private FieldConversionMapping
getConversions()
protected boolean
handleConversionError(java.lang.Throwable ex, java.lang.Object[] row, int column)
Handles an error that occurred when applying conversions over a value.protected void
initializeConversions(java.lang.String[] row, Context context)
private void
populateReverseFieldIndexes()
boolean
reverseConversions(boolean executeInReverseOrder, java.lang.Object[] row, NormalizedString[] headers, int[] indexesToWrite)
Executes the sequences of reverse conversions defined usingconvertFields(Conversion...)
,convertIndexes(Conversion...)
andconvertAll(Conversion...)
, for every field in the given row.protected DataProcessingException
toDataProcessingException(java.lang.Throwable ex, java.lang.Object[] row, int column)
private boolean
validateAllValues(java.lang.Object[] objectRow)
-
-
-
Field Detail
-
conversionsByType
private java.util.Map<java.lang.Class<?>,Conversion[]> conversionsByType
-
conversions
protected FieldConversionMapping conversions
-
conversionsInitialized
private boolean conversionsInitialized
-
fieldIndexes
private int[] fieldIndexes
-
reverseFieldIndexes
private int[] reverseFieldIndexes
-
fieldsReordered
private boolean fieldsReordered
-
errorHandler
ProcessorErrorHandler errorHandler
-
context
Context context
-
-
Method Detail
-
convertIndexes
public final FieldSet<java.lang.Integer> convertIndexes(Conversion... conversions)
Description copied from interface:ConversionProcessor
Applies a set ofConversion
objects over indexes of a record.The idiom to define which indexes should have these conversions applies is as follows:
processor.convertIndexes(Conversions.trim(), Conversions.toUpperCase()).add(2, 5); // applies trim and uppercase conversions to fields in indexes 2 and 5
- Specified by:
convertIndexes
in interfaceConversionProcessor
- Parameters:
conversions
- The sequence of conversions to be executed in a set of field indexes.- Returns:
- A
FieldSet
for indexes.
-
convertAll
public final void convertAll(Conversion... conversions)
Description copied from interface:ConversionProcessor
Applies a set ofConversion
objects over all elements of a record- Specified by:
convertAll
in interfaceConversionProcessor
- Parameters:
conversions
- The sequence of conversions to be executed in all elements of a record
-
convertFields
public final FieldSet<java.lang.String> convertFields(Conversion... conversions)
Description copied from interface:ConversionProcessor
Applies a set ofConversion
objects over fields of a record by name.The idiom to define which fields should have these conversions applied is as follows:
processor.convertFields(Conversions.trim(), Conversions.toUpperCase()).add("name", "position"); // applies trim and uppercase conversions to fields with headers "name" and "position"
- Specified by:
convertFields
in interfaceConversionProcessor
- Parameters:
conversions
- The sequence of conversions to be executed in a set of field indexes.- Returns:
- A
FieldSet
for field names.
-
getConversions
private FieldConversionMapping getConversions()
-
initializeConversions
protected void initializeConversions(java.lang.String[] row, Context context)
-
applyConversions
public final java.lang.Object[] applyConversions(java.lang.String[] row, Context context)
Executes the sequences of conversions defined usingconvertFields(Conversion...)
,convertIndexes(Conversion...)
andconvertAll(Conversion...)
, for every field in the given row.Each field will be transformed using the
Conversion.execute(Object)
method.In general the conversions will process a String and convert it to some object value (such as booleans, dates, etc).
- Parameters:
row
- the parsed record with its individual records as extracted from the original input.context
- the current state of the parsing process.- Returns:
- an row of Object instances containing the values obtained after the execution of all conversions.
Fields that do not have any conversion defined will just be copied to the object array into their original positions.
-
populateReverseFieldIndexes
private void populateReverseFieldIndexes()
-
validateAllValues
private boolean validateAllValues(java.lang.Object[] objectRow)
-
reverseConversions
public final boolean reverseConversions(boolean executeInReverseOrder, java.lang.Object[] row, NormalizedString[] headers, int[] indexesToWrite)
Executes the sequences of reverse conversions defined usingconvertFields(Conversion...)
,convertIndexes(Conversion...)
andconvertAll(Conversion...)
, for every field in the given row.Each field will be transformed using the
Conversion.revert(Object)
method.In general the conversions will process an Object (such as a Boolean, Date, etc), and convert it to a String representation.
- Parameters:
executeInReverseOrder
- flag to indicate whether the conversion sequence should be executed in the reverse order of its declaration.row
- the row of objects that will be convertedheaders
- All field names used to produce records in a given destination. May be null if no headers have been defined inCommonSettings.getHeaders()
indexesToWrite
- The indexes of the headers that are actually being written. May be null if no fields have been selected usingCommonSettings.selectFields(String...)
orCommonSettings.selectIndexes(Integer...)
- Returns:
true
if the the row should be discarded
-
applyConversionsByType
private boolean applyConversionsByType(boolean reverse, java.lang.Object[] row, boolean[] convertedFlags)
-
handleConversionError
protected final boolean handleConversionError(java.lang.Throwable ex, java.lang.Object[] row, int column)
Handles an error that occurred when applying conversions over a value. If the user defined aProcessorErrorHandler
the user will receive the exception and is able to determine whether or not processing should continue, discarding the record. If the error handler is an instance ofRetryableErrorHandler
, the user can provide a default value to use in place of the one that could not be converted, and decide whether or not the record should be kept with the use of theRetryableErrorHandler.keepRecord()
method.- Parameters:
ex
- the exception that occurred when applying a conversionrow
- the record being processed at the time the exception happened.column
- the column if the given row, whose value could not be converted. If negative, it's not possible to keep the record.- Returns:
true
if the error has been handled by the user and the record can still be processed, otherwisefalse
if the record should be discarded.
-
toDataProcessingException
protected DataProcessingException toDataProcessingException(java.lang.Throwable ex, java.lang.Object[] row, int column)
-
convertType
public final void convertType(java.lang.Class<?> type, Conversion... conversions)
Description copied from interface:ConversionProcessor
Applies a sequence of conversions over values of a given type. Used for writing.- Specified by:
convertType
in interfaceConversionProcessor
- Parameters:
type
- the type over which a sequence of conversions should be appliedconversions
- the sequence of conversions to apply over values of the given type.
-
applyTypeConversion
private java.lang.Object applyTypeConversion(boolean revert, java.lang.Object input)
-
-