com.univocity.parsers.common.processor
Class BeanProcessor<T>

java.lang.Object
  extended by com.univocity.parsers.common.processor.BeanProcessor<T>
Type Parameters:
T - the annotated class type.
All Implemented Interfaces:
RowProcessor
Direct Known Subclasses:
BeanListProcessor

public abstract class BeanProcessor<T>
extends Object
implements RowProcessor

A RowProcessor implementation for converting rows extracted from any implementation of AbstractParser into java objects.

The class type of the object must contain the annotations provided in com.univocity.parsers.annotations.

For each row processed, a java bean instance of a given class will be created with its fields populated.

This instance will then be sent to the beanProcessed(Object, ParsingContext) method, where the user can access it.

Author:
uniVocity Software Pty Ltd - [email protected]
See Also:
AbstractParser, RowProcessor

Constructor Summary
BeanProcessor(Class<T> beanType)
          Creates a processor for java beans of a given type.
 
Method Summary
 Object[] applyConversions(String[] row, ParsingContext context)
          Executes the sequences of conversions defined using ConversionProcessor.convertFields(Conversion...), ConversionProcessor.convertIndexes(Conversion...) and ConversionProcessor.convertAll(Conversion...), for every field in the given row.
abstract  void beanProcessed(T bean, ParsingContext context)
          Invoked by the processor after all values of a valid record have been processed and converted into a java object.
 void convertAll(Conversion... conversions)
          Applies a set of Conversion objects over all elements of a record
 FieldSet<String> convertFields(Conversion... conversions)
          Applies a set of Conversion objects over fields of a record by name.
 FieldSet<Integer> convertIndexes(Conversion... conversions)
          Applies a set of Conversion objects over indexes of a record.
 T createBean(String[] row, ParsingContext context)
          Converts a record with values extracted from the parser into a java bean instance.
protected  void initialize()
          Identifies and extracts fields annotated with the Parsed annotation
 void processEnded(ParsingContext context)
          This method will by invoked by the parser once, after the parsing process stopped and all resources were closed.
 void processStarted(ParsingContext context)
          This method will by invoked by the parser once, when it is ready to start processing the input.
 void reverseConversions(boolean executeInReverseOrder, Object[] row, String[] headers, int[] indexesToWrite)
          Executes the sequences of reverse conversions defined using ConversionProcessor.convertFields(Conversion...), ConversionProcessor.convertIndexes(Conversion...) and ConversionProcessor.convertAll(Conversion...), for every field in the given row.
 Object[] reverseConversions(T bean, String[] headers, int[] indexesToWrite)
          Converts a java bean instance into a sequence of values for writing.
 void rowProcessed(String[] row, ParsingContext context)
          Converts a parsed row to a java object
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BeanProcessor

public BeanProcessor(Class<T> beanType)
Creates a processor for java beans of a given type.

Parameters:
beanType - the class with its attributes mapped to fields of records parsed by an AbstractParser or written by an AbstractWriter.
Method Detail

rowProcessed

public final void rowProcessed(String[] row,
                               ParsingContext context)
Converts a parsed row to a java object

Specified by:
rowProcessed in interface RowProcessor
Parameters:
row - the data extracted by the parser for an individual record. Note that:
context - A contextual object with information and controls over the current state of the parsing process

beanProcessed

public abstract void beanProcessed(T bean,
                                   ParsingContext context)
Invoked by the processor after all values of a valid record have been processed and converted into a java object.

Parameters:
bean - java object created with the information extracted by the parser for an individual record.
context - A contextual object with information and controls over the current state of the parsing process

processStarted

public void processStarted(ParsingContext context)
This method will by invoked by the parser once, when it is ready to start processing the input.

Specified by:
processStarted in interface RowProcessor
Parameters:
context - A contextual object with information and controls over the current state of the parsing process

processEnded

public void processEnded(ParsingContext context)
This method will by invoked by the parser once, after the parsing process stopped and all resources were closed.

It will always be called by the parser: in case of errors, if the end of the input us reached, or if the user stopped the process manually using ParsingContext.stop().

Specified by:
processEnded in interface RowProcessor
Parameters:
context - A contextual object with information and controls over the state of the parsing process

initialize

protected final void initialize()
Identifies and extracts fields annotated with the Parsed annotation


createBean

public final T createBean(String[] row,
                          ParsingContext context)
Converts a record with values extracted from the parser into a java bean instance.

Parameters:
row - The values extracted from the parser
context - The current state of the parsing process
Returns:
an instance of the java bean type defined in this class constructor.

reverseConversions

public final Object[] reverseConversions(T bean,
                                         String[] headers,
                                         int[] indexesToWrite)
Converts a java bean instance into a sequence of values for writing.

Parameters:
bean - an instance of the type defined in this class constructor.
headers - All field names used to produce records in a given destination. May be null if no headers have been defined in CommonSettings.getHeaders()
indexesToWrite - The indexes of the headers that are actually being written. May be null if no fields have been selected using CommonSettings.selectFields(String...) or CommonSettings.selectIndexes(Integer...)
Returns:
a row of objects containing the values extracted from the java bean

convertIndexes

public final FieldSet<Integer> convertIndexes(Conversion... conversions)
Applies a set of Conversion 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
 

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)
Applies a set of Conversion objects over all elements of a record

Parameters:
conversions - The sequence of conversions to be executed in all elements of a record

convertFields

public final FieldSet<String> convertFields(Conversion... conversions)
Applies a set of Conversion 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"
 

Parameters:
conversions - The sequence of conversions to be executed in a set of field indexes.
Returns:
A FieldSet for field names.

applyConversions

public final Object[] applyConversions(String[] row,
                                       ParsingContext context)
Executes the sequences of conversions defined using ConversionProcessor.convertFields(Conversion...), ConversionProcessor.convertIndexes(Conversion...) and ConversionProcessor.convertAll(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.


reverseConversions

public final void reverseConversions(boolean executeInReverseOrder,
                                     Object[] row,
                                     String[] headers,
                                     int[] indexesToWrite)
Executes the sequences of reverse conversions defined using ConversionProcessor.convertFields(Conversion...), ConversionProcessor.convertIndexes(Conversion...) and ConversionProcessor.convertAll(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 (used in BeanConversionProcessor.reverseConversions(T, java.lang.String[], int[])) .
row - the row of objects that will be converted
headers - All field names used to produce records in a given destination. May be null if no headers have been defined in CommonSettings.getHeaders()
indexesToWrite - The indexes of the headers that are actually being written. May be null if no fields have been selected using CommonSettings.selectFields(String...) or CommonSettings.selectIndexes(Integer...)


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