Back to index

wims  3.65+svn20090927
Public Member Functions | Package Functions | Private Member Functions | Private Attributes
SharpTools.Config Class Reference

This class reads configuration file and provides get functions for other classes to retrieve the information. More...

Collaboration diagram for SharpTools.Config:
Collaboration graph
[legend]

List of all members.

Public Member Functions

void load ()
void save ()
String get (String key)
 get value by string name
int getInt (String key)
 get integer value by string name
boolean getBoolean (String key)
 get boolean value by string name
void set (String key, String value)
 set value by string name
void setInt (String key, int value)
 set integer value by string name
void setBoolean (String key, boolean value)
 set boolean value by string name

Package Functions

 Config (String filename)

Private Member Functions

String[] getPair (String line)
 If a string is of form "Str1=Str2", return a pair.

Private Attributes

HashMap map
File file
boolean modified

Detailed Description

This class reads configuration file and provides get functions for other classes to retrieve the information.

This provides a central repository for global information.

Note, Config only saves a pair when the key is already in the config file. This is very important!!!

Author:
Hua Zhong huaz@.nosp@m.cs.c.nosp@m.olumb.nosp@m.ia.e.nosp@m.du
Version:
Revision:
1.11

Definition at line 25 of file Config.java.


Constructor & Destructor Documentation

SharpTools.Config.Config ( String  filename) [inline, package]

Definition at line 31 of file Config.java.

                            {
       modified = false;
       map = new HashMap();
       file = new File(filename);
    }

Member Function Documentation

String SharpTools.Config.get ( String  key) [inline]

get value by string name

Parameters:
keythe variable name
Returns:
the value as a string

Definition at line 147 of file Config.java.

                                  {
       return (String)map.get(key);
    }

Here is the caller graph for this function:

boolean SharpTools.Config.getBoolean ( String  key) [inline]

get boolean value by string name

Parameters:
keythe variable name
Returns:
the value as boolean; FALSE by default

Definition at line 180 of file Config.java.

                                          {
       String value = (String)map.get(key);
       if (value == null)
           return false;

       return value.equals("TRUE");
    }

Here is the caller graph for this function:

int SharpTools.Config.getInt ( String  key) [inline]

get integer value by string name

Parameters:
keythe variable name
Returns:
the value as an integer; -1 if not available

Definition at line 157 of file Config.java.

                                  {
       String value = (String)map.get(key);
       if (value == null)
           return -1;

       int intValue = -1;
       try {
           intValue = Integer.parseInt(value);
       }
       catch (NumberFormatException e) {
           //     Debug.println("Return -1 for "+key+";"+value);
           return -1;
       }

       return intValue;
    }

Here is the caller graph for this function:

String [] SharpTools.Config.getPair ( String  line) [inline, private]

If a string is of form "Str1=Str2", return a pair.

Parameters:
lineinput string
Returns:
a pair of (key, value); null if it's not a pair

Definition at line 124 of file Config.java.

                                          {
       
       int index = line.indexOf('=');
       if (index < 0)
           return null;

       // ignore comments
       if (line.startsWith("#") || line.startsWith(";"))
           return null;
       
       String[] pair = new String[2];
       pair[0] = line.substring(0, index).trim();
       pair[1] = line.substring(index+1).trim();
       return pair;
       
    }

Here is the caller graph for this function:

void SharpTools.Config.load ( ) [inline]

Definition at line 38 of file Config.java.

                       {
       Debug.println("Loading configuration file...");
       try {
           // open the file
           BufferedReader in = new BufferedReader
              (new FileReader(file));
           String line;
           while ((line = in.readLine()) != null) {

              // convert to <key,value> pair
              String[] pair = getPair(line);
              
              if (pair == null)
                  continue;

              // key must not be empty
              if (pair[0] == null || pair[0].length() == 0)
                  continue;

              map.put(pair[0], pair[1]);
           }
       }
       catch (IOException e) {
       }
       catch (Exception e) {
           System.err.println("error parsing config file: "+e);
       }

       modified = false;
       //Debug.println(map);
    }

Here is the call graph for this function:

Here is the caller graph for this function:

void SharpTools.Config.save ( ) [inline]

Definition at line 71 of file Config.java.

                       {
       if (!modified)
           return;
       
       Debug.println("Saving configuration file...");
       String tmpfilename = file+".tmp";
       File tmp = new File(tmpfilename);
       
       try {
           // open the file
           BufferedReader in = new BufferedReader
              (new FileReader(file));
           PrintWriter out = new PrintWriter
              (new BufferedWriter
                  (new FileWriter(tmp)));
           
           String line;
           while ((line = in.readLine()) != null) {
              
              String[] pair = getPair(line.toUpperCase());

              if (pair != null && pair[0] != null &&
                  map.get(pair[0]) != null)
                  out.println(pair[0]+'='+map.get(pair[0]));
              else
                  out.println(line);

           }

           in.close();
           out.close();
       }
       catch (IOException e) {
           System.err.println("io error saving config file: "+e);
           return;
       }
       catch (Exception e) {
           System.err.println("error saving config file: "+e);
           return;
       }

       // move tmp file bak
       file.delete();
       tmp.renameTo(file);
       modified = false;
    }

Here is the call graph for this function:

Here is the caller graph for this function:

void SharpTools.Config.set ( String  key,
String  value 
) [inline]

set value by string name

Parameters:
keythe variable name
thevalue as a string

Definition at line 195 of file Config.java.

                                              {
       modified = true;
       map.put(key, value);
    }

Here is the caller graph for this function:

void SharpTools.Config.setBoolean ( String  key,
boolean  value 
) [inline]

set boolean value by string name

Parameters:
keythe variable name
valuethe boolean value

Definition at line 216 of file Config.java.

                                                      {
       set(key, value?"TRUE":"FALSE");
    }    

Here is the caller graph for this function:

void SharpTools.Config.setInt ( String  key,
int  value 
) [inline]

set integer value by string name

Parameters:
keythe variable name
thevalue as an integer

Definition at line 206 of file Config.java.

                                              {
       set(key, String.valueOf(value));
    }

Here is the caller graph for this function:


Member Data Documentation

File SharpTools.Config.file [private]

Definition at line 28 of file Config.java.

HashMap SharpTools.Config.map [private]

Definition at line 27 of file Config.java.

Definition at line 29 of file Config.java.


The documentation for this class was generated from the following file: