001 /*
002 * Copyright 2005,2009 Ivan SZKIBA
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.ini4j;
017
018 import java.io.Serializable;
019
020 import java.nio.charset.Charset;
021
022 public class Config implements Cloneable, Serializable
023 {
024 public static final String KEY_PREFIX = "org.ini4j.config.";
025 public static final String PROP_EMPTY_OPTION = "emptyOption";
026 public static final String PROP_EMPTY_SECTION = "emptySection";
027 public static final String PROP_GLOBAL_SECTION = "globalSection";
028 public static final String PROP_GLOBAL_SECTION_NAME = "globalSectionName";
029 public static final String PROP_INCLUDE = "include";
030 public static final String PROP_LOWER_CASE_OPTION = "lowerCaseOption";
031 public static final String PROP_LOWER_CASE_SECTION = "lowerCaseSection";
032 public static final String PROP_MULTI_OPTION = "multiOption";
033 public static final String PROP_MULTI_SECTION = "multiSection";
034 public static final String PROP_STRICT_OPERATOR = "strictOperator";
035 public static final String PROP_UNNAMED_SECTION = "unnamedSection";
036 public static final String PROP_ESCAPE = "escape";
037 public static final String PROP_PATH_SEPARATOR = "pathSeparator";
038 public static final String PROP_TREE = "tree";
039 public static final String PROP_PROPERTY_FIRST_UPPER = "propertyFirstUpper";
040 public static final String PROP_FILE_ENCODING = "fileEncoding";
041 public static final String PROP_LINE_SEPARATOR = "lineSeparator";
042 public static final boolean DEFAULT_EMPTY_OPTION = false;
043 public static final boolean DEFAULT_EMPTY_SECTION = false;
044 public static final boolean DEFAULT_GLOBAL_SECTION = false;
045 public static final String DEFAULT_GLOBAL_SECTION_NAME = "?";
046 public static final boolean DEFAULT_INCLUDE = false;
047 public static final boolean DEFAULT_LOWER_CASE_OPTION = false;
048 public static final boolean DEFAULT_LOWER_CASE_SECTION = false;
049 public static final boolean DEFAULT_MULTI_OPTION = true;
050 public static final boolean DEFAULT_MULTI_SECTION = false;
051 public static final boolean DEFAULT_STRICT_OPERATOR = false;
052 public static final boolean DEFAULT_UNNAMED_SECTION = false;
053 public static final boolean DEFAULT_ESCAPE = true;
054 public static final boolean DEFAULT_TREE = true;
055 public static final boolean DEFAULT_PROPERTY_FIRST_UPPER = false;
056 public static final char DEFAULT_PATH_SEPARATOR = '/';
057 public static final String DEFAULT_LINE_SEPARATOR = getSystemProperty("line.separator", "\n");
058 public static final Charset DEFAULT_FILE_ENCODING = Charset.forName("UTF-8");
059 private static final Config GLOBAL = new Config();
060 private static final long serialVersionUID = 2865793267410367814L;
061 private boolean _emptyOption;
062 private boolean _emptySection;
063 private boolean _escape;
064 private Charset _fileEncoding;
065 private boolean _globalSection;
066 private String _globalSectionName;
067 private boolean _include;
068 private String _lineSeparator;
069 private boolean _lowerCaseOption;
070 private boolean _lowerCaseSection;
071 private boolean _multiOption;
072 private boolean _multiSection;
073 private char _pathSeparator;
074 private boolean _propertyFirstUpper;
075 private boolean _strictOperator;
076 private boolean _tree;
077 private boolean _unnamedSection;
078
079 public Config()
080 {
081 reset();
082 }
083
084 public static String getEnvironment(String name)
085 {
086 return getEnvironment(name, null);
087 }
088
089 public static String getEnvironment(String name, String defaultValue)
090 {
091 String value;
092
093 try
094 {
095 value = System.getenv(name);
096 }
097 catch (SecurityException x)
098 {
099 value = null;
100 }
101
102 return (value == null) ? defaultValue : value;
103 }
104
105 public static Config getGlobal()
106 {
107 return GLOBAL;
108 }
109
110 public static String getSystemProperty(String name)
111 {
112 return getSystemProperty(name, null);
113 }
114
115 public static String getSystemProperty(String name, String defaultValue)
116 {
117 String value;
118
119 try
120 {
121 value = System.getProperty(name);
122 }
123 catch (SecurityException x)
124 {
125 value = null;
126 }
127
128 return (value == null) ? defaultValue : value;
129 }
130
131 public boolean isEscape()
132 {
133 return _escape;
134 }
135
136 public boolean isInclude()
137 {
138 return _include;
139 }
140
141 public boolean isTree()
142 {
143 return _tree;
144 }
145
146 public void setEmptyOption(boolean value)
147 {
148 _emptyOption = value;
149 }
150
151 public void setEmptySection(boolean value)
152 {
153 _emptySection = value;
154 }
155
156 public void setEscape(boolean value)
157 {
158 _escape = value;
159 }
160
161 public Charset getFileEncoding()
162 {
163 return _fileEncoding;
164 }
165
166 public void setFileEncoding(Charset value)
167 {
168 _fileEncoding = value;
169 }
170
171 public void setGlobalSection(boolean value)
172 {
173 _globalSection = value;
174 }
175
176 public String getGlobalSectionName()
177 {
178 return _globalSectionName;
179 }
180
181 public void setGlobalSectionName(String value)
182 {
183 _globalSectionName = value;
184 }
185
186 public void setInclude(boolean value)
187 {
188 _include = value;
189 }
190
191 public String getLineSeparator()
192 {
193 return _lineSeparator;
194 }
195
196 public void setLineSeparator(String value)
197 {
198 _lineSeparator = value;
199 }
200
201 public void setLowerCaseOption(boolean value)
202 {
203 _lowerCaseOption = value;
204 }
205
206 public void setLowerCaseSection(boolean value)
207 {
208 _lowerCaseSection = value;
209 }
210
211 public void setMultiOption(boolean value)
212 {
213 _multiOption = value;
214 }
215
216 public void setMultiSection(boolean value)
217 {
218 _multiSection = value;
219 }
220
221 public boolean isEmptyOption()
222 {
223 return _emptyOption;
224 }
225
226 public boolean isEmptySection()
227 {
228 return _emptySection;
229 }
230
231 public boolean isGlobalSection()
232 {
233 return _globalSection;
234 }
235
236 public boolean isLowerCaseOption()
237 {
238 return _lowerCaseOption;
239 }
240
241 public boolean isLowerCaseSection()
242 {
243 return _lowerCaseSection;
244 }
245
246 public boolean isMultiOption()
247 {
248 return _multiOption;
249 }
250
251 public boolean isMultiSection()
252 {
253 return _multiSection;
254 }
255
256 public boolean isUnnamedSection()
257 {
258 return _unnamedSection;
259 }
260
261 public char getPathSeparator()
262 {
263 return _pathSeparator;
264 }
265
266 public void setPathSeparator(char value)
267 {
268 _pathSeparator = value;
269 }
270
271 public void setPropertyFirstUpper(boolean value)
272 {
273 _propertyFirstUpper = value;
274 }
275
276 public boolean isPropertyFirstUpper()
277 {
278 return _propertyFirstUpper;
279 }
280
281 public boolean isStrictOperator()
282 {
283 return _strictOperator;
284 }
285
286 public void setStrictOperator(boolean value)
287 {
288 _strictOperator = value;
289 }
290
291 public void setTree(boolean value)
292 {
293 _tree = value;
294 }
295
296 public void setUnnamedSection(boolean value)
297 {
298 _unnamedSection = value;
299 }
300
301 @Override public Config clone()
302 {
303 try
304 {
305 return (Config) super.clone();
306 }
307 catch (CloneNotSupportedException x)
308 {
309 throw new AssertionError(x);
310 }
311 }
312
313 public final void reset()
314 {
315 _emptyOption = getBoolean(PROP_EMPTY_OPTION, DEFAULT_EMPTY_OPTION);
316 _emptySection = getBoolean(PROP_EMPTY_SECTION, DEFAULT_EMPTY_SECTION);
317 _globalSection = getBoolean(PROP_GLOBAL_SECTION, DEFAULT_GLOBAL_SECTION);
318 _globalSectionName = getString(PROP_GLOBAL_SECTION_NAME, DEFAULT_GLOBAL_SECTION_NAME);
319 _include = getBoolean(PROP_INCLUDE, DEFAULT_INCLUDE);
320 _lowerCaseOption = getBoolean(PROP_LOWER_CASE_OPTION, DEFAULT_LOWER_CASE_OPTION);
321 _lowerCaseSection = getBoolean(PROP_LOWER_CASE_SECTION, DEFAULT_LOWER_CASE_SECTION);
322 _multiOption = getBoolean(PROP_MULTI_OPTION, DEFAULT_MULTI_OPTION);
323 _multiSection = getBoolean(PROP_MULTI_SECTION, DEFAULT_MULTI_SECTION);
324 _strictOperator = getBoolean(PROP_STRICT_OPERATOR, DEFAULT_STRICT_OPERATOR);
325 _unnamedSection = getBoolean(PROP_UNNAMED_SECTION, DEFAULT_UNNAMED_SECTION);
326 _escape = getBoolean(PROP_ESCAPE, DEFAULT_ESCAPE);
327 _pathSeparator = getChar(PROP_PATH_SEPARATOR, DEFAULT_PATH_SEPARATOR);
328 _tree = getBoolean(PROP_TREE, DEFAULT_TREE);
329 _propertyFirstUpper = getBoolean(PROP_PROPERTY_FIRST_UPPER, DEFAULT_PROPERTY_FIRST_UPPER);
330 _lineSeparator = getString(PROP_LINE_SEPARATOR, DEFAULT_LINE_SEPARATOR);
331 _fileEncoding = getCharset(PROP_FILE_ENCODING, DEFAULT_FILE_ENCODING);
332 }
333
334 private boolean getBoolean(String name, boolean defaultValue)
335 {
336 String value = getSystemProperty(KEY_PREFIX + name);
337
338 return (value == null) ? defaultValue : Boolean.parseBoolean(value);
339 }
340
341 private char getChar(String name, char defaultValue)
342 {
343 String value = getSystemProperty(KEY_PREFIX + name);
344
345 return (value == null) ? defaultValue : value.charAt(0);
346 }
347
348 private Charset getCharset(String name, Charset defaultValue)
349 {
350 String value = getSystemProperty(KEY_PREFIX + name);
351
352 return (value == null) ? defaultValue : Charset.forName(value);
353 }
354
355 private String getString(String name, String defaultValue)
356 {
357 return getSystemProperty(KEY_PREFIX + name, defaultValue);
358 }
359 }