1*1b0aaa91SAndrew Rist /**************************************************************
2*1b0aaa91SAndrew Rist  *
3*1b0aaa91SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*1b0aaa91SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*1b0aaa91SAndrew Rist  * distributed with this work for additional information
6*1b0aaa91SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*1b0aaa91SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*1b0aaa91SAndrew Rist  * "License"); you may not use this file except in compliance
9*1b0aaa91SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*1b0aaa91SAndrew Rist  *
11*1b0aaa91SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*1b0aaa91SAndrew Rist  *
13*1b0aaa91SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*1b0aaa91SAndrew Rist  * software distributed under the License is distributed on an
15*1b0aaa91SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*1b0aaa91SAndrew Rist  * KIND, either express or implied.  See the License for the
17*1b0aaa91SAndrew Rist  * specific language governing permissions and limitations
18*1b0aaa91SAndrew Rist  * under the License.
19*1b0aaa91SAndrew Rist  *
20*1b0aaa91SAndrew Rist  *************************************************************/
21*1b0aaa91SAndrew Rist 
22cdf0e10cSrcweir import java.io.File;
23cdf0e10cSrcweir import java.io.FileReader;
24cdf0e10cSrcweir import java.io.FileInputStream;
25cdf0e10cSrcweir import java.io.FileOutputStream;
26cdf0e10cSrcweir import java.util.Properties;
27cdf0e10cSrcweir 
28cdf0e10cSrcweir /** Load from and save options into a file.
29cdf0e10cSrcweir */
30cdf0e10cSrcweir class Options
31cdf0e10cSrcweir     extends Properties
32cdf0e10cSrcweir {
Instance()33cdf0e10cSrcweir     static public Options Instance ()
34cdf0e10cSrcweir     {
35cdf0e10cSrcweir         if (saOptions == null)
36cdf0e10cSrcweir             saOptions = new Options ();
37cdf0e10cSrcweir         return saOptions;
38cdf0e10cSrcweir     }
39cdf0e10cSrcweir 
SetString(String sName, String sValue)40cdf0e10cSrcweir     static public void SetString (String sName, String sValue)
41cdf0e10cSrcweir     {
42cdf0e10cSrcweir         Instance().setProperty (sName, sValue);
43cdf0e10cSrcweir     }
44cdf0e10cSrcweir 
GetString(String sName)45cdf0e10cSrcweir     static public String GetString (String sName)
46cdf0e10cSrcweir     {
47cdf0e10cSrcweir         return Instance().getProperty (sName);
48cdf0e10cSrcweir     }
49cdf0e10cSrcweir 
SetBoolean(String sName, boolean bValue)50cdf0e10cSrcweir     static public void SetBoolean (String sName, boolean bValue)
51cdf0e10cSrcweir     {
52cdf0e10cSrcweir         Instance().setProperty (sName, Boolean.toString(bValue));
53cdf0e10cSrcweir     }
54cdf0e10cSrcweir 
GetBoolean(String sName)55cdf0e10cSrcweir     static public boolean GetBoolean (String sName)
56cdf0e10cSrcweir     {
57cdf0e10cSrcweir         return Boolean.getBoolean(Instance().getProperty (sName));
58cdf0e10cSrcweir     }
59cdf0e10cSrcweir 
SetInteger(String sName, int nValue)60cdf0e10cSrcweir     static public void SetInteger (String sName, int nValue)
61cdf0e10cSrcweir     {
62cdf0e10cSrcweir         Instance().setProperty (sName, Integer.toString(nValue));
63cdf0e10cSrcweir     }
64cdf0e10cSrcweir 
GetInteger(String sName, int nDefault)65cdf0e10cSrcweir     static public int GetInteger (String sName, int nDefault)
66cdf0e10cSrcweir     {
67cdf0e10cSrcweir         String sValue = Instance().getProperty (sName);
68cdf0e10cSrcweir         if (sValue == null)
69cdf0e10cSrcweir             return nDefault;
70cdf0e10cSrcweir         else
71cdf0e10cSrcweir             return Integer.parseInt (sValue);
72cdf0e10cSrcweir     }
73cdf0e10cSrcweir 
Load(String sBaseName)74cdf0e10cSrcweir     public void Load (String sBaseName)
75cdf0e10cSrcweir     {
76cdf0e10cSrcweir         try
77cdf0e10cSrcweir         {
78cdf0e10cSrcweir             load (new FileInputStream (ProvideFile(sBaseName)));
79cdf0e10cSrcweir         }
80cdf0e10cSrcweir         catch (java.io.IOException e)
81cdf0e10cSrcweir         {
82cdf0e10cSrcweir             // Ignore a non-existing options file.
83cdf0e10cSrcweir         }
84cdf0e10cSrcweir     }
85cdf0e10cSrcweir 
Save(String sBaseName)86cdf0e10cSrcweir     public void Save (String sBaseName)
87cdf0e10cSrcweir     {
88cdf0e10cSrcweir         try
89cdf0e10cSrcweir         {
90cdf0e10cSrcweir             store (new FileOutputStream (ProvideFile(sBaseName)), null);
91cdf0e10cSrcweir         }
92cdf0e10cSrcweir         catch (java.io.IOException e)
93cdf0e10cSrcweir         {
94cdf0e10cSrcweir         }
95cdf0e10cSrcweir     }
96cdf0e10cSrcweir 
Options()97cdf0e10cSrcweir     private Options ()
98cdf0e10cSrcweir     {
99cdf0e10cSrcweir     }
100cdf0e10cSrcweir 
ProvideFile(String sBaseName)101cdf0e10cSrcweir     private File ProvideFile (String sBaseName)
102cdf0e10cSrcweir     {
103cdf0e10cSrcweir         return new File (
104cdf0e10cSrcweir             System.getProperty ("user.home"),
105cdf0e10cSrcweir             sBaseName);
106cdf0e10cSrcweir     }
107cdf0e10cSrcweir 
108cdf0e10cSrcweir     static private Options saOptions = null;
109cdf0e10cSrcweir }
110