KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > myvietnam > mvncore > configuration > BaseConfiguration


1 package net.myvietnam.mvncore.configuration;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowledgement:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgement may appear in the software itself,
26  * if and wherever such third-party acknowledgements normally appear.
27  *
28  * 4. The names "The Jakarta Project", "Commons", and "Apache Software
29  * Foundation" must not be used to endorse or promote products derived
30  * from this software without prior written permission. For written
31  * permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache"
34  * nor may "Apache" appear in their names without prior written
35  * permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import java.util.Iterator JavaDoc;
58
59 import org.apache.commons.collections.SequencedHashMap;
60
61 /**
62  * Basic configuration classe. Stores the configuration data but does not
63  * provide any load or save functions. If you want to load your Configuration
64  * from a file use PropertiesConfiguration or XmlConfiguration.
65  *
66  * This class extends normal Java properties by adding the possibility
67  * to use the same key many times concatenating the value strings
68  * instead of overwriting them.
69  *
70  * @author <a HREF="mailto:stefano@apache.org">Stefano Mazzocchi</a>
71  * @author <a HREF="mailto:jon@latchkey.com">Jon S. Stevens</a>
72  * @author <a HREF="mailto:daveb@miceda-data">Dave Bryson</a>
73  * @author <a HREF="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
74  * @author <a HREF="mailto:leon@opticode.co.za">Leon Messerschmidt</a>
75  * @author <a HREF="mailto:kjohnson@transparent.com">Kent Johnson</a>
76  * @author <a HREF="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
77  * @author <a HREF="mailto:ipriha@surfeu.fi">Ilkka Priha</a>
78  * @author <a HREF="mailto:jvanzyl@apache.org">Jason van Zyl</a>
79  * @author <a HREF="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
80  * @author <a HREF="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
81  * @author <a HREF="mailto:ksh@scand.com">Konstantin Shaposhnikov</a>
82  * @author <a HREF="mailto:oliver.heger@t-online.de">Oliver Heger</a>
83  *
84  * @version $Id: BaseConfiguration.java,v 1.1 2003/12/09 08:25:30 huumai Exp $
85  */

86 public class BaseConfiguration extends AbstractConfiguration
87 {
88     /** stores the configuration key-value pairs */
89     private SequencedHashMap store = new SequencedHashMap();
90
91     /**
92      * Empty constructor. You must add all the values to this configuration.
93      */

94     public BaseConfiguration()
95     {
96         super();
97     }
98
99     /**
100      * Creates an empty BaseConfiguration object with
101      * a Super-Object which is queries for every key.
102      *
103      * @param defaults Configuration defaults to use if key not in file
104      */

105     public BaseConfiguration(Configuration defaults)
106     {
107         super(defaults);
108     }
109
110     /**
111      * Adds a key/value pair to the map. This routine does no magic morphing.
112      * It ensures the keylist is maintained
113      *
114      * @param key key to use for mapping
115      * @param obj object to store
116      */

117     protected void addPropertyDirect(String JavaDoc key, Object JavaDoc obj)
118     {
119         Object JavaDoc o = getPropertyDirect(key);
120         Object JavaDoc objAdd = null;
121
122         if(o == null)
123         {
124             objAdd = obj;
125         }
126         else
127         {
128             if (o instanceof Container)
129             {
130                 ((Container) o).add(obj);
131             }
132             else
133             {
134                 // The token key is not a container.
135
Container c = new Container();
136
137                 // There is an element. Put it into the container
138
// at the first position
139
c.add(o);
140
141                 // Now gobble up the supplied object
142
c.add(obj);
143
144                 objAdd = c;
145             }
146         }
147
148         if(objAdd != null)
149         {
150             store.put(key, objAdd);
151         }
152     }
153
154     /**
155      * Read property from underlying map.
156      *
157      * @param key key to use for mapping
158      *
159      * @return object associated with the given configuration key.
160      */

161     protected Object JavaDoc getPropertyDirect(String JavaDoc key)
162     {
163         return store.get(key);
164     }
165
166     /**
167      * Check if the configuration is empty
168      *
169      * @return <code>true</code> if Configuration is empty,
170      * <code>false</code> otherwise.
171      */

172     public boolean isEmpty()
173     {
174         return store.isEmpty();
175     }
176
177     /**
178      * check if the configuration contains the key
179      *
180      * @param key the configuration key
181      *
182      * @return <code>true</code> if Configuration contain given key,
183      * <code>false</code> otherwise.
184      */

185     public boolean containsKey(String JavaDoc key)
186     {
187         return store.containsKey(key);
188     }
189
190     /**
191      * Clear a property in the configuration.
192      *
193      * @param key the key to remove along with corresponding value.
194      */

195     public void clearProperty(String JavaDoc key)
196     {
197         if (containsKey(key))
198         {
199             store.remove(key);
200         }
201     }
202
203     /**
204      * Get the list of the keys contained in the configuration
205      * repository.
206      *
207      * @return An Iterator.
208      */

209     public Iterator JavaDoc getKeys()
210     {
211         return store.iterator();
212     }
213 }
214
Popular Tags