webfunds.ricardian
Class IniFileReader

java.lang.Object
  |
  +--webfunds.ricardian.IniFileReader

public class IniFileReader
extends java.lang.Object

This class handles files in the windows ini format


Field Summary
protected static java.lang.String lastEoln
           
protected static java.lang.String lastErrors
           
protected static int numErrors
           
 
Constructor Summary
IniFileReader()
          Default constructor creates an empty IniFileReader
IniFileReader(byte[] data)
          read a ini formated file and put in the class
 
Method Summary
protected  void add(byte[] iniFileAsByteArray)
          Add a files worth to the hashtables.
 void addByteArray(byte[] data)
          Add the contents of an ini-file formated byte array.
 void addMultilineSectionItem(java.lang.String sectionName, java.lang.String item, java.lang.String value)
          This method can be used to add a item/value pair to a section using for mutliline
 void addSection(java.lang.String sectionName)
          This method add a section to the IniFileReader class
 void addSectionItem(java.lang.String sectionName, java.lang.String item, java.lang.String value)
          This method can be used to add a item/value pair to a section if the value is mutliline then you should use addMultilineSectionItem
 void changeSectionItemValue(java.lang.String sect, java.lang.String item, java.lang.String value)
          This method can be used to change a value pair of a item in a section
protected static void doLines(java.lang.String desc, java.lang.String test)
           
 void dump()
           
 java.util.Hashtable getHashOfSectionItems(java.lang.String section)
          This is used to get a Hashtable of the items in the specific section of the IniFileReader class
 java.util.Hashtable getIniFileHash()
           
static java.lang.String getLastErrors()
           
 int getNumberOfSectionItems(java.lang.String section)
          this is used for getting the amount of items in a section of a IniFileReader class
 int getNumberOfSections()
          this method returns the number of sections in the IniFileReader class
static int getNumErrors()
           
 java.lang.String[] getSectionItemArray(java.lang.String sectionName, java.lang.String item)
           
 java.lang.Object getSectionItemObject(java.lang.String section, java.lang.String item)
           
 java.lang.String[] getSectionItems(java.lang.String section)
           
 java.lang.String getSectionItemValue(java.lang.String section, java.lang.String item)
          Get an item from a section.
 java.lang.String[] getSectionNames()
           
 java.util.Hashtable getWriteHashOfSectionItems(java.lang.String section)
          This is used to get a Hashtable of the items in the specific section of the IniFileReader class for the purpose of adding or setting a new key.
static void main(java.lang.String[] arg)
          Test method requires a ini file called test.ini
 void removeSection(java.lang.String sectionName)
          This method can be used to remove sections in the IniFileReader class
 void removeSectionItem(java.lang.String sectionName, java.lang.String item)
          This method can be used to remove an item form a section
 void removeSectionItemArray(java.lang.String sectionName, java.lang.String item, java.lang.String value)
           
 byte[] saveToByteArray()
          Make a windows ini-file-formatted representation of the data
 void saveToFile(java.lang.String fileName)
          Save the IniFileReader contect to a file
protected  void startEmpty()
           
static void testLines()
           
static java.lang.String[] toLines(byte[] txt)
          Take a byte array and turn it into an array of trimmed lines.
static java.lang.String[] toLines(byte[] txt, int version)
          Take a byte array and turn it into an array of lines.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

lastErrors

protected static java.lang.String lastErrors

lastEoln

protected static java.lang.String lastEoln

numErrors

protected static int numErrors
Constructor Detail

IniFileReader

public IniFileReader()
Default constructor creates an empty IniFileReader

IniFileReader

public IniFileReader(byte[] data)
              throws ContractException
read a ini formated file and put in the class
Parameters:
iniFileAsByteArray - the byte array containing the ini file
Method Detail

startEmpty

protected void startEmpty()

addByteArray

public void addByteArray(byte[] data)
                  throws ContractException
Add the contents of an ini-file formated byte array.
Parameters:
data - the byte array containing the additional ini file

getLastErrors

public static java.lang.String getLastErrors()

getNumErrors

public static int getNumErrors()

toLines

public static java.lang.String[] toLines(byte[] txt)
Take a byte array and turn it into an array of trimmed lines.

toLines

public static java.lang.String[] toLines(byte[] txt,
                                         int version)
Take a byte array and turn it into an array of lines. A line is terminated by an eoln string, which must be the the same throughout the array. Note. This is used because the semantics are precise, whereas the equivalent in java.io.* is unclear, or sufficiently murky for the hashing task. Also, the exceptions are boring.
Parameters:
If - version >= 3 then proper trim is used.

doLines

protected static void doLines(java.lang.String desc,
                              java.lang.String test)

testLines

public static void testLines()

add

protected void add(byte[] iniFileAsByteArray)
            throws ContractException
Add a files worth to the hashtables.

getIniFileHash

public java.util.Hashtable getIniFileHash()
Returns:
This method returns a hash of selections
The has returned contains more Hashes
  • sections
    (String sectionName) (Hash of items)

    (Hash of items)
    (String item) (String Value)
  • to get the items back explicit cast is needed

saveToByteArray

public byte[] saveToByteArray()
                       throws java.io.IOException
Make a windows ini-file-formatted representation of the data
Returns:
a byte array of the file

saveToFile

public void saveToFile(java.lang.String fileName)
                throws java.io.IOException
Save the IniFileReader contect to a file
Parameters:
fileName - the file name on wich to save

getNumberOfSections

public int getNumberOfSections()
this method returns the number of sections in the IniFileReader class
Returns:
The number of sections

getSectionNames

public java.lang.String[] getSectionNames()
Returns:
an array of Strings of the section names

addSection

public void addSection(java.lang.String sectionName)
This method add a section to the IniFileReader class
Parameters:
sectionName - the name of the new section

removeSection

public void removeSection(java.lang.String sectionName)
This method can be used to remove sections in the IniFileReader class
Parameters:
sectionName - the name of the section to remove

getNumberOfSectionItems

public int getNumberOfSectionItems(java.lang.String section)
this is used for getting the amount of items in a section of a IniFileReader class
Parameters:
section - section is an existing section on the ini file
Returns:
returns the number of items in the section

getWriteHashOfSectionItems

public java.util.Hashtable getWriteHashOfSectionItems(java.lang.String section)
This is used to get a Hashtable of the items in the specific section of the IniFileReader class for the purpose of adding or setting a new key. On return, there is a hash, even if there wasn't one before. This changes the semantics somewhat - create on demand.
Parameters:
section - section is an existing section on the ini file
Returns:
A Hashtable of Items contained in the section

getHashOfSectionItems

public java.util.Hashtable getHashOfSectionItems(java.lang.String section)
This is used to get a Hashtable of the items in the specific section of the IniFileReader class
Parameters:
section - section is an existing section on the ini file
Returns:
A Hashtable of Items contained in the section, or null if none.

getSectionItems

public java.lang.String[] getSectionItems(java.lang.String section)
Parameters:
section - section is an existing section on the ini file
Returns:
an array of Strings of the items contained by the section

getSectionItemObject

public java.lang.Object getSectionItemObject(java.lang.String section,
                                             java.lang.String item)

getSectionItemValue

public java.lang.String getSectionItemValue(java.lang.String section,
                                            java.lang.String item)
Get an item from a section. Also checks for section_item, which is equivalent.
Parameters:
section - is an existing section in the ini file
item - is an existing item in the section in the ini file
Returns:
A Strings containing the value of the item

addSectionItem

public void addSectionItem(java.lang.String sectionName,
                           java.lang.String item,
                           java.lang.String value)
This method can be used to add a item/value pair to a section if the value is mutliline then you should use addMultilineSectionItem
Parameters:
sectionName - the name of the section
item - the name of the item in the section
value - the value of the item

addMultilineSectionItem

public void addMultilineSectionItem(java.lang.String sectionName,
                                    java.lang.String item,
                                    java.lang.String value)
This method can be used to add a item/value pair to a section using for mutliline
Parameters:
sectionName - the name of the section
item - the name of the item in the section
value - the value of the item

changeSectionItemValue

public void changeSectionItemValue(java.lang.String sect,
                                   java.lang.String item,
                                   java.lang.String value)
This method can be used to change a value pair of a item in a section
Parameters:
sectionName - the name of the section
item - the name of the item in the section
value - the value of the item

removeSectionItem

public void removeSectionItem(java.lang.String sectionName,
                              java.lang.String item)
This method can be used to remove an item form a section
Parameters:
sectionName - the name of the section
item - the name of the item to remove

getSectionItemArray

public java.lang.String[] getSectionItemArray(java.lang.String sectionName,
                                              java.lang.String item)

removeSectionItemArray

public void removeSectionItemArray(java.lang.String sectionName,
                                   java.lang.String item,
                                   java.lang.String value)

dump

public void dump()

main

public static void main(java.lang.String[] arg)
                 throws java.io.IOException,
                        ContractException
Test method requires a ini file called test.ini