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    @SuppressWarnings("PMD.ExcessivePublicCount")
023    public class Config implements Cloneable, Serializable
024    {
025        public static final String KEY_PREFIX = "org.ini4j.config.";
026        public static final String PROP_EMPTY_OPTION = "emptyOption";
027        public static final String PROP_EMPTY_SECTION = "emptySection";
028        public static final String PROP_GLOBAL_SECTION = "globalSection";
029        public static final String PROP_GLOBAL_SECTION_NAME = "globalSectionName";
030        public static final String PROP_INCLUDE = "include";
031        public static final String PROP_LOWER_CASE_OPTION = "lowerCaseOption";
032        public static final String PROP_LOWER_CASE_SECTION = "lowerCaseSection";
033        public static final String PROP_MULTI_OPTION = "multiOption";
034        public static final String PROP_MULTI_SECTION = "multiSection";
035        public static final String PROP_STRICT_OPERATOR = "strictOperator";
036        public static final String PROP_UNNAMED_SECTION = "unnamedSection";
037        public static final String PROP_ESCAPE = "escape";
038        public static final String PROP_PATH_SEPARATOR = "pathSeparator";
039        public static final String PROP_TREE = "tree";
040        public static final String PROP_PROPERTY_FIRST_UPPER = "propertyFirstUpper";
041        public static final String PROP_FILE_ENCODING = "fileEncoding";
042        public static final String PROP_LINE_SEPARATOR = "lineSeparator";
043        public static final String PROP_COMMENT = "comment";
044        public static final String PROP_HEADER_COMMENT = "headerComment";
045        public static final boolean DEFAULT_EMPTY_OPTION = false;
046        public static final boolean DEFAULT_EMPTY_SECTION = false;
047        public static final boolean DEFAULT_GLOBAL_SECTION = false;
048        public static final String DEFAULT_GLOBAL_SECTION_NAME = "?";
049        public static final boolean DEFAULT_INCLUDE = false;
050        public static final boolean DEFAULT_LOWER_CASE_OPTION = false;
051        public static final boolean DEFAULT_LOWER_CASE_SECTION = false;
052        public static final boolean DEFAULT_MULTI_OPTION = true;
053        public static final boolean DEFAULT_MULTI_SECTION = false;
054        public static final boolean DEFAULT_STRICT_OPERATOR = false;
055        public static final boolean DEFAULT_UNNAMED_SECTION = false;
056        public static final boolean DEFAULT_ESCAPE = true;
057        public static final boolean DEFAULT_TREE = true;
058        public static final boolean DEFAULT_PROPERTY_FIRST_UPPER = false;
059        public static final boolean DEFAULT_COMMENT = true;
060        public static final boolean DEFAULT_HEADER_COMMENT = true;
061        public static final char DEFAULT_PATH_SEPARATOR = '/';
062        public static final String DEFAULT_LINE_SEPARATOR = getSystemProperty("line.separator", "\n");
063        public static final Charset DEFAULT_FILE_ENCODING = Charset.forName("UTF-8");
064        private static final Config GLOBAL = new Config();
065        private static final long serialVersionUID = 2865793267410367814L;
066        private boolean _comment;
067        private boolean _emptyOption;
068        private boolean _emptySection;
069        private boolean _escape;
070        private Charset _fileEncoding;
071        private boolean _globalSection;
072        private String _globalSectionName;
073        private boolean _headerComment;
074        private boolean _include;
075        private String _lineSeparator;
076        private boolean _lowerCaseOption;
077        private boolean _lowerCaseSection;
078        private boolean _multiOption;
079        private boolean _multiSection;
080        private char _pathSeparator;
081        private boolean _propertyFirstUpper;
082        private boolean _strictOperator;
083        private boolean _tree;
084        private boolean _unnamedSection;
085    
086        public Config()
087        {
088            reset();
089        }
090    
091        public static String getEnvironment(String name)
092        {
093            return getEnvironment(name, null);
094        }
095    
096        public static String getEnvironment(String name, String defaultValue)
097        {
098            String value;
099    
100            try
101            {
102                value = System.getenv(name);
103            }
104            catch (SecurityException x)
105            {
106                value = null;
107            }
108    
109            return (value == null) ? defaultValue : value;
110        }
111    
112        public static Config getGlobal()
113        {
114            return GLOBAL;
115        }
116    
117        public static String getSystemProperty(String name)
118        {
119            return getSystemProperty(name, null);
120        }
121    
122        public static String getSystemProperty(String name, String defaultValue)
123        {
124            String value;
125    
126            try
127            {
128                value = System.getProperty(name);
129            }
130            catch (SecurityException x)
131            {
132                value = null;
133            }
134    
135            return (value == null) ? defaultValue : value;
136        }
137    
138        public void setComment(boolean value)
139        {
140            _comment = value;
141        }
142    
143        public boolean isEscape()
144        {
145            return _escape;
146        }
147    
148        public boolean isInclude()
149        {
150            return _include;
151        }
152    
153        public boolean isTree()
154        {
155            return _tree;
156        }
157    
158        public void setEmptyOption(boolean value)
159        {
160            _emptyOption = value;
161        }
162    
163        public void setEmptySection(boolean value)
164        {
165            _emptySection = value;
166        }
167    
168        public void setEscape(boolean value)
169        {
170            _escape = value;
171        }
172    
173        public Charset getFileEncoding()
174        {
175            return _fileEncoding;
176        }
177    
178        public void setFileEncoding(Charset value)
179        {
180            _fileEncoding = value;
181        }
182    
183        public void setGlobalSection(boolean value)
184        {
185            _globalSection = value;
186        }
187    
188        public String getGlobalSectionName()
189        {
190            return _globalSectionName;
191        }
192    
193        public void setGlobalSectionName(String value)
194        {
195            _globalSectionName = value;
196        }
197    
198        public void setHeaderComment(boolean value)
199        {
200            _headerComment = value;
201        }
202    
203        public void setInclude(boolean value)
204        {
205            _include = value;
206        }
207    
208        public String getLineSeparator()
209        {
210            return _lineSeparator;
211        }
212    
213        public void setLineSeparator(String value)
214        {
215            _lineSeparator = value;
216        }
217    
218        public void setLowerCaseOption(boolean value)
219        {
220            _lowerCaseOption = value;
221        }
222    
223        public void setLowerCaseSection(boolean value)
224        {
225            _lowerCaseSection = value;
226        }
227    
228        public void setMultiOption(boolean value)
229        {
230            _multiOption = value;
231        }
232    
233        public void setMultiSection(boolean value)
234        {
235            _multiSection = value;
236        }
237    
238        public boolean isEmptyOption()
239        {
240            return _emptyOption;
241        }
242    
243        public boolean isEmptySection()
244        {
245            return _emptySection;
246        }
247    
248        public boolean isGlobalSection()
249        {
250            return _globalSection;
251        }
252    
253        public boolean isLowerCaseOption()
254        {
255            return _lowerCaseOption;
256        }
257    
258        public boolean isLowerCaseSection()
259        {
260            return _lowerCaseSection;
261        }
262    
263        public boolean isMultiOption()
264        {
265            return _multiOption;
266        }
267    
268        public boolean isMultiSection()
269        {
270            return _multiSection;
271        }
272    
273        public boolean isUnnamedSection()
274        {
275            return _unnamedSection;
276        }
277    
278        public char getPathSeparator()
279        {
280            return _pathSeparator;
281        }
282    
283        public void setPathSeparator(char value)
284        {
285            _pathSeparator = value;
286        }
287    
288        public void setPropertyFirstUpper(boolean value)
289        {
290            _propertyFirstUpper = value;
291        }
292    
293        public boolean isPropertyFirstUpper()
294        {
295            return _propertyFirstUpper;
296        }
297    
298        public boolean isStrictOperator()
299        {
300            return _strictOperator;
301        }
302    
303        public void setStrictOperator(boolean value)
304        {
305            _strictOperator = value;
306        }
307    
308        public boolean isComment()
309        {
310            return _comment;
311        }
312    
313        public boolean isHeaderComment()
314        {
315            return _headerComment;
316        }
317    
318        public void setTree(boolean value)
319        {
320            _tree = value;
321        }
322    
323        public void setUnnamedSection(boolean value)
324        {
325            _unnamedSection = value;
326        }
327    
328        @Override public Config clone()
329        {
330            try
331            {
332                return (Config) super.clone();
333            }
334            catch (CloneNotSupportedException x)
335            {
336                throw new AssertionError(x);
337            }
338        }
339    
340        public final void reset()
341        {
342            _emptyOption = getBoolean(PROP_EMPTY_OPTION, DEFAULT_EMPTY_OPTION);
343            _emptySection = getBoolean(PROP_EMPTY_SECTION, DEFAULT_EMPTY_SECTION);
344            _globalSection = getBoolean(PROP_GLOBAL_SECTION, DEFAULT_GLOBAL_SECTION);
345            _globalSectionName = getString(PROP_GLOBAL_SECTION_NAME, DEFAULT_GLOBAL_SECTION_NAME);
346            _include = getBoolean(PROP_INCLUDE, DEFAULT_INCLUDE);
347            _lowerCaseOption = getBoolean(PROP_LOWER_CASE_OPTION, DEFAULT_LOWER_CASE_OPTION);
348            _lowerCaseSection = getBoolean(PROP_LOWER_CASE_SECTION, DEFAULT_LOWER_CASE_SECTION);
349            _multiOption = getBoolean(PROP_MULTI_OPTION, DEFAULT_MULTI_OPTION);
350            _multiSection = getBoolean(PROP_MULTI_SECTION, DEFAULT_MULTI_SECTION);
351            _strictOperator = getBoolean(PROP_STRICT_OPERATOR, DEFAULT_STRICT_OPERATOR);
352            _unnamedSection = getBoolean(PROP_UNNAMED_SECTION, DEFAULT_UNNAMED_SECTION);
353            _escape = getBoolean(PROP_ESCAPE, DEFAULT_ESCAPE);
354            _pathSeparator = getChar(PROP_PATH_SEPARATOR, DEFAULT_PATH_SEPARATOR);
355            _tree = getBoolean(PROP_TREE, DEFAULT_TREE);
356            _propertyFirstUpper = getBoolean(PROP_PROPERTY_FIRST_UPPER, DEFAULT_PROPERTY_FIRST_UPPER);
357            _lineSeparator = getString(PROP_LINE_SEPARATOR, DEFAULT_LINE_SEPARATOR);
358            _fileEncoding = getCharset(PROP_FILE_ENCODING, DEFAULT_FILE_ENCODING);
359            _comment = getBoolean(PROP_COMMENT, DEFAULT_COMMENT);
360            _headerComment = getBoolean(PROP_HEADER_COMMENT, DEFAULT_HEADER_COMMENT);
361        }
362    
363        private boolean getBoolean(String name, boolean defaultValue)
364        {
365            String value = getSystemProperty(KEY_PREFIX + name);
366    
367            return (value == null) ? defaultValue : Boolean.parseBoolean(value);
368        }
369    
370        private char getChar(String name, char defaultValue)
371        {
372            String value = getSystemProperty(KEY_PREFIX + name);
373    
374            return (value == null) ? defaultValue : value.charAt(0);
375        }
376    
377        private Charset getCharset(String name, Charset defaultValue)
378        {
379            String value = getSystemProperty(KEY_PREFIX + name);
380    
381            return (value == null) ? defaultValue : Charset.forName(value);
382        }
383    
384        private String getString(String name, String defaultValue)
385        {
386            return getSystemProperty(KEY_PREFIX + name, defaultValue);
387        }
388    }