|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.billpringle.utils.WrpCsv
public class WrpCsv
This class encapsulates a parser for CSV files. This class can also be used to read CSV data from a buffered input stream, such as a file.
The input string is separated into fields by any of the specified delimiters, which defaults to a comma.
This implementation is based on a similar routine that I wrote in C, which was in turn based loosely on Kernighan and Pike's "The Practice of Programming." The C version I wrote, and this Java version are both available under a Creative Commons license ( Attribution, Non-Commercial, and Share-Alike)).
This class is normally called from other classes, but contains a test driver main routine for testing.
![]() |
![]() |
Unless noted otherwise, all materials available for download from my site are copyrighted by Bill Pringle, and are licensed under a Creative Commons License. |
Field Summary | |
---|---|
private static java.lang.String |
defaultDelims
Default collection of delimiters. |
private static int |
defaultMaxFields
Default maximum number of fields. |
private static int |
defaultMaxLine
Default maximum size of the input line. |
protected java.lang.String |
delims
Possible field separators. |
protected java.lang.String |
errorMessage
Error message for last error. |
protected java.util.Vector<java.lang.String> |
fields
List of fields parsed from input line |
protected java.lang.Integer |
maxFields
Maximum number of fields expected. |
protected int |
maxLine
Maximum input line size (not used) |
protected java.lang.Integer |
minFields
Minimum number of fields expected (not used) |
protected java.lang.Integer |
numFields
Number of fields found in the input line |
protected java.lang.String |
parseLine
Input line to be split into fields |
Constructor Summary | |
---|---|
WrpCsv()
Default constructor. |
|
WrpCsv(java.lang.String str)
Creates a new instance of WrpCsv and parses the specified string into fields. |
|
WrpCsv(java.lang.String str,
java.lang.String delstr)
Creates a new instance of WrpCsv and parses the specified string into fields, using the specified delimiter(s). |
Method Summary | |
---|---|
void |
clearErrorMessage()
Clear the current error message. |
private java.lang.String |
copyField(java.lang.String str)
Copy string, transforming consecutive double quotes into one. |
java.lang.String |
getDelims()
Return current delimiters. |
java.lang.String |
getErrorMessage()
Return the most recent error message. |
java.lang.String |
getField(int loc)
Get the specific field. |
java.lang.String |
getLine()
Return the original line. |
int |
getMaxFields()
Return the current maximum number of fields. |
int |
getMaxLine()
Get the maximum line size (not really used) |
int |
getNumFields()
Return the current number of fields. |
private void |
init()
Initializes class variables. |
static void |
main(java.lang.String[] args)
Test driver. |
private int |
nonquotedField(java.lang.String str,
int sloc)
Return the offset for the terminating delimiter for a non-quoted field. |
int |
parseString(java.lang.String str)
Main parsing routine - parse string into fields. |
int |
parseString(java.lang.String str,
java.lang.String delims)
Parse string using specified delimiters. |
private int |
quotedField(java.lang.String str,
int sloc)
Return index location of the closing quote for the current quoted field. |
int |
readLine(java.io.BufferedReader inp)
Read a line from an input stream and parse it. |
void |
setDelims(java.lang.String str)
Set field delimiters for current CSV. |
void |
setMaxFields(int val)
Set the maximum number of fields. |
void |
setMaxLine(int val)
Set the maximum size of the input line |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private static int defaultMaxFields
private static int defaultMaxLine
private static java.lang.String defaultDelims
protected int maxLine
protected java.lang.String parseLine
protected java.util.Vector<java.lang.String> fields
protected java.lang.Integer numFields
protected java.lang.Integer maxFields
protected java.lang.Integer minFields
protected java.lang.String delims
protected java.lang.String errorMessage
Constructor Detail |
---|
public WrpCsv()
This constructor must be used if the default values are not appropriate. The client should use this constructor, and then the appropriate setter routines to modify the default values.
If this constructor is used, one of the parseString methods must be used to parse the string.
public WrpCsv(java.lang.String str)
This method will use the default values and delimiters to parse the fields. If different delimiters are to be used, the client can use the WrpCsv(String, String) constructor.
str
- string to be parsedpublic WrpCsv(java.lang.String str, java.lang.String delstr)
This constructor creates a new instance of WrpCsv and parses the string into fields using the specified set of delimiters, using the default values.
str
- input string to parsedelstr
- field delimitersMethod Detail |
---|
public int getMaxLine()
public void setMaxLine(int val)
public void setDelims(java.lang.String str)
str
- public java.lang.String getDelims()
public java.lang.String getErrorMessage()
public void clearErrorMessage()
public int getMaxFields()
public void setMaxFields(int val)
val
- new max. number of fieldspublic int parseString(java.lang.String str)
This method drives the parsing, by scanning the input line and calling either quotedField or nonquotedField to extract the actual field.
Upon return, each field is stored in the fields vector, and can be accessed using the getField() method.
str
- string to parse
public int parseString(java.lang.String str, java.lang.String delims)
The delimiters are saved, and will be used for all future calls unless they are reset.
str
- string to parsedelims
- delimiter characters to use
public int readLine(java.io.BufferedReader inp) throws java.io.IOException
This method can be used to parse each line in a file. Once the CSV is created, by calling this method, you will read and parse the next line in the input file.
After making this call, the client can call the various get routines to retrieve the actual fields.
inp
- BufferedReader input file
java.io.IOException
public int getNumFields()
public java.lang.String getLine()
public java.lang.String getField(int loc)
private void init()
This method is called by the constructors and the parseString() methods to initialize all class variables.
private int quotedField(java.lang.String str, int sloc)
Internal double quotes are indicated by two double quotes in a row, which is treated as a single character. If not closing quote is found, the location past the end of the string is returned. This means that the value returned can be used as an argument for the substring method to extract the field.
str
- string being parsedsloc
- offset to start searching (starting with zero)
private int nonquotedField(java.lang.String str, int sloc)
This method will scan for the first occurrence of any of the currently defined delimiter characters.
str
- string being parsedsloc
- offset of where to start searching
private java.lang.String copyField(java.lang.String str)
str
- field to copy
public static void main(java.lang.String[] args)
args
- not used
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |