public class DataParser extends java.lang.Object implements BfsParser
setInputStream(InputStream)
. The actual parsing can be done
in two different ways:
parseToBof()
and nextData()
methods.
parse(EventHandler)
method.
This parser issues the following events:
DATA_EVENT
: For each data line that is parsed.
END_OF_FILE_EVENT
: When the end of the file has been reached.
MatrixModel
class implements a simple event handler that
collects the information from the data file and provides methods
for accessing it, though it is recommended only for small files.
This class may be subclassed to provide customized behaviour.
Modifier and Type | Field and Description |
---|---|
static EventType<java.lang.String[]> |
DATA_EVENT
Event type that is issued for each data line.
|
private EncoderDecoder |
decoder |
static EventType<java.lang.Object> |
END_OF_FILE_EVENT
Event type that is issued when the end-of-file had been reached.
|
private FlatFileParser |
ffp |
private java.lang.String |
filename |
private long |
fileSize |
private java.io.InputStream |
in |
private int |
numColumns |
Constructor and Description |
---|
DataParser()
Create a new data parser.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Close the parser and relase underlying system resources that
are associated with it.
|
static DataParser |
create(File file)
Utility method for creating a data parser for a file in the BASE
file system.
|
static DataParser |
create(java.io.File file)
Utility method for creating a data parser for a file in the local
file system.
|
static DataParser |
create(java.io.InputStream in,
java.lang.String filename,
long fileSize)
Utility method for creating a data parser when you have an
input stream.
|
protected FlatFileParser |
createFlatFileParser(java.io.InputStream in)
Create a new flat file parser that can parse BFS annotation
files.
|
java.lang.String |
decodeValue(java.lang.String value)
Decode an encoded value.
|
int |
getCurrentLine()
Get the current line number.
|
java.lang.String |
getFilename()
Get the file name that this parser is reading from.
|
long |
getFileSize()
Get the size in bytes of the file that this parser is reading from.
|
long |
getParsedBytes()
Get the number of parsed bytes so far.
|
protected void |
handleData(EventHandler handler,
java.lang.String[] data)
Handle the data event.
|
protected void |
handleEndOfFile(EventHandler handler)
Handle the end-of-file event.
|
java.lang.String[] |
nextData()
Get the next data line in the file.
|
void |
parse(EventHandler handler)
Parse the input stream and notify the specified event handler with events.
|
void |
parseToBof()
Initialize parsing of the data file.
|
void |
setFilename(java.lang.String filename)
Set the file name that this parser is reading from.
|
void |
setFileSize(long fileSize)
Set the size of the file or -1 if not known.
|
void |
setInputStream(java.io.InputStream in)
Set the input stream that should be parsed.
|
void |
setNumColumns(int numColumns)
Set the number of data columns that are expected to be found in this
file.
|
public static final EventType<java.lang.String[]> DATA_EVENT
public static final EventType<java.lang.Object> END_OF_FILE_EVENT
private final EncoderDecoder decoder
private java.io.InputStream in
private java.lang.String filename
private long fileSize
private FlatFileParser ffp
private int numColumns
public static DataParser create(java.io.InputStream in, java.lang.String filename, long fileSize)
in
- The input stream the parser should read fromfilename
- Optional, the name of the file the input stream
is reading frompublic static DataParser create(File file)
file
- The file in the BASE file systempublic static DataParser create(java.io.File file) throws java.io.IOException
file
- The file in the local file systemjava.io.IOException
public java.lang.String decodeValue(java.lang.String value)
TabCrLfEncoderDecoder
.value
- The encoded valuepublic java.lang.String getFilename()
getFilename
in interface BfsParser
public void setFilename(java.lang.String filename)
public long getFileSize()
getFileSize
in interface BfsParser
public void setFileSize(long fileSize)
public void close()
BfsParser
public void setInputStream(java.io.InputStream in)
in
- The stream to parse (in UTF-8 format)public void setNumColumns(int numColumns)
numColumns
- The number of columns or -1 to let the parser
automatically detect this on the first data linepublic void parse(EventHandler handler) throws java.io.IOException
handler
- An event handlerjava.io.IOException
- If there is an error reading the stream datajava.lang.NullPointerException
- If the stream or event handler is nullpublic void parseToBof() throws java.io.IOException
setInputStream(InputStream)
again to reset the parser.java.io.IOException
- If the header line is not found
or if there is any other IO errorjava.lang.IllegalStateException
- If the parsing has already startedjava.lang.NullPointerException
- If no input stream has been specifiedpublic java.lang.String[] nextData() throws java.io.IOException
java.io.IOException
- If there is an IO error or if the data is invalidjava.lang.IllegalStateException
- If not parseToBof()
has been calledpublic int getCurrentLine()
BfsParser
getCurrentLine
in interface BfsParser
public long getParsedBytes()
BfsParser
getParsedBytes
in interface BfsParser
protected FlatFileParser createFlatFileParser(java.io.InputStream in)
protected void handleData(EventHandler handler, java.lang.String[] data)
DATA_EVENT
to the event handlerhandler
- The event handler from the parse(EventHandler)
methoddata
- An array with the dataprotected void handleEndOfFile(EventHandler handler)
END_OF_FILE_EVENT
notification to the event handler.handler
- The event handler from the parse(EventHandler)
method