-
Notifications
You must be signed in to change notification settings - Fork 216
Expand file tree
/
Copy pathDefaultSettings.java
More file actions
261 lines (229 loc) · 8.31 KB
/
DefaultSettings.java
File metadata and controls
261 lines (229 loc) · 8.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// This file is part of the "IBController".
// Copyright (C) 2004 Steven M. Kearns (skearns23@yahoo.com )
// Copyright (C) 2004 - 2011 Richard L King (rlking@aultan.com)
// For conditions of distribution and use, see copyright notice in COPYING.txt
// IBController is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// IBController is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with IBController. If not, see <http://www.gnu.org/licenses/>.
package ibcontroller;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class DefaultSettings extends Settings {
private final Properties props = new Properties();
private String path;
public DefaultSettings() {
load(generateDefaultIniPath());
}
public DefaultSettings(String[] args) {
load(getSettingsPath(args));
}
public DefaultSettings(String path) {
load(path);
}
private void load(String path) {
this.path = path;
props.clear();
try {
File f = new File(path);
InputStream is = new BufferedInputStream(new FileInputStream(f));
props.load(is);
is.close();
} catch (FileNotFoundException e) {
Utils.logToConsole("Properties file " + path + " not found");
} catch (IOException e) {
Utils.logToConsole(
"Exception accessing Properties file " + path);
Utils.logToConsole(e.toString());
}
}
static String generateDefaultIniPath() {
if (System.getProperty("os.name").startsWith("Windows")) {
return System.getenv("HOMEDRIVE") +
System.getenv("HOMEPATH") + File.separator +
"Documents" + File.separator +
"IBController" + File.separator +
"IBController.ini";
} else {
return System.getProperty("user.home") + File.separator +
"IBController" + File.separator +
"IBController.ini";
}
}
static String getSettingsPath(String [] args) {
String iniPath;
if (args.length == 0 || args[0].equalsIgnoreCase("NULL")) {
iniPath = getWorkingDirectory() + "IBController." + getComputerUserName() + ".ini";
} else if (args[0].length() == 0) {
iniPath = generateDefaultIniPath();
} else {// args.length >= 1
iniPath = args[0];
}
File finiPath = new File(iniPath);
if (!finiPath.isFile() || !finiPath.exists()) {
Utils.logError("ini file \"" + iniPath +
"\" either does not exist, or is a directory. quitting...");
System.exit(1);
}
return iniPath;
}
private static String getComputerUserName() {
StringBuilder sb = new StringBuilder(System.getProperty("user.name"));
int i;
for (i = 0; i < sb.length(); i++) {
char c = sb.charAt(i);
if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
continue;
}
if (c >= 'A' && c <= 'Z') {
sb.setCharAt(i, Character.toLowerCase(c));
} else {
sb.setCharAt(i, '_');
}
}
return sb.toString();
}
private static String getWorkingDirectory() {
return System.getProperty("user.dir") + File.separator;
}
@Override
public void logDiagnosticMessage(){
Utils.logToConsole("using default settings provider: ini file is " + path);
}
/**
returns the value associated with property named key.
Returns defaultValue if no such property.
* @param key
* @param defaultValue
* @return
*/
@Override
public String getString(String key,
String defaultValue) {
String value = props.getProperty(key, defaultValue);
// handle key=[empty string] in .ini file
if (value.isEmpty()) {
value = defaultValue;
}
return value;
}
/**
returns the int value associated with property named key.
Returns defaultValue if there is no such property,
or if the property value cannot be converted to an int.
* @param key
* @param defaultValue
* @return
*/
@Override
public int getInt(String key,
int defaultValue) {
String value = props.getProperty(key);
// handle key missing or key=[empty string] in .ini file
if (value == null || value.length() == 0) {
return defaultValue;
}
try {
return Integer.parseInt(value);
} catch (NumberFormatException e) {
Utils.logToConsole(
"Invalid number \""
+ value
+ "\" for property \""
+ key
+ "\"");
return defaultValue;
}
}
/**
*
* @param key
* @param defaultValue
* @return
*/
@Override
public char getChar(String key,
String defaultValue) {
String value = props.getProperty(key, defaultValue);
// handle key missing or key=[empty string] in .ini file
if (value == null || value.length() == 0) {
return defaultValue.charAt(0);
}
if (value.length() != 1) {
Utils.logToConsole(
"Invalid character \""
+ value
+ "\" for property \""
+ key
+ "\"");
}
return value.charAt(0);
}
/**
returns the double value associated with property named key.
Returns defaultVAlue if there is no such property,
or if the property value cannot be converted to a double.
* @param key
* @param defaultValue
* @return
*/
@Override
public double getDouble(String key,
double defaultValue) {
String value = props.getProperty(key);
// handle key missing or key=[empty string] in .ini file
if (value == null || value.length() == 0) {
return defaultValue;
}
try {
return Double.parseDouble(value);
} catch (NumberFormatException e) {
Utils.logToConsole(
"Invalid number \""
+ value
+ "\" for property \""
+ key
+ "\"");
return defaultValue;
}
}
/**
returns the boolean value associated with property named key.
Returns defaultValue if there is no such property,
or if the property value cannot be converted to a boolean.
* @param key
* @param defaultValue
* @return
*/
@Override
public boolean getBoolean(String key,
boolean defaultValue) {
String value = props.getProperty(key);
// handle key missing or key=[empty string] in .ini file
if (value == null || value.length() == 0) {
return defaultValue;
}
if (value.equalsIgnoreCase("true")) {
return true;
} else if (value.equalsIgnoreCase("yes")) {
return true;
} else if (value.equalsIgnoreCase("false")) {
return false;
} else if (value.equalsIgnoreCase("no")) {
return false;
} else {
return defaultValue;
}
}
}