Copyright (C) 2003,2004 dr. Cristiano Sadun

org.sadun.text.ffp
Class BaseListener

java.lang.Object
  extended byorg.sadun.text.ffp.BaseListener
All Implemented Interfaces:
FlatFileParser.Listener
Direct Known Subclasses:
DBInsertionListener, EchoListener

public abstract class BaseListener
extends java.lang.Object
implements FlatFileParser.Listener

A base implementation of FlatFileParser.Listener which activates only when on a specific line format name (or a specific regular expression match) is successfully parsed.

Multiple listeners can be registered at a FlatFileParser each reacting to a specific line format name.

When #matched(String, String[]) is executed, the method compares the received format name to the string or regular expression passed at construction and if they correspond, invokes #onFormatName(String, String[]).

The net result is that the reaction code does not need to perform checks on the format name, since it's invoked only if a match is verified. For example,

  FlatFileParser ffp = new FlatFileParser();
 
  ..set up two LineFormat objects, one named "header", the other "data"..
 
  ffp.addListener(new BaseListener("header") {
   protected void onFormatName(String formatName, String[] values) {
     System.out.println("HEADER line");
   }
  });
  ffp.addListener(new BaseListener("data") {
   protected void onFormatName(String formatName, String[] values) {
     System.out.println("DATA line");
   }
  });
 

Author:
Cristiano Sadun

Constructor Summary
protected BaseListener(java.lang.String formatName)
          Create a listener which matches exactly the given line format name.
protected BaseListener(java.lang.String formatName, boolean isRegExp)
          Create a listener which matches the given line format name or the given regular expression.
 
Method Summary
 void lineParsed(LineFormat format, int logicalLineCount, int physicalLineCount, java.lang.String[] values)
          This implementation checks wether the received format name matches the name or regular expression provided at construction, and invokes #onFormatName(String, String[]) if so.
protected abstract  void onFormatName(LineFormat format, int logicalLineCount, int physicalLineCount, java.lang.String[] values)
          Invoked when a line whose format name matches the name or regexp provided at construction is found.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BaseListener

protected BaseListener(java.lang.String formatName)
Create a listener which matches exactly the given line format name.

Parameters:
formatName - the name of the desired LineFormat

BaseListener

protected BaseListener(java.lang.String formatName,
                       boolean isRegExp)
Create a listener which matches the given line format name or the given regular expression.

Parameters:
formatName - the format name or regular expression
isRegExp - true if the given formatName is to be treated as a regular expression
Method Detail

lineParsed

public final void lineParsed(LineFormat format,
                             int logicalLineCount,
                             int physicalLineCount,
                             java.lang.String[] values)
                      throws AbortFFPException
This implementation checks wether the received format name matches the name or regular expression provided at construction, and invokes #onFormatName(String, String[]) if so.

Specified by:
lineParsed in interface FlatFileParser.Listener
Parameters:
format - the LineFormat object which has executed the parsing
values - the values resulting from the parsing
Throws:
AbortFFPException

onFormatName

protected abstract void onFormatName(LineFormat format,
                                     int logicalLineCount,
                                     int physicalLineCount,
                                     java.lang.String[] values)
                              throws AbortFFPException
Invoked when a line whose format name matches the name or regexp provided at construction is found.

Parameters:
values - the parsed values
Throws:
AbortFFPException

Copyright (C) 2003,2004 dr. Cristiano Sadun