Class IniFile

java.lang.Object
  extended by IniFile

public class IniFile
extends java.lang.Object

A class for handling Windows-style INI files. The file format is as follows:

[subject]
anything beginning with [ and ending with ] is a subject
;comment
anything beginning with a ; is a comment
variable=value
anything of the format string=string is an assignment
comment
anything that doesn't match any of the above is a comment

Author:
Steve DeGroof, degroof@mindspring.com, http://www.mindspring.com/~degroof

Field Summary
protected  java.lang.String fileName
          Name of the file
protected  java.util.Vector lines
          Actual text lines of the file stored in a vector.
protected  boolean saveOnChange
          If true, INI file will be saved every time a value is changed.
protected  java.util.Vector subjects
          A vector of all subjects
protected  java.util.Vector values
          A vector of variable value vectors grouped by subject
protected  java.util.Vector variables
          A vector of variable name vectors grouped by subject
 
Constructor Summary
IniFile(java.lang.String name)
          Creates an INI file object using the specified name If the named file doesn't exist, create one
IniFile(java.lang.String name, boolean save)
          Creates an INI file object using the specified name If the named file doesn't exist, create one
 
Method Summary
protected  boolean addAssignment(java.lang.String subject, java.lang.String assignment)
          Adds and assignment (i.e.
protected  void addSubjectLine(java.lang.String subject)
          add a subject line to the end of the lines vector
protected  boolean addValue(java.lang.String subject, java.lang.String variable, java.lang.String value, boolean addToLines)
          Sets a specific subject/variable combination the given value.
protected  boolean createFile()
          Create a new INI file.
 void deleteSubject(java.lang.String subject)
          delete a subject and all its variables
 void deleteValue(java.lang.String subject, java.lang.String variable)
          delete variable within a subject
protected  int endOfSubject(int start)
          find the line number which is 1 past the last assignment in a subject starting at a given line
protected  void finalize()
          clean up
protected  int findAssignmentBetween(java.lang.String variable, int start, int end)
          find the line containing a variable within a range of lines
protected  int findAssignmentLine(java.lang.String subject, java.lang.String variable)
          find the line containing a variable within a subject
protected  int findSubjectLine(java.lang.String subject)
          find a subject line within the lines vector
 java.util.Vector getLines()
          get a copy of the lines vector
 java.lang.String[] getSubjects()
          get an array containing all subjects
 java.lang.String getValue(java.lang.String subject, java.lang.String variable)
          get the value of a variable within a subject
 java.lang.String[] getVariables(java.lang.String subject)
          get a vector containing all variables in a subject
protected  boolean isanAssignment(java.lang.String line)
          does the line represent an assignment?
protected  boolean isaSubject(java.lang.String line)
          does the line represent a subject?
 void loadFile()
          Loads and parses the INI file.
static void main(java.lang.String[] args)
           
protected  void parseLines()
          Reads lines, filling in subjects, variables and values.
 void saveFile()
          save the lines vector back to the INI file
protected  void setLine(java.lang.String subject, java.lang.String variable, java.lang.String value)
          set a line in the lines vector
 boolean setValue(java.lang.String subject, java.lang.String variable, java.lang.String value)
          Sets a specific subject/variable combination the given value.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

lines

protected java.util.Vector lines
Actual text lines of the file stored in a vector.


subjects

protected java.util.Vector subjects
A vector of all subjects


variables

protected java.util.Vector variables
A vector of variable name vectors grouped by subject


values

protected java.util.Vector values
A vector of variable value vectors grouped by subject


fileName

protected java.lang.String fileName
Name of the file


saveOnChange

protected boolean saveOnChange
If true, INI file will be saved every time a value is changed. Defaults to false

Constructor Detail

IniFile

public IniFile(java.lang.String name)
Creates an INI file object using the specified name If the named file doesn't exist, create one

Parameters:
name - the name of the file

IniFile

public IniFile(java.lang.String name,
               boolean save)
Creates an INI file object using the specified name If the named file doesn't exist, create one

Parameters:
name - the name of the file
saveOnSet - save file whenever a value is set
Method Detail

loadFile

public void loadFile()
Loads and parses the INI file. Can be used to reload from file.


createFile

protected boolean createFile()
Create a new INI file.


parseLines

protected void parseLines()
Reads lines, filling in subjects, variables and values.


addAssignment

protected boolean addAssignment(java.lang.String subject,
                                java.lang.String assignment)
Adds and assignment (i.e. "variable=value") to a subject.


setValue

public boolean setValue(java.lang.String subject,
                        java.lang.String variable,
                        java.lang.String value)
Sets a specific subject/variable combination the given value. If the subject doesn't exist, create it. If the variable doesn't exist, create it. If saveOnChange is true, save the file;

Parameters:
subject - the subject heading (e.g. "Widget Settings")
variable - the variable name (e.g. "Color")
value - the value of the variable (e.g. "green")
Returns:
true if successful

addValue

protected boolean addValue(java.lang.String subject,
                           java.lang.String variable,
                           java.lang.String value,
                           boolean addToLines)
Sets a specific subject/variable combination the given value. If the subject doesn't exist, create it. If the variable doesn't exist, create it.

Parameters:
subject - the subject heading (e.g. "Widget Settings")
variable - the variable name (e.g. "Color")
value - the value of the variable (e.g. "green")
addToLines - add the information to the lines vector
Returns:
true if successful

isaSubject

protected boolean isaSubject(java.lang.String line)
does the line represent a subject?

Parameters:
line - a string representing a line from an INI file
Returns:
true if line is a subject

setLine

protected void setLine(java.lang.String subject,
                       java.lang.String variable,
                       java.lang.String value)
set a line in the lines vector

Parameters:
subject - the subject heading (e.g. "Widget Settings")
variable - the variable name (e.g. "Color")
value - the value of the variable (e.g. "green")

findAssignmentLine

protected int findAssignmentLine(java.lang.String subject,
                                 java.lang.String variable)
find the line containing a variable within a subject

Parameters:
subject - the subject heading (e.g. "Widget Settings")
variable - the variable name (e.g. "Color")
Returns:
the line number of the assignment, -1 if not found

findAssignmentBetween

protected int findAssignmentBetween(java.lang.String variable,
                                    int start,
                                    int end)
find the line containing a variable within a range of lines

Parameters:
variable - the variable name (e.g. "Color")
start - the start of the range (inclusive)
end - the end of the range (exclusive)
Returns:
the line number of the assignment, -1 if not found

addSubjectLine

protected void addSubjectLine(java.lang.String subject)
add a subject line to the end of the lines vector

Parameters:
subject - the subject heading (e.g. "Widget Settings")

findSubjectLine

protected int findSubjectLine(java.lang.String subject)
find a subject line within the lines vector

Parameters:
subject - the subject heading (e.g. "Widget Settings")
Returns:
the line number of the subject, -1 if not found

endOfSubject

protected int endOfSubject(int start)
find the line number which is 1 past the last assignment in a subject starting at a given line

Parameters:
start - the line number at which to start looking
Returns:
the line number of the last assignment + 1

isanAssignment

protected boolean isanAssignment(java.lang.String line)
does the line represent an assignment?

Parameters:
line - a string representing a line from an INI file
Returns:
true if line is an assignment

getLines

public java.util.Vector getLines()
get a copy of the lines vector


getVariables

public java.lang.String[] getVariables(java.lang.String subject)
get a vector containing all variables in a subject

Parameters:
subject - the subject heading (e.g. "Widget Settings")
Returns:
a list of variables, empty vector if subject not found

getSubjects

public java.lang.String[] getSubjects()
get an array containing all subjects

Returns:
a list of subjects

getValue

public java.lang.String getValue(java.lang.String subject,
                                 java.lang.String variable)
get the value of a variable within a subject

Parameters:
subject - the subject heading (e.g. "Widget Settings")
variable - the variable name (e.g. "Color")
Returns:
the value of the variable (e.g. "green"), empty string if not found

deleteValue

public void deleteValue(java.lang.String subject,
                        java.lang.String variable)
delete variable within a subject

Parameters:
subject - the subject heading (e.g. "Widget Settings")
variable - the variable name (e.g. "Color")

deleteSubject

public void deleteSubject(java.lang.String subject)
delete a subject and all its variables

Parameters:
subject - the subject heading (e.g. "Widget Settings")

saveFile

public void saveFile()
save the lines vector back to the INI file


finalize

protected void finalize()
clean up

Overrides:
finalize in class java.lang.Object

main

public static void main(java.lang.String[] args)