databionics.io
Class BaseFile

java.lang.Object
  extended by databionics.io.BaseFile
Direct Known Subclasses:
Array2DFile, ListFile, ModFile, PngFile, TxtFile

public abstract class BaseFile
extends java.lang.Object

Base class for all filetypes. Parsing of the lines when loading and construction of the lines when saving is delegated to subclasses. Headers starting with % and comments starting with # are handled here, globally. Many methods are implemented empty instead of declared abstract so they don't have to be implemented by subclasses if they are not needed.


Field Summary
protected  boolean allowEmptyLines
          Flag whether to allow empty lines, usually false, except rules files
protected  StringList comment
          Comment of file.
protected  java.lang.String commentPrefix
          Comment prefix.
protected  java.lang.String delimiter
          Default saving delimiter character.
protected  java.lang.String delimiters
          Default parsing delimiter characters.
protected  java.io.File file
          Path of file.
protected  StringList header
          Header of file.
protected  java.lang.String headerPrefix
          Header prefix.
protected  java.util.Locale loc
          Current locale.
protected static org.apache.log4j.Logger log
          Log4j logging.
protected  java.text.NumberFormat nf
          Current number format.
 
Constructor Summary
BaseFile()
          Standard constructor
BaseFile(java.lang.String filename)
          Constructor with a filename
 
Method Summary
protected  void buildHeader()
          Generate a fresh header based on the data structure for saving.
protected  java.lang.String buildLine(int row)
          Construct a line in subclasses for saving the data to a file.
protected  void checkHeader()
          Check consistency of header information with data structure in subclasses.
protected  void checkHeaderAgainstSize(int row)
          Check consistency of header line with size information.
 StringList getComment()
          Get the comment of file.
 java.io.File getFile()
          Get the file.
 java.lang.String getFilename()
          Get the name of file.
 StringList getHeader()
          Get the header of file.
 java.util.Locale getLocale()
          Get the current locale.
 java.text.NumberFormat getNumberFormat()
          Get the current number format.
 int getSize()
          Get the number of lines in file.
protected  void init(int rows, int cols)
          Init data structures in subclasses given the number of lines in the file and the number of tokens in the first line after header and comments.
protected  boolean isHeader(java.lang.String line)
          Determine if a line still contains header information or whether the data lines started
protected static boolean isNaN(java.lang.String s)
          Check if a string represents a NaN.
protected  boolean isNumber(java.lang.String s)
          Check if a string represents a number
 void load()
          Load data.
 void load(java.lang.String filename)
          Load data from this file
protected static void parseError(int row, int col, java.lang.String msg)
          Report a parsing error
protected  void parseHeader()
          Parse the header in subclasses.
protected  void parseLine(int row, java.lang.String line)
          Parse a line in subclasses to fill the data structures with values.
 void save()
          Save the data to a file.
 void save(java.lang.String filename)
          Save data to this file.
 void setComment(StringList comment)
          Change the comment of file.
 void setFilename(java.lang.String filename)
          Change the name of the file.
 void setLocale(java.util.Locale loc)
          Change the current locale.
protected static void writeStringList(java.io.BufferedWriter w, StringList l)
          Write a StringList to a writer.
protected static void writeStringList(java.io.BufferedWriter w, StringList l, java.lang.String prefix)
          Write a StringList to a writer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected static org.apache.log4j.Logger log
Log4j logging.


comment

protected StringList comment
Comment of file.


header

protected StringList header
Header of file.


file

protected java.io.File file
Path of file.


loc

protected java.util.Locale loc
Current locale.


nf

protected java.text.NumberFormat nf
Current number format.


commentPrefix

protected java.lang.String commentPrefix
Comment prefix.


headerPrefix

protected java.lang.String headerPrefix
Header prefix.


delimiters

protected java.lang.String delimiters
Default parsing delimiter characters.


delimiter

protected java.lang.String delimiter
Default saving delimiter character.


allowEmptyLines

protected boolean allowEmptyLines
Flag whether to allow empty lines, usually false, except rules files

Constructor Detail

BaseFile

public BaseFile()
Standard constructor


BaseFile

public BaseFile(java.lang.String filename)
Constructor with a filename

Parameters:
filename - Name of the file
Method Detail

isNaN

protected static boolean isNaN(java.lang.String s)
Check if a string represents a NaN.

Parameters:
s - String to check

isNumber

protected boolean isNumber(java.lang.String s)
Check if a string represents a number

Parameters:
s - String to check

writeStringList

protected static void writeStringList(java.io.BufferedWriter w,
                                      StringList l,
                                      java.lang.String prefix)
                               throws java.io.IOException
Write a StringList to a writer.

Parameters:
w - Writer to save data to
l - List of strings to save
prefix - Prefix to add in front of each line
Throws:
java.io.IOException

parseError

protected static void parseError(int row,
                                 int col,
                                 java.lang.String msg)
Report a parsing error

Parameters:
row - line number
coll - token number
msg - error message

writeStringList

protected static void writeStringList(java.io.BufferedWriter w,
                                      StringList l)
                               throws java.io.IOException
Write a StringList to a writer.

Parameters:
w - Writer to save data to
l - List of strings to save
Throws:
java.io.IOException

isHeader

protected boolean isHeader(java.lang.String line)
Determine if a line still contains header information or whether the data lines started

Parameters:
line - Line to check

parseHeader

protected void parseHeader()
Parse the header in subclasses. Some filetypes depend on header information (e.g. *.net) to initialize thedata structure, but this should be avoided whenever possible. Called before init/2.


init

protected void init(int rows,
                    int cols)
Init data structures in subclasses given the number of lines in the file and the number of tokens in the first line after header and comments.

Parameters:
rows - Number of lines in file.
cols - Number of tokens in first data line.

parseLine

protected void parseLine(int row,
                         java.lang.String line)
Parse a line in subclasses to fill the data structures with values.

Parameters:
row - Number of current line.
line - Current line.

checkHeaderAgainstSize

protected void checkHeaderAgainstSize(int row)
Check consistency of header line with size information.

Parameters:
row - Row of header where size is stored (starting with 0)

checkHeader

protected void checkHeader()
Check consistency of header information with data structure in subclasses. Called after init/2 and parsing. Only issue warnings when something is wrong.


load

public void load()
          throws java.io.IOException
Load data. Parsing of lines and header is delegated to subclasses.

Throws:
java.io.IOException

load

public void load(java.lang.String filename)
          throws java.io.IOException
Load data from this file

Throws:
java.io.IOException

buildHeader

protected void buildHeader()
Generate a fresh header based on the data structure for saving.


buildLine

protected java.lang.String buildLine(int row)
Construct a line in subclasses for saving the data to a file.

Parameters:
row - Number of current line.
Returns:
Line with that number.

save

public void save()
          throws java.io.IOException
Save the data to a file. Construction of a line is delegated to subclasses.

Throws:
java.io.IOException

save

public void save(java.lang.String filename)
          throws java.io.IOException
Save data to this file.

Throws:
java.io.IOException

getSize

public int getSize()
Get the number of lines in file.

Returns:
Number of lines in file.

getComment

public StringList getComment()
Get the comment of file.


setComment

public void setComment(StringList comment)
Change the comment of file.


getFilename

public java.lang.String getFilename()
Get the name of file.


setFilename

public void setFilename(java.lang.String filename)
Change the name of the file.


getFile

public java.io.File getFile()
Get the file.


getHeader

public StringList getHeader()
Get the header of file.


getLocale

public java.util.Locale getLocale()
Get the current locale.


setLocale

public void setLocale(java.util.Locale loc)
Change the current locale.


getNumberFormat

public java.text.NumberFormat getNumberFormat()
Get the current number format.



Copyright © 2005-2006 Databionics Research Group. All Rights Reserved.