com.knowgate.misc
Class CSVParser

java.lang.Object
  extended by com.knowgate.misc.CSVParser
Direct Known Subclasses:
DespatchAdviceLoader

public class CSVParser
extends java.lang.Object

Delimited Text Parser

Parses a delimited text file into a memory array


Constructor Summary
CSVParser()
           
CSVParser(java.lang.String sCharSetName)
          Create CSV Parser and set encoding to be used
 
Method Summary
 java.lang.String charSet()
           
 void charSet(java.lang.String sCharSetName)
           
 int errorLine()
           
 int find(int iCol, java.lang.String sVal)
          Find first occurence of a value at a given column Search is case sensitive
 int findi(int iCol, java.lang.String sVal)
          Find first occurence of a value at a given column Search is case insensitive
 int getColumnCount()
          Get column count
 int getColumnPosition(java.lang.String sColumnName)
           
 char getDelimiter()
           
 java.lang.String getField(int iCol, int iRow)
          Get value for a field at a given row and column.
 java.lang.String getField(java.lang.String sCol, int iRow)
          Get value for a field at a given row and column.
 java.lang.String getLine(int iLine)
          Get line from a parsed file.
 java.util.HashMap getLineAsMap(int iLine)
          Get line from a parsed file as a Map of named values.
 int getLineCount()
          Get line count
 void parseData(char[] aCharData, java.lang.String sFileDescriptor)
          Parse data from a character array Parsed values are stored at an internal array in this CSVParser.
 void parseFile(java.io.File oFile, java.lang.String sFileDescriptor)
          Parse a delimited text file Parsed values are stored at an internal array in this CSVParser.
 void parseFile(java.lang.String sFilePath, java.lang.String sFileDescriptor)
          Parse a delimited text file Parsed values are stored at an internal array in this CSVParser.
 void writeToFile(java.lang.String sFilePath)
          Write CSVParser matrix to delimited text file
 void writeToStream(java.io.OutputStream oStrm)
          Write CSVParser matrix to an output stream
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CSVParser

public CSVParser()

CSVParser

public CSVParser(java.lang.String sCharSetName)
Create CSV Parser and set encoding to be used

Parameters:
sCharSetName - Name of charset encoding
Method Detail

charSet

public java.lang.String charSet()

charSet

public void charSet(java.lang.String sCharSetName)

getLineCount

public int getLineCount()
Get line count

Returns:
int
Since:
3.0

getColumnCount

public int getColumnCount()
Get column count

Returns:
int
Since:
3.0

errorLine

public int errorLine()

getDelimiter

public char getDelimiter()

parseData

public void parseData(char[] aCharData,
                      java.lang.String sFileDescriptor)
               throws java.lang.ArrayIndexOutOfBoundsException,
                      java.lang.RuntimeException,
                      java.lang.NullPointerException,
                      java.lang.IllegalArgumentException

Parse data from a character array

Parsed values are stored at an internal array in this CSVParser.

Parameters:
sFileDescriptor - A list of column names separated by ',' ';' '|' '`' or '\t'. Column names may be quoted. Lines are delimiter by '\n' characters
Example 1) tx_mail,tx_name,tx_surname
Example 2) "tx_name","tx_surname","tx_salutation"
Throws:
java.lang.ArrayIndexOutOfBoundsException - Delimited values for a file is greater than columns specified at descriptor.
java.lang.RuntimeException - If delimiter is not one of { ',' ';' '|' '`' or '\t' }
java.lang.NullPointerException - if sFileDescriptor is null
java.lang.IllegalArgumentException - if sFileDescriptor is ""

parseFile

public void parseFile(java.io.File oFile,
                      java.lang.String sFileDescriptor)
               throws java.lang.ArrayIndexOutOfBoundsException,
                      java.io.IOException,
                      java.io.FileNotFoundException,
                      java.lang.RuntimeException,
                      java.lang.NullPointerException,
                      java.lang.IllegalArgumentException,
                      java.io.UnsupportedEncodingException

Parse a delimited text file

Parsed values are stored at an internal array in this CSVParser.
File is readed using the character set specifid at constructor

Parameters:
oFile - CSV File
sFileDescriptor - A list of column names separated by ',' ';' '|' '`' or '\t'. Column names may be quoted. Lines are delimiter by '\n' characters
Example 1) tx_mail,tx_name,tx_surname
Example 2) "tx_name","tx_surname","tx_salutation"
Throws:
java.io.IOException
java.io.FileNotFoundException
java.lang.ArrayIndexOutOfBoundsException - Delimited values for a file is greater than columns specified at descriptor.
java.lang.RuntimeException - If delimiter is not one of { ',' ';' '|' '`' or '\t' }
java.lang.NullPointerException - if oFile or sFileDescriptor are null
java.lang.IllegalArgumentException - if sFileDescriptor is ""
java.io.UnsupportedEncodingException
Since:
3.0

parseFile

public void parseFile(java.lang.String sFilePath,
                      java.lang.String sFileDescriptor)
               throws java.lang.ArrayIndexOutOfBoundsException,
                      java.io.IOException,
                      java.io.FileNotFoundException,
                      java.lang.RuntimeException,
                      java.lang.NullPointerException,
                      java.lang.IllegalArgumentException,
                      java.io.UnsupportedEncodingException

Parse a delimited text file

Parsed values are stored at an internal array in this CSVParser.

Parameters:
sFilePath - File Path
sFileDescriptor - A list of column names separated by ',' ';' '|' '`' or '\t'. Column names may be quoted. Lines are delimiter by '\n' characters
Example 1) tx_mail,tx_name,tx_surname
Example 2) "tx_name","tx_surname","tx_salutation"
Throws:
java.io.IOException
java.io.FileNotFoundException
java.lang.ArrayIndexOutOfBoundsException - Delimited values for a file is greater than columns specified at descriptor.
java.lang.RuntimeException - If delimiter is not one of { ',' ';' '|' '`' or '\t' }
java.lang.NullPointerException - if oFile or sFileDescriptor are null
java.lang.IllegalArgumentException - if sFileDescriptor is ""
java.io.UnsupportedEncodingException

getColumnPosition

public int getColumnPosition(java.lang.String sColumnName)
Parameters:
sColumnName - Column Name
Returns:
Zero based index for column position or -1 if column was not found.

getLine

public java.lang.String getLine(int iLine)
                         throws java.lang.IllegalStateException,
                                java.io.UnsupportedEncodingException

Get line from a parsed file.

Lines are delimited by the Line Feed (LF, CHAR(10), '\n') character

Parameters:
iLine - Line Number [0..getLineCount()-1]
Returns:
Full Text for Line. If iLine<0 or iLine>=getLineCount() then null
Throws:
java.lang.IllegalStateException - If parseFile() has not been called prior to getLine()
java.io.UnsupportedEncodingException

getLineAsMap

public java.util.HashMap getLineAsMap(int iLine)
                               throws java.lang.IllegalStateException,
                                      java.io.UnsupportedEncodingException,
                                      java.lang.ArrayIndexOutOfBoundsException

Get line from a parsed file as a Map of named values.

This method is usefull when parsing plain text lines into DBPersist instances

Parameters:
iLine - Line Number [0..getLineCount()-1]
Returns:
HashMap
Throws:
java.lang.IllegalStateException - If parseFile() has not been called prior to getLine()
java.io.UnsupportedEncodingException
java.lang.ArrayIndexOutOfBoundsException
Since:
4.0

getField

public java.lang.String getField(int iCol,
                                 int iRow)
                          throws java.lang.IllegalStateException,
                                 java.lang.ArrayIndexOutOfBoundsException,
                                 java.lang.StringIndexOutOfBoundsException,
                                 java.io.UnsupportedEncodingException

Get value for a field at a given row and column.

Column indexes are zero based.
Row indexes range from 0 to getLineCount()-1.

Parameters:
iCol - Column Index
iRow - Row Index
Returns:
Field Value
Throws:
java.lang.IllegalStateException - If parseFile() method was not called prior to getField()
java.lang.ArrayIndexOutOfBoundsException - If Column or Row Index is out of bounds.
java.lang.StringIndexOutOfBoundsException - If Row is malformed.
java.io.UnsupportedEncodingException - If charset encoding name is not recognized.

getField

public java.lang.String getField(java.lang.String sCol,
                                 int iRow)
                          throws java.lang.IllegalStateException,
                                 java.lang.ArrayIndexOutOfBoundsException,
                                 java.lang.StringIndexOutOfBoundsException,
                                 java.io.UnsupportedEncodingException

Get value for a field at a given row and column.

Parameters:
sCol - Column name
iRow - Row position [0..getLineCount()-1]
Returns:
Field value
Throws:
java.lang.IllegalStateException
java.lang.ArrayIndexOutOfBoundsException
java.lang.StringIndexOutOfBoundsException
java.io.UnsupportedEncodingException

find

public int find(int iCol,
                java.lang.String sVal)
         throws java.io.UnsupportedEncodingException

Find first occurence of a value at a given column

Search is case sensitive

Parameters:
iCol - int Column index [0..getColumnCount()-1]
sVal - String Value sought
Returns:
int
Throws:
java.io.UnsupportedEncodingException
Since:
3.0

findi

public int findi(int iCol,
                 java.lang.String sVal)
          throws java.io.UnsupportedEncodingException

Find first occurence of a value at a given column

Search is case insensitive

Parameters:
iCol - int Column index [0..getColumnCount()-1]
sVal - String Value sought
Returns:
int
Throws:
java.io.UnsupportedEncodingException
Since:
3.0

writeToStream

public void writeToStream(java.io.OutputStream oStrm)
                   throws java.io.IOException
Write CSVParser matrix to an output stream

Parameters:
oStrm - OutputStream
Throws:
java.io.IOException
Since:
3.0

writeToFile

public void writeToFile(java.lang.String sFilePath)
                 throws java.io.IOException,
                        java.lang.SecurityException
Write CSVParser matrix to delimited text file

Parameters:
oStrm - OutputStream
Throws:
java.io.IOException
java.lang.SecurityException
Since:
3.0