Class CsvLoader
- java.lang.Object
-
- mondrian.test.loader.CsvLoader
-
public class CsvLoader extends Object
This is a basic Comma-separated-value (CSV, Csv) reader. As input it ultimately takes ajava.io.Reader
but has helper support forjava.io.InputStream, file
names andjava.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 thehasNextLine
andnextLine
methods much like ajava.io.Iterator
but in this case thenextLine
method returns aString[]
holding the, possibly null, values of the parsed next line. The size of theString[]
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 theString[]
returned by thenextLine
method are null. On the other hand, if there are more separator characters in a subsequent line, the world ends with anIndexOutOfBoundsException
(sorry, making this more graceful is also a task for others). When one is through using aCsvLoader
instance one should call the close method (which closes theReader
).All well and good, but there are two additional methods that can be used to extend the capabilities of this CSV parser, the
nextSet
andputBack
methods. With these methods one can, basically, reset theCsvLoader
to a state where it does not yet know how many separator characters to expect per line (while stay at the current line in theReader
). ThenextSet
(next set of CSV lines) resets the loader while theputBack
method can be used to place the last line returned back into loader. These methods are used inCsvDBLoader
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
Fields Modifier and Type Field Description 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
Constructors Constructor Description 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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description 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)
-
-
-
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)
-
-