com.partnersoft.formats.text
Class LineTextDataRecordSource

java.lang.Object
  extended by com.partnersoft.io.AbstractIterableInput<Naming<java.lang.Object>>
      extended by com.partnersoft.io.DataRecordSource
          extended by com.partnersoft.formats.text.LineTextDataRecordSource
All Implemented Interfaces:
Coggable, IterableInput<Naming<java.lang.Object>>, java.lang.Iterable<Naming<java.lang.Object>>
Direct Known Subclasses:
CsvDataRecordSource

public abstract class LineTextDataRecordSource
extends DataRecordSource

An abstract DataRecordSource for data formatted as a text file with line feeds and/or carriage returns separating the data records. Examples include fixed-width text formats, comma-separated, tab-separated, etc.

Subclasses must provide the code for parsing field values from each line. This is done by implementing the parseLine(String) method.

This class takes care of opening and closing the file, tracking line numbers, etc.

Copyright 2006-2007 Partner Software, Inc.

Version:
$Id: LineTextDataRecordSource.java 2474 2010-03-13 14:28:43Z paul $
Author:
Paul Reavis

Nested Class Summary
 
Nested classes/interfaces inherited from interface com.partnersoft.io.IterableInput
IterableInput.Status
 
Field Summary
 java.lang.String currentLine
           
 int currentLineNumber
           
 
Fields inherited from class com.partnersoft.io.DataRecordSource
currentValues, fieldNames, verbose
 
Constructor Summary
LineTextDataRecordSource()
           
LineTextDataRecordSource(Cog state)
           
LineTextDataRecordSource(VfsFile file)
           
LineTextDataRecordSource(VfsFile file, java.util.List<java.lang.String> fieldNames)
           
LineTextDataRecordSource(VfsFile file, java.lang.String... fieldNames)
           
 
Method Summary
 void closeImp()
          Subclass implementation of guts of close() method.
 boolean fetchImp()
          Subclass implementation of guts of fetch() method.
 VfsFile getFile()
           
 void open()
          Initialize the input, opening the underlying file or other resource.
 void openImp()
          Subclass implementation of guts of open() method.
abstract  boolean parseLine(java.lang.String line)
          Subclasses must implement this.
protected  java.lang.String readLine()
          Reads a line and returns the result.
 
Methods inherited from class com.partnersoft.io.DataRecordSource
copy, fetch, getCurrentValues, getFetched, getFieldNames, isVerbose, setVerbose, toCog
 
Methods inherited from class com.partnersoft.io.AbstractIterableInput
close, getException, getStatus, isFetchValid, iterator
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

currentLineNumber

public transient int currentLineNumber

currentLine

public transient java.lang.String currentLine
Constructor Detail

LineTextDataRecordSource

public LineTextDataRecordSource()

LineTextDataRecordSource

public LineTextDataRecordSource(VfsFile file)

LineTextDataRecordSource

public LineTextDataRecordSource(VfsFile file,
                                java.util.List<java.lang.String> fieldNames)

LineTextDataRecordSource

public LineTextDataRecordSource(VfsFile file,
                                java.lang.String... fieldNames)

LineTextDataRecordSource

public LineTextDataRecordSource(Cog state)
Method Detail

open

public void open()
Description copied from interface: IterableInput
Initialize the input, opening the underlying file or other resource. Note that this does not start the actual fetch, and getFetched() will throw IllegalStateException until you do a fetch().

This method can only be called if the current status is CLOSED. Calling it in any other state will result in an IllegalStateException.

If the input is opened successfully, the status is changed to OPENED.

If an exception occurs during the open, the exception is made available via getException(), and the status is changed to END_OF_INPUT. This means close() should still be called.

Specified by:
open in interface IterableInput<Naming<java.lang.Object>>
Overrides:
open in class AbstractIterableInput<Naming<java.lang.Object>>

openImp

public void openImp()
             throws java.lang.Exception
Description copied from class: AbstractIterableInput
Subclass implementation of guts of open() method.

Subclasses must implement this to do the actual work required by open(). Feel free to throw exceptions; these are caught inside the open() itself and applied to the exception variable. Thus, you can focus on the actual task instead of paperwork.

Specified by:
openImp in class AbstractIterableInput<Naming<java.lang.Object>>
Throws:
java.lang.Exception

fetchImp

public boolean fetchImp()
                 throws java.lang.Exception
Description copied from class: AbstractIterableInput
Subclass implementation of guts of fetch() method.

Subclasses must implement this to do the actual work required by fetch(). Feel free to throw exceptions; these are caught inside the fetch() itself and applied to the exception variable. Thus, you can focus on the actual task instead of paperwork.

Specified by:
fetchImp in class AbstractIterableInput<Naming<java.lang.Object>>
Returns:
true if fetch successful
Throws:
java.lang.Exception

closeImp

public void closeImp()
              throws java.lang.Exception
Description copied from class: AbstractIterableInput
Subclass implementation of guts of close() method.

Subclasses must implement this to do the actual work required by close(). Feel free to throw exceptions; these are caught inside the close() itself and applied to the exception variable. Thus, you can focus on the actual task instead of paperwork.

Specified by:
closeImp in class AbstractIterableInput<Naming<java.lang.Object>>
Throws:
java.lang.Exception

parseLine

public abstract boolean parseLine(java.lang.String line)
                           throws java.lang.Exception
Subclasses must implement this. It should read the input string, representing a single line of input, set the currentValues array in DataRecordSource, and return true if successful.

Parameters:
line -
Returns:
Throws:
java.lang.Exception

getFile

public VfsFile getFile()

readLine

protected java.lang.String readLine()
                             throws java.lang.Exception
Reads a line and returns the result. Also increments currentLineNumber and sets currentLine to the same value as returned. This consumes the line; calling it multiple times gets you each line in the file in turn. Interleaving this with calls to an iterator will cause gaps. Use with caution.

Throws:
java.lang.Exception