net.sf.jmatchparser.util.csv.fieldreader
Class FieldSource

java.lang.Object
  extended by net.sf.jmatchparser.util.csv.fieldreader.FieldSource

public abstract class FieldSource
extends Object

A description of the "source" from where data should be filled into the field, as seen from the perspective of the "provider" that provides the CSV files.


Nested Class Summary
static class FieldSource.EmptyAction
          How to treat a field source that returns an empty string.
 
Constructor Summary
FieldSource()
           
 
Method Summary
static FieldSource concat(FieldSource... sources)
          Create a new field source that returns a value concatenated from several other FieldSources.
static FieldSource fromColumn(String columnName)
          Create a new field source that returns a value from a CSV column.
static FieldSource fromConstant(String constant)
          Create a new field source that always returns the same constant value.
abstract  String getField()
          Return an artificial name for this field, used for debugging and in error messages.
abstract  String getValue(Map<String,String> columns)
          Return the value of this field source.
 boolean isMandatoryIfNew()
          Return whether this field has to be filled for new records
static FieldSource parse(String columns, FieldSource.EmptyAction emptyAction, String mask, String mappings)
          Parse a field source from several parameters.
static FieldSource withChecks(FieldSource source, FieldSource.EmptyAction emptyAction, Pattern mask)
          Create a field source that performs checks on the output of another field source and returns the result verbatim.
static FieldSource withChecks(FieldSource source, FieldSource.EmptyAction emptyAction, String mask)
          Create a field source that performs checks on the output of another field source and returns the result verbatim.
static FieldSource withFormat(FieldSource source, ParseFormat<String> format)
          Create a field source that applies a ParseFormat on another field source.
static FieldSource withMapping(FieldSource source, Pattern regex, String replacement)
          Create a field source that performs a regex replacement on another field source.
static FieldSource withMapping(FieldSource source, String regex, String replacement)
          Create a field source that performs a regex replacement on another field source.
static FieldSource withMappings(FieldSource source, String mappings)
          Create a field source that performs a series of regex replacements on another field source.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FieldSource

public FieldSource()
Method Detail

getValue

public abstract String getValue(Map<String,String> columns)
                         throws FieldReaderException
Return the value of this field source.

Parameters:
columns - Hashtable of fields read from the CSV file
Returns:
Value, or null if the field should be treated as absent
Throws:
FieldReaderException

getField

public abstract String getField()
Return an artificial name for this field, used for debugging and in error messages.


isMandatoryIfNew

public boolean isMandatoryIfNew()
Return whether this field has to be filled for new records


fromConstant

public static FieldSource fromConstant(String constant)
Create a new field source that always returns the same constant value.

Parameters:
constant - The constant value

fromColumn

public static FieldSource fromColumn(String columnName)
Create a new field source that returns a value from a CSV column.

Parameters:
columnName - Name or index of the column

concat

public static FieldSource concat(FieldSource... sources)
Create a new field source that returns a value concatenated from several other FieldSources.

Parameters:
sources - Other field sources to be concatenated

withMappings

public static FieldSource withMappings(FieldSource source,
                                       String mappings)
Create a field source that performs a series of regex replacements on another field source.

Parameters:
source - Field source
mappings - A list delimited by ;; of regex replacement pairs, where :: separates regex and replacement, or null for no mappings

withMapping

public static FieldSource withMapping(FieldSource source,
                                      String regex,
                                      String replacement)
Create a field source that performs a regex replacement on another field source.

Parameters:
source - Field source
regex - regular expression
replacement - replacement string

withMapping

public static FieldSource withMapping(FieldSource source,
                                      Pattern regex,
                                      String replacement)
Create a field source that performs a regex replacement on another field source.

Parameters:
source - Field source
regex - regular expression
replacement - replacement string

withFormat

public static FieldSource withFormat(FieldSource source,
                                     ParseFormat<String> format)
Create a field source that applies a ParseFormat on another field source.

Parameters:
source - Field source
format - ParseFormat

withChecks

public static FieldSource withChecks(FieldSource source,
                                     FieldSource.EmptyAction emptyAction,
                                     String mask)
Create a field source that performs checks on the output of another field source and returns the result verbatim. This field source is usually the outermost field source used in a stack of other field sources.

Parameters:
source - field source
emptyAction - Action to perform when the field source returns an empty string
mask - regex pattern the field source result has to match, or null

withChecks

public static FieldSource withChecks(FieldSource source,
                                     FieldSource.EmptyAction emptyAction,
                                     Pattern mask)
Create a field source that performs checks on the output of another field source and returns the result verbatim. This field source is usually the outermost field source used in a stack of other field sources.

Parameters:
source - field source
emptyAction - Action to perform when the field source returns an empty string
mask - regex pattern the field source result has to match, or null

parse

public static FieldSource parse(String columns,
                                FieldSource.EmptyAction emptyAction,
                                String mask,
                                String mappings)
Parse a field source from several parameters.

Parameters:
columns - Columns spec, either one or multiple column names separated by one of |,;: (the separator will be added to the result), or a literal string where column names are included in curly braces
emptyAction - Action to perform when the field source returns an empty string
mask - regex pattern the field source result has to match
mappings - A list delimited by ;; of regex replacement pairs, where :: separates regex and replacement.
See Also:
withChecks(FieldSource, EmptyAction, Pattern), withMappings(FieldSource, String)


Copyright © 2011. All Rights Reserved.