QJCC homepage

biz.chitec.quarterback.util
Class CSVReader

java.lang.Object
  extended bybiz.chitec.quarterback.util.CSVHandler
      extended bybiz.chitec.quarterback.util.CSVReader

public class CSVReader
extends CSVHandler

Reads CSV data in and transforms them into a vector of hashtables After instantiation, a CSVReader should be given all fields of the table that have to be recognized. Then, the data stream should be opened. This could happen in two ways:

Version:
$Id: 4767238edda60772f77865b28b5ae17de01ed547 $
Author:
Dirk Hillbrecht 1997-2000, chitec/Dirk Hillbrecht 2000-2003, cantamen/Dirk Hillbrecht 2003-2006. Distributed under the terms of the GNU LGPL.
See Also:
CSVHandler, CSVWriter

Field Summary
private  java.io.BufferedReader br
           
protected  java.lang.String[] column
           
private  int defaulttype
           
private  java.lang.String nextline
           
protected  java.util.Map valuetype
           
 
Fields inherited from class biz.chitec.quarterback.util.CSVHandler
BOOL, coding, columnseparator, DATE, decimalseparator, FLOAT, iBOOL, iDATE, iFLOAT, iINT, iMEMSTRINT, INT, iSTRING, iSTRINT, MEMSTRINT, saveescaping, STRING, STRINT
 
Constructor Summary
CSVReader()
          Initializes the reader with a column separator char of ';'
CSVReader(char xsep)
          Initializes the reader.
 
Method Summary
private  java.lang.String cropQuotes(java.lang.String s)
           
 java.util.List getColumnList()
          Returns the recognized columns as list preserving their order in the CSV file
 java.util.Map getNextLine()
          returns the next line of the table as a hashtable.
 boolean hasMoreLines()
          returns whether there are more lines in the table
 java.util.Map interpret(java.lang.String nextline)
          Interpret one line of the CSV table
private  void interpretHeader(java.lang.String headerx)
           
private  boolean lineHasOnlySeparators()
           
private  boolean lineIsContinuing(java.lang.String theline)
           
static java.lang.Boolean makeBool(java.lang.String elem)
           
static EDate makeDate(java.lang.String elem)
           
static java.lang.Double makeFloat(java.lang.String elem)
           
static java.lang.Integer makeInteger(java.lang.String elem)
          makes an Integer object out of a line portion which represents an Integer
static java.lang.String makeString(java.lang.String elem, boolean saveescaping)
          makes a string object out of a line portion which represents a string
static java.lang.Object makeStrint(java.lang.String elem)
           
protected  void putInValueType(java.lang.String key, java.util.Map h)
           
private  java.lang.String readLine()
          reads a line internally.
private  void readNextLine()
           
 void removeKeyValues()
          removes all key definitions (e.g. to reuse the CSVReader for a new table)
protected  void removeValueType(java.lang.String key)
           
 void setDefaultType(java.lang.Integer typex)
           
 void setHeader(java.lang.String headerx)
          Sets and initializes the CSV header line for interpretation of strings.
 void setInputStream(java.io.InputStream is)
          Sets and initializes the input stream which contains the CSV header in the first line.
 void setInputStream(java.io.InputStream is, boolean interpretheader)
          Sets and initializes the given input stream.
protected  java.util.List splitLine(java.lang.String line)
          splits a line at its semicolons.
 
Methods inherited from class biz.chitec.quarterback.util.CSVHandler
getColumnSeparator, getDecimalSeparator, isSaveEscaping, removeKeyValue, setCoding, setColumnSeparator, setDecimalSeparator, setKeyValues, setKeyValues, setKeyValues, setSaveEscaping
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

valuetype

protected java.util.Map valuetype

br

private java.io.BufferedReader br

column

protected java.lang.String[] column

nextline

private java.lang.String nextline

defaulttype

private int defaulttype
Constructor Detail

CSVReader

public CSVReader(char xsep)
Initializes the reader.

Parameters:
xsep - column separator char.

CSVReader

public CSVReader()
Initializes the reader with a column separator char of ';'

Method Detail

setDefaultType

public void setDefaultType(java.lang.Integer typex)

putInValueType

protected void putInValueType(java.lang.String key,
                              java.util.Map h)
Specified by:
putInValueType in class CSVHandler

removeValueType

protected void removeValueType(java.lang.String key)
Specified by:
removeValueType in class CSVHandler

removeKeyValues

public void removeKeyValues()
Description copied from class: CSVHandler
removes all key definitions (e.g. to reuse the CSVReader for a new table)

Specified by:
removeKeyValues in class CSVHandler

setInputStream

public void setInputStream(java.io.InputStream is,
                           boolean interpretheader)
                    throws java.io.IOException
Sets and initializes the given input stream. The input stream is assumed to have a CSV table definition from its current position to its end. It is wrapped internally with a BufferedReader and then read linewise. If the first line contains the header, set interpretheader to true. Otherwise, setHeader(String) must have been called before with the header definition and false has to be passed as interpretheader parameter. After the header, every line contains exactly one row of the table. The CSVReader reads always one line ahead its currently computed and delivered line.

Parameters:
is - InputStream to read the CSV table from
interpretheader - Flag whether the first line really contains the header of the CSV table. If false, setHeader(String) must have been called explicitly.
Throws:
java.io.IOException

setInputStream

public void setInputStream(java.io.InputStream is)
                    throws java.io.IOException
Sets and initializes the input stream which contains the CSV header in the first line.

Parameters:
is - Stream to read the CSV file from.
Throws:
java.io.IOException

setHeader

public void setHeader(java.lang.String headerx)
Sets and initializes the CSV header line for interpretation of strings.


interpretHeader

private void interpretHeader(java.lang.String headerx)

getColumnList

public java.util.List getColumnList()
Returns the recognized columns as list preserving their order in the CSV file


hasMoreLines

public boolean hasMoreLines()
returns whether there are more lines in the table


lineHasOnlySeparators

private boolean lineHasOnlySeparators()

readNextLine

private void readNextLine()
                   throws java.io.IOException
Throws:
java.io.IOException

getNextLine

public java.util.Map getNextLine()
                          throws java.io.IOException
returns the next line of the table as a hashtable. Only those columns are delivered for which setKeyValues() has been called. Columns which are empty in the table (but have setKeyValues() called) are either left out in the Map (defaultvalue=null, forcetonull=false), delivered with its default value (defaultvalue!=null), or delivered bound to Null object (defaultvalue=null, forcetonull=true).

In case that there are no more lines in the table, null is delivered.

Throws:
java.io.IOException

interpret

public java.util.Map interpret(java.lang.String nextline)
Interpret one line of the CSV table


lineIsContinuing

private boolean lineIsContinuing(java.lang.String theline)

readLine

private java.lang.String readLine()
                           throws java.io.IOException
reads a line internally. Throws NullPointerException in case the line is empty.

Throws:
java.io.IOException

splitLine

protected java.util.List splitLine(java.lang.String line)
splits a line at its semicolons.


cropQuotes

private java.lang.String cropQuotes(java.lang.String s)

makeString

public static java.lang.String makeString(java.lang.String elem,
                                          boolean saveescaping)
makes a string object out of a line portion which represents a string


makeInteger

public static java.lang.Integer makeInteger(java.lang.String elem)
makes an Integer object out of a line portion which represents an Integer


makeFloat

public static java.lang.Double makeFloat(java.lang.String elem)

makeStrint

public static java.lang.Object makeStrint(java.lang.String elem)

makeBool

public static java.lang.Boolean makeBool(java.lang.String elem)

makeDate

public static EDate makeDate(java.lang.String elem)

QJCC homepage