1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.ini4j;
17
18 import java.io.Serializable;
19
20 import java.nio.charset.Charset;
21
22 @SuppressWarnings("PMD.ExcessivePublicCount")
23 public class Config implements Cloneable, Serializable
24 {
25 public static final String KEY_PREFIX = "org.ini4j.config.";
26 public static final String PROP_EMPTY_OPTION = "emptyOption";
27 public static final String PROP_EMPTY_SECTION = "emptySection";
28 public static final String PROP_GLOBAL_SECTION = "globalSection";
29 public static final String PROP_GLOBAL_SECTION_NAME = "globalSectionName";
30 public static final String PROP_INCLUDE = "include";
31 public static final String PROP_LOWER_CASE_OPTION = "lowerCaseOption";
32 public static final String PROP_LOWER_CASE_SECTION = "lowerCaseSection";
33 public static final String PROP_MULTI_OPTION = "multiOption";
34 public static final String PROP_MULTI_SECTION = "multiSection";
35 public static final String PROP_STRICT_OPERATOR = "strictOperator";
36 public static final String PROP_UNNAMED_SECTION = "unnamedSection";
37 public static final String PROP_ESCAPE = "escape";
38 public static final String PROP_ESCAPE_NEWLINE = "escapeNewline";
39 public static final String PROP_PATH_SEPARATOR = "pathSeparator";
40 public static final String PROP_TREE = "tree";
41 public static final String PROP_PROPERTY_FIRST_UPPER = "propertyFirstUpper";
42 public static final String PROP_FILE_ENCODING = "fileEncoding";
43 public static final String PROP_LINE_SEPARATOR = "lineSeparator";
44 public static final String PROP_COMMENT = "comment";
45 public static final String PROP_HEADER_COMMENT = "headerComment";
46 public static final boolean DEFAULT_EMPTY_OPTION = false;
47 public static final boolean DEFAULT_EMPTY_SECTION = false;
48 public static final boolean DEFAULT_GLOBAL_SECTION = false;
49 public static final String DEFAULT_GLOBAL_SECTION_NAME = "?";
50 public static final boolean DEFAULT_INCLUDE = false;
51 public static final boolean DEFAULT_LOWER_CASE_OPTION = false;
52 public static final boolean DEFAULT_LOWER_CASE_SECTION = false;
53 public static final boolean DEFAULT_MULTI_OPTION = true;
54 public static final boolean DEFAULT_MULTI_SECTION = false;
55 public static final boolean DEFAULT_STRICT_OPERATOR = false;
56 public static final boolean DEFAULT_UNNAMED_SECTION = false;
57 public static final boolean DEFAULT_ESCAPE = true;
58 public static final boolean DEFAULT_ESCAPE_NEWLINE = true;
59 public static final boolean DEFAULT_TREE = true;
60 public static final boolean DEFAULT_PROPERTY_FIRST_UPPER = false;
61 public static final boolean DEFAULT_COMMENT = true;
62 public static final boolean DEFAULT_HEADER_COMMENT = true;
63 public static final char DEFAULT_PATH_SEPARATOR = '/';
64 public static final String DEFAULT_LINE_SEPARATOR = getSystemProperty("line.separator", "\n");
65 public static final Charset DEFAULT_FILE_ENCODING = Charset.forName("UTF-8");
66 private static final Config GLOBAL = new Config();
67 private static final long serialVersionUID = 2865793267410367814L;
68 private boolean _comment;
69 private boolean _emptyOption;
70 private boolean _emptySection;
71 private boolean _escape;
72 private boolean _escapeNewline;
73 private Charset _fileEncoding;
74 private boolean _globalSection;
75 private String _globalSectionName;
76 private boolean _headerComment;
77 private boolean _include;
78 private String _lineSeparator;
79 private boolean _lowerCaseOption;
80 private boolean _lowerCaseSection;
81 private boolean _multiOption;
82 private boolean _multiSection;
83 private char _pathSeparator;
84 private boolean _propertyFirstUpper;
85 private boolean _strictOperator;
86 private boolean _tree;
87 private boolean _unnamedSection;
88
89 public Config()
90 {
91 reset();
92 }
93
94 public static String getEnvironment(String name)
95 {
96 return getEnvironment(name, null);
97 }
98
99 public static String getEnvironment(String name, String defaultValue)
100 {
101 String value;
102
103 try
104 {
105 value = System.getenv(name);
106 }
107 catch (SecurityException x)
108 {
109 value = null;
110 }
111
112 return (value == null) ? defaultValue : value;
113 }
114
115 public static Config getGlobal()
116 {
117 return GLOBAL;
118 }
119
120 public static String getSystemProperty(String name)
121 {
122 return getSystemProperty(name, null);
123 }
124
125 public static String getSystemProperty(String name, String defaultValue)
126 {
127 String value;
128
129 try
130 {
131 value = System.getProperty(name);
132 }
133 catch (SecurityException x)
134 {
135 value = null;
136 }
137
138 return (value == null) ? defaultValue : value;
139 }
140
141 public void setComment(boolean value)
142 {
143 _comment = value;
144 }
145
146 public boolean isEscape()
147 {
148 return _escape;
149 }
150
151 public boolean isEscapeNewline()
152 {
153 return _escapeNewline;
154 }
155
156 public boolean isInclude()
157 {
158 return _include;
159 }
160
161 public boolean isTree()
162 {
163 return _tree;
164 }
165
166 public void setEmptyOption(boolean value)
167 {
168 _emptyOption = value;
169 }
170
171 public void setEmptySection(boolean value)
172 {
173 _emptySection = value;
174 }
175
176 public void setEscape(boolean value)
177 {
178 _escape = value;
179 }
180
181 public void setEscapeNewline(boolean value)
182 {
183 _escapeNewline = value;
184 }
185
186 public Charset getFileEncoding()
187 {
188 return _fileEncoding;
189 }
190
191 public void setFileEncoding(Charset value)
192 {
193 _fileEncoding = value;
194 }
195
196 public void setGlobalSection(boolean value)
197 {
198 _globalSection = value;
199 }
200
201 public String getGlobalSectionName()
202 {
203 return _globalSectionName;
204 }
205
206 public void setGlobalSectionName(String value)
207 {
208 _globalSectionName = value;
209 }
210
211 public void setHeaderComment(boolean value)
212 {
213 _headerComment = value;
214 }
215
216 public void setInclude(boolean value)
217 {
218 _include = value;
219 }
220
221 public String getLineSeparator()
222 {
223 return _lineSeparator;
224 }
225
226 public void setLineSeparator(String value)
227 {
228 _lineSeparator = value;
229 }
230
231 public void setLowerCaseOption(boolean value)
232 {
233 _lowerCaseOption = value;
234 }
235
236 public void setLowerCaseSection(boolean value)
237 {
238 _lowerCaseSection = value;
239 }
240
241 public void setMultiOption(boolean value)
242 {
243 _multiOption = value;
244 }
245
246 public void setMultiSection(boolean value)
247 {
248 _multiSection = value;
249 }
250
251 public boolean isEmptyOption()
252 {
253 return _emptyOption;
254 }
255
256 public boolean isEmptySection()
257 {
258 return _emptySection;
259 }
260
261 public boolean isGlobalSection()
262 {
263 return _globalSection;
264 }
265
266 public boolean isLowerCaseOption()
267 {
268 return _lowerCaseOption;
269 }
270
271 public boolean isLowerCaseSection()
272 {
273 return _lowerCaseSection;
274 }
275
276 public boolean isMultiOption()
277 {
278 return _multiOption;
279 }
280
281 public boolean isMultiSection()
282 {
283 return _multiSection;
284 }
285
286 public boolean isUnnamedSection()
287 {
288 return _unnamedSection;
289 }
290
291 public char getPathSeparator()
292 {
293 return _pathSeparator;
294 }
295
296 public void setPathSeparator(char value)
297 {
298 _pathSeparator = value;
299 }
300
301 public void setPropertyFirstUpper(boolean value)
302 {
303 _propertyFirstUpper = value;
304 }
305
306 public boolean isPropertyFirstUpper()
307 {
308 return _propertyFirstUpper;
309 }
310
311 public boolean isStrictOperator()
312 {
313 return _strictOperator;
314 }
315
316 public void setStrictOperator(boolean value)
317 {
318 _strictOperator = value;
319 }
320
321 public boolean isComment()
322 {
323 return _comment;
324 }
325
326 public boolean isHeaderComment()
327 {
328 return _headerComment;
329 }
330
331 public void setTree(boolean value)
332 {
333 _tree = value;
334 }
335
336 public void setUnnamedSection(boolean value)
337 {
338 _unnamedSection = value;
339 }
340
341 @Override public Config clone()
342 {
343 try
344 {
345 return (Config) super.clone();
346 }
347 catch (CloneNotSupportedException x)
348 {
349 throw new AssertionError(x);
350 }
351 }
352
353 public final void reset()
354 {
355 _emptyOption = getBoolean(PROP_EMPTY_OPTION, DEFAULT_EMPTY_OPTION);
356 _emptySection = getBoolean(PROP_EMPTY_SECTION, DEFAULT_EMPTY_SECTION);
357 _globalSection = getBoolean(PROP_GLOBAL_SECTION, DEFAULT_GLOBAL_SECTION);
358 _globalSectionName = getString(PROP_GLOBAL_SECTION_NAME, DEFAULT_GLOBAL_SECTION_NAME);
359 _include = getBoolean(PROP_INCLUDE, DEFAULT_INCLUDE);
360 _lowerCaseOption = getBoolean(PROP_LOWER_CASE_OPTION, DEFAULT_LOWER_CASE_OPTION);
361 _lowerCaseSection = getBoolean(PROP_LOWER_CASE_SECTION, DEFAULT_LOWER_CASE_SECTION);
362 _multiOption = getBoolean(PROP_MULTI_OPTION, DEFAULT_MULTI_OPTION);
363 _multiSection = getBoolean(PROP_MULTI_SECTION, DEFAULT_MULTI_SECTION);
364 _strictOperator = getBoolean(PROP_STRICT_OPERATOR, DEFAULT_STRICT_OPERATOR);
365 _unnamedSection = getBoolean(PROP_UNNAMED_SECTION, DEFAULT_UNNAMED_SECTION);
366 _escape = getBoolean(PROP_ESCAPE, DEFAULT_ESCAPE);
367 _escapeNewline = getBoolean(PROP_ESCAPE_NEWLINE, DEFAULT_ESCAPE_NEWLINE);
368 _pathSeparator = getChar(PROP_PATH_SEPARATOR, DEFAULT_PATH_SEPARATOR);
369 _tree = getBoolean(PROP_TREE, DEFAULT_TREE);
370 _propertyFirstUpper = getBoolean(PROP_PROPERTY_FIRST_UPPER, DEFAULT_PROPERTY_FIRST_UPPER);
371 _lineSeparator = getString(PROP_LINE_SEPARATOR, DEFAULT_LINE_SEPARATOR);
372 _fileEncoding = getCharset(PROP_FILE_ENCODING, DEFAULT_FILE_ENCODING);
373 _comment = getBoolean(PROP_COMMENT, DEFAULT_COMMENT);
374 _headerComment = getBoolean(PROP_HEADER_COMMENT, DEFAULT_HEADER_COMMENT);
375 }
376
377 private boolean getBoolean(String name, boolean defaultValue)
378 {
379 String value = getSystemProperty(KEY_PREFIX + name);
380
381 return (value == null) ? defaultValue : Boolean.parseBoolean(value);
382 }
383
384 private char getChar(String name, char defaultValue)
385 {
386 String value = getSystemProperty(KEY_PREFIX + name);
387
388 return (value == null) ? defaultValue : value.charAt(0);
389 }
390
391 private Charset getCharset(String name, Charset defaultValue)
392 {
393 String value = getSystemProperty(KEY_PREFIX + name);
394
395 return (value == null) ? defaultValue : Charset.forName(value);
396 }
397
398 private String getString(String name, String defaultValue)
399 {
400 return getSystemProperty(KEY_PREFIX + name, defaultValue);
401 }
402 }