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.Readerbut has helper support forjava.io.InputStream, filenames 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.Readerand then calls thehasNextLineandnextLinemethods much like ajava.io.Iteratorbut in this case thenextLinemethod 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 thenextLinemethod 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 aCsvLoaderinstance 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
nextSetandputBackmethods. With these methods one can, basically, reset theCsvLoaderto 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 theputBackmethod can be used to place the last line returned back into loader. These methods are used inCsvDBLoaderallowing 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 charBRACES_ENDstatic charBRACES_STARTstatic charBRACKET_ENDstatic charBRACKET_STARTstatic charCHEVRON_ENDstatic charCHEVRON_STARTstatic charDEFAULT_SEPARATORstatic charDOUBLE_QUOTEprotected static org.apache.log4j.LoggerLOGGERstatic charPAREN_ENDstatic charPAREN_STARTstatic charSINGLE_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 voidclose()String[]getColumnNames()intgetNextSetCount()booleanhasNextLine()booleaninComment()protected voidinitialize()static voidmain(String[] args)protected String[]nextColumns()String[]nextLine()voidnextSet()voidputBack(String[] columns)protected voidrecordInCommentLine(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)
-
-