org.mmbase.util
Class CSVReader

java.lang.Object
  extended by org.mmbase.util.CSVReader

public class CSVReader
extends Object

Reads Comma Separated Values (CSV). Uses utf-8 as the default characterset. The csv should live somewhere in the webroot. The actual csv parsing is done with examplecode from O'Reilly's Java Cookbook written by Ian F. Darwin: "\"([^\"]+?)\",?|([^,]+),?|,".

Version:
$Id: CSVReader.java 41055 2010-02-15 23:59:16Z michiel $
Author:
Andr\U00e9 vanToly <andre@toly.nl>
To Do:
I don't think it supports multiple line records, The entire CSV is stored in memory twice (in public structures), The CSV is stored in a Map with List values, it could more logically be stored in a List of List, It would also be possible to parse a CSV without storing it at all, so that you can also parse really huge ones., The 'header' member is unused and public., Test-cases?

Field Summary
 List<String> header
           
 List<String> lines
           
 Map<Integer,List<String>> rows
           
 
Constructor Summary
CSVReader()
           
CSVReader(String filename)
           
CSVReader(String filename, String delimiter)
           
CSVReader(String filename, String delimiter, String charset)
          Constructor
 
Method Summary
 String getDescription()
          Description of the CSVReader, reports its configuration f.e.
 String getElement(int r, int c)
          Returns the element at the given row and column.
 Map<Integer,List<String>> getValues(String filename)
           
 Map<Integer,List<String>> getValues(String filename, String delimiter)
           
 Map<Integer,List<String>> getValues(String filename, String delimiter, String charset)
          Map to use in a taglib function.
 void readCSV(String filename, String delimiter, String charset)
          Reads the contents of the CSV file.
 int size()
          Returns the number of rows in the CVS file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

lines

public List<String> lines

header

public List<String> header

rows

public Map<Integer,List<String>> rows
Constructor Detail

CSVReader

public CSVReader(String filename,
                 String delimiter,
                 String charset)
Constructor


CSVReader

public CSVReader(String filename,
                 String delimiter)

CSVReader

public CSVReader(String filename)

CSVReader

public CSVReader()
Method Detail

readCSV

public void readCSV(String filename,
                    String delimiter,
                    String charset)
Reads the contents of the CSV file. The values are stored in arrays.

Parameters:
filename - CSV file
delimiter - the komma or something else TODO!
charset - by default UTF-8
To Do:
the params are members too, which is a bit confusing for a non-static method.

getElement

public String getElement(int r,
                         int c)
Returns the element at the given row and column.

Parameters:
r - the element row
c - the element column
Returns:
the element as a String.

getValues

public Map<Integer,List<String>> getValues(String filename)

getValues

public Map<Integer,List<String>> getValues(String filename,
                                           String delimiter)

getValues

public Map<Integer,List<String>> getValues(String filename,
                                           String delimiter,
                                           String charset)
Map to use in a taglib function. Calls readCSV(java.lang.String, java.lang.String, java.lang.String) and returns csv-file rows.

Parameters:
filename - CSV file
Returns:
map with an array per row with values
To Do:
I don't understand why it does not return List> in stead.

size

public int size()
Returns the number of rows in the CVS file.


getDescription

public String getDescription()
Description of the CSVReader, reports its configuration f.e.

Returns:
some information about CSVReader, like config etc.


MMBase 2.0-SNAPSHOT - null