Clover coverage report -
Coverage timestamp: Do Okt 21 2004 12:21:23 CEST
file stats: LOC: 379   Methods: 15
NCLOC: 288   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
NestedConfiguration.java 75,5% 83,7% 80% 80,9%
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.Collection;
 11    import java.util.Iterator;
 12    import java.util.StringTokenizer;
 13    import java.util.Vector;
 14    import java.util.Set;
 15    import java.util.SortedMap;
 16    import javax.swing.event.EventListenerList;
 17   
 18    import org.jconfig.event.ConfigurationChangedEvent;
 19    import org.jconfig.event.ConfigurationChangedEventImpl;
 20    /**
 21    * This class is the configuration itself. The Configuration is
 22    * useful if one wants to manage multiple configurations. A single
 23    * instance of the Configuration may contain, for example, information
 24    * for one application or user.
 25    *
 26    * @author Andreas Mecky andreas.mecky@xcom.de
 27    * @author Terry Dye terry.dye@xcom.de
 28    */
 29    public class NestedConfiguration extends DefaultConfiguration implements Serializable, Configuration {
 30   
 31    private static final VariableManager vm = VariableManager.getInstance();
 32    // The List of ConfigurationListeners
 33    private EventListenerList configurationListenerList = new EventListenerList();
 34   
 35  0 protected NestedConfiguration() {
 36    }
 37   
 38    /**
 39    * The constructor that creates a new configuration
 40    * with one empty category called "general". This
 41    * category is also the default category.
 42    *
 43    * @param configName the name of the configuration
 44    */
 45  26 public NestedConfiguration(String configName) {
 46  26 super(configName);
 47    }
 48   
 49  5 public String[] getCategoryNames() {
 50  5 Set allCategories = categories.keySet();
 51  5 Vector all = new Vector();
 52  5 Iterator it = allCategories.iterator();
 53  5 while ( it.hasNext() ) {
 54  24 String name = (String)it.next();
 55  24 addCategoryName(all,name);
 56    }
 57  5 if ( baseConfigName != null ) {
 58  2 Configuration cfg = ConfigurationManager.getConfiguration(baseConfigName);
 59  2 String[] parentCategories = cfg.getCategoryNames();
 60  2 for ( int i = 0 ; i < parentCategories.length ; i++) {
 61  14 if ( all.indexOf(parentCategories[i]) == -1 ) {
 62  8 all.add(parentCategories[i]);
 63    }
 64    }
 65    }
 66  5 return (String[]) all.toArray(new String[0]);
 67    }
 68   
 69  32 private void addCategoryName(Vector all,String name) {
 70  32 all.add(name);
 71  32 NestedCategory myCat = (NestedCategory)getCategory(name);
 72  32 Collection children = myCat.getChildCategories();
 73  32 if ( children.size() > 0 ) {
 74  8 Iterator it = children.iterator();
 75  8 while ( it.hasNext() ) {
 76  8 NestedCategory nc = (NestedCategory)it.next();
 77  8 String cp = name + "/"+ nc.getCategoryName();
 78  8 addCategoryName(all,cp);
 79    }
 80    }
 81    }
 82   
 83  2 protected String[] getCategoryNames(boolean includeParent) {
 84  2 Set allCategories = categories.keySet();
 85  2 Vector all = new Vector(allCategories);
 86  2 if ( baseConfigName != null && includeParent ) {
 87  0 Configuration cfg = ConfigurationManager.getConfiguration(baseConfigName);
 88  0 String[] parentCategories = cfg.getCategoryNames();
 89  0 for ( int i = 0 ; i < parentCategories.length ; i++) {
 90  0 if ( all.indexOf(parentCategories[i]) == -1 ) {
 91  0 all.add(parentCategories[i]);
 92    }
 93    }
 94    }
 95  2 return (String[]) all.toArray(new String[0]);
 96    }
 97   
 98    /**
 99    * Besides setting the category, it will also set this
 100    * category as default category if main is true. It will
 101    * only set (ie create) the category if it does not exist. If you
 102    * want delete a category then use @see #removeCategory(String)
 103    *
 104    * @param name the name of the category
 105    * @param main if true then this category is the default category
 106    */
 107  30 public void setCategory(String name, boolean main) {
 108  30 if (name != null) {
 109  30 if (main) {
 110  26 mainCategory = name;
 111    }
 112  30 if (!categories.containsKey(name)) {
 113  30 Category category = new NestedCategory(name);
 114  30 category.setConfigurationName(configName);
 115  30 category.addCategoryListener(new MyCategoryListener());
 116  30 categories.put(name, category);
 117  30 markDirty();
 118  30 category.fireCategoryChangedEvent(
 119    new ConfigurationChangedEventImpl(ConfigurationChangedEvent.CATEGORY_ADDED, category, null, null, null ));
 120    }
 121    }
 122    }
 123   
 124  122 public void setCategory(Category category) {
 125    //if (!categories.containsKey(category.getCategoryName())) {
 126  122 category.setConfigurationName(configName);
 127  122 category.addCategoryListener(new MyCategoryListener());
 128  122 categories.put(category.getCategoryName(), category);
 129  122 markDirty();
 130  122 category.fireCategoryChangedEvent(
 131    new ConfigurationChangedEventImpl(ConfigurationChangedEvent.CATEGORY_ADDED, category, null, null, null ));
 132    //}
 133    }
 134   
 135    /**
 136    * This is the real implementation. It will return the value of the property
 137    * with the specific name. First of all, it checks if the name of the category
 138    * exists. If not, then it will use the name of the default category.
 139    * The next step is that it will look for the property. If it is not found in
 140    * the category, it will look inside the default category (inheritance). If
 141    * it still cannot find the property, it will return the defaultValue
 142    *
 143    * @param key the name of the property
 144    * @param defaultValue the default value
 145    * @param categoryName the name of the category
 146    * @return the value as String
 147    */
 148  42 public String getProperty(String key,String defaultValue,String categoryName) {
 149  42 boolean isMainCat = false;
 150  42 if (key == null) {
 151  0 return defaultValue;
 152    }
 153  42 if ( categoryName == null ) {
 154  0 isMainCat = true;
 155  0 categoryName = mainCategory;
 156    }
 157  42 else if ( categoryName.indexOf("/") == -1 ) {
 158  15 if (!categories.containsKey(categoryName)) {
 159  1 isMainCat = true;
 160  1 categoryName = mainCategory;
 161    }
 162    }
 163  42 String tmp = null;
 164  42 Category category = getCategory(categoryName);
 165  42 if ( category != null ) {
 166  42 if ( category.getCategoryName().equals(mainCategory)) {
 167  7 isMainCat = true;
 168    }
 169  42 tmp = category.getProperty(key);
 170    }
 171    // property not found so look in mainCategory
 172    // if it is not already the mainCategory
 173  42 if ( tmp == null && !isMainCat) {
 174  19 category = getCategory(mainCategory);
 175  19 tmp = category.getProperty(key);
 176    }
 177   
 178    // maybe this config extends another one
 179  42 if ( tmp == null ) {
 180  20 if ( baseConfigName != null ) {
 181  12 Configuration cfg = ConfigurationManager.getConfiguration(baseConfigName);
 182  12 tmp = cfg.getProperty(key,defaultValue,categoryName);
 183    }
 184    else {
 185  8 tmp = defaultValue;
 186    }
 187    }
 188  42 return tmp;
 189    }
 190   
 191    /**
 192    * This method creates a string representation of the configuration.
 193    *
 194    * @return a string with the configuration
 195    */
 196  0 public String toString() {
 197  0 StringBuffer buffer = new StringBuffer();
 198  0 String[] cats = getCategoryNames();
 199  0 for (int i = 0; i < cats.length; i++) {
 200  0 buffer.append("Category=");
 201  0 buffer.append(cats[i]);
 202  0 String[] propNames = getPropertyNames(cats[i]);
 203  0 if (propNames != null) {
 204  0 buffer.append("\n");
 205  0 for (int j = 0; j < propNames.length; j++) {
 206  0 buffer.append(" ");
 207  0 buffer.append(propNames[j]);
 208  0 buffer.append("=");
 209    //buffer.append(replaceVariables(value));
 210  0 buffer.append("\n");
 211    }
 212    }
 213    }
 214  0 return buffer.toString();
 215    }
 216   
 217    /**
 218    * This method converts the Configuration into a String
 219    * which looks like XML.
 220    *
 221    * @return the Configuration as String in XML format
 222    */
 223  2 public String getXMLAsString() {
 224  2 StringBuffer buffer = new StringBuffer();
 225    // first we will write out the variable block
 226    // if we have some
 227  2 buffer.append("<?xml version=\"1.0\"");
 228  2 if ( getEncoding() != null ) {
 229  0 buffer.append(" encoding=\""+getEncoding()+"\"");
 230    }
 231  2 buffer.append(" ?>\n");
 232  2 buffer.append("<properties");
 233  2 if ( baseConfigName != null ) {
 234  1 buffer.append(" extends=\"");
 235  1 buffer.append(baseConfigName);
 236  1 buffer.append("\"");
 237    }
 238  2 buffer.append(">\n");
 239  2 addIncludeBlock(buffer);
 240  2 addVariableBlock(buffer);
 241    // this is only the list of top level categories
 242  2 String[] cats = getCategoryNames(false);
 243  2 for (int i = 0; i < cats.length; i++) {
 244  10 NestedCategory nc = (NestedCategory)getCategory(cats[i]);
 245  10 generateCategoryString(buffer, nc,nc.getCategoryName(),0);
 246    }
 247  2 buffer.append("</properties>\n");
 248  2 return buffer.toString();
 249    }
 250   
 251  16 private void generateCategoryString(StringBuffer buffer,NestedCategory category,String categoryPath,int indent) {
 252  16 indent +=2;
 253  16 for ( int i = 0; i < indent;i++) {
 254  48 buffer.append(" ");
 255    }
 256  16 buffer.append("<category name=\"");
 257  16 buffer.append(escapeForXML(category.getCategoryName()));
 258  16 buffer.append("\">\n");
 259  16 SortedMap sm = getSortedProperties(categoryPath,false);
 260  16 if (sm != null) {
 261  16 Iterator nit = sm.keySet().iterator();
 262  16 while (nit.hasNext()) {
 263  21 String name = (String) nit.next();
 264  21 String value = (String)sm.get(name);
 265  21 for ( int i = 0; i < indent+2;i++) {
 266  102 buffer.append(" ");
 267    }
 268  21 buffer.append("<");
 269  21 buffer.append(escapeForXML(name));
 270  21 buffer.append(">");
 271    // do not convert the value
 272  21 buffer.append(escapeForXML(value));
 273  21 buffer.append("</");
 274  21 buffer.append(escapeForXML(name));
 275  21 buffer.append(">\n");
 276    }
 277  16 Collection children = category.getChildCategories();
 278  16 if ( children.size() > 0 ) {
 279  4 Iterator it = children.iterator();
 280  4 while ( it.hasNext() ) {
 281  6 NestedCategory nc = (NestedCategory)it.next();
 282  6 String cp = categoryPath + "/"+ nc.getCategoryName();
 283  6 generateCategoryString(buffer, nc,cp,indent);
 284    }
 285    }
 286  16 for ( int i = 0; i < indent;i++) {
 287  48 buffer.append(" ");
 288    }
 289  16 buffer.append("</category>\n");
 290  16 indent -=2;
 291    }
 292    }
 293   
 294    /**
 295    * Returns a category based on the name provided.
 296    * @param name The name of the category (if null, main category will be used)
 297    * @return The category object (new instance if necessary)
 298    */
 299  136 public Category getCategory(String name) {
 300  136 return getCategory(name,true);
 301    }
 302   
 303  136 private Category getCategory(String name,boolean create) {
 304  136 if(name == null) {
 305  0 name = mainCategory;
 306    }
 307  136 NestedCategory category = null;
 308  136 if ( name.indexOf("/") != -1 ) {
 309  50 boolean first = true;
 310  50 StringTokenizer sto = new StringTokenizer(name,"/");
 311  50 while ( sto.hasMoreElements() ) {
 312  125 String current = (String)sto.nextElement();
 313  125 if ( first ) {
 314  50 category = (NestedCategory)categories.get(current);
 315  50 first = false;
 316    }
 317    else {
 318  75 if ( category != null ) {
 319  71 category = category.getCategory(current);
 320    }
 321    }
 322    }
 323    }
 324    else {
 325  86 category = (NestedCategory)categories.get(name);
 326  86 if( category == null && create ) {
 327  0 category = new NestedCategory(name);
 328  0 categories.put(name, category);
 329    }
 330    }
 331  136 if ( category == null && create ) {
 332  8 category = createCategory(name);
 333    }
 334  136 return category;
 335    }
 336   
 337  8 private NestedCategory createCategory(String name) {
 338  8 StringTokenizer sto = new StringTokenizer(name,"/");
 339  8 String dir = null;
 340  8 boolean first = true;
 341  8 NestedCategory category = null;
 342  8 NestedCategory parent = null;
 343  8 while ( sto.hasMoreElements() ) {
 344  18 String current = (String)sto.nextElement();
 345  18 if ( first ) {
 346  8 category = (NestedCategory)categories.get(current);
 347  8 if ( category == null ) {
 348  4 setCategory(current);
 349  4 category = (NestedCategory)categories.get(current);
 350    }
 351  8 parent = category;
 352  8 first = false;
 353    }
 354    else {
 355  10 if ( category != null ) {
 356  10 category = category.getCategory(current);
 357  10 if ( category == null ) {
 358  8 NestedCategory newCategory = new NestedCategory(current);
 359  8 parent.addCategory(newCategory);
 360  8 category = newCategory;
 361    }
 362    }
 363  10 parent = category;
 364    }
 365    }
 366  8 return category;
 367    }
 368   
 369  0 public boolean containsCategory(String categoryName) {
 370  0 Category cat = getCategory(categoryName,false);
 371  0 if ( cat == null ) {
 372  0 return false;
 373    }
 374    else {
 375  0 return true;
 376    }
 377    }
 378   
 379    }