1 16 package org.apache.avalon.framework.configuration; 17 18 import java.io.Serializable ; 19 import java.util.ArrayList ; 20 import java.util.HashMap ; 21 22 28 public class DefaultImmutableConfiguration 29 extends AbstractConfiguration 30 implements Serializable 31 { 32 35 protected static final Configuration[] EMPTY_ARRAY = new Configuration[ 0 ]; 36 37 private final String m_name; 38 private final String m_location; 39 private final String m_namespace; 40 private final String m_prefix; 41 private final HashMap m_attributes; 42 private final ArrayList m_children; 43 private final String m_value; 44 45 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 [] attributes = config.getAttributeNames(); 61 if( attributes.length > 0 ) 62 { 63 m_attributes = new HashMap (); 64 for( int i = 0; i < attributes.length; i++ ) 65 { 66 final String name = attributes[ i ]; 67 final String 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 (); 80 for( int i = 0; i < children.length; i++ ) 81 { 82 m_children.add( new DefaultImmutableConfiguration( children[i] ) ); 84 } 85 } 86 else 87 { 88 m_children = null; 89 } 90 } 91 92 96 public String getName() 97 { 98 return m_name; 99 } 100 101 107 public String 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 128 protected String 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 148 public String getLocation() 149 { 150 return m_location; 151 } 152 153 159 public String getValue( final String defaultValue ) 160 { 161 if( null != m_value ) 162 { 163 return m_value; 164 } 165 else 166 { 167 return defaultValue; 168 } 169 } 170 171 177 public String 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 195 public String [] getAttributeNames() 196 { 197 if( null == m_attributes ) 198 { 199 return new String [ 0 ]; 200 } 201 else 202 { 203 return (String [])m_attributes.keySet().toArray( new String [ 0 ] ); 204 } 205 } 206 207 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 233 public String getAttribute( final String name ) 234 throws ConfigurationException 235 { 236 final String value = 237 ( null != m_attributes ) ? (String )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 259 public Configuration getChild( final String 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 293 public Configuration[] getChildren( final String name ) 294 { 295 if( null == m_children ) 296 { 297 return new Configuration[ 0 ]; 298 } 299 else 300 { 301 final ArrayList children = new ArrayList (); 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 321 public int getChildCount() 322 { 323 if( null == m_children ) 324 { 325 return 0; 326 } 327 328 return m_children.size(); 329 } 330 331 337 public boolean equals( Object 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 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 |