1 43 44 package org.jfree.base.config; 45 46 import java.io.IOException ; 47 import java.io.ObjectInputStream ; 48 import java.io.ObjectOutputStream ; 49 import java.util.Collections ; 50 import java.util.Enumeration ; 51 import java.util.Iterator ; 52 import java.util.Properties ; 53 import java.util.TreeSet ; 54 55 import org.jfree.util.Configuration; 56 import org.jfree.util.PublicCloneable; 57 58 64 public class HierarchicalConfiguration 65 implements ModifiableConfiguration, PublicCloneable 66 { 67 68 71 private Properties configuration; 72 73 76 private transient Configuration parentConfiguration; 77 78 81 public HierarchicalConfiguration() 82 { 83 this.configuration = new Properties (); 84 } 85 86 91 public HierarchicalConfiguration(final Configuration parentConfiguration) 92 { 93 this(); 94 this.parentConfiguration = parentConfiguration; 95 } 96 97 103 public String getConfigProperty(final String key) 104 { 105 return getConfigProperty(key, null); 106 } 107 108 119 public String getConfigProperty(final String key, final String defaultValue) 120 { 121 String value = this.configuration.getProperty(key); 122 if (value == null) 123 { 124 if (isRootConfig()) 125 { 126 value = defaultValue; 127 } 128 else 129 { 130 value = this.parentConfiguration.getConfigProperty(key, defaultValue); 131 } 132 } 133 return value; 134 } 135 136 142 public void setConfigProperty(final String key, final String value) 143 { 144 if (key == null) 145 { 146 throw new NullPointerException (); 147 } 148 149 if (value == null) 150 { 151 this.configuration.remove(key); 152 } 153 else 154 { 155 this.configuration.setProperty(key, value); 156 } 157 } 158 159 164 private boolean isRootConfig() 165 { 166 return this.parentConfiguration == null; 167 } 168 169 176 public boolean isLocallyDefined(final String key) 177 { 178 return this.configuration.containsKey(key); 179 } 180 181 186 protected Properties getConfiguration() 187 { 188 return this.configuration; 189 } 190 191 198 public void insertConfiguration(final HierarchicalConfiguration config) 199 { 200 config.setParentConfig(getParentConfig()); 201 setParentConfig(config); 202 } 203 204 211 protected void setParentConfig(final Configuration config) 212 { 213 if (this.parentConfiguration == this) 214 { 215 throw new IllegalArgumentException ("Cannot add myself as parent configuration."); 216 } 217 this.parentConfiguration = config; 218 } 219 220 227 protected Configuration getParentConfig() 228 { 229 return this.parentConfiguration; 230 } 231 232 239 public Enumeration getConfigProperties() 240 { 241 return this.configuration.keys(); 242 } 243 244 250 public Iterator findPropertyKeys(final String prefix) 251 { 252 final TreeSet keys = new TreeSet (); 253 collectPropertyKeys(prefix, this, keys); 254 return Collections.unmodifiableSet(keys).iterator(); 255 } 256 257 265 private void collectPropertyKeys(final String prefix, 266 final Configuration config, 267 final TreeSet collector) 268 { 269 final Enumeration enum1 = config.getConfigProperties(); 270 while (enum1.hasMoreElements()) 271 { 272 final String key = (String ) enum1.nextElement(); 273 if (key.startsWith(prefix)) 274 { 275 if (collector.contains(key) == false) 276 { 277 collector.add(key); 278 } 279 } 280 } 281 282 if (config instanceof HierarchicalConfiguration) 283 { 284 final HierarchicalConfiguration hconfig = (HierarchicalConfiguration) config; 285 if (hconfig.parentConfiguration != null) 286 { 287 collectPropertyKeys(prefix, hconfig.parentConfiguration, collector); 288 } 289 } 290 } 291 292 298 protected boolean isParentSaved() 299 { 300 return true; 301 } 302 303 307 protected void configurationLoaded() 308 { 309 } 310 311 317 private void writeObject(final ObjectOutputStream out) 318 throws IOException 319 { 320 out.defaultWriteObject(); 321 if (isParentSaved() == false) 322 { 323 out.writeBoolean(false); 324 } 325 else 326 { 327 out.writeBoolean(true); 328 out.writeObject(parentConfiguration); 329 } 330 } 331 332 340 private void readObject(final ObjectInputStream in) 341 throws IOException , ClassNotFoundException 342 { 343 in.defaultReadObject(); 344 final boolean readParent = in.readBoolean(); 345 if (readParent) 346 { 347 parentConfiguration = (ModifiableConfiguration) in.readObject(); 348 } 349 else 350 { 351 parentConfiguration = null; 352 } 353 configurationLoaded(); 354 } 355 356 public Object clone() throws CloneNotSupportedException 357 { 358 HierarchicalConfiguration config = (HierarchicalConfiguration) super.clone(); 359 config.configuration = (Properties ) configuration.clone(); 360 return config; 361 } 362 } 363 | Popular Tags |