org.apache.turbine.util.parser
Class DataStreamParser

java.lang.Object
  extended byorg.apache.turbine.util.parser.DataStreamParser
All Implemented Interfaces:
java.util.Iterator
Direct Known Subclasses:
CSVParser, TSVParser

public abstract class DataStreamParser
extends java.lang.Object
implements java.util.Iterator

DataStreamParser is used to parse a stream with a fixed format and generate ValueParser objects which can be used to extract the values in the desired type.

The class itself is abstract - a concrete subclass which implements the initTokenizer method such as CSVParser or TSVParser is required to use the functionality.

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

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

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

Field Summary
protected static java.lang.String EMPTYFIELDNAME
          The constant for empty fields
 
Constructor Summary
DataStreamParser(java.io.Reader in, java.util.List columnNames, java.lang.String characterEncoding)
          Create a new DataStreamParser instance.
 
Method Summary
 java.util.List getColumnNames()
          get the list of column names.
 boolean hasNext()
          Determine whether a further row of values exists in the input.
 boolean hasNextRow()
          Determine whether a further row of values exists in the input.
protected  void initTokenizer(java.io.StreamTokenizer tokenizer)
          Initialize the StreamTokenizer instance used to read the lines from the input reader.
 java.lang.Object next()
          Returns a ValueParser object containing the next row of values.
 ValueParser nextRow()
          Returns a ValueParser object containing the next row of values.
 void readColumnNames()
          Read the list of column names from the input reader using the tokenizer.
 void remove()
          The optional Iterator.remove method is not supported.
 void setColumnNames(java.util.List columnNames)
          Set the list of column names explicitly.
 void setFieldSeparator(char fieldSeparator)
          This method must be called to setup the field seperator
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTYFIELDNAME

protected static final java.lang.String EMPTYFIELDNAME
The constant for empty fields

See Also:
Constant Field Values
Constructor Detail

DataStreamParser

public DataStreamParser(java.io.Reader in,
                        java.util.List columnNames,
                        java.lang.String characterEncoding)
Create a new DataStreamParser 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. This must be implemented in subclasses to set up other tokenizing properties.

Parameters:
tokenizer - the tokenizer to adjust

setFieldSeparator

public void setFieldSeparator(char fieldSeparator)
This method must be called to setup the field seperator

Parameters:
fieldSeparator - the char which separates the fields

setColumnNames

public void setColumnNames(java.util.List columnNames)
Set the list of column names explicitly.

Parameters:
columnNames - A list of column names.

getColumnNames

public java.util.List getColumnNames()
get the list of column names.


readColumnNames

public void readColumnNames()
                     throws java.io.IOException
Read the list of column names from the input reader using the tokenizer. If fieldNames are empty, we use the current fieldNumber + the EMPTYFIELDNAME to make one up.

Throws:
java.io.IOException - an IOException occurred.

hasNextRow

public boolean hasNextRow()
                   throws java.io.IOException
Determine whether a further row of values exists in the input.

Returns:
true if the input has more rows.
Throws:
java.io.IOException - an IOException occurred.

nextRow

public ValueParser nextRow()
                    throws java.io.IOException,
                           java.util.NoSuchElementException
Returns a ValueParser object containing the next row of values.

Returns:
a ValueParser object.
Throws:
java.io.IOException - an IOException occurred.
java.util.NoSuchElementException - there are no more rows in the input.

hasNext

public boolean hasNext()
Determine whether a further row of values exists in the input.

Specified by:
hasNext in interface java.util.Iterator
Returns:
true if the input has more rows.

next

public java.lang.Object next()
                      throws java.util.NoSuchElementException
Returns a ValueParser object containing the next row of values.

Specified by:
next in interface java.util.Iterator
Returns:
a ValueParser object as an Object.
Throws:
java.util.NoSuchElementException - there are no more rows in the input or an IOException occurred.

remove

public void remove()
            throws java.lang.UnsupportedOperationException
The optional Iterator.remove method is not supported.

Specified by:
remove in interface java.util.Iterator
Throws:
java.lang.UnsupportedOperationException - the operation is not supported.


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