org.apache.turbine.util.parser
Class CSVParser

java.lang.Object
  extended byorg.apache.turbine.util.parser.DataStreamParser
      extended byorg.apache.turbine.util.parser.CSVParser
All Implemented Interfaces:
java.util.Iterator

public class CSVParser
extends DataStreamParser

CSVParser is used to parse a stream with comma-separated values and generate ParameterParser objects which can be used to extract the values in the desired type.

The class extends the abstract class DataStreamParser and implements initTokenizer with suitable values for CSV files to provide this functionality.

The class (indirectly through DataStreamParser) implements the java.util.Iterator interface for convenience. This allows simple use in a Velocity template for example:

 #foreach ($row in $csvfile)
   Name: $row.Name
   Description: $row.Description
 #end
 

Version:
$Id: CSVParser.java 534527 2007-05-02 16:10:59Z tv $
Author:
Sean Legassick, Martin van den Bemt, Henning P. Schmiedehausen

Field Summary
 
Fields inherited from class org.apache.turbine.util.parser.DataStreamParser
EMPTYFIELDNAME
 
Constructor Summary
CSVParser(java.io.Reader in)
          Create a new CSVParser instance.
CSVParser(java.io.Reader in, java.util.List columnNames)
          Create a new CSVParser instance.
CSVParser(java.io.Reader in, java.util.List columnNames, java.lang.String characterEncoding)
          Create a new CSVParser instance.
 
Method Summary
protected  void initTokenizer(java.io.StreamTokenizer tokenizer)
          Initialize the StreamTokenizer instance used to read the lines from the input reader.
 
Methods inherited from class org.apache.turbine.util.parser.DataStreamParser
getColumnNames, hasNext, hasNextRow, next, nextRow, readColumnNames, remove, setColumnNames, setFieldSeparator
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CSVParser

public CSVParser(java.io.Reader in)
Create a new CSVParser instance. Requires a Reader to read the comma-separated values from. The column headers must be set independently either explicitly, or by reading the first line of the CSV values.

Parameters:
in - the input reader.

CSVParser

public CSVParser(java.io.Reader in,
                 java.util.List columnNames)
Create a new CSVParser instance. Requires a Reader to read the comma-separated values from, and a list of column names.

Parameters:
in - the input reader.
columnNames - a list of column names.

CSVParser

public CSVParser(java.io.Reader in,
                 java.util.List columnNames,
                 java.lang.String characterEncoding)
Create a new CSVParser instance. Requires a Reader to read the comma-separated values from, a list of column names and a character encoding.

Parameters:
in - the input reader.
columnNames - a list of column names.
characterEncoding - the character encoding of the input.
Method Detail

initTokenizer

protected void initTokenizer(java.io.StreamTokenizer tokenizer)
Initialize the StreamTokenizer instance used to read the lines from the input reader. It is now only needed to set the fieldSeparator

Overrides:
initTokenizer in class DataStreamParser
Parameters:
tokenizer - the tokenizer to adjust


Copyright © 2000-2008 Apache Software Foundation. All Rights Reserved.