Package net.sf.basedb.util.bfs
Class AnnotationParser
- java.lang.Object
-
- net.sf.basedb.util.bfs.AnnotationParser
-
- All Implemented Interfaces:
AutoCloseable
,BfsParser
public class AnnotationParser extends Object implements BfsParser
Parser implementation that parses a BFS annotation files. Before parsing is started the stream to parse must be specified by callingsetInputStream(InputStream)
. The actual parsing can be done in two different ways:- "Manual" parsing, using the
parseToBof()
andnextData()
methods. - Event-based parsing using the
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.
- Version:
- 2.15
- Author:
- Nicklas
- Last modified
- $Date: 2010-04-20 10:02:16 +0200 (ti, 20 apr 2010) $
-
-
Field Summary
Fields Modifier and Type Field Description static EventType<String[]>
DATA_EVENT
Event thep that is issued for each data line.private EncoderDecoder
decoder
static EventType<Object>
END_OF_FILE_EVENT
Event type that is issued when the end-of-file had been reached.private FlatFileParser
ffp
private String
filename
private long
fileSize
static EventType<String[]>
HEADER_EVENT
Event type that is issued when the header line is found.private InputStream
in
private int
numColumns
private Set<Integer>
usedIds
-
Constructor Summary
Constructors Constructor Description AnnotationParser()
Create a new annotation parser.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method 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 local file system.static AnnotationParser
create(InputStream in, String filename, long size)
Utility method for creating an annotation parser when you have an input stream.static AnnotationParser
create(File file)
Utility method for creating an annotation parser for a file in the BASE file system.protected FlatFileParser
createFlatFileParser(InputStream in)
Create a new flat file parser that can parse BFS annotation files.String
decodeValue(String value)
Decode an encoded value.int
getCurrentLine()
Get the current line number.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, String[] data)
Handle the data event.protected void
handleEndOfFile(EventHandler handler)
Handle the end-of-file event.protected void
handleHeader(EventHandler handler, String[] headers)
Handle the header-found event.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.String[]
parseToBof()
Parse the header line of the annotations file.void
setFilename(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(InputStream in)
Set the input stream that should be parsed.
-
-
-
Field Detail
-
HEADER_EVENT
public static final EventType<String[]> HEADER_EVENT
Event type that is issued when the header line is found. The event data is a string array with the headers, including the ID header.
-
DATA_EVENT
public static final EventType<String[]> DATA_EVENT
Event thep that is issued for each data line. The event data is a string array with that data, including the ID column.
-
END_OF_FILE_EVENT
public static final EventType<Object> END_OF_FILE_EVENT
Event type that is issued when the end-of-file had been reached. No event data is submitted.
-
decoder
private final EncoderDecoder decoder
-
in
private InputStream in
-
filename
private String filename
-
fileSize
private long fileSize
-
ffp
private FlatFileParser ffp
-
numColumns
private int numColumns
-
-
Method Detail
-
create
public static AnnotationParser create(InputStream in, String filename, long size)
Utility method for creating an annotation parser when you have an input stream.- Parameters:
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 known
-
create
public static AnnotationParser create(File file)
Utility method for creating an annotation parser for a file in the BASE file system.- Parameters:
file
- The file in the BASE file system
-
create
public static AnnotationParser create(File file) throws IOException
Utility method for creating an annotation parser for a file in the local file system.- Parameters:
file
- The file in the local file system- Throws:
IOException
-
decodeValue
public String decodeValue(String value)
Decode an encoded value. Values are encoded/decoded with aTabCrLfEncoderDecoder
.- Parameters:
value
- The encoded value- Returns:
- The decoded value
-
getFilename
public String getFilename()
Get the file name that this parser is reading from.- Specified by:
getFilename
in interfaceBfsParser
- Returns:
- The file name or null if not known
-
setFilename
public void setFilename(String filename)
Set the file name that this parser is reading from.
-
getFileSize
public long getFileSize()
Get the size in bytes of the file that this parser is reading from.- Specified by:
getFileSize
in interfaceBfsParser
- Returns:
- The size or -1 if not known
-
setFileSize
public void setFileSize(long fileSize)
Set the size of the file or -1 if not known.
-
close
public void close()
Description copied from interface:BfsParser
Close the parser and relase underlying system resources that are associated with it.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceBfsParser
-
setInputStream
public void setInputStream(InputStream in)
Set the input stream that should be parsed. This will also reset the parser. All information from a previously parsed file is lost.- Parameters:
in
- The stream to parse (in UTF-8 format)
-
parse
public void parse(EventHandler handler) throws IOException
Parse the input stream and notify the specified event handler with events. The event handler is responsible for keeping track of and storing the data of interest.- Parameters:
handler
- An event handler- Throws:
IOException
- If there is an error reading the stream dataNullPointerException
- If the stream or event handler is null
-
parseToBof
public String[] parseToBof() throws IOException
Parse the header line of the annotations file. This method can only be called once on a given input stream. To parse a second file, callsetInputStream(InputStream)
again to reset the parser.- Returns:
- An array with the column headers
- Throws:
IOException
- If the header line is not found or if there is any other IO errorIllegalStateException
- If the parsing has already startedNullPointerException
- If no input stream has been specified
-
nextData
public String[] nextData() throws IOException
Get the next data line in the annotations file.- Returns:
- An array with the data, or null if no more data is found
- Throws:
IOException
- If there is an IO error or if the data is invalidIllegalStateException
- If notparseToBof()
has been called
-
getCurrentLine
public int getCurrentLine()
Description copied from interface:BfsParser
Get the current line number.- Specified by:
getCurrentLine
in interfaceBfsParser
- Returns:
- The line number, starting at 1 for the first line in the file, or -1 if not known
-
getParsedBytes
public long getParsedBytes()
Description copied from interface:BfsParser
Get the number of parsed bytes so far.- Specified by:
getParsedBytes
in interfaceBfsParser
- Returns:
- The number of bytes that has been parsed, or -1 if not known
-
createFlatFileParser
protected FlatFileParser createFlatFileParser(InputStream in)
Create a new flat file parser that can parse BFS annotation files. This method should set all regular expressions that are needed to parse the stream (assumed to be UTF-8).
-
handleHeader
protected void handleHeader(EventHandler handler, String[] headers)
Handle the header-found event. The default implemention sends aHEADER_EVENT
to the event handler- Parameters:
handler
- The event handler from theparse(EventHandler)
methodheaders
- An array with the column headers
-
handleData
protected void handleData(EventHandler handler, String[] data)
Handle the data event. The default implemention sends aDATA_EVENT
to the event handler- Parameters:
handler
- The event handler from theparse(EventHandler)
methoddata
- An array with the data
-
handleEndOfFile
protected void handleEndOfFile(EventHandler handler)
Handle the end-of-file event. The default implementation sends anEND_OF_FILE_EVENT
notification to the event handler.- Parameters:
handler
- The event handler from theparse(EventHandler)
method
-
-