wyvern.common.util
Class InitFile

java.lang.Object
  extended bywyvern.common.util.InitFile

public class InitFile
extends java.lang.Object

This class can read & write an INI-file, like java.util.Properties, but it preserves the structure of the file, including comments and whitespace. It's not as efficient as a Properties, so if you can bear to have your keys rearranged, that's what you should use.

Keys and values are currently case-sensitive.

Version:
1.0, Oct 18, 1998
Author:
Steve Yegge

Field Summary
protected  java.lang.String DEFAULT_CATEGORY
           
 
Constructor Summary
InitFile()
          Constructs a new InitFile with no file.
InitFile(java.lang.String filename)
          Constructs a new InitFile for the specified .ini file.
 
Method Summary
 java.util.Hashtable getProperties(java.lang.String cat)
          Returns a list of the properties under a given category.
 java.lang.String getProperty(java.lang.String cat, java.lang.String name)
          Returns a value given the category and key: alias for readValue().
 void load(java.io.BufferedReader in)
          Loads from the specified BufferedReader.
 void load(java.io.InputStream input)
          Loads from the specified input stream.
 void load(java.lang.String filename)
          Reads the file into a data structure.
static void main(java.lang.String[] args)
          Tests the functionality.
 boolean readBoolean(java.lang.String cat, java.lang.String key)
          Returns a key's value as a boolean.
 int readInteger(java.lang.String cat, java.lang.String key)
          Returns a key's value as an integer.
 java.lang.String readString(java.lang.String cat, java.lang.String key)
          Returns the String value for the passed key.
 java.lang.String readValue(java.lang.String cat, java.lang.String key)
          Returns the (String) value for the passed key.
 void removeValue(java.lang.String cat, java.lang.String key)
          Removes a value from the file
 void save(java.lang.String filename)
          Saves the current set of properties to the specified file.
 void writeBoolean(java.lang.String cat, java.lang.String key, boolean value)
          Writes a boolean value for a given key.
 void writeInteger(java.lang.String cat, java.lang.String key, int value)
          Writes an integer value for a given key.
 void writeString(java.lang.String cat, java.lang.String key, java.lang.String value)
          Writes a new value for the passed key.
 void writeValue(java.lang.String cat, java.lang.String key, java.lang.String value)
          Writes a new value for the passed key.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_CATEGORY

protected final java.lang.String DEFAULT_CATEGORY
See Also:
Constant Field Values
Constructor Detail

InitFile

public InitFile()
Constructs a new InitFile with no file.


InitFile

public InitFile(java.lang.String filename)
         throws java.io.IOException
Constructs a new InitFile for the specified .ini file.

Throws:
java.io.IOException - if the file can't be read
Method Detail

readValue

public java.lang.String readValue(java.lang.String cat,
                                  java.lang.String key)
Returns the (String) value for the passed key.

Parameters:
cat - the category, null if it's not in a category
key - the key to look for
Returns:
the value for the key, or null if not found. If there is no "=" on the line containing the key, the value is the empty string.

readString

public java.lang.String readString(java.lang.String cat,
                                   java.lang.String key)
Returns the String value for the passed key. Same as readValue().

Parameters:
cat - the category, null if it's not in a category
key - the key to look for
Returns:
the value for the key, or null if not found

readInteger

public int readInteger(java.lang.String cat,
                       java.lang.String key)
Returns a key's value as an integer.

Parameters:
cat - the category, null if it's not in a category
key - the key to look for
Returns:
the integer value for the key, or null if not found
Throws:
java.lang.NumberFormatException - if the value isn't an integer

readBoolean

public boolean readBoolean(java.lang.String cat,
                           java.lang.String key)
Returns a key's value as a boolean.

Parameters:
cat - the category, null if it's not in a category
key - the key to look for
Returns:
true if the key's String value is "true", else false.

writeValue

public void writeValue(java.lang.String cat,
                       java.lang.String key,
                       java.lang.String value)
Writes a new value for the passed key.

Parameters:
cat - the category, null if it's not in a category
key - the key to look for
value - the new value for the key

removeValue

public void removeValue(java.lang.String cat,
                        java.lang.String key)
Removes a value from the file

Parameters:
cat - the category, null if it's not in a category
key - the key

writeString

public void writeString(java.lang.String cat,
                        java.lang.String key,
                        java.lang.String value)
Writes a new value for the passed key.

Parameters:
cat - the category, null if it's not in a category
key - the key to look for
value - the new value for the key

writeInteger

public void writeInteger(java.lang.String cat,
                         java.lang.String key,
                         int value)
Writes an integer value for a given key.

Parameters:
cat - the category, null if it's not in a category
key - the key to look for
value - the int value to write

writeBoolean

public void writeBoolean(java.lang.String cat,
                         java.lang.String key,
                         boolean value)
Writes a boolean value for a given key.

Parameters:
cat - the category, null if it's not in a category
key - the key to look for
value - the boolean value to write

load

public void load(java.lang.String filename)
          throws java.io.IOException
Reads the file into a data structure. The file is expected to have zero or more categories demarcated by a single line of the form "[category]". All lines up to the next category are placed in a list, including blank lines.

If the first line of the file isn't a category, all lines up to the first category found are placed in a category called [@default] internally. "[@default]" is never written back out to the file.

Internally the InitFile keeps a table of categories. Under each category is a list of the lines in that category. New entries to the category are added to the end. The category names are kept in a sequential list so their ordering can be preserved when writing the file.

Parameters:
filename - the file to load
Throws:
java.io.IOException

load

public void load(java.io.InputStream input)
          throws java.io.IOException
Loads from the specified input stream.

Throws:
java.io.IOException

load

public void load(java.io.BufferedReader in)
          throws java.io.IOException
Loads from the specified BufferedReader.

Parameters:
in - input reader
Throws:
java.io.IOException

save

public void save(java.lang.String filename)
          throws java.io.IOException
Saves the current set of properties to the specified file.

Parameters:
filename - name to save to
Throws:
java.io.IOException

getProperty

public java.lang.String getProperty(java.lang.String cat,
                                    java.lang.String name)
Returns a value given the category and key: alias for readValue().


getProperties

public java.util.Hashtable getProperties(java.lang.String cat)
Returns a list of the properties under a given category.

Parameters:
cat - the initfile category

main

public static void main(java.lang.String[] args)
                 throws java.io.IOException
Tests the functionality.

Parameters:
args - an array whose first entry is the filename to test.
Throws:
java.io.IOException