Clover coverage report - [ini4j]
Coverage timestamp: Sze nov. 30 2005 08:31:05 CET
file stats: LOC: 134   Methods: 6
NCLOC: 98   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
IniPreferencesFactory.java 100% 93,5% 100% 95,7%
coverage coverage
 1    /*
 2    * Copyright 2005 [ini4j] Development Team
 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   
 17    package org.ini4j;
 18   
 19    import java.util.prefs.PreferencesFactory;
 20    import java.util.prefs.Preferences;
 21    import java.util.Properties;
 22    import java.io.InputStream;
 23   
 24    import java.net.URI;
 25    import java.net.URL;
 26   
 27    public class IniPreferencesFactory implements PreferencesFactory
 28    {
 29    public static final String PROPERTIES = "ini4j.properties";
 30   
 31    public static final String KEY_USER = "org.ini4j.prefs.user";
 32    public static final String KEY_SYSTEM = "org.ini4j.prefs.system";
 33   
 34    private Preferences _system;
 35    private Preferences _user;
 36   
 37  2 public synchronized Preferences systemRoot()
 38    {
 39  2 if ( _system == null )
 40    {
 41  1 _system = newIniPreferences(KEY_SYSTEM);
 42    }
 43   
 44  2 return _system;
 45    }
 46   
 47  2 public synchronized Preferences userRoot()
 48    {
 49  2 if ( _user == null )
 50    {
 51  1 _user = newIniPreferences(KEY_USER);
 52    }
 53   
 54  2 return _user;
 55    }
 56   
 57  2 protected Preferences newIniPreferences(String key)
 58    {
 59  2 Ini ini = new Ini();
 60   
 61  2 String location = getIniLocation(key);
 62   
 63  2 if ( location != null )
 64    {
 65  1 try
 66    {
 67  1 ini.load(getResourceAsStream(location));
 68    }
 69    catch (Exception x)
 70    {
 71  0 throw (IllegalArgumentException) new IllegalArgumentException().initCause(x);
 72    }
 73    }
 74   
 75  2 return new IniPreferences(ini);
 76    }
 77   
 78  2 protected String getIniLocation(String key)
 79    {
 80  2 String location = System.getProperty(key);
 81   
 82  2 if ( location == null )
 83    {
 84  1 try
 85    {
 86  1 Properties props = new Properties();
 87  1 props.load(getClass().getClassLoader().getResourceAsStream(PROPERTIES));
 88  0 location = props.getProperty(key);
 89    }
 90    catch (Exception x)
 91    {
 92    ;
 93    }
 94    }
 95   
 96  2 return location;
 97    }
 98   
 99  5 protected URL getResource(String location) throws IllegalArgumentException
 100    {
 101  5 try
 102    {
 103  5 URI uri = new URI(location);
 104  4 URL url;
 105   
 106  4 if ( uri.getScheme() == null )
 107    {
 108  3 url = getClass().getClassLoader().getResource(location);
 109    }
 110    else
 111    {
 112  1 url = uri.toURL();
 113    }
 114   
 115  4 return url;
 116    }
 117    catch (Exception x)
 118    {
 119  1 throw (IllegalArgumentException) new IllegalArgumentException().initCause(x);
 120    }
 121    }
 122   
 123  5 protected InputStream getResourceAsStream(String location) throws IllegalArgumentException
 124    {
 125  5 try
 126    {
 127  5 return getResource(location).openStream();
 128    }
 129    catch (Exception x)
 130    {
 131  1 throw (IllegalArgumentException) new IllegalArgumentException().initCause(x);
 132    }
 133    }
 134    }