net.sf.jmatchparser.util.csv.fieldreader
Class FieldReader<K,V>

java.lang.Object
  extended by net.sf.jmatchparser.util.csv.fieldreader.FieldReader<K,V>
Type Parameters:
K - The type of the fields
V - The type of the parsed values
Direct Known Subclasses:
EnumFieldReader

public class FieldReader<K,V>
extends Object

A parser that can be used to parse different CSV files into the same set of records. It is useful for building parsers that need a fixed range of information, but the files that contain them can have a multitude of different formats; often the parser that consumes the records of this field reader (who supplies FieldDefinitions) is in a different library or component than the creator of this field reader (who supplies FieldSources).

Each record is formatted by this field reader into a Map that maps fields (usually either Strings or an Enum that implements FieldEnum, see EnumFieldReader) to values (sometimes strings, but often other objects as well).

By convention, keys that are mapped to null are expected to be cleared by the parser (when updating anything), and records that are not present are expected not to be touched. Therefore, parsers that use this reader should use the Map.containsKey(Object) method to determine if a key is present, instead of checking the value of Map.get(Object) for null. Alternatively, the setEmptyFieldValue(Object) method can be used to set the "empty" (clear) value to something else.


Constructor Summary
FieldReader(Map<K,FieldDefinition<? extends V>> fieldDefinitions, AbstractCSVReader reader)
          Create a new field reader.
 
Method Summary
 void close()
          Close this field reader.
 void computeFieldSourcesFromIndex(FieldSource.EmptyAction emptyAction, K[] fields)
          Compute the field sources automatically from the index of the key inside the given array.
 void computeFieldSourcesFromName(FieldSource.EmptyAction emptyAction)
          Compute the field sources automatically from the names (see Object.toString()) of all fields To be used when header parsing is enabled.
 void computeFieldSourcesFromName(FieldSource.EmptyAction emptyAction, Collection<K> fields)
          Compute the field sources automatically from the names (see Object.toString()) of the given fields To be used when header parsing is enabled.
 Map<K,V> postprocessNewRecord(Map<K,V> record)
          Verify and postprocess a new record (i. e. one that does not already exist in the underlying database and has to be created instead of updated.
 Map<K,V> read()
          Read a record from this field reader.
 void readHeader(boolean ignore)
          Read a header line from the CSV reader, and optionally use it for column assignment.
 void setEmptyFieldValue(V emptyFieldValue)
          Set the value that should be used for records that should be updated, but to an empty value (by default null).
 void setFieldSources(Map<K,FieldSource> fieldSources)
          Set the FieldSources to be used for this field reader.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FieldReader

public FieldReader(Map<K,FieldDefinition<? extends V>> fieldDefinitions,
                   AbstractCSVReader reader)
Create a new field reader.

Parameters:
fieldDefinitions - Field definitions to use
reader - Reader to read the records from
Method Detail

setFieldSources

public void setFieldSources(Map<K,FieldSource> fieldSources)
Set the FieldSources to be used for this field reader.


computeFieldSourcesFromIndex

public void computeFieldSourcesFromIndex(FieldSource.EmptyAction emptyAction,
                                         K[] fields)
Compute the field sources automatically from the index of the key inside the given array. To be used when header parsing is disabled.

Parameters:
emptyAction - FieldSource.EmptyAction to use
fields - Fields in the desired order

computeFieldSourcesFromName

public void computeFieldSourcesFromName(FieldSource.EmptyAction emptyAction)
Compute the field sources automatically from the names (see Object.toString()) of all fields To be used when header parsing is enabled.

Parameters:
emptyAction - FieldSource.EmptyAction to use

computeFieldSourcesFromName

public void computeFieldSourcesFromName(FieldSource.EmptyAction emptyAction,
                                        Collection<K> fields)
Compute the field sources automatically from the names (see Object.toString()) of the given fields To be used when header parsing is enabled.

Parameters:
emptyAction - FieldSource.EmptyAction to use
fields - Fields to use

readHeader

public void readHeader(boolean ignore)
                throws IOException
Read a header line from the CSV reader, and optionally use it for column assignment.

Parameters:
ignore - Whether to ignore the read header (not use it for column assignment)
Throws:
IOException

setEmptyFieldValue

public void setEmptyFieldValue(V emptyFieldValue)
Set the value that should be used for records that should be updated, but to an empty value (by default null).

Parameters:
emptyFieldValue - new value for empty fields

read

public Map<K,V> read()
              throws IOException,
                     FieldReaderException
Read a record from this field reader.

Throws:
IOException
FieldReaderException

postprocessNewRecord

public Map<K,V> postprocessNewRecord(Map<K,V> record)
                              throws FieldReaderException
Verify and postprocess a new record (i. e. one that does not already exist in the underlying database and has to be created instead of updated. Note that the current implementation does not perform any postprocessing and returns the record as is.

Parameters:
record - Record that has been read by this field reader before
Returns:
postprocessed record
Throws:
FieldReaderException - if mandatory fields are missing

close

public void close()
           throws IOException
Close this field reader.

Throws:
IOException


Copyright © 2011. All Rights Reserved.