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 Object implements Processor<T>, ColumnOrderDependent
A special Processor implementation that combines and allows switching among different Processors. Each Processor will have its own Context. Concrete implementations of this class are expected to implement the switchRowProcessor(String[], Context) method and analyze the input row to determine whether or not the current Processor 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, the processorSwitched(Processor, Processor) will be called, and must be overridden, to notify the change to the user.
  • Field Details

    • processors

      private Map<Processor,T extends Context> processors
    • selectedProcessor

      private Processor selectedProcessor
    • contextForProcessor

      private T extends Context contextForProcessor
  • Constructor Details

    • AbstractProcessorSwitch

      public AbstractProcessorSwitch()
  • Method Details

    • switchRowProcessor

      protected abstract Processor<T> switchRowProcessor(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 input
      context - 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 String[] getHeaders()
      Returns the headers in use by the current row processor implementation, which can vary among row processors. If null, the headers parsed by the input, or defined in CommonSettings.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. If null 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 of Processor implementation. Users are expected to override this method to receive the notification.
      Parameters:
      from - the processor previously in use
      to - the new processor to use to continue processing the input.
    • rowProcessorSwitched

      public void rowProcessorSwitched(RowProcessor from, RowProcessor to)
      Notifies a change of RowProcessor implementation. Users are expected to override this method to receive the notification.
      Parameters:
      from - the row processor previously in use
      to - 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 interface Processor<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() and getIndexes()
    • rowProcessed

      public final void rowProcessed(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 interface Processor<T extends Context>
      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
    • 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 interface Processor<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 interface ColumnOrderDependent
      Returns:
      a flag indicating whether or not columns should be reordered by the parser