|
2.17.2: 2011-06-17 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object net.sf.basedb.util.bfs.MetadataParser
public class MetadataParser
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:
parseToBof()
, nextSection()
and nextEntry()
methods.
parse(EventHandler)
method.
This parser issues the following events:
BOF_EVENT
: When the beginning-of-file marker is found.
SECTION_EVENT
: When the start of a new section is found.
ENTRY_EVENT
: For each entry in a section.
SECTION_END_EVENT
: After the last entry in a section.
END_OF_FILE_EVENT
: When the end of the file has been reached.
MetadataModel
class implements a simple event handler that
collects the information from the metadata file and provides methods
for accessing it.
This class may be subclassed to provide customized behaviour.
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 |
---|
public static final EventType<String> BOF_EVENT
public static final EventType<String> SECTION_EVENT
public static final EventType<MetadataModel.SectionEntry> ENTRY_EVENT
MetadataModel.SectionEntry
object with the decoded key and values.
public static final EventType<String> SECTION_END_EVENT
public static final EventType<Object> END_OF_FILE_EVENT
private final EncoderDecoder decoder
private InputStream in
private String filename
private long fileSize
private FlatFileParser ffp
private List<MetadataModel.SectionEntry> sectionEntries
Constructor Detail |
---|
public MetadataParser()
Method Detail |
---|
public static MetadataParser create(InputStream in, 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 MetadataParser create(File file)
file
- The file in the BASE file systempublic static MetadataParser create(File file) throws IOException
file
- The file in the local file system
IOException
public String decodeValue(String value)
TabCrLfEncoderDecoder
.
value
- The encoded value
public String getFilename()
getFilename
in interface BfsParser
public void setFilename(String filename)
public long getFileSize()
getFileSize
in interface BfsParser
public void setFileSize(long fileSize)
public void close()
BfsParser
close
in interface BfsParser
public void setInputStream(InputStream in)
in
- The stream to parse (in UTF-8 format)public void parse(EventHandler handler) throws IOException
handler
- An event handler
IOException
- If there is an error reading the stream data
NullPointerException
- If the stream or event handler is nullpublic String parseToBof() throws IOException
setInputStream(InputStream)
again to reset the parser.
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 specifiedpublic String nextSection() throws IOException
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.public MetadataModel.SectionEntry nextEntry() throws IOException
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.public int getCurrentLine()
BfsParser
getCurrentLine
in interface BfsParser
public long getParsedBytes()
BfsParser
getParsedBytes
in interface BfsParser
protected FlatFileParser createFlatFileParser(InputStream in)
protected void handleBof(EventHandler handler, String subtype)
BOF_EVENT
notification to the
event handler.
handler
- The event handler from the parse(EventHandler)
methodsubtype
- The decoded subtype or null if no subtype was specified in the fileprotected void handleSection(EventHandler handler, String name)
SECTION_EVENT
notification to the event handler.
handler
- The event handler from the parse(EventHandler)
methodname
- The decoded name of the sectionprotected void handleEntry(EventHandler handler, MetadataModel.SectionEntry entry)
ENTRY_EVENT
notification to the event handler.
handler
- The event handler from the parse(EventHandler)
methodentry
- Key and values from the section entryprotected void handleSectionEnd(EventHandler handler, String name)
SECTION_END_EVENT
notification to the event handler.
handler
- The event handler from the parse(EventHandler)
methodname
- The decoded name of the sectionprotected void handleEndOfFile(EventHandler handler)
END_OF_FILE_EVENT
notification to the event handler.
handler
- The event handler from the parse(EventHandler)
method
|
2.17.2: 2011-06-17 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |