Class DataParser

All Implemented Interfaces:
AutoCloseable, BfsParser

public class DataParser
extends Object
implements BfsParser
Parser implementation that parses BFS data files. Before parsing is started the stream to parse must be specified by calling setInputStream(InputStream). The actual parsing can be done in two different ways:
  • "Manual" parsing, using the parseToBof() and nextData() methods.
  • Event-based parsing using the parse(EventHandler) method. This parser issues the following events: The 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.

Last modified
$Date: 2010-04-20 10:02:16 +0200 (ti, 20 apr 2010) $
  • Field Details


      public static final EventType<String[]> DATA_EVENT
      Event type that is issued for each data line. The event data is a string array with that data.

      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
  • Constructor Details

    • DataParser

      public DataParser()
      Create a new data parser.
  • Method Details

    • create

      public static DataParser create​(InputStream in, String filename, long fileSize)
      Utility method for creating a data parser when you have an input stream.
      in - The input stream the parser should read from
      filename - Optional, the name of the file the input stream is reading from
    • create

      public static DataParser create​(File file)
      Utility method for creating a data parser for a file in the BASE file system.
      file - The file in the BASE file system
    • create

      public static DataParser create​(File file) throws IOException
      Utility method for creating a data parser for a file in the local file system.
      file - The file in the local file system
    • decodeValue

      public String decodeValue​(String value)
      Decode an encoded value. Values are encoded/decoded with a TabCrLfEncoderDecoder.
      value - The encoded value
      The decoded value
    • getFilename

      public String getFilename()
      Get the file name that this parser is reading from.
      Specified by:
      getFilename in interface BfsParser
      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 interface BfsParser
      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 interface AutoCloseable
      Specified by:
      close in interface BfsParser
    • 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.
      in - The stream to parse (in UTF-8 format)
    • setNumColumns

      public void setNumColumns​(int numColumns)
      Set the number of data columns that are expected to be found in this file.
      numColumns - The number of columns or -1 to let the parser automatically detect this on the first data line
    • 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.
      handler - An event handler
      IOException - If there is an error reading the stream data
      NullPointerException - If the stream or event handler is null
    • parseToBof

      public void parseToBof() throws IOException
      Initialize parsing of the data file. This method can only be called once on a given input stream. To parse a second file, call setInputStream(InputStream) again to reset the parser.
      IOException - If the header line is not found or if there is any other IO error
      IllegalStateException - If the parsing has already started
      NullPointerException - If no input stream has been specified
    • nextData

      public String[] nextData() throws IOException
      Get the next data line in the file.
      An array with the data, or null if no more data is found
      IOException - If there is an IO error or if the data is invalid
      IllegalStateException - If not parseToBof() has been called
    • getCurrentLine

      public int getCurrentLine()
      Description copied from interface: BfsParser
      Get the current line number.
      Specified by:
      getCurrentLine in interface BfsParser
      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 interface BfsParser
      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).
    • handleData

      protected void handleData​(EventHandler handler, String[] data)
      Handle the data event. The default implemention sends a DATA_EVENT to the event handler
      handler - The event handler from the parse(EventHandler) method
      data - An array with the data
    • handleEndOfFile

      protected void handleEndOfFile​(EventHandler handler)
      Handle the end-of-file event. The default implementation sends an END_OF_FILE_EVENT notification to the event handler.
      handler - The event handler from the parse(EventHandler) method