Class AbstractProcessorSwitch<T extends Context>
- java.lang.Object
-
- com.univocity.parsers.common.processor.core.AbstractProcessorSwitch<T>
-
- All Implemented Interfaces:
ColumnOrderDependent
,Processor<T>
- Direct Known Subclasses:
AbstractInputValueSwitch
,RowProcessorSwitch
public abstract class AbstractProcessorSwitch<T extends Context> extends java.lang.Object implements Processor<T>, ColumnOrderDependent
A specialProcessor
implementation that combines and allows switching among different Processors. Each Processor will have its ownContext
. Concrete implementations of this class are expected to implement theswitchRowProcessor(String[], Context)
method and analyze the input row to determine whether or not the currentProcessor
implementation must be changed to handle a special circumstance (determined by the concrete implementation) such as a different row format. When the processor is switched, theprocessorSwitched(Processor, Processor)
will be called, and must be overridden, to notify the change to the user.
-
-
Field Summary
Fields Modifier and Type Field Description private T
contextForProcessor
private java.util.Map<Processor,T>
processors
private Processor
selectedProcessor
-
Constructor Summary
Constructors Constructor Description AbstractProcessorSwitch()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description java.lang.String[]
getHeaders()
Returns the headers in use by the current row processor implementation, which can vary among row processors.int[]
getIndexes()
Returns the indexes in use by the current row processor implementation, which can vary among row processors.boolean
preventColumnReordering()
Returns a flag indicating whether or not columns should be reordered by the parservoid
processEnded(T context)
This method will by invoked by the parser once, after the parsing process stopped and all resources were closed.void
processorSwitched(Processor<T> from, Processor<T> to)
Notifies a change ofProcessor
implementation.void
processStarted(T context)
This method will by invoked by the parser once, when it is ready to start processing the input.void
rowProcessed(java.lang.String[] row, T context)
Invoked by the parser after all values of a valid record have been processed.void
rowProcessorSwitched(RowProcessor from, RowProcessor to)
Notifies a change ofRowProcessor
implementation.protected abstract Processor<T>
switchRowProcessor(java.lang.String[] row, T context)
Analyzes the input to determine whether or not the row processor implementation must be changedprotected abstract T
wrapContext(T context)
Wraps a given parser context object that returns headers and extracted field indexes associated with a given processor in this switch.
-
-
-
Method Detail
-
switchRowProcessor
protected abstract Processor<T> switchRowProcessor(java.lang.String[] row, T context)
Analyzes the input to determine whether or not the row processor implementation must be changed- Parameters:
row
- a row parsed from the inputcontext
- the current parsing context (not associated with the current row processor used by this class)- Returns:
- the row processor implementation to use. If it is not the same as the one used by the previous row,
the returned row processor will be used, and the
processorSwitched(Processor, Processor)
method will be called.
-
getHeaders
public java.lang.String[] getHeaders()
Returns the headers in use by the current row processor implementation, which can vary among row processors. Ifnull
, the headers parsed by the input, or defined inCommonSettings.getHeaders()
will be returned.- Returns:
- the current sequence of headers to use.
-
getIndexes
public int[] getIndexes()
Returns the indexes in use by the current row processor implementation, which can vary among row processors. Ifnull
all columns of a given record will be considered.- Returns:
- the current sequence of indexes to use.
-
processorSwitched
public void processorSwitched(Processor<T> from, Processor<T> to)
Notifies a change ofProcessor
implementation. Users are expected to override this method to receive the notification.- Parameters:
from
- the processor previously in useto
- the new processor to use to continue processing the input.
-
rowProcessorSwitched
public void rowProcessorSwitched(RowProcessor from, RowProcessor to)
Notifies a change ofRowProcessor
implementation. Users are expected to override this method to receive the notification.- Parameters:
from
- the row processor previously in useto
- the new row processor to use to continue processing the input.
-
processStarted
public void processStarted(T context)
Description copied from interface:Processor
This method will by invoked by the parser once, when it is ready to start processing the input.- Specified by:
processStarted
in interfaceProcessor<T extends Context>
- Parameters:
context
- A contextual object with information and controls over the current state of the parsing process
-
wrapContext
protected abstract T wrapContext(T context)
Wraps a given parser context object that returns headers and extracted field indexes associated with a given processor in this switch.- Parameters:
context
- the context to wrap- Returns:
- a wrapped context that returns the headers and extracted field
indexes from
getHeaders()
andgetIndexes()
-
rowProcessed
public final void rowProcessed(java.lang.String[] row, T context)
Description copied from interface:Processor
Invoked by the parser after all values of a valid record have been processed.- Specified by:
rowProcessed
in interfaceProcessor<T extends Context>
- Parameters:
row
- the data extracted by the parser for an individual record. Note that:- it will never by null.
- it will never be empty unless explicitly configured using
CommonSettings.setSkipEmptyLines(boolean)
- it won't contain lines identified by the parser as comments. To disable comment processing set
Format.setComment(char)
to '\0'
context
- A contextual object with information and controls over the current state of the parsing process
-
processEnded
public void processEnded(T context)
Description copied from interface:Processor
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
Context.stop()
.- Specified by:
processEnded
in interfaceProcessor<T extends Context>
- Parameters:
context
- A contextual object with information and controls over the state of the parsing process
-
preventColumnReordering
public boolean preventColumnReordering()
Description copied from interface:ColumnOrderDependent
Returns a flag indicating whether or not columns should be reordered by the parser- Specified by:
preventColumnReordering
in interfaceColumnOrderDependent
- Returns:
- a flag indicating whether or not columns should be reordered by the parser
-
-