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