Coverage Report - org.ini4j.Config
 
Classes in this File Line Coverage Branch Coverage Complexity
Config
93%
104/111
90%
9/10
1.192
 
 1  
 /*
 2  
  * Copyright 2005,2009 Ivan SZKIBA
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.ini4j;
 17  
 
 18  
 import java.io.Serializable;
 19  
 
 20  
 import java.nio.charset.Charset;
 21  
 
 22  0
 @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  1
     public static final String DEFAULT_LINE_SEPARATOR = getSystemProperty("line.separator", "\n");
 65  1
     public static final Charset DEFAULT_FILE_ENCODING = Charset.forName("UTF-8");
 66  1
     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  30
     {
 91  30
         reset();
 92  30
     }
 93  
 
 94  
     public static String getEnvironment(String name)
 95  
     {
 96  2
         return getEnvironment(name, null);
 97  
     }
 98  
 
 99  
     public static String getEnvironment(String name, String defaultValue)
 100  
     {
 101  
         String value;
 102  
 
 103  
         try
 104  
         {
 105  2
             value = System.getenv(name);
 106  
         }
 107  0
         catch (SecurityException x)
 108  
         {
 109  0
             value = null;
 110  2
         }
 111  
 
 112  2
         return (value == null) ? defaultValue : value;
 113  
     }
 114  
 
 115  
     public static Config getGlobal()
 116  
     {
 117  343
         return GLOBAL;
 118  
     }
 119  
 
 120  
     public static String getSystemProperty(String name)
 121  
     {
 122  547
         return getSystemProperty(name, null);
 123  
     }
 124  
 
 125  
     public static String getSystemProperty(String name, String defaultValue)
 126  
     {
 127  
         String value;
 128  
 
 129  
         try
 130  
         {
 131  609
             value = System.getProperty(name);
 132  
         }
 133  0
         catch (SecurityException x)
 134  
         {
 135  0
             value = null;
 136  609
         }
 137  
 
 138  609
         return (value == null) ? defaultValue : value;
 139  
     }
 140  
 
 141  
     public void setComment(boolean value)
 142  
     {
 143  7
         _comment = value;
 144  7
     }
 145  
 
 146  
     public boolean isEscape()
 147  
     {
 148  8574
         return _escape;
 149  
     }
 150  
 
 151  
     public boolean isEscapeNewline()
 152  
     {
 153  4079
         return _escapeNewline;
 154  
     }
 155  
 
 156  
     public boolean isInclude()
 157  
     {
 158  4014
         return _include;
 159  
     }
 160  
 
 161  
     public boolean isTree()
 162  
     {
 163  562
         return _tree;
 164  
     }
 165  
 
 166  
     public void setEmptyOption(boolean value)
 167  
     {
 168  69
         _emptyOption = value;
 169  69
     }
 170  
 
 171  
     public void setEmptySection(boolean value)
 172  
     {
 173  22
         _emptySection = value;
 174  22
     }
 175  
 
 176  
     public void setEscape(boolean value)
 177  
     {
 178  60
         _escape = value;
 179  60
     }
 180  
 
 181  
     public void setEscapeNewline(boolean value)
 182  
     {
 183  12
         _escapeNewline = value;
 184  12
     }
 185  
 
 186  
     public Charset getFileEncoding()
 187  
     {
 188  95
         return _fileEncoding;
 189  
     }
 190  
 
 191  
     public void setFileEncoding(Charset value)
 192  
     {
 193  35
         _fileEncoding = value;
 194  35
     }
 195  
 
 196  
     public void setGlobalSection(boolean value)
 197  
     {
 198  32
         _globalSection = value;
 199  32
     }
 200  
 
 201  
     public String getGlobalSectionName()
 202  
     {
 203  17
         return _globalSectionName;
 204  
     }
 205  
 
 206  
     public void setGlobalSectionName(String value)
 207  
     {
 208  3
         _globalSectionName = value;
 209  3
     }
 210  
 
 211  
     public void setHeaderComment(boolean value)
 212  
     {
 213  5
         _headerComment = value;
 214  5
     }
 215  
 
 216  
     public void setInclude(boolean value)
 217  
     {
 218  5
         _include = value;
 219  5
     }
 220  
 
 221  
     public String getLineSeparator()
 222  
     {
 223  3361
         return _lineSeparator;
 224  
     }
 225  
 
 226  
     public void setLineSeparator(String value)
 227  
     {
 228  21
         _lineSeparator = value;
 229  21
     }
 230  
 
 231  
     public void setLowerCaseOption(boolean value)
 232  
     {
 233  34
         _lowerCaseOption = value;
 234  34
     }
 235  
 
 236  
     public void setLowerCaseSection(boolean value)
 237  
     {
 238  33
         _lowerCaseSection = value;
 239  33
     }
 240  
 
 241  
     public void setMultiOption(boolean value)
 242  
     {
 243  65
         _multiOption = value;
 244  65
     }
 245  
 
 246  
     public void setMultiSection(boolean value)
 247  
     {
 248  33
         _multiSection = value;
 249  33
     }
 250  
 
 251  
     public boolean isEmptyOption()
 252  
     {
 253  208
         return _emptyOption;
 254  
     }
 255  
 
 256  
     public boolean isEmptySection()
 257  
     {
 258  60
         return _emptySection;
 259  
     }
 260  
 
 261  
     public boolean isGlobalSection()
 262  
     {
 263  124
         return _globalSection;
 264  
     }
 265  
 
 266  
     public boolean isLowerCaseOption()
 267  
     {
 268  3410
         return _lowerCaseOption;
 269  
     }
 270  
 
 271  
     public boolean isLowerCaseSection()
 272  
     {
 273  572
         return _lowerCaseSection;
 274  
     }
 275  
 
 276  
     public boolean isMultiOption()
 277  
     {
 278  3597
         return _multiOption;
 279  
     }
 280  
 
 281  
     public boolean isMultiSection()
 282  
     {
 283  548
         return _multiSection;
 284  
     }
 285  
 
 286  
     public boolean isUnnamedSection()
 287  
     {
 288  13
         return _unnamedSection;
 289  
     }
 290  
 
 291  
     public char getPathSeparator()
 292  
     {
 293  1730
         return _pathSeparator;
 294  
     }
 295  
 
 296  
     public void setPathSeparator(char value)
 297  
     {
 298  31
         _pathSeparator = value;
 299  31
     }
 300  
 
 301  
     public void setPropertyFirstUpper(boolean value)
 302  
     {
 303  3
         _propertyFirstUpper = value;
 304  3
     }
 305  
 
 306  
     public boolean isPropertyFirstUpper()
 307  
     {
 308  1490
         return _propertyFirstUpper;
 309  
     }
 310  
 
 311  
     public boolean isStrictOperator()
 312  
     {
 313  555
         return _strictOperator;
 314  
     }
 315  
 
 316  
     public void setStrictOperator(boolean value)
 317  
     {
 318  25
         _strictOperator = value;
 319  25
     }
 320  
 
 321  
     public boolean isComment()
 322  
     {
 323  1104
         return _comment;
 324  
     }
 325  
 
 326  
     public boolean isHeaderComment()
 327  
     {
 328  135
         return _headerComment;
 329  
     }
 330  
 
 331  
     public void setTree(boolean value)
 332  
     {
 333  3
         _tree = value;
 334  3
     }
 335  
 
 336  
     public void setUnnamedSection(boolean value)
 337  
     {
 338  4
         _unnamedSection = value;
 339  4
     }
 340  
 
 341  
     @Override public Config clone()
 342  
     {
 343  
         try
 344  
         {
 345  104
             return (Config) super.clone();
 346  
         }
 347  0
         catch (CloneNotSupportedException x)
 348  
         {
 349  0
             throw new AssertionError(x);
 350  
         }
 351  
     }
 352  
 
 353  
     public final void reset()
 354  
     {
 355  30
         _emptyOption = getBoolean(PROP_EMPTY_OPTION, DEFAULT_EMPTY_OPTION);
 356  30
         _emptySection = getBoolean(PROP_EMPTY_SECTION, DEFAULT_EMPTY_SECTION);
 357  30
         _globalSection = getBoolean(PROP_GLOBAL_SECTION, DEFAULT_GLOBAL_SECTION);
 358  30
         _globalSectionName = getString(PROP_GLOBAL_SECTION_NAME, DEFAULT_GLOBAL_SECTION_NAME);
 359  30
         _include = getBoolean(PROP_INCLUDE, DEFAULT_INCLUDE);
 360  30
         _lowerCaseOption = getBoolean(PROP_LOWER_CASE_OPTION, DEFAULT_LOWER_CASE_OPTION);
 361  30
         _lowerCaseSection = getBoolean(PROP_LOWER_CASE_SECTION, DEFAULT_LOWER_CASE_SECTION);
 362  30
         _multiOption = getBoolean(PROP_MULTI_OPTION, DEFAULT_MULTI_OPTION);
 363  30
         _multiSection = getBoolean(PROP_MULTI_SECTION, DEFAULT_MULTI_SECTION);
 364  30
         _strictOperator = getBoolean(PROP_STRICT_OPERATOR, DEFAULT_STRICT_OPERATOR);
 365  30
         _unnamedSection = getBoolean(PROP_UNNAMED_SECTION, DEFAULT_UNNAMED_SECTION);
 366  30
         _escape = getBoolean(PROP_ESCAPE, DEFAULT_ESCAPE);
 367  30
         _escapeNewline = getBoolean(PROP_ESCAPE_NEWLINE, DEFAULT_ESCAPE_NEWLINE);
 368  30
         _pathSeparator = getChar(PROP_PATH_SEPARATOR, DEFAULT_PATH_SEPARATOR);
 369  30
         _tree = getBoolean(PROP_TREE, DEFAULT_TREE);
 370  30
         _propertyFirstUpper = getBoolean(PROP_PROPERTY_FIRST_UPPER, DEFAULT_PROPERTY_FIRST_UPPER);
 371  30
         _lineSeparator = getString(PROP_LINE_SEPARATOR, DEFAULT_LINE_SEPARATOR);
 372  30
         _fileEncoding = getCharset(PROP_FILE_ENCODING, DEFAULT_FILE_ENCODING);
 373  30
         _comment = getBoolean(PROP_COMMENT, DEFAULT_COMMENT);
 374  30
         _headerComment = getBoolean(PROP_HEADER_COMMENT, DEFAULT_HEADER_COMMENT);
 375  30
     }
 376  
 
 377  
     private boolean getBoolean(String name, boolean defaultValue)
 378  
     {
 379  480
         String value = getSystemProperty(KEY_PREFIX + name);
 380  
 
 381  480
         return (value == null) ? defaultValue : Boolean.parseBoolean(value);
 382  
     }
 383  
 
 384  
     private char getChar(String name, char defaultValue)
 385  
     {
 386  30
         String value = getSystemProperty(KEY_PREFIX + name);
 387  
 
 388  30
         return (value == null) ? defaultValue : value.charAt(0);
 389  
     }
 390  
 
 391  
     private Charset getCharset(String name, Charset defaultValue)
 392  
     {
 393  30
         String value = getSystemProperty(KEY_PREFIX + name);
 394  
 
 395  30
         return (value == null) ? defaultValue : Charset.forName(value);
 396  
     }
 397  
 
 398  
     private String getString(String name, String defaultValue)
 399  
     {
 400  60
         return getSystemProperty(KEY_PREFIX + name, defaultValue);
 401  
     }
 402  
 }