public class AnnotationParser 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:
HEADER_EVENT
: When the header-line (should be the first line)
has been found.
DATA_EVENT
: For each data line that is parsed.
END_OF_FILE_EVENT
: When the end of the file has been reached.
AnnotationModel
class implements a simple event handler that
collects the information from the annotations file and provides methods
for accessing it.
This class may be subclassed to provide customized behaviour.
Modifier and Type | Field and Description |
---|---|
static EventType<java.lang.String[]> |
DATA_EVENT
Event thep 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 |
static EventType<java.lang.String[]> |
HEADER_EVENT
Event type that is issued when the header line is found.
|
private java.io.InputStream |
in |
private int |
numColumns |
private java.util.Set<java.lang.Integer> |
usedIds |
Constructor and Description |
---|
AnnotationParser()
Create a new annotation parser.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Close the parser and relase underlying system resources that
are associated with it.
|
static AnnotationParser |
create(File file)
Utility method for creating an annotation parser for a file in the BASE
file system.
|
static AnnotationParser |
create(java.io.File file)
Utility method for creating an annotation parser for a file in the local
file system.
|
static AnnotationParser |
create(java.io.InputStream in,
java.lang.String filename,
long size)
Utility method for creating an annotation 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.
|
protected void |
handleHeader(EventHandler handler,
java.lang.String[] headers)
Handle the header-found event.
|
java.lang.String[] |
nextData()
Get the next data line in the annotations file.
|
void |
parse(EventHandler handler)
Parse the input stream and notify the specified event handler with events.
|
java.lang.String[] |
parseToBof()
Parse the header line of the annotations 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.
|
public static final EventType<java.lang.String[]> HEADER_EVENT
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
private java.util.Set<java.lang.Integer> usedIds
public static AnnotationParser create(java.io.InputStream in, java.lang.String filename, long size)
in
- The input stream the parser should read fromfilename
- Optional, the name of the file the input stream
is reading fromsize
- The size of the file in bytes, or -1 if not
knownpublic static AnnotationParser create(File file)
file
- The file in the BASE file systempublic static AnnotationParser 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 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 java.lang.String[] 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 handleHeader(EventHandler handler, java.lang.String[] headers)
HEADER_EVENT
to the event handlerhandler
- The event handler from the parse(EventHandler)
methodheaders
- An array with the column headersprotected 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