Back to index

wims  3.65+svn20090927
Config.java
Go to the documentation of this file.
00001 /*
00002  * @(#)SharpTools.java
00003  * 
00004  * $Id: Config.java,v 1.11 2001/05/27 05:20:44 huaz Exp $
00005  * 
00006  * Created on October 10, 2000, 1:15 AM
00007  */
00008 package SharpTools; 
00009 import java.util.*;
00010 import java.io.*;
00011 
00025 final class Config {
00026 
00027     private HashMap map;
00028     private File file;
00029     private boolean modified;
00030     
00031     Config(String filename) {
00032        modified = false;
00033        map = new HashMap();
00034        file = new File(filename);
00035     }
00036 
00037     // load from file
00038     public void load() {
00039        Debug.println("Loading configuration file...");
00040        try {
00041            // open the file
00042            BufferedReader in = new BufferedReader
00043               (new FileReader(file));
00044            String line;
00045            while ((line = in.readLine()) != null) {
00046 
00047               // convert to <key,value> pair
00048               String[] pair = getPair(line);
00049               
00050               if (pair == null)
00051                   continue;
00052 
00053               // key must not be empty
00054               if (pair[0] == null || pair[0].length() == 0)
00055                   continue;
00056 
00057               map.put(pair[0], pair[1]);
00058            }
00059        }
00060        catch (IOException e) {
00061        }
00062        catch (Exception e) {
00063            System.err.println("error parsing config file: "+e);
00064        }
00065 
00066        modified = false;
00067        //Debug.println(map);
00068     }
00069 
00070     // save to file
00071     public void save() {
00072        if (!modified)
00073            return;
00074        
00075        Debug.println("Saving configuration file...");
00076        String tmpfilename = file+".tmp";
00077        File tmp = new File(tmpfilename);
00078        
00079        try {
00080            // open the file
00081            BufferedReader in = new BufferedReader
00082               (new FileReader(file));
00083            PrintWriter out = new PrintWriter
00084               (new BufferedWriter
00085                   (new FileWriter(tmp)));
00086            
00087            String line;
00088            while ((line = in.readLine()) != null) {
00089               
00090               String[] pair = getPair(line.toUpperCase());
00091 
00092               if (pair != null && pair[0] != null &&
00093                   map.get(pair[0]) != null)
00094                   out.println(pair[0]+'='+map.get(pair[0]));
00095               else
00096                   out.println(line);
00097 
00098            }
00099 
00100            in.close();
00101            out.close();
00102        }
00103        catch (IOException e) {
00104            System.err.println("io error saving config file: "+e);
00105            return;
00106        }
00107        catch (Exception e) {
00108            System.err.println("error saving config file: "+e);
00109            return;
00110        }
00111 
00112        // move tmp file bak
00113        file.delete();
00114        tmp.renameTo(file);
00115        modified = false;
00116     }
00117     
00124     private String[] getPair(String line) {
00125        
00126        int index = line.indexOf('=');
00127        if (index < 0)
00128            return null;
00129 
00130        // ignore comments
00131        if (line.startsWith("#") || line.startsWith(";"))
00132            return null;
00133        
00134        String[] pair = new String[2];
00135        pair[0] = line.substring(0, index).trim();
00136        pair[1] = line.substring(index+1).trim();
00137        return pair;
00138        
00139     }
00140 
00147     public String get(String key) {
00148        return (String)map.get(key);
00149     }
00150 
00157     public int getInt(String key) {
00158        String value = (String)map.get(key);
00159        if (value == null)
00160            return -1;
00161 
00162        int intValue = -1;
00163        try {
00164            intValue = Integer.parseInt(value);
00165        }
00166        catch (NumberFormatException e) {
00167            //     Debug.println("Return -1 for "+key+";"+value);
00168            return -1;
00169        }
00170 
00171        return intValue;
00172     }
00173 
00180     public boolean getBoolean(String key) {
00181        String value = (String)map.get(key);
00182        if (value == null)
00183            return false;
00184 
00185        return value.equals("TRUE");
00186     }
00187 
00188     
00195     public void set(String key, String value) {
00196        modified = true;
00197        map.put(key, value);
00198     }
00199 
00206     public void setInt(String key, int value) {
00207        set(key, String.valueOf(value));
00208     }
00209 
00216     public void setBoolean(String key, boolean value) {
00217        set(key, value?"TRUE":"FALSE");
00218     }    
00219 }
00220 
00221 
00222 
00223 
00224 
00225 
00226 
00227 
00228 
00229 
00230