1 16 17 package org.apache.commons.configuration; 18 19 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 import java.util.LinkedList ; 22 import java.util.List ; 23 24 35 public class CompositeConfiguration extends AbstractConfiguration 36 { 37 38 private List configList = new LinkedList (); 39 40 44 private Configuration inMemoryConfiguration; 45 46 50 public CompositeConfiguration() 51 { 52 clear(); 53 } 54 55 62 public CompositeConfiguration(Configuration inMemoryConfiguration) 63 { 64 configList.clear(); 65 this.inMemoryConfiguration = inMemoryConfiguration; 66 configList.add(inMemoryConfiguration); 67 } 68 69 74 public void addConfiguration(Configuration config) 75 { 76 if (!configList.contains(config)) 77 { 78 configList.add(configList.indexOf(inMemoryConfiguration), config); 83 84 if (config instanceof AbstractConfiguration) 85 { 86 ((AbstractConfiguration) config).setThrowExceptionOnMissing(isThrowExceptionOnMissing()); 87 } 88 } 89 } 90 91 96 public void removeConfiguration(Configuration config) 97 { 98 if (!config.equals(inMemoryConfiguration)) 101 { 102 configList.remove(config); 103 } 104 } 105 106 111 public int getNumberOfConfigurations() 112 { 113 return configList.size(); 114 } 115 116 119 public void clear() 120 { 121 configList.clear(); 122 inMemoryConfiguration = new BaseConfiguration(); 124 ((BaseConfiguration) inMemoryConfiguration).setThrowExceptionOnMissing(isThrowExceptionOnMissing()); 125 configList.add(inMemoryConfiguration); 126 } 127 128 134 protected void addPropertyDirect(String key, Object token) 135 { 136 inMemoryConfiguration.addProperty(key, token); 137 } 138 139 146 public Object getProperty(String key) 147 { 148 Configuration firstMatchingConfiguration = null; 149 for (Iterator i = configList.iterator(); i.hasNext();) 150 { 151 Configuration config = (Configuration) i.next(); 152 if (config.containsKey(key)) 153 { 154 firstMatchingConfiguration = config; 155 break; 156 } 157 } 158 159 if (firstMatchingConfiguration != null) 160 { 161 return firstMatchingConfiguration.getProperty(key); 162 } 163 else 164 { 165 return null; 166 } 167 } 168 169 172 public Iterator getKeys() 173 { 174 List keys = new ArrayList (); 175 for (Iterator i = configList.iterator(); i.hasNext();) 176 { 177 Configuration config = (Configuration) i.next(); 178 179 Iterator j = config.getKeys(); 180 while (j.hasNext()) 181 { 182 String key = (String ) j.next(); 183 if (!keys.contains(key)) 184 { 185 keys.add(key); 186 } 187 } 188 } 189 190 return keys.iterator(); 191 } 192 193 196 public Iterator getKeys(String key) 197 { 198 List keys = new ArrayList (); 199 for (Iterator i = configList.iterator(); i.hasNext();) 200 { 201 Configuration config = (Configuration) i.next(); 202 203 Iterator j = config.getKeys(key); 204 while (j.hasNext()) 205 { 206 String newKey = (String ) j.next(); 207 if (!keys.contains(newKey)) 208 { 209 keys.add(newKey); 210 } 211 } 212 } 213 214 return keys.iterator(); 215 } 216 217 220 public boolean isEmpty() 221 { 222 boolean isEmpty = true; 223 for (Iterator i = configList.iterator(); i.hasNext();) 224 { 225 Configuration config = (Configuration) i.next(); 226 if (!config.isEmpty()) 227 { 228 return false; 229 } 230 } 231 232 return isEmpty; 233 } 234 235 238 public void clearProperty(String key) 239 { 240 for (Iterator i = configList.iterator(); i.hasNext();) 241 { 242 Configuration config = (Configuration) i.next(); 243 config.clearProperty(key); 244 } 245 } 246 247 250 public boolean containsKey(String key) 251 { 252 for (Iterator i = configList.iterator(); i.hasNext();) 253 { 254 Configuration config = (Configuration) i.next(); 255 if (config.containsKey(key)) 256 { 257 return true; 258 } 259 } 260 return false; 261 } 262 263 266 public List getList(String key, List defaultValue) 267 { 268 List list = new ArrayList (); 269 270 Iterator it = configList.iterator(); 272 while (it.hasNext() && list.isEmpty()) 273 { 274 Configuration config = (Configuration) it.next(); 275 if (config != inMemoryConfiguration && config.containsKey(key)) 276 { 277 list.addAll(config.getList(key)); 278 } 279 } 280 281 list.addAll(inMemoryConfiguration.getList(key)); 283 284 if (list.isEmpty()) 285 { 286 return defaultValue; 287 } 288 289 return list; 290 } 291 292 295 public String [] getStringArray(String key) 296 { 297 List list = getList(key); 298 299 String [] tokens = new String [list.size()]; 301 302 for (int i = 0; i < tokens.length; i++) 303 { 304 tokens[i] = interpolate(String.valueOf(list.get(i))); 305 } 306 307 return tokens; 308 } 309 310 315 public Configuration getConfiguration(int index) 316 { 317 return (Configuration) configList.get(index); 318 } 319 320 323 public Configuration getInMemoryConfiguration() 324 { 325 return inMemoryConfiguration; 326 } 327 } 328 | Popular Tags |