View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License")
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.configuration;
18  
19  import java.util.Iterator;
20  import java.util.Map;
21  import java.util.List;
22  import java.util.ArrayList;
23  
24  import org.apache.commons.collections.map.LinkedMap;
25  
26  /***
27   * Basic configuration classe. Stores the configuration data but does not
28   * provide any load or save functions. If you want to load your Configuration
29   * from a file use PropertiesConfiguration or XmlConfiguration.
30   *
31   * This class extends normal Java properties by adding the possibility
32   * to use the same key many times concatenating the value strings
33   * instead of overwriting them.
34   *
35   * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
36   * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
37   * @author <a href="mailto:daveb@miceda-data">Dave Bryson</a>
38   * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
39   * @author <a href="mailto:leon@opticode.co.za">Leon Messerschmidt</a>
40   * @author <a href="mailto:kjohnson@transparent.com">Kent Johnson</a>
41   * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
42   * @author <a href="mailto:ipriha@surfeu.fi">Ilkka Priha</a>
43   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
44   * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
45   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
46   * @author <a href="mailto:ksh@scand.com">Konstantin Shaposhnikov</a>
47   * @author <a href="mailto:oliver.heger@t-online.de">Oliver Heger</a>
48   * @version $Id: BaseConfiguration.java,v 1.8 2004/07/05 09:54:17 ebourg Exp $
49   */
50  public class BaseConfiguration extends AbstractConfiguration
51  {
52      /*** stores the configuration key-value pairs */
53      private Map store = new LinkedMap();
54  
55      /***
56       * Empty constructor.  You must add all the values to this configuration.
57       */
58      public BaseConfiguration()
59      {
60          super();
61      }
62  
63  
64      /***
65       * Adds a key/value pair to the map.  This routine does no magic morphing.
66       * It ensures the keylist is maintained
67       *
68       * @param key key to use for mapping
69       * @param obj object to store
70       */
71      protected void addPropertyDirect(String key, Object obj)
72      {
73          Object o = getPropertyDirect(key);
74          Object objAdd = null;
75  
76          if (o == null)
77          {
78              objAdd = obj;
79          }
80          else
81          {
82              if (o instanceof List)
83              {
84                  ((List) o).add(obj);
85              }
86              else
87              {
88                  // The token key is not a list.
89                  List list = new ArrayList();
90  
91                  // There is an element. Put it into the list
92                  // at the first position
93                  list.add(o);
94  
95                  // Now gobble up the supplied object
96                  list.add(obj);
97  
98                  objAdd = list;
99              }
100         }
101 
102         if (objAdd != null)
103         {
104             store.put(key, objAdd);
105         }
106     }
107 
108     /***
109      * Read property from underlying map.
110      *
111      * @param key key to use for mapping
112      *
113      * @return object associated with the given configuration key.
114      */
115     protected Object getPropertyDirect(String key)
116     {
117         return store.get(key);
118     }
119 
120     /***
121      * Check if the configuration is empty
122      *
123      * @return <code>true</code> if Configuration is empty,
124      * <code>false</code> otherwise.
125      */
126     public boolean isEmpty()
127     {
128         return store.isEmpty();
129     }
130 
131     /***
132      * check if the configuration contains the key
133      *
134      * @param key the configuration key
135      *
136      * @return <code>true</code> if Configuration contain given key,
137      * <code>false</code> otherwise.
138      */
139     public boolean containsKey(String key)
140     {
141         return store.containsKey(key);
142 
143     }
144 
145     /***
146      * Clear a property in the configuration.
147      *
148      * @param key the key to remove along with corresponding value.
149      */
150     public void clearProperty(String key)
151     {
152         if (containsKey(key))
153         {
154             store.remove(key);
155         }
156     }
157 
158     /***
159      * Get the list of the keys contained in the configuration
160      * repository.
161      *
162      * @return An Iterator.
163      */
164     public Iterator getKeys()
165     {
166         return store.keySet().iterator();
167     }
168 }