001    /* ========================================================================
002     * JCommon : a free general purpose class library for the Java(tm) platform
003     * ========================================================================
004     *
005     * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006     *
007     * Project Info:  http://www.jfree.org/jcommon/index.html
008     *
009     * This library is free software; you can redistribute it and/or modify it
010     * under the terms of the GNU Lesser General Public License as published by
011     * the Free Software Foundation; either version 2.1 of the License, or
012     * (at your option) any later version.
013     *
014     * This library is distributed in the hope that it will be useful, but
015     * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016     * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017     * License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public
020     * License along with this library; if not, write to the Free Software
021     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
022     * USA.
023     *
024     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025     * in the United States and other countries.]
026     *
027     * ---------------------------------
028     * ExtendedConfigurationWrapper.java
029     * ---------------------------------
030     * (C)opyright 2002-2005, by Thomas Morgner and Contributors.
031     *
032     * Original Author:  Thomas Morgner;
033     * Contributor(s):   David Gilbert (for Object Refinery Limited);
034     *
035     * $Id: ExtendedConfigurationWrapper.java,v 1.8 2008/09/10 09:22:04 mungady Exp $
036     *
037     * Changes
038     * -------
039     * 20-May-2005 : Initial version.
040     */
041    
042    package org.jfree.util;
043    
044    import java.util.Enumeration;
045    import java.util.Iterator;
046    
047    /**
048     * A wrapper for the extended configuration interface around a plain configuration.
049     *
050     * @author Thomas Morgner
051     */
052    public class ExtendedConfigurationWrapper
053            implements ExtendedConfiguration
054    {
055      /** The base configuration. */
056      private Configuration parent;
057    
058      /**
059       * Creates a wrapper around the given configuration.
060       *
061       * @param parent the wrapped up configuration.
062       * @throws NullPointerException if the parent is null.
063       */
064      public ExtendedConfigurationWrapper (final Configuration parent)
065      {
066        if (parent == null)
067        {
068          throw new NullPointerException("Parent given must not be null");
069        }
070        this.parent = parent;
071      }
072    
073      /**
074       * Returns the boolean value of a given configuration property. The boolean value true
075       * is returned, if the contained string is equal to 'true'.
076       *
077       * @param name the name of the property
078       * @return the boolean value of the property.
079       */
080      public boolean getBoolProperty (final String name)
081      {
082        return getBoolProperty(name, false);
083      }
084    
085      /**
086       * Returns the boolean value of a given configuration property. The boolean value true
087       * is returned, if the contained string is equal to 'true'. If the property is not set,
088       * the default value is returned.
089       *
090       * @param name the name of the property
091       * @param defaultValue the default value to be returned if the property is not set
092       * @return the boolean value of the property.
093       */
094      public boolean getBoolProperty (final String name,
095                                      final boolean defaultValue)
096      {
097        return "true".equals(this.parent.getConfigProperty(name, String.valueOf(defaultValue)));
098      }
099    
100      /**
101       * Returns a given property as int value. Zero is returned if the
102       * property value is no number or the property is not set.
103       *
104       * @param name the name of the property
105       * @return the parsed number value or zero
106       */
107      public int getIntProperty (final String name)
108      {
109        return getIntProperty(name, 0);
110      }
111    
112      /**
113       * Returns a given property as int value. The specified default value is returned if the
114       * property value is no number or the property is not set.
115       *
116       * @param name the name of the property
117       * @param defaultValue the value to be returned if the property is no integer value
118       * @return the parsed number value or the specified default value
119       */
120      public int getIntProperty (final String name,
121                                 final int defaultValue)
122      {
123        final String retval = this.parent.getConfigProperty(name);
124        if (retval == null)
125        {
126          return defaultValue;
127        }
128        try
129        {
130          return Integer.parseInt(retval);
131        }
132        catch (Exception e)
133        {
134          return defaultValue;
135        }
136      }
137    
138      /**
139       * Checks, whether a given property is defined.
140       *
141       * @param name the name of the property
142       * @return true, if the property is defined, false otherwise.
143       */
144      public boolean isPropertySet (final String name)
145      {
146        return this.parent.getConfigProperty(name) != null;
147      }
148    
149      /**
150       * Returns all keys with the given prefix.
151       *
152       * @param prefix the prefix
153       * @return the iterator containing all keys with that prefix
154       */
155      public Iterator findPropertyKeys (final String prefix)
156      {
157        return this.parent.findPropertyKeys(prefix);
158      }
159    
160      /**
161       * Returns the configuration property with the specified key.
162       *
163       * @param key the property key.
164       * @return the property value.
165       */
166      public String getConfigProperty (final String key)
167      {
168        return this.parent.getConfigProperty(key);
169      }
170    
171      /**
172       * Returns the configuration property with the specified key (or the specified default
173       * value if there is no such property).
174       * <p/>
175       * If the property is not defined in this configuration, the code will lookup the
176       * property in the parent configuration.
177       *
178       * @param key          the property key.
179       * @param defaultValue the default value.
180       * @return the property value.
181       */
182      public String getConfigProperty (final String key, final String defaultValue)
183      {
184        return this.parent.getConfigProperty(key, defaultValue);
185      }
186    
187      /**
188       * Returns an enumeration of the configuration properties.
189       *
190       * @return An enumeration.
191       */
192      public Enumeration getConfigProperties()
193      {
194        return this.parent.getConfigProperties();
195      }
196    
197      /**
198       * Returns a clone of this instance.
199       *
200       * @return A clone.
201       *
202       * @throws CloneNotSupportedException if there is a problem cloning.
203       */
204      public Object clone () throws CloneNotSupportedException
205      {
206        ExtendedConfigurationWrapper wrapper = (ExtendedConfigurationWrapper) super.clone();
207        wrapper.parent = (Configuration) this.parent.clone();
208        return this.parent;
209      }
210    }