mondrian.test.loader
Class CsvLoader

java.lang.Object
  extended by mondrian.test.loader.CsvLoader

public class CsvLoader
extends Object

This is a basic Comma-separated-value (CSV, Csv) reader. As input it ultimately takes a java.io.Reader but has helper support for java.io.InputStream, file names and java.io.File. One can also specify a separator character other than the default comma, ',', character and, also, that the input's first line contains the names of the columns (by default this is not assumed). Lastly, this supports only the comment character '#' and only at the start of a line. This comment support could be generalized but that task is left to others.

To use this class one gives it a java.io.Reader and then calls the hasNextLine and nextLine methods much like a java.io.Iterator but in this case the nextLine method returns a String[] holding the, possibly null, values of the parsed next line. The size of the String[] is the size of the first line parsed that contains the separator character (comment lines are not used). If the number of separator characters in subsequent lines is less than the initial numbers, the trailing entries in the String[] returned by the nextLine method are null. On the other hand, if there are more separator characters in a subsequent line, the world ends with an IndexOutOfBoundsException (sorry, making this more graceful is also a task for others). When one is through using a CsvLoader instance one should call the close method (which closes the Reader).

All well and good, but there are two additional methods that can be used to extend the capabilities of this CSV parser, the nextSet and putBack methods. With these methods one can, basically, reset the CsvLoader to a state where it does not yet know how many separator characters to expect per line (while stay at the current line in the Reader). The nextSet (next set of CSV lines) resets the loader while the putBack method can be used to place the last line returned back into loader. These methods are used in CsvDBLoader allowing one to have multiple sets of CSV rows with differing number of values per sets.

There are six special start/end characters when seen prevent the recognition of both the separator character and new lines:

    double quotes: "" ""
    single quotes: '  '
    bracket: i     [ ]
    parenthesis:   ()
    braces:        { }
    chevrons:      < >
 

Its certainly not the penultimate such parser but its hoped that its adequate.

Author:
Richard M. Emberson

Field Summary
static char BRACES_END
           
static char BRACES_START
           
static char BRACKET_END
           
static char BRACKET_START
           
static char CHEVRON_END
           
static char CHEVRON_START
           
static char DEFAULT_SEPARATOR
           
static char DOUBLE_QUOTE
           
protected static org.apache.log4j.Logger LOGGER
           
static char PAREN_END
           
static char PAREN_START
           
static char SINGLE_QUOTE
           
 
Constructor Summary
CsvLoader(File file)
           
CsvLoader(File file, char separator, boolean includesHeader)
           
CsvLoader(InputStream in)
           
CsvLoader(InputStream in, char separator, boolean includesHeader)
           
CsvLoader(InputStream in, char separator, boolean includesHeader, String charset)
           
CsvLoader(InputStream in, String charset)
           
CsvLoader(Reader reader)
           
CsvLoader(Reader reader, char separator, boolean includesHeader)
           
CsvLoader(String filename)
           
CsvLoader(String filename, char separator, boolean includesHeader)
           
 
Method Summary
 void close()
           
 String[] getColumnNames()
           
 int getNextSetCount()
           
 boolean hasNextLine()
           
 boolean inComment()
           
protected  void initialize()
           
static void main(String[] args)
           
protected  String[] nextColumns()
           
 String[] nextLine()
           
 void nextSet()
           
 void putBack(String[] columns)
           
protected  void recordInCommentLine(String line)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOGGER

protected static final org.apache.log4j.Logger LOGGER

DEFAULT_SEPARATOR

public static final char DEFAULT_SEPARATOR
See Also:
Constant Field Values

DOUBLE_QUOTE

public static final char DOUBLE_QUOTE
See Also:
Constant Field Values

SINGLE_QUOTE

public static final char SINGLE_QUOTE
See Also:
Constant Field Values

BRACKET_START

public static final char BRACKET_START
See Also:
Constant Field Values

BRACKET_END

public static final char BRACKET_END
See Also:
Constant Field Values

PAREN_START

public static final char PAREN_START
See Also:
Constant Field Values

PAREN_END

public static final char PAREN_END
See Also:
Constant Field Values

BRACES_START

public static final char BRACES_START
See Also:
Constant Field Values

BRACES_END

public static final char BRACES_END
See Also:
Constant Field Values

CHEVRON_START

public static final char CHEVRON_START
See Also:
Constant Field Values

CHEVRON_END

public static final char CHEVRON_END
See Also:
Constant Field Values
Constructor Detail

CsvLoader

public CsvLoader(InputStream in,
                 String charset)
          throws UnsupportedEncodingException
Throws:
UnsupportedEncodingException

CsvLoader

public CsvLoader(InputStream in,
                 char separator,
                 boolean includesHeader,
                 String charset)
          throws UnsupportedEncodingException
Throws:
UnsupportedEncodingException

CsvLoader

public CsvLoader(InputStream in)

CsvLoader

public CsvLoader(InputStream in,
                 char separator,
                 boolean includesHeader)

CsvLoader

public CsvLoader(String filename)
          throws FileNotFoundException
Throws:
FileNotFoundException

CsvLoader

public CsvLoader(String filename,
                 char separator,
                 boolean includesHeader)
          throws FileNotFoundException
Throws:
FileNotFoundException

CsvLoader

public CsvLoader(File file)
          throws FileNotFoundException
Throws:
FileNotFoundException

CsvLoader

public CsvLoader(File file,
                 char separator,
                 boolean includesHeader)
          throws FileNotFoundException
Throws:
FileNotFoundException

CsvLoader

public CsvLoader(Reader reader)

CsvLoader

public CsvLoader(Reader reader,
                 char separator,
                 boolean includesHeader)
Method Detail

initialize

protected void initialize()
                   throws IOException
Throws:
IOException

getColumnNames

public String[] getColumnNames()
                        throws IOException
Throws:
IOException

inComment

public boolean inComment()

hasNextLine

public boolean hasNextLine()
                    throws IOException
Throws:
IOException

nextLine

public String[] nextLine()

getNextSetCount

public int getNextSetCount()

nextSet

public void nextSet()

putBack

public void putBack(String[] columns)

close

public void close()
           throws IOException
Throws:
IOException

nextColumns

protected String[] nextColumns()
                        throws IOException
Throws:
IOException

recordInCommentLine

protected void recordInCommentLine(String line)

main

public static void main(String[] args)
                 throws Exception
Throws:
Exception

Get Mondrian at SourceForge.net. Fast, secure and free Open Source software downloads