info.fingo.csv
Class CSVParser

java.lang.Object
  extended by info.fingo.csv.CSVParser

public class CSVParser
extends java.lang.Object

Class used to parse CSV (comma separated values) files. The first line of the file has to contain column headers (key values) and subsequent lines have to contain data.

Author:
FINGO - Robert Marek

Field Summary
static java.lang.String DEFAULT_CHARSET
          The default charset for parsed data
static int TYPE_ANY
          Field type: any type = 0
static int TYPE_BOOLEAN
          Field type: boolean = 4
static int TYPE_DATE
          Field type: andate = 5
static int TYPE_EMAIL
          Field type: email = 6
static int TYPE_ENUMERATION
          Field type: enumeration = 7
static int TYPE_INTEGER
          Field type: any integer = 2
static int TYPE_NUMERIC
          Field type: numeric = 3
static int TYPE_STRING
          Field type: string = 1
 
Constructor Summary
CSVParser()
          Default constructor.
CSVParser(java.lang.String charset)
          Creates new parser with the given charset.
 
Method Summary
 void defineField(java.lang.String key, int type, boolean required)
          Defines field in CSV file for validation.
 void defineFieldBoolean(java.lang.String key, boolean required)
          Defines field of type Boolean in CSV file for validation.
 void defineFieldDate(java.lang.String key, boolean required, java.util.Date minValue, java.util.Date maxValue)
          Define field of type Date in CSV file for validation.
 void defineFieldEmail(java.lang.String key, boolean required, java.lang.Integer maxLength)
          Define field of type Email in CSV file for validation.
 void defineFieldEnumeration(java.lang.String key, boolean required, java.lang.Object[] enumeration)
          Define field of type Enumeration in CSV file for validation.
 void defineFieldInteger(java.lang.String key, boolean required, java.lang.Integer minValue, java.lang.Integer maxValue)
          Defines field of type Integer in CSV file for validation.
 void defineFieldNumeric(java.lang.String key, boolean required, java.lang.Number minValue, java.lang.Number maxValue)
          Defines field of type Numeric in CSV file for validation.
 void defineFieldString(java.lang.String key, boolean required, java.lang.Integer minLength, java.lang.Integer maxLength)
          Defines field of type String in CSV file for validation.
 java.util.HashMap<java.lang.String,java.lang.String>[] parse(java.io.InputStream is, char delim)
          Function for csv file parsing.
 java.util.HashMap<java.lang.String,java.lang.String>[] parse(java.lang.String path, char delim)
          Function for csv file parsing.
 CSVStatus validate()
          Validate data read by parse().
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_CHARSET

public static final java.lang.String DEFAULT_CHARSET
The default charset for parsed data

See Also:
Constant Field Values

TYPE_ANY

public static final int TYPE_ANY
Field type: any type = 0

See Also:
Constant Field Values

TYPE_STRING

public static final int TYPE_STRING
Field type: string = 1

See Also:
Constant Field Values

TYPE_INTEGER

public static final int TYPE_INTEGER
Field type: any integer = 2

See Also:
Constant Field Values

TYPE_NUMERIC

public static final int TYPE_NUMERIC
Field type: numeric = 3

See Also:
Constant Field Values

TYPE_BOOLEAN

public static final int TYPE_BOOLEAN
Field type: boolean = 4

See Also:
Constant Field Values

TYPE_DATE

public static final int TYPE_DATE
Field type: andate = 5

See Also:
Constant Field Values

TYPE_EMAIL

public static final int TYPE_EMAIL
Field type: email = 6

See Also:
Constant Field Values

TYPE_ENUMERATION

public static final int TYPE_ENUMERATION
Field type: enumeration = 7

See Also:
Constant Field Values
Constructor Detail

CSVParser

public CSVParser()
Default constructor.


CSVParser

public CSVParser(java.lang.String charset)
Creates new parser with the given charset.

Parameters:
charset - the charset to be used for parsing data
Method Detail

defineField

public void defineField(java.lang.String key,
                        int type,
                        boolean required)
Defines field in CSV file for validation.

Parameters:
key - field code
type - field type, one of TYPE_* constants
required - field is mandatory if set

defineFieldString

public void defineFieldString(java.lang.String key,
                              boolean required,
                              java.lang.Integer minLength,
                              java.lang.Integer maxLength)
Defines field of type String in CSV file for validation.

Parameters:
key - field code
required - field is mandatory if set
minLength - minimum length of string or null
maxLength - maximum length of string or null

defineFieldInteger

public void defineFieldInteger(java.lang.String key,
                               boolean required,
                               java.lang.Integer minValue,
                               java.lang.Integer maxValue)
Defines field of type Integer in CSV file for validation.

Parameters:
key - field code
required - field is mandatory if set
minValue - minimum value or null
maxValue - maximum value or null

defineFieldNumeric

public void defineFieldNumeric(java.lang.String key,
                               boolean required,
                               java.lang.Number minValue,
                               java.lang.Number maxValue)
Defines field of type Numeric in CSV file for validation.

Parameters:
key - field code
required - field is mandatory if set
minValue - minimum value or null
maxValue - maximum value or null

defineFieldBoolean

public void defineFieldBoolean(java.lang.String key,
                               boolean required)
Defines field of type Boolean in CSV file for validation. Possible values for boolean are:

Parameters:
key - field code
required - field is mandatory if set

defineFieldDate

public void defineFieldDate(java.lang.String key,
                            boolean required,
                            java.util.Date minValue,
                            java.util.Date maxValue)
Define field of type Date in CSV file for validation. Date can be specified in one of the formats: with one or two digits day and month

Parameters:
key - field code
required - field is mandatory if set
minValue - minimum value or null
maxValue - maximum value or null

defineFieldEmail

public void defineFieldEmail(java.lang.String key,
                             boolean required,
                             java.lang.Integer maxLength)
Define field of type Email in CSV file for validation.

Parameters:
key - field code
required - field is mandatory if set
maxLength - maximum length pf string

defineFieldEnumeration

public void defineFieldEnumeration(java.lang.String key,
                                   boolean required,
                                   java.lang.Object[] enumeration)
Define field of type Enumeration in CSV file for validation.

Parameters:
key - field code
required - field is mandatory if set
enumeration - Array of Objects containing enumerated values

parse

public java.util.HashMap<java.lang.String,java.lang.String>[] parse(java.lang.String path,
                                                                    char delim)
                                                             throws java.io.FileNotFoundException,
                                                                    java.io.IOException,
                                                                    CSVException
Function for csv file parsing.

Parameters:
path - full input file name and path
delim - tokens delimiter
Returns:
HashMap[] with key (from first line) and value pairs
Throws:
java.io.FileNotFoundException
java.io.IOException
CSVException

parse

public java.util.HashMap<java.lang.String,java.lang.String>[] parse(java.io.InputStream is,
                                                                    char delim)
                                                             throws java.io.IOException,
                                                                    CSVException
Function for csv file parsing.

Parameters:
is - input stream to parse
delim - char containing tokens delimiter
Returns:
HashMap[] with key (from first line) and value pairs
Throws:
java.io.IOException
CSVException

validate

public CSVStatus validate()
                   throws CSVException
Validate data read by parse(). Use defineField() to prepare validation

Returns:
CSVStatus - status information
Throws:
CSVException