KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rcm > util > Config


1 /*
2  * Copyright (c) 1998-2002 Carnegie Mellon University. All rights
3  * reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
18  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
21  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  */

30
31 package rcm.util;
32
33 import java.util.*;
34 import java.io.*;
35
36 public class Config extends Properties {
37     File file;
38     IOException lastException;
39
40     public Config (String JavaDoc fileBaseName) {
41         this (fileBaseName, null);
42     }
43
44     public Config (File file) {
45         this (file, null);
46     }
47
48     public Config (String JavaDoc fileBaseName, Config defaults) {
49         this (new File (getHomeDirectory (), fileBaseName),
50               defaults);
51     }
52
53     public Config (File file, Config defaults) {
54         super (defaults);
55
56         this.file = file;
57         try {
58             FileInputStream in =
59                 new FileInputStream (file);
60             load (in);
61             in.close ();
62         } catch (IOException e) {
63             lastException = e;
64         }
65     }
66
67     public IOException getLastException () {
68         return lastException;
69     }
70
71 // public String getProperty (String key, String defaultValue) {
72
// String val = super.getProperty (key, defaultValue);
73
// if (val != null && val == defaultValue)
74
// put (key, defaultValue);
75
// return val;
76
// }
77

78     public void save () {
79         try {
80             FileOutputStream out = new FileOutputStream (file);
81             save (out, "");
82             out.close ();
83             lastException = null;
84         } catch (IOException e) {
85             lastException = e;
86         }
87     }
88
89     public int countKeysStartingWith (String JavaDoc prefix) {
90         int n = 0;
91         for (Enumeration e = propertyNames (); e.hasMoreElements (); ) {
92             String JavaDoc name = (String JavaDoc)e.nextElement ();
93             if (name.startsWith (prefix))
94                 ++n;
95         }
96         return n;
97     }
98
99     public void removeAllKeysStartingWith (String JavaDoc prefix) {
100         Vector keysToDelete = new Vector ();
101         for (Enumeration e = propertyNames (); e.hasMoreElements (); ) {
102             String JavaDoc name = (String JavaDoc)e.nextElement ();
103             if (name.startsWith (prefix))
104                 keysToDelete.addElement (name);
105         }
106         
107         for (Enumeration e = keysToDelete.elements ();
108              e.hasMoreElements (); )
109             remove (e.nextElement ());
110     }
111     
112     public static File getHomeDirectory () {
113         String JavaDoc homedir;
114         if ((homedir = System.getProperty ("user.home")) == null
115             && (homedir = System.getProperty ("user.dir")) == null)
116             homedir = ".";
117         
118         return new File (homedir);
119     }
120 }
121
Popular Tags