KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > configuration > BaseConfiguration


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 JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.ArrayList JavaDoc;
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 155408 2005-02-26 12:56:39Z dirkv $
49  */

50 public class BaseConfiguration extends AbstractConfiguration
51 {
52     /** stores the configuration key-value pairs */
53     private Map JavaDoc 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      * Adds a key/value pair to the map. This routine does no magic morphing.
65      * It ensures the keylist is maintained
66      *
67      * @param key key to use for mapping
68      * @param obj object to store
69      */

70     protected void addPropertyDirect(String JavaDoc key, Object JavaDoc obj)
71     {
72         Object JavaDoc o = getProperty(key);
73         Object JavaDoc objAdd = null;
74
75         if (o == null)
76         {
77             objAdd = obj;
78         }
79         else
80         {
81             if (o instanceof List JavaDoc)
82             {
83                 ((List JavaDoc) o).add(obj);
84             }
85             else
86             {
87                 // The token key is not a list.
88
List JavaDoc list = new ArrayList JavaDoc();
89
90                 // There is an element. Put it into the list
91
// at the first position
92
list.add(o);
93
94                 // Now gobble up the supplied object
95
list.add(obj);
96
97                 objAdd = list;
98             }
99         }
100
101         if (objAdd != null)
102         {
103             store.put(key, objAdd);
104         }
105     }
106
107     /**
108      * Read property from underlying map.
109      *
110      * @param key key to use for mapping
111      *
112      * @return object associated with the given configuration key.
113      */

114     public Object JavaDoc getProperty(String JavaDoc key)
115     {
116         return store.get(key);
117     }
118
119     /**
120      * Check if the configuration is empty
121      *
122      * @return <code>true</code> if Configuration is empty,
123      * <code>false</code> otherwise.
124      */

125     public boolean isEmpty()
126     {
127         return store.isEmpty();
128     }
129
130     /**
131      * check if the configuration contains the key
132      *
133      * @param key the configuration key
134      *
135      * @return <code>true</code> if Configuration contain given key,
136      * <code>false</code> otherwise.
137      */

138     public boolean containsKey(String JavaDoc key)
139     {
140         return store.containsKey(key);
141     }
142
143     /**
144      * Clear a property in the configuration.
145      *
146      * @param key the key to remove along with corresponding value.
147      */

148     public void clearProperty(String JavaDoc key)
149     {
150         if (containsKey(key))
151         {
152             store.remove(key);
153         }
154     }
155
156     /**
157      * {@inheritDoc}
158      */

159     public void clear()
160     {
161         store.clear();
162     }
163
164     /**
165      * Get the list of the keys contained in the configuration
166      * repository.
167      *
168      * @return An Iterator.
169      */

170     public Iterator JavaDoc getKeys()
171     {
172         return store.keySet().iterator();
173     }
174 }
175
Popular Tags