KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > framework > configuration > DefaultImmutableConfiguration


1 /*
2  * Copyright 1997-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 package org.apache.avalon.framework.configuration;
17
18 import java.io.Serializable JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.HashMap JavaDoc;
21
22 /**
23  * An immutable implementation of the <code>Configuration</code> interface.
24  *
25  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
26  * @version CVS $Revision: 1.2 $ $Date: 2004/02/11 14:34:25 $
27  */

28 public class DefaultImmutableConfiguration
29     extends AbstractConfiguration
30     implements Serializable JavaDoc
31 {
32     /**
33      * An empty (length zero) array of configuration objects.
34      */

35     protected static final Configuration[] EMPTY_ARRAY = new Configuration[ 0 ];
36     
37     private final String JavaDoc m_name;
38     private final String JavaDoc m_location;
39     private final String JavaDoc m_namespace;
40     private final String JavaDoc m_prefix;
41     private final HashMap JavaDoc m_attributes;
42     private final ArrayList JavaDoc m_children;
43     private final String JavaDoc m_value;
44     
45     /**
46      * Deep copy constructor.
47      *
48      * @param config the <code>Configuration</code> to do a deep copy of.
49      * @throws ConfigurationException if an error occurs when copying
50      */

51     public DefaultImmutableConfiguration( Configuration config ) throws ConfigurationException
52     {
53         m_name = config.getName();
54         m_location = config.getLocation();
55         m_namespace = config.getNamespace();
56         m_prefix = (config instanceof AbstractConfiguration) ? ((AbstractConfiguration)config).getPrefix() : "";
57
58         m_value = config.getValue( null );
59         
60         final String JavaDoc[] attributes = config.getAttributeNames();
61         if( attributes.length > 0 )
62         {
63             m_attributes = new HashMap JavaDoc ();
64             for( int i = 0; i < attributes.length; i++ )
65             {
66                 final String JavaDoc name = attributes[ i ];
67                 final String JavaDoc value = config.getAttribute( name, null );
68                 m_attributes.put( name, value );
69             }
70         }
71         else
72         {
73              m_attributes = null;
74         }
75     
76         Configuration[] children = config.getChildren();
77         if( children.length > 0 )
78         {
79             m_children = new ArrayList JavaDoc ();
80             for( int i = 0; i < children.length; i++ )
81             {
82                 // Deep copy
83
m_children.add( new DefaultImmutableConfiguration( children[i] ) );
84             }
85         }
86         else
87         {
88             m_children = null;
89         }
90     }
91     
92     /**
93      * Returns the name of this configuration element.
94      * @return a <code>String</code> value
95      */

96     public String JavaDoc getName()
97     {
98         return m_name;
99     }
100     
101     /**
102      * Returns the namespace of this configuration element
103      * @return a <code>String</code> value
104      * @throws ConfigurationException if an error occurs
105      * @since 4.1
106      */

107     public String JavaDoc getNamespace() throws ConfigurationException
108     {
109         if( null != m_namespace )
110         {
111             return m_namespace;
112         }
113         else
114         {
115             throw new ConfigurationException
116                 ( "No namespace (not even default \"\") is associated with the "
117                 + "configuration element \"" + getName()
118                 + "\" at " + getLocation() );
119         }
120     }
121     
122     /**
123      * Returns the prefix of the namespace
124      * @return a <code>String</code> value
125      * @throws ConfigurationException if prefix is not present (<code>null</code>).
126      * @since 4.1
127      */

128     protected String JavaDoc getPrefix() throws ConfigurationException
129     {
130         if( null != m_prefix )
131         {
132             return m_prefix;
133         }
134         else
135         {
136             throw new ConfigurationException
137                 ( "No prefix (not even default \"\") is associated with the "
138                 + "configuration element \"" + getName()
139                 + "\" at " + getLocation() );
140         }
141         
142     }
143     
144     /**
145      * Returns a description of location of element.
146      * @return a <code>String</code> value
147      */

148     public String JavaDoc getLocation()
149     {
150         return m_location;
151     }
152     
153     /**
154      * Returns the value of the configuration element as a <code>String</code>.
155      *
156      * @param defaultValue the default value to return if value malformed or empty
157      * @return a <code>String</code> value
158      */

159     public String JavaDoc getValue( final String JavaDoc defaultValue )
160     {
161         if( null != m_value )
162         {
163             return m_value;
164         }
165         else
166         {
167             return defaultValue;
168         }
169     }
170     
171     /**
172      * Returns the value of the configuration element as a <code>String</code>.
173      *
174      * @return a <code>String</code> value
175      * @throws ConfigurationException If the value is not present.
176      */

177     public String JavaDoc getValue() throws ConfigurationException
178     {
179         if( null != m_value )
180         {
181             return m_value;
182         }
183         else
184         {
185             throw new ConfigurationException( "No value is associated with the "
186                 + "configuration element \"" + getName()
187                 + "\" at " + getLocation() );
188         }
189     }
190     
191     /**
192      * Return an array of all attribute names.
193      * @return a <code>String[]</code> value
194      */

195     public String JavaDoc[] getAttributeNames()
196     {
197         if( null == m_attributes )
198         {
199             return new String JavaDoc[ 0 ];
200         }
201         else
202         {
203             return (String JavaDoc[])m_attributes.keySet().toArray( new String JavaDoc[ 0 ] );
204         }
205     }
206     
207     /**
208      * Return an array of <code>Configuration</code>
209      * elements containing all node children.
210      *
211      * @return The child nodes with name
212      */

213     public Configuration[] getChildren()
214     {
215         if( null == m_children )
216         {
217             return new Configuration[ 0 ];
218         }
219         else
220         {
221             return (Configuration[])m_children.toArray( new Configuration[ 0 ] );
222         }
223     }
224     
225     /**
226      * Returns the value of the attribute specified by its name as a
227      * <code>String</code>.
228      *
229      * @param name a <code>String</code> value
230      * @return a <code>String</code> value
231      * @throws ConfigurationException If the attribute is not present.
232      */

233     public String JavaDoc getAttribute( final String JavaDoc name )
234         throws ConfigurationException
235     {
236         final String JavaDoc value =
237             ( null != m_attributes ) ? (String JavaDoc)m_attributes.get( name ) : null;
238         
239         if( null != value )
240         {
241             return value;
242         }
243         else
244         {
245             throw new ConfigurationException(
246                 "No attribute named \"" + name + "\" is "
247                 + "associated with the configuration element \""
248                 + getName() + "\" at " + getLocation() );
249         }
250     }
251     
252     /**
253      * Return the first <code>Configuration</code> object child of this
254      * associated with the given name.
255      * @param name a <code>String</code> value
256      * @param createNew a <code>boolean</code> value
257      * @return a <code>Configuration</code> value
258      */

259     public Configuration getChild( final String JavaDoc name, final boolean createNew )
260     {
261         if( null != m_children )
262         {
263             final int size = m_children.size();
264             for( int i = 0; i < size; i++ )
265             {
266                 final Configuration configuration = (Configuration)m_children.get( i );
267                 if( name.equals( configuration.getName() ) )
268                 {
269                     return configuration;
270                 }
271             }
272         }
273         
274         if( createNew )
275         {
276             return new DefaultConfiguration( name, "<generated>" + getLocation(), m_namespace, m_prefix );
277         }
278         else
279         {
280             return null;
281         }
282     }
283     
284     /**
285      * Return an array of <code>Configuration</code> objects
286      * children of this associated with the given name.
287      * <br>
288      * The returned array may be empty but is never <code>null</code>.
289      *
290      * @param name The name of the required children <code>Configuration</code>.
291      * @return a <code>Configuration[]</code> value
292      */

293     public Configuration[] getChildren( final String JavaDoc name )
294     {
295         if( null == m_children )
296         {
297             return new Configuration[ 0 ];
298         }
299         else
300         {
301             final ArrayList JavaDoc children = new ArrayList JavaDoc();
302             final int size = m_children.size();
303             
304             for( int i = 0; i < size; i++ )
305             {
306                 final Configuration configuration = (Configuration)m_children.get( i );
307                 if( name.equals( configuration.getName() ) )
308                 {
309                     children.add( configuration );
310                 }
311             }
312             
313             return (Configuration[])children.toArray( new Configuration[ 0 ] );
314         }
315     }
316     
317     /**
318      * Return count of children.
319      * @return an <code>int</code> value
320      */

321     public int getChildCount()
322     {
323         if( null == m_children )
324         {
325             return 0;
326         }
327         
328         return m_children.size();
329     }
330     
331     /**
332      * Compare if this configuration is equal to another.
333      *
334      * @param other The other configuration
335      * @return <code>true</code> if they are the same.
336      */

337     public boolean equals( Object JavaDoc other )
338     {
339         if( other == null ) return false;
340         if( !( other instanceof Configuration ) ) return false;
341         return ConfigurationUtil.equals( this, (Configuration) other );
342     }
343     
344     /**
345      * Obtaine the hashcode for this configuration.
346      *
347      * @return the hashcode.
348      */

349     public int hashCode()
350     {
351         int hash = m_prefix.hashCode();
352         if( m_name != null ) hash ^= m_name.hashCode();
353         hash >>>= 7;
354         if( m_location != null ) hash ^= m_location.hashCode();
355         hash >>>= 7;
356         if( m_namespace != null ) hash ^= m_namespace.hashCode();
357         hash >>>= 7;
358         if( m_attributes != null ) hash ^= m_attributes.hashCode();
359         hash >>>= 7;
360         if( m_children != null ) hash ^= m_children.hashCode();
361         hash >>>= 7;
362         if( m_value != null ) hash ^= m_value.hashCode();
363         hash >>>= 7;
364         return hash;
365     }
366 }
367
Popular Tags