|
Copyright (C) 2003,2004 dr. Cristiano Sadun | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.sadun.text.ffp.FlatFileParser
A parser for flat files.
This parser employs LineFormat
objects to specify the format of the flat file.
The FlatFileParser(LineFormat)
constructor is useful when the flat file is simple, each logical line
having identical format. Together with LineFormat.fromImage(String)
allows
to declare the parser and the format in one line of code:
FlatFileParser ffp = new FlatFileParser(LineFormat.fromImage("##### type1 @@@ @@@"));will parse flat files with a number in position 0-5, the constant type1 at position 5-10, and two alphanumeric fields of three characters each (see
LineFormat
for image syntax).
Alternatively, if a flat file contains lines with different formats, the corresponding
LineFormat
objects can be associated to a condition
by using #declare(Condition, LineFormat)
.
Parsing will occur by registering a FlatFileParser.Listener
(or associating a specific listener to each line format
by using addDispatch
and invoking one of the
parse()
methods:
ffp.addListener(new EchoListener()); ffp.parse(new File("myfile.txt"));
For each line, the parser will examine the conditions and use the line format which matches. Only one condition
is allowed to match for each line, and it's programmer responsability to ensure that the given
conditions do not overlap.
Nested Class Summary | |
static interface |
FlatFileParser.AdvancedListener
Classes implementing this extension of Listener will receive more events
on flat file parsing. |
static interface |
FlatFileParser.Condition
An object implementing this interface can decide whether or not the physical line currently available for reading match or not a certain condition, in the FlatFileParser.Condition#holds(int, int, LineReader) method.
|
static interface |
FlatFileParser.LineReader
Objects implementing this interface allow to access the flat file during parsing, from outside the parser. |
static interface |
FlatFileParser.Listener
Classes implementing this interface can be registered in a FlatFileParser
(using FlatFileParser#addListener(Listener) ) to react to successfully parsed
lines in the flat file.
|
static interface |
FlatFileParser.UnmatchedLineListener
Classes implementing this extension of Listener will receive notification
about lines where no condition matches on parsing, if the failOnNoMatchingConditions property is false. |
Field Summary | |
static boolean |
defaultFailOnLineParsingError
The default value used to intialize the failOnLineParsingError
property when a parser instance is constructed. |
static java.lang.String |
LOGGER_CHANNEL_NAME
The logger channel used by this package. |
Constructor Summary | |
FlatFileParser()
Create a parser with no associated line formats. |
|
FlatFileParser(LineFormat format)
Creates a parser which parses flat files whose all lines have the given format. |
Method Summary | |
void |
addDispatch(LineFormat format,
FlatFileParser.Listener l)
Associates the given LineFormat to the given listener, so that when a line is parsed by the given format, the event is sent to the given listener. |
void |
addListener(FlatFileParser.Listener l)
Add the given listener to the registered listeners set. |
void |
declare(FlatFileParser.Condition condition,
LineFormat format)
Declare that a certain LineFormat is to be used under the given condition. |
void |
declare(FlatFileParser.Condition condition,
java.lang.String formatImage)
Declare that a certain LineFormat , whose image is given, is to be used
under the given condition. |
void |
declare(LineFormat format)
Declare that the only line format recognized by the parser be the one given. |
void |
declare(java.lang.String formatImage)
Declare that the only line format recognized by the parser be the one whose image is given. |
protected java.util.Map |
getFieldsByCondition()
|
java.lang.String |
getLineSeparator()
Return the line separator string used by this parser. |
boolean |
isAutoTrimMode()
Return the current state of the auto trim mode. |
boolean |
isFailOnLineParsingError()
Return whether or not the parser should fail when a exception occurs parsing a line, or just emit a warning on the LOGGER_CHANNEL_NAME
channel. |
boolean |
isFailOnNoMatchingConditions()
Return whether the parser will fail or not if no conditions match a line. |
boolean |
isFastMatchMode()
Return the current state of the the fast match mode. |
void |
parse(java.io.File file)
Parse the text from in the given file. |
void |
parse(java.io.Reader sourceReader)
Parse the text from the given reader. |
void |
removeListener(FlatFileParser.Listener l)
Remove the given listener from the registered listeners set. |
void |
setAutoTrimMode(boolean autoTrimMode)
Set the auto trim mode. |
void |
setFailOnLineParsingError(boolean failOnLineParsingError)
Set whether or not the parser should fail when a exception occurs parsing a line, or just emit a warning on the LOGGER_CHANNEL_NAME
channel. |
void |
setFailOnNoMatchingConditions(boolean failOnNoMatchingConditions)
Set whether the parser will fail or not if no conditions match a line. |
void |
setFastMatchMode(boolean fastMatchMode)
Set the current state of the the fast match mode. |
void |
setLineSeparator(java.lang.String lineSeparator)
Set the line separator string used by this parser. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final java.lang.String LOGGER_CHANNEL_NAME
public static boolean defaultFailOnLineParsingError
failOnLineParsingError
property when a parser instance is constructed.
Constructor Detail |
public FlatFileParser()
Before running parse(Reader)
, at least one format must be added
by invoking declare()
.
public FlatFileParser(LineFormat format)
format
- Method Detail |
public void declare(FlatFileParser.Condition condition, LineFormat format)
LineFormat
is to be used under the given condition.
condition
- the FlatFileParser.Condition
condition that must hold.format
- the LineFormat
to use.public void declare(LineFormat format)
Invoking this method after creating a parser with new FlatFileParser()
is
equivalent to use the constructor FlatFileParser(LineFormat)
.
format
- the LineFormat
to use for each line in the flat file.public void declare(java.lang.String formatImage) throws ImageParseException
Invoking this method after creating a parser with new FlatFileParser()
is
equivalent to use the constructor FlatFileParser(LineFormat)
.
formatImage
- the LineFormat
's image to use for each line in the flat file.
ImageParseException
- if the given image is not correct{@link LineFormat#fromImage(String)}
public void declare(FlatFileParser.Condition condition, java.lang.String formatImage) throws ImageParseException
LineFormat
, whose image is given, is to be used
under the given condition.
condition
- the FlatFileParser.Condition
condition that must hold.formatImage
- the LineFormat
's image to use for each line in the flat file.
ImageParseException
- if the given image is not correct{@link LineFormat#fromImage(String)}
public void parse(java.io.File file) throws java.io.IOException, FFPParseException
Notification of successful parsed lines is sent to any registered FlatFileParser.Listener
(FlatFileParser#addListener(Listener)
is used for registration).
file
- the file to parse
java.io.IOException
- if an I/O exception occurs
FFPParseException
- if the text format cannot be parsed correctlypublic void parse(java.io.Reader sourceReader) throws java.io.IOException, FFPParseException
Notification of successful parsed lines is sent to any registered FlatFileParser.Listener
(FlatFileParser#addListener(Listener)
is used for registration).
sourceReader
- the reader to read the text from
java.io.IOException
- if an I/O exception occurs
FFPParseException
- if the text format cannot be parsed correctlypublic void addListener(FlatFileParser.Listener l)
See also #addDispatch(LineFormat, Listener)
.
l
- the listener to addpublic void addDispatch(LineFormat format, FlatFileParser.Listener l)
An internal object of DispatcherListener
class is used.
format
- the line format on which the dispatch is to be basedl
- the listener to associatepublic void removeListener(FlatFileParser.Listener l)
l
- the listener to removepublic java.lang.String getLineSeparator()
public void setLineSeparator(java.lang.String lineSeparator)
LineFormat
s currently defined.
lineSeparator
- the line separator string used by this parser.public boolean isFailOnLineParsingError()
LOGGER_CHANNEL_NAME
channel.
LOGGER_CHANNEL_NAME
channel.public void setFailOnLineParsingError(boolean failOnLineParsingError)
LOGGER_CHANNEL_NAME
channel.
failOnLineParsingError
- if true, the parse(Reader)
methods
will fail if there's a parsing problem on a line.public boolean isFastMatchMode()
LineFormat
associated of the last holding
FlatFileParser.Condition
if it still holds, avoiding to check all the conditions.
public void setFastMatchMode(boolean fastMatchMode)
LineFormat
associated of the last holding
FlatFileParser.Condition
if it still holds, avoiding to check all the conditions.
fastMatchMode
- if true, enables the fastmatch mode, else disables it.public boolean isAutoTrimMode()
public void setAutoTrimMode(boolean autoTrimMode)
autoTrimMode
- if true, enables the auto trim mode, else disables it.protected java.util.Map getFieldsByCondition()
public boolean isFailOnNoMatchingConditions()
public void setFailOnNoMatchingConditions(boolean failOnNoMatchingConditions)
failOnNoMatchingConditions
- true if the parser must fail if no conditions match a line, otherwise false.
|
Copyright (C) 2003,2004 dr. Cristiano Sadun | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |