Clover coverage report -
Coverage timestamp: Do Okt 21 2004 12:21:23 CEST
file stats: LOC: 1.028   Methods: 76
NCLOC: 574   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
DefaultConfiguration.java 66,9% 79,9% 84,2% 77,4%
coverage coverage
 1    /*
 2    * Configuration.java
 3    *
 4    * Created on 19. November 2002, 22:27
 5    */
 6   
 7    package org.jconfig;
 8   
 9    import java.io.Serializable;
 10    import java.util.Enumeration;
 11    import java.util.HashMap;
 12    import java.util.Iterator;
 13    import java.util.Properties;
 14    import java.util.Set;
 15    import java.util.SortedMap;
 16    import java.util.StringTokenizer;
 17    import java.util.TreeMap;
 18    import java.util.Vector;
 19    import javax.swing.event.EventListenerList;
 20   
 21    import org.jconfig.event.CategoryChangedEvent;
 22    import org.jconfig.event.CategoryListener;
 23    import org.jconfig.event.ConfigurationChangedEvent;
 24    import org.jconfig.event.ConfigurationChangedEventImpl;
 25    import org.jconfig.event.ConfigurationListener;
 26    import org.jconfig.event.PropertyChangedEvent;
 27    import org.jconfig.event.PropertyListener;
 28    import org.jconfig.utils.IncludeEntry;
 29    /**
 30    * This class is the configuration itself. The Configuration is
 31    * useful if one wants to manage multiple configurations. A single
 32    * instance of the Configuration may contain, for example, information
 33    * for one application or user.
 34    *
 35    * @author Andreas Mecky andreas.mecky@xcom.de
 36    * @author Terry Dye terry.dye@xcom.de
 37    */
 38    public class DefaultConfiguration implements Serializable,Configuration {
 39   
 40    protected static final VariableManager vm = VariableManager.getInstance();
 41    // the name of the configuration
 42    protected String configName;
 43    // a map containing all properties for each category
 44    protected HashMap categories;
 45    // the name of the default category
 46    protected String mainCategory;
 47    // The List of ConfigurationListeners
 48    private EventListenerList configurationListenerList = new EventListenerList();
 49    // flag to determine if this is a newly created configuration
 50    private boolean created = true;
 51   
 52    private String encoding;
 53   
 54    protected String baseConfigName;
 55   
 56    private boolean dirtyFlag = false;
 57   
 58    private Vector includes = new Vector();
 59   
 60  0 protected DefaultConfiguration() {
 61    }
 62    /**
 63    * The constructor that creates a new configuration
 64    * with one empty category called "general". This
 65    * category is also the default category.
 66    *
 67    * @param configName the name of the configuration
 68    */
 69  89 public DefaultConfiguration(String configName) {
 70  89 categories = new HashMap();
 71  89 this.configName = configName;
 72  89 setCategory("general", true);
 73  89 created = true;
 74    }
 75   
 76    /**
 77    * This method sets a category but it does not set
 78    * this category as default.
 79    *
 80    * @param name the name of the category
 81    */
 82  58 public void setCategory(String name) {
 83  58 setCategory(name, false);
 84    }
 85   
 86    /**
 87    * Besides setting the category, it will also set this
 88    * category as default category if main is true. It will
 89    * only set (ie create) the category if it does not exist. If you
 90    * want delete a category then use @see #removeCategory(String)
 91    *
 92    * @param name the name of the category
 93    * @param main if true then this category is the default category
 94    */
 95  120 public void setCategory(String name, boolean main) {
 96  120 if (name != null) {
 97  120 if (main) {
 98  65 mainCategory = name;
 99    }
 100  120 if (!categories.containsKey(name)) {
 101  109 Category category = new DefaultCategory(name);
 102  109 category.setConfigurationName(configName);
 103  109 category.addCategoryListener(new MyCategoryListener());
 104  109 categories.put(name, category);
 105  109 markDirty();
 106  109 category.fireCategoryChangedEvent(
 107    new ConfigurationChangedEventImpl(ConfigurationChangedEvent.CATEGORY_ADDED, category, null, null, null ));
 108  109 fireConfigurationChangedEvent(
 109    new ConfigurationChangedEventImpl(ConfigurationChangedEvent.CATEGORY_ADDED, category, null, null, null ));
 110    }
 111    }
 112    }
 113   
 114  5 public void setCategory(Category category) {
 115  5 if (!categories.containsKey(category.getCategoryName())) {
 116  3 category.setConfigurationName(configName);
 117  3 category.addCategoryListener(new MyCategoryListener());
 118  3 categories.put(category.getCategoryName(), category);
 119  3 category.fireCategoryChangedEvent(
 120    new ConfigurationChangedEventImpl(ConfigurationChangedEvent.CATEGORY_ADDED, category, null, null, null ));
 121  3 fireConfigurationChangedEvent(
 122    new ConfigurationChangedEventImpl(ConfigurationChangedEvent.CATEGORY_ADDED, category, null, null, null ));
 123    }
 124    }
 125    /**
 126    * This method returns the name of the
 127    * default category
 128    *
 129    * @return name of the default category
 130    */
 131  2 public String getMainCategoryName() {
 132  2 return mainCategory;
 133    }
 134   
 135    /**
 136    * This method returns a string array with all
 137    * category names.
 138    *
 139    * @return a string array with all category names
 140    */
 141  11 public String[] getCategoryNames() {
 142  11 return getCategoryNames(true);
 143    }
 144   
 145  14 protected String[] getCategoryNames(boolean includeParent) {
 146  14 Set allCategories = categories.keySet();
 147  14 Vector all = new Vector(allCategories);
 148  14 if ( baseConfigName != null && includeParent ) {
 149  0 Configuration cfg = ConfigurationManager.getConfiguration(baseConfigName);
 150  0 String[] parentCategories = cfg.getCategoryNames();
 151  0 for ( int i = 0 ; i < parentCategories.length ; i++) {
 152  0 if ( all.indexOf(parentCategories[i]) == -1 ) {
 153  0 all.add(parentCategories[i]);
 154    }
 155    }
 156    }
 157  14 return (String[]) all.toArray(new String[0]);
 158    }
 159   
 160    /**
 161    * This method returns the String value based on the given key.
 162    *
 163    * Implementation details: It calls getProperty(name,null,null).
 164    * It searches inside the default category for the property.
 165    *
 166    * @param key the name of the property
 167    * @return the value as String if it is found or null
 168    */
 169  14 public String getProperty(String key) {
 170  14 return getProperty(key, null, null);
 171    }
 172   
 173    /**
 174    * This method is the same as getProperty(key) but it
 175    * returns the defaultValue if the property cannot be found.
 176    *
 177    * Implementation details: It calls getProperty(key,defaultValue,null).
 178    *
 179    * @param key the name of the property
 180    * @param defaultValue the defaultValue that will be returned if the property cannot be found
 181    * @return the value as String
 182    */
 183  1 public String getProperty(String key, String defaultValue) {
 184  1 return getProperty(key, defaultValue, null);
 185    }
 186   
 187    /**
 188    * This is the real implementation. It will return the value of the property
 189    * with the specific name. First of all, it checks if the name of the category
 190    * exists. If not, then it will use the name of the default category.
 191    * The next step is that it will look for the property. If it is not found in
 192    * the category, it will look inside the default category (inheritance). If
 193    * it still cannot find the property, it will return the defaultValue
 194    *
 195    * @param key the name of the property
 196    * @param defaultValue the default value
 197    * @param categoryName the name of the category
 198    * @return the value as String
 199    */
 200  49 public String getProperty(String key,String defaultValue,String categoryName) {
 201  49 boolean isMainCat = false;
 202  49 if (key == null) {
 203  0 return defaultValue;
 204    }
 205  49 if (!categories.containsKey(categoryName)) {
 206  12 isMainCat = true;
 207  12 categoryName = mainCategory;
 208    }
 209  49 Category category = getCategory(categoryName);
 210  49 if ( category.getCategoryName().equals(mainCategory)) {
 211  24 isMainCat = true;
 212    }
 213  49 String tmp = category.getProperty(key);
 214    // property not found so look in mainCategory
 215    // if it is not already the mainCategory
 216  49 if ( tmp == null && !isMainCat) {
 217  3 category = getCategory(mainCategory);
 218  3 tmp = category.getProperty(key);
 219    }
 220  49 if ( tmp == null ) {
 221  8 if ( baseConfigName != null ) {
 222  1 Configuration cfg = ConfigurationManager.getConfiguration(baseConfigName);
 223  1 tmp = cfg.getProperty(key,defaultValue,categoryName);
 224    }
 225    else {
 226  7 tmp = defaultValue;
 227    }
 228    }
 229  49 return tmp;
 230    }
 231   
 232    /**
 233    * This method sets a property with the name and the value
 234    * in the default category. It calls setProperty(name.value,null).
 235    *
 236    * @param name the name of the property
 237    * @param value the value as String
 238    */
 239  33 public void setProperty(String name, String value) {
 240  33 setProperty(name, value, null);
 241    }
 242   
 243    /**
 244    * This method sets the value for a property for the given
 245    * category. It also raises a PropertyEvent. If the category
 246    * is null then it uses the default category.
 247    *
 248    * @param name the name of the property
 249    * @param value the value as String
 250    * @param categoryName the name of the category
 251    */
 252  215 public void setProperty(String name, String value, String categoryName) {
 253  215 if (name != null) {
 254  215 if (categoryName == null) {
 255  33 categoryName = mainCategory;
 256    }
 257  215 Category category = getCategory(categoryName);
 258  215 category.setProperty(name, value);
 259  215 markDirty();
 260  215 fireConfigurationChangedEvent(
 261    new ConfigurationChangedEventImpl(ConfigurationChangedEvent.PROPERTY_CHANGED, category, name, value,null ));
 262    }
 263    }
 264   
 265    /**
 266    * This method deletes a property from the default category.
 267    * It calls removeProperty(name,null).
 268    *
 269    * @param name the name of the property
 270    */
 271  1 public void removeProperty(String name) {
 272  1 if (name != null) {
 273  1 removeProperty(name, null);
 274    }
 275    }
 276   
 277    /**
 278    * This method deletes a property with the given name
 279    * from the specific category. If the category is null
 280    * then it will delete the property from the default category.
 281    *
 282    * @param name the name of the property
 283    * @param category the name of the category
 284    */
 285  3 public void removeProperty(String name, String category) {
 286  3 if (category == null) {
 287  1 category = mainCategory;
 288    }
 289  3 if (name != null) {
 290    //if (categories.containsKey(category)) {
 291  3 Category cat = getCategory(category);
 292  3 String tmp = cat.getProperty(name,null);
 293  3 cat.setProperty(name, null);
 294  3 fireConfigurationChangedEvent(
 295    new ConfigurationChangedEventImpl(ConfigurationChangedEvent.PROPERTY_REMOVED,cat, name, tmp,null ));
 296  3 markDirty();
 297    }
 298   
 299    }
 300   
 301    /**
 302    * The method returns the number of categories inside this configuration.
 303    *
 304    * @return the number of categories
 305    */
 306  4 public int getNumberOfCategories() {
 307  4 return getCategoryNames().length;
 308    }
 309   
 310    /**
 311    * This method deletes a category with all its properties
 312    *
 313    * @param category the name of the category
 314    */
 315  1 public void removeCategory(String category) {
 316  1 if (categories.containsKey(category)) {
 317  1 Category cat = getCategory(category);
 318  1 categories.remove(category);
 319  1 cat.fireCategoryChangedEvent(
 320    new ConfigurationChangedEventImpl(ConfigurationChangedEvent.CATEGORY_REMOVED, cat, null, null, null));
 321  1 fireConfigurationChangedEvent(
 322    new ConfigurationChangedEventImpl(ConfigurationChangedEvent.CATEGORY_REMOVED,cat, null, null,null ));
 323  1 cat = null;
 324  1 markDirty();
 325    }
 326    }
 327   
 328  1 public Properties getProperties(String category) {
 329  1 return getProperties(category,true);
 330    }
 331   
 332  1 public Properties getProperties() {
 333  1 return getProperties(mainCategory,true);
 334    }
 335   
 336  30 protected Properties getProperties(String category,boolean includeParent) {
 337  30 Category cat = getCategory(category);
 338  30 Properties props = new Properties();
 339  30 if ( cat != null ) {
 340  30 props = (Properties)cat.getProperties().clone();
 341    }
 342  30 if ( baseConfigName != null && includeParent ) {
 343  1 Configuration cfg = ConfigurationManager.getConfiguration(baseConfigName);
 344  1 Properties parentProps = cfg.getProperties(category);
 345  1 Enumeration num = parentProps.keys();
 346  1 while ( num.hasMoreElements() ) {
 347  5 String name = (String)num.nextElement();
 348  5 if ( !props.contains(name) ) {
 349  5 props.setProperty(name, (String)parentProps.get(name));
 350    }
 351    }
 352    }
 353  30 return props;
 354    }
 355   
 356    /**
 357    * This method returns all the names of the properties
 358    * for the specific category
 359    *
 360    * @param category the name of the category
 361    * @return the names as string array
 362    */
 363  1 public String[] getPropertyNames(String category) {
 364  1 if ( containsCategory(category) ) {
 365    // FIXME: include parent properties
 366  0 Properties properties = getProperties(category);
 367  0 if (properties != null) {
 368  0 Enumeration en = properties.propertyNames();
 369  0 Vector keys = new Vector();
 370  0 while ( en.hasMoreElements() ) {
 371  0 String nm = (String)en.nextElement();
 372  0 keys.add(nm);
 373    }
 374    /*
 375    Set keys = properties.keySet();
 376    */
 377  0 return (String[]) keys.toArray(new String[0]);
 378    }
 379    }
 380  1 return null;
 381    }
 382   
 383    /**
 384    * This method sets a variable inside the configuration.
 385    *
 386    * @param name the name of the variable
 387    * @param value the value of the variable
 388    */
 389  79 public void setVariable(String name, String value) {
 390  79 if (name != null && value != null) {
 391  79 vm.addVariable(name, value,configName);
 392    }
 393    }
 394   
 395    /**
 396    * It returns a HashMap with all variables with the
 397    * variable name as key
 398    *
 399    * @return a HashMap with all variables
 400    */
 401  3 public HashMap getVariables() {
 402  3 return vm.getVariables(configName);
 403    }
 404   
 405  14 public String getVariable(String name) {
 406  14 return vm.getVariable(configName, name);
 407    }
 408   
 409   
 410    /**
 411    * @param name
 412    * @param defaultValue
 413    * @return
 414    */
 415  4 public int getIntProperty(String name, int defaultValue) {
 416  4 return getIntProperty(name, defaultValue, mainCategory);
 417    }
 418   
 419    /**
 420    * @param name
 421    * @param defaultValue
 422    * @param category
 423    * @return
 424    */
 425  7 public int getIntProperty(String name, int defaultValue, String category) {
 426  7 String value = getProperty(name,null,category);
 427  7 if (value == null) {
 428  0 return defaultValue;
 429    } else {
 430  7 try {
 431  7 return Integer.parseInt(vm.replaceVariables(value,configName));
 432    } catch (NumberFormatException nfe) {
 433  0 return defaultValue;
 434    }
 435    }
 436    }
 437   
 438    /**
 439    * Wrapper method to keep API simple
 440    *
 441    * @see Category#getBooleanProperty(String, boolean)
 442    * @param name
 443    * @param defaultValue
 444    * @return
 445    */
 446  2 public boolean getBooleanProperty(String name, boolean defaultValue) {
 447  2 return getBooleanProperty(name, defaultValue, mainCategory);
 448    }
 449   
 450    /**
 451    * Wrapper method to Category method
 452    *
 453    * @see Category#getBooleanProperty(String, boolean, String)
 454    * @param defaultValue
 455    * @param categoryName
 456    * @return
 457    */
 458  5 public boolean getBooleanProperty(String name,boolean defaultValue,String categoryName) {
 459  5 String value = getProperty(name,null,categoryName);
 460  5 if ( value == null ) {
 461  1