2.17.2: 2011-06-17

net.sf.basedb.util.bfs
Class MetadataParser

java.lang.Object
  extended by net.sf.basedb.util.bfs.MetadataParser
All Implemented Interfaces:
BfsParser

public class MetadataParser
extends Object
implements BfsParser

Parser implementation that parses a BFS metadata file. 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:

This class may be subclassed to provide customized behaviour.

Version:
2.15
Author:
Nicklas
Last modified
$Date: 2010-04-20 10:02:16 +0200 (Tue, 20 Apr 2010) $

Field Summary
static EventType<String> BOF_EVENT
          Event type that is issued when the beginning-of-file marker is found.
private  EncoderDecoder decoder
           
static EventType<Object> END_OF_FILE_EVENT
          Event type that is issued when the end-of-file had been reached.
static EventType<MetadataModel.SectionEntry> ENTRY_EVENT
          Event type that is issued for each entry in a section in the order they are present in the file.
private  FlatFileParser ffp
           
private  String filename
           
private  long fileSize
           
private  InputStream in
           
static EventType<String> SECTION_END_EVENT
          Event type that is issued at the end of each section in the file.
static EventType<String> SECTION_EVENT
          Event type that is issued for each new section that is found in the file.
private  List<MetadataModel.SectionEntry> sectionEntries
           
 
Constructor Summary
MetadataParser()
          Create a new metadata parser.
 
Method Summary
 void close()
          Close the parser and relase underlying system resources that are associated with it.
static MetadataParser create(File file)
          Utility method for creating a metadata parser for a file in the BASE file system.
static MetadataParser create(File file)
          Utility method for creating a metadata parser for a file in the local file system.
static MetadataParser create(InputStream in, String filename, long fileSize)
          Utility method for creating a metadata parser when you have an input stream.
protected  FlatFileParser createFlatFileParser(InputStream in)
          Create a new flat file parser that can parse BFS metadata files.
 String decodeValue(String value)
          Decode an encoded value.
 int getCurrentLine()
          Get the current line number.
 String getFilename()
          Get the file name that this writer is printing to.
 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 handleBof(EventHandler handler, String subtype)
          Handle the beginning-of-file marker event.
protected  void handleEndOfFile(EventHandler handler)
          Handle the end-of-file event.
protected  void handleEntry(EventHandler handler, MetadataModel.SectionEntry entry)
          Handle the section entry event.
protected  void handleSection(EventHandler handler, String name)
          Handle the new section event.
protected  void handleSectionEnd(EventHandler handler, String name)
          Handle the end-of-section event.
 MetadataModel.SectionEntry nextEntry()
          Get the next entry in the current section.
 String nextSection()
          Parse to the next section in the metadata file.
 void parse(EventHandler handler)
          Parse the input stream and notify the specified event handler with events.
 String parseToBof()
          Parse the start of the file until the beginning-of-file marker (BFSfile) is found.
 void setFilename(String filename)
          Set the file name that this writer is printing to.
 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BOF_EVENT

public static final EventType<String> BOF_EVENT
Event type that is issued when the beginning-of-file marker is found. The event data is a string with the BFS subtype, or null if no subtype was defined.


SECTION_EVENT

public static final EventType<String> SECTION_EVENT
Event type that is issued for each new section that is found in the file. The event data is a string with the name (decoded) of the section.


ENTRY_EVENT

public static final EventType<MetadataModel.SectionEntry> ENTRY_EVENT
Event type that is issued for each entry in a section in the order they are present in the file. The event data is a MetadataModel.SectionEntry object with the decoded key and values.


SECTION_END_EVENT

public static final EventType<String> SECTION_END_EVENT
Event type that is issued at the end of each section in the file. No event data is submitted.


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

sectionEntries

private List<MetadataModel.SectionEntry> sectionEntries
Constructor Detail

MetadataParser

public MetadataParser()
Create a new metadata parser.

Method Detail

create

public static MetadataParser create(InputStream in,
                                    String filename,
                                    long fileSize)
Utility method for creating a metadata parser when you have an input stream.

Parameters:
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 MetadataParser create(File file)
Utility method for creating a metadata parser for a file in the BASE file system.

Parameters:
file - The file in the BASE file system

create

public static MetadataParser create(File file)
                             throws IOException
Utility method for creating a metadata 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. Metadata values are encoded/decoded with a TabCrLfEncoderDecoder.

Parameters:
value - The encoded value
Returns:
The decoded value

getFilename

public String getFilename()
Get the file name that this writer is printing to.

Specified by:
getFilename in interface BfsParser
Returns:
The file name or null if not known

setFilename

public void setFilename(String filename)
Set the file name that this writer is printing to.


getFileSize

public long getFileSize()
Get the size in bytes of the file that this parser is reading from.

Specified by:
getFileSize in interface BfsParser
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 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.

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 data
NullPointerException - If the stream or event handler is null

parseToBof

public String parseToBof()
                  throws IOException
Parse the start of the file until the beginning-of-file marker (BFSfile) is found. 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.

Returns:
The subtype of the BFS or null if no subtype is found
Throws:
IOException - If the beginning-of-file marker 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

nextSection

public String nextSection()
                   throws IOException
Parse to the next section in the metadata file. This method can be called repeatedly until null is returned which indicates that the end of file has been reached.

Returns:
The name of the section, or null if no more sections could be found
Throws:
IOException - If there is an IO error during the parsing
IllegalStateException - If the parsing has not been started. parseToBof() must be called before this method is called.

nextEntry

public MetadataModel.SectionEntry nextEntry()
                                     throws IOException
Get the next entry in the current section. This method can be called repeatedly until null is returned which indicates that the end of the section has been reached.

Returns:
The key and values or the entry, or null if no more entries could be found
Throws:
IOException - If there is an IO error during the parsing
IllegalStateException - If the parsing has not been started. parseToBof() must be called before this method is called.

getCurrentLine

public int getCurrentLine()
Description copied from interface: BfsParser
Get the current line number.

Specified by:
getCurrentLine in interface BfsParser
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 interface BfsParser
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 metadata files. This method should set all regular expressions that are needed to parse the stream (assumed to be UTF-8).


handleBof

protected void handleBof(EventHandler handler,
                         String subtype)
Handle the beginning-of-file marker event. The default implementation sends a BOF_EVENT notification to the event handler.

Parameters:
handler - The event handler from the parse(EventHandler) method
subtype - The decoded subtype or null if no subtype was specified in the file

handleSection

protected void handleSection(EventHandler handler,
                             String name)
Handle the new section event. The default implementation sends a SECTION_EVENT notification to the event handler.

Parameters:
handler - The event handler from the parse(EventHandler) method
name - The decoded name of the section

handleEntry

protected void handleEntry(EventHandler handler,
                           MetadataModel.SectionEntry entry)
Handle the section entry event. The default implementation sends a ENTRY_EVENT notification to the event handler.

Parameters:
handler - The event handler from the parse(EventHandler) method
entry - Key and values from the section entry

handleSectionEnd

protected void handleSectionEnd(EventHandler handler,
                                String name)
Handle the end-of-section event. The default implementation sends a SECTION_END_EVENT notification to the event handler.

Parameters:
handler - The event handler from the parse(EventHandler) method
name - The decoded name of the section

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.

Parameters:
handler - The event handler from the parse(EventHandler) method

2.17.2: 2011-06-17