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    public class Config implements Cloneable
019    {
020        public static final String KEY_PREFIX = "org.ini4j.config.";
021        public static final String PROP_EMPTY_OPTION = "emptyOption";
022        public static final String PROP_GLOBAL_SECTION = "globalSection";
023        public static final String PROP_GLOBAL_SECTION_NAME = "globalSectionName";
024        public static final String PROP_INCLUDE = "include";
025        public static final String PROP_LOWER_CASE_OPTION = "lowerCaseOption";
026        public static final String PROP_LOWER_CASE_SECTION = "lowerCaseSection";
027        public static final String PROP_MULTI_OPTION = "multiOption";
028        public static final String PROP_MULTI_SECTION = "multiSection";
029        public static final String PROP_STRICT_OPERATOR = "strictOperator";
030        public static final String PROP_UNNAMED_SECTION = "unnamedSection";
031        public static final String PROP_ESCAPE = "escape";
032        public static final boolean DEFAULT_EMPTY_OPTION = false;
033        public static final boolean DEFAULT_GLOBAL_SECTION = false;
034        public static final String DEFAULT_GLOBAL_SECTION_NAME = "?";
035        public static final boolean DEFAULT_INCLUDE = false;
036        public static final boolean DEFAULT_LOWER_CASE_OPTION = false;
037        public static final boolean DEFAULT_LOWER_CASE_SECTION = false;
038        public static final boolean DEFAULT_MULTI_OPTION = true;
039        public static final boolean DEFAULT_MULTI_SECTION = false;
040        public static final boolean DEFAULT_STRICT_OPERATOR = false;
041        public static final boolean DEFAULT_UNNAMED_SECTION = false;
042        public static final boolean DEFAULT_ESCAPE = true;
043        private static final Config GLOBAL = new Config();
044        private boolean _emptyOption;
045        private boolean _escape;
046        private boolean _globalSection;
047        private String _globalSectionName;
048        private boolean _include;
049        private boolean _lowerCaseOption;
050        private boolean _lowerCaseSection;
051        private boolean _multiOption;
052        private boolean _multiSection;
053        private boolean _strictOperator;
054        private boolean _unnamedSection;
055    
056        public Config()
057        {
058            reset();
059        }
060    
061        public static Config getGlobal()
062        {
063            return GLOBAL;
064        }
065    
066        public boolean isEscape()
067        {
068            return _escape;
069        }
070    
071        public boolean isInclude()
072        {
073            return _include;
074        }
075    
076        public void setEmptyOption(boolean value)
077        {
078            _emptyOption = value;
079        }
080    
081        public void setEscape(boolean value)
082        {
083            _escape = value;
084        }
085    
086        public void setGlobalSection(boolean value)
087        {
088            _globalSection = value;
089        }
090    
091        public String getGlobalSectionName()
092        {
093            return _globalSectionName;
094        }
095    
096        public void setGlobalSectionName(String value)
097        {
098            _globalSectionName = value;
099        }
100    
101        public void setInclude(boolean value)
102        {
103            _include = value;
104        }
105    
106        public void setLowerCaseOption(boolean value)
107        {
108            _lowerCaseOption = value;
109        }
110    
111        public void setLowerCaseSection(boolean value)
112        {
113            _lowerCaseSection = value;
114        }
115    
116        public void setMultiOption(boolean value)
117        {
118            _multiOption = value;
119        }
120    
121        public void setMultiSection(boolean value)
122        {
123            _multiSection = value;
124        }
125    
126        public boolean isEmptyOption()
127        {
128            return _emptyOption;
129        }
130    
131        public boolean isGlobalSection()
132        {
133            return _globalSection;
134        }
135    
136        public boolean isLowerCaseOption()
137        {
138            return _lowerCaseOption;
139        }
140    
141        public boolean isLowerCaseSection()
142        {
143            return _lowerCaseSection;
144        }
145    
146        public boolean isMultiOption()
147        {
148            return _multiOption;
149        }
150    
151        public boolean isMultiSection()
152        {
153            return _multiSection;
154        }
155    
156        public boolean isUnnamedSection()
157        {
158            return _unnamedSection;
159        }
160    
161        public boolean isStrictOperator()
162        {
163            return _strictOperator;
164        }
165    
166        public void setStrictOperator(boolean value)
167        {
168            _strictOperator = value;
169        }
170    
171        public void setUnnamedSection(boolean value)
172        {
173            _unnamedSection = value;
174        }
175    
176        @Override public Config clone()
177        {
178            try
179            {
180                return (Config) super.clone();
181            }
182            catch (CloneNotSupportedException x)
183            {
184                throw new AssertionError(x);
185            }
186        }
187    
188        public final void reset()
189        {
190            _emptyOption = getBoolean(PROP_EMPTY_OPTION, DEFAULT_EMPTY_OPTION);
191            _globalSection = getBoolean(PROP_GLOBAL_SECTION, DEFAULT_GLOBAL_SECTION);
192            _globalSectionName = getString(PROP_GLOBAL_SECTION_NAME, DEFAULT_GLOBAL_SECTION_NAME);
193            _include = getBoolean(PROP_INCLUDE, DEFAULT_INCLUDE);
194            _lowerCaseOption = getBoolean(PROP_LOWER_CASE_OPTION, DEFAULT_LOWER_CASE_OPTION);
195            _lowerCaseSection = getBoolean(PROP_LOWER_CASE_SECTION, DEFAULT_LOWER_CASE_SECTION);
196            _multiOption = getBoolean(PROP_MULTI_OPTION, DEFAULT_MULTI_OPTION);
197            _multiSection = getBoolean(PROP_MULTI_SECTION, DEFAULT_MULTI_SECTION);
198            _strictOperator = getBoolean(PROP_STRICT_OPERATOR, DEFAULT_STRICT_OPERATOR);
199            _unnamedSection = getBoolean(PROP_UNNAMED_SECTION, DEFAULT_UNNAMED_SECTION);
200            _escape = getBoolean(PROP_ESCAPE, DEFAULT_ESCAPE);
201        }
202    
203        private boolean getBoolean(String name, boolean defaultValue)
204        {
205            String key = KEY_PREFIX + name;
206    
207            return System.getProperties().containsKey(key) ? Boolean.getBoolean(key) : defaultValue;
208        }
209    
210        private String getString(String name, String defaultValue)
211        {
212            String key = KEY_PREFIX + name;
213    
214            return System.getProperties().containsKey(key) ? System.getProperty(key) : defaultValue;
215        }
216    }