de.fuberlin.wiwiss.d2rq.helpers
Class CSV

java.lang.Object
  extended byde.fuberlin.wiwiss.d2rq.helpers.CSV

class CSV
extends java.lang.Object

Parse comma-separated values (CSV), a common Windows file format. Sample input: "LU",86.25,"11/4/1998","2:19PM",+4.0625

Inner logic adapted from a C++ original that was Copyright (C) 1999 Lucent Technologies Excerpted from 'The Practice of Programming' by Brian W. Kernighan and Rob Pike.

Included by permission of the http://tpop.awl.com/ web site, which says: "You may use this code for any purpose, as long as you leave the copyright notice and book citation attached." I have done so.

History:
08-03-2004: Lifted from somewhere on the Web

Version:
V0.2
Author:
Brian W. Kernighan and Rob Pike (C++ original), Ian F. Darwin (translation into Java and removal of I/O), Ben Ballard (rewrote advQuoted to handle '""' and for readability), Don Brown (wanted to return a String array instead of Iterator), Richard Cyganiak (adapted to local coding style)

Field Summary
static char DEFAULT_SEP
           
private  char fieldSep
          the separator char for this parser
private  java.util.ArrayList list
          The fields in the current String
 
Constructor Summary
CSV()
          Construct a CSV parser, with the default separator (`,').
CSV(char sep)
          Construct a CSV parser with a given separator.
 
Method Summary
private  int advPlain(java.lang.String s, java.lang.StringBuffer sb, int i)
          advPlain: unquoted field; return index of next separator
private  int advQuoted(java.lang.String s, java.lang.StringBuffer sb, int i)
          advQuoted: quoted field; return index of next separator
 java.lang.String[] parse(java.lang.String line)
          parse: break the input String into fields
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_SEP

public static final char DEFAULT_SEP
See Also:
Constant Field Values

list

private java.util.ArrayList list
The fields in the current String


fieldSep

private char fieldSep
the separator char for this parser

Constructor Detail

CSV

public CSV()
Construct a CSV parser, with the default separator (`,').


CSV

public CSV(char sep)
Construct a CSV parser with a given separator. Must be exactly the string that is the separator, not a list of separator characters!

Method Detail

parse

public java.lang.String[] parse(java.lang.String line)
parse: break the input String into fields

Returns:
String array containing each field from the original as a String, in order.

advQuoted

private int advQuoted(java.lang.String s,
                      java.lang.StringBuffer sb,
                      int i)
advQuoted: quoted field; return index of next separator


advPlain

private int advPlain(java.lang.String s,
                     java.lang.StringBuffer sb,
                     int i)
advPlain: unquoted field; return index of next separator