KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > neu > ccs > jmk > swing > JmkProperties


1 // $Id: JmkProperties.java,v 1.3 2001/12/07 11:58:08 ramsdell Exp $
2

3 package edu.neu.ccs.jmk.swing;
4
5 /*
6  * Copyright 1997 by Olivier Refalo
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 import java.util.*;
24 import java.io.*;
25
26 public final
27 class JmkProperties
28 {
29
30     private final static String JavaDoc JMK_PROPERTIES = "jmk.properties";
31
32     // This is a singleton
33
private Properties properties_ = null ;
34     private static JmkProperties instance_ = null;
35
36     private JmkProperties()
37     {
38         properties_=new Properties() ;
39     }
40     public static JmkProperties getInstance()
41     {
42         if (instance_==null)
43             instance_ = new JmkProperties();
44         return(instance_);
45     }
46
47     // Accessors for resources.
48

49     public String JavaDoc getString(String JavaDoc _key, String JavaDoc _defaultValue)
50     {
51         String JavaDoc value = properties_.getProperty(_key);
52         if (value == null)
53         {
54             return(_defaultValue);
55         }
56         else
57         {
58             value=value.trim();
59             return(value);
60         }
61     }
62
63     public boolean getBoolean(String JavaDoc _key, boolean _defaultValue)
64     {
65         String JavaDoc value = properties_.getProperty(_key);
66         if (value == null)
67         {
68             return(_defaultValue);
69         }
70         else
71         {
72             value=value.trim();
73             if (value.toUpperCase().compareTo("TRUE")==0)
74                 return(true);
75             else if (value.toUpperCase().compareTo("FALSE")==0)
76                 return(false);
77             else
78             {
79                 System.err.println("Invalid boolean property for key <"+_key+">");
80                 return (_defaultValue);
81             }
82         }
83     }
84
85     public int getInteger(String JavaDoc _key, int _defaultValue)
86     {
87
88         String JavaDoc value = properties_.getProperty(_key);
89         if (value == null)
90         {
91             return(_defaultValue);
92         }
93         else
94         {
95             value=value.trim();
96             try
97             {
98                 int v=Integer.parseInt(value);
99                 return(v);
100             }
101             catch (NumberFormatException JavaDoc e)
102             {
103                 System.err.println("Type mismatch for key <"+_key+">, defaulting to <"+_defaultValue+">");
104                 return(_defaultValue);
105             }
106         }
107     }
108
109     public String JavaDoc put(String JavaDoc _key, String JavaDoc _value)
110     {
111         return(String JavaDoc)properties_.put(_key, _value);
112     }
113
114     // load the property file
115
public void load()
116     {
117         try
118         {
119             InputStream fis = new FileInputStream(JMK_PROPERTIES);
120             properties_.load(fis);
121             fis.close();
122         }
123         catch (Exception JavaDoc e)
124         {
125             // property file not found
126
}
127     }
128
129     // save the property file
130
public void save()
131     {
132         try
133         {
134             FileOutputStream fos = new FileOutputStream(JMK_PROPERTIES);
135             properties_.store(fos, "Jmk property file");
136             fos.close();
137             properties_.clear();
138         }
139         catch (IOException e)
140         {
141             System.err.println(e);
142         }
143     }
144
145 }
146
Popular Tags