ymikulski    2002/08/21 13:14:31

  Added:       csframework/src/cs/Configuration DefaultConfiguration.cs
                        AbstractConfiguration.cs
  Log:
  no message
  
  Revision  Changes    Path
  1.1                  
jakarta-avalon-excalibur/csframework/src/cs/Configuration/DefaultConfiguration.cs
  
  Index: DefaultConfiguration.cs
  ===================================================================
  ///
  /// Copyright (C) The Apache Software Foundation. All rights reserved.
  ///
  /// This software is published under the terms of the Apache Software License
  /// version 1.1, a copy of which has been included  with this distribution in
  /// the LICENSE.txt file.
  ///
  using System;
  using System.Collections;
  
  namespace Apache.Avalon.Configuration
  {
        /// <summary>
        /// This is the default <see cref="IConfiguration"/> implementation.
        /// </summary>
        [Serializable]
        public class DefaultConfiguration: AbstractConfiguration
        {
   
                /// <summary>
                /// Creates a new <c>DefaultConfiguration</c> instance.
                /// </summary>
                public DefaultConfiguration()
                {
                }
  
                /// <summary>
                /// Creates a new <c>DefaultConfiguration</c> instance.
                /// </summary>
                /// <param name="name"></param>
                /// <param name="location"></param>
                public DefaultConfiguration(string name, string location )
                {
                        Name = name;
                        Location = location;
                }
  
                /// <summary>
                /// Creates a new <c>DefaultConfiguration</c> instance.
                /// </summary>
                /// <param name="name"></param>
                /// <param name="location"></param>
                /// <param name="ns"></param>
                /// <param name="prefix"></param>
                public DefaultConfiguration(string name, string location, string ns, 
string prefix): this(name, location)
                {
                        Namespace = ns;
                        Prefix = prefix;
                }
  
                /// <summary>
                ///     Gets a <c>IConfiguration</c> instance encapsulating the 
specified
                /// child node.
                /// </summary>
                /// <param name="name">The Name of the child node.</param> 
                /// <param name="createNew">
                ///     If <c>true</c>, a new <c>IConfiguration</c>
                /// will be created and returned if the specified child does not exist.
                /// If <c>false</c>, <c>null</c> will be returned when the specified
                /// child doesn't exist.
                /// </param>
                /// <returns><c>IConfiguration</c></returns>  
                public override IConfiguration GetChild(string name, bool createNew )
                {
                        IConfiguration result = null;
                
                        if (Children.Count != 0) 
                        {
  
                                foreach (IConfiguration configuration in Children)
                                {
                                        if (string.Compare(configuration.Name, name) 
== 0)
                                        {
                                                result = configuration;
                                                break;
                                        }
                                }
                        }
                        else
                        {
                                if( createNew )
                                {
                                        result = new DefaultConfiguration( name, 
string.Empty );
                                }
                        }
  
                        return result;
                }
  
                /// <summary>
                /// Return an <see cref="ConfigurationCollection"/> of 
<c>IConfiguration</c>
                /// elements containing all node children with the specified name.
                /// </summary>
                /// <param name="name">The Name of the children to get.</param> 
                /// <returns>
                /// An <see cref="ConfigurationCollection"/> of
                /// <c>IConfiguration</c> objects children of 
                /// this associated with the given name.
                /// </returns> 
                public override ConfigurationCollection GetChildren(string name)
                {
                        ConfigurationCollection result = new ConfigurationCollection();
  
                        foreach (IConfiguration configuration in Children)
                        {
                                if (string.Compare(configuration.Name, name) == 0)
                                {
                                        result.Add(configuration);
                                }
                        }
  
                        return result;
                }
        }
  }
  
  
  
  1.1                  
jakarta-avalon-excalibur/csframework/src/cs/Configuration/AbstractConfiguration.cs
  
  Index: AbstractConfiguration.cs
  ===================================================================
  ///
  /// Copyright (C) The Apache Software Foundation. All rights reserved.
  ///
  /// This software is published under the terms of the Apache Software License
  /// version 1.1, a copy of which has been included  with this distribution in
  /// the LICENSE.txt file.
  ///
  using System;
  using System.Collections;
  
  using Apache.Avalon.Util; 
  
  namespace Apache.Avalon.Configuration
  {
        /// <summary>
        /// This is an abstract <see cref="IConfiguration"/> implementation
        /// that deals with methods that can be abstracted away 
        /// from underlying implementations.
        /// </summary>
        public abstract class AbstractConfiguration: IConfiguration
        {
                private bool readOnly;
                private string name;
                private string location;
                private string val;
                private string ns;
                private string prefix;
                private Hashtable attributes = new Hashtable();
                private ConfigurationCollection children = new 
ConfigurationCollection();
  
                /// <summary>
                /// Gets a value indicating whether the <c>IConfiguration</c> is 
read-only.
                /// </summary>
                /// <value>
                /// True if the <c>IConfiguration</c> is read-only; otherwise, false.
                /// </value> 
                public bool IsReadOnly
                {
                        get
                        {
                                return readOnly;
                        }
                }
  
                /// <summary>
                /// Gets the name of the node.
                /// </summary>
                /// <value>
                /// The Name of the node.
                /// </value> 
                public string Name
                {
                        get
                        {
                                return name;
                        }
                        set
                        {
                                CheckReadOnly();
  
                                name = value;
                        }
                }
  
                /// <summary>
                /// Gets a string describing location of <c>IConfiguration</c>.
                /// </summary>
                /// <value>
                /// A String describing location of <c>IConfiguration</c>.
                /// </value> 
                public string Location
                {
                        get
                        {
                                return location;
                        }
                        set
                        {
                                CheckReadOnly();
  
                                location = value;
                        }
                }
  
                /// <summary>
                /// Gets the value of the node.
                /// </summary>
                /// <value>
                /// The Value of the node.
                /// </value> 
                public string Value
                {
                        get
                        {
                                if (val == null)
                                {
                                        throw new ConfigurationException( 
string.Format("No value is associated with the " +
                                                "configuration element {0} at {1}", 
Name, Location));
                                }
                                return val;
                        }
                        set
                        {
                                CheckReadOnly();
  
                                val = value;
                        }
                }
  
                /// <summary>
                /// Gets the Namespace of the node.
                /// </summary>
                /// <value>
                /// The Namespace of the node.
                /// </value>
                public string Namespace
                {
                        get
                        {
                                return ns;
                        }
  
                        set
                        {
                                CheckReadOnly();
  
                                ns = value;
                        }
                }
  
                /// <summary>
                /// Gets the Prefix of the node.
                /// </summary>
                /// <value>
                /// The Prefix of the node.
                /// </value>
                public string Prefix
                {
                        get
                        {
                                return prefix;
                        }
  
                        set
                        {
                                CheckReadOnly();
  
                                prefix = value;
                        }
                }
  
  
                /// <summary>
                /// Gets an <see cref="ConfigurationCollection"/> of 
<c>IConfiguration</c>
                /// elements containing all node children.
                /// </summary>
                /// <value>The Collection of child nodes.</value>
                public ConfigurationCollection Children
                {
                        get
                        {
                                if (children == null)
                                {
                                        children = new ConfigurationCollection();
                                }
  
                                return children;
                        }
  
                        set 
                        {
                                CheckReadOnly();
  
                                children = value;
                        }
                }
  
                /// <summary>
                /// Gets an <see cref="IDictionary"/> of the configuration attributes.
                /// </summary>
                public  IDictionary Attributes
                {
                        get
                        {
                                if (attributes == null)
                                {
                                        attributes = new Hashtable();
                                }
  
                                return attributes;
                        }
  
                        set
                        {
                                CheckReadOnly();
  
                                attributes = new Hashtable(value);
                        }
                }
  
                public IConfiguration GetChild(string child)
                {
                        return GetChild(child, false);
                }
                
                /// <summary>
                ///     Gets a <c>IConfiguration</c> instance encapsulating the 
specified
                /// child node.
                /// </summary>
                /// <param name="child">The Name of the child node.</param> 
                /// <param name="createNew">
                ///     If <c>true</c>, a new <c>IConfiguration</c>
                /// will be created and returned if the specified child does not exist.
                /// If <c>false</c>, <c>null</c> will be returned when the specified
                /// child doesn't exist.
                /// </param>
                /// <returns><c>IConfiguration</c></returns>  
                public abstract IConfiguration GetChild(string child, bool createNew);
  
                /// <summary>
                /// Return an <see cref="ConfigurationCollection"/> of 
<c>IConfiguration</c>
                /// elements containing all node children with the specified name.
                /// </summary>
                /// <param name="name">The Name of the children to get.</param> 
                /// <returns>
                /// An <see cref="ConfigurationCollection"/> of
                /// <c>IConfiguration</c> objects children of 
                /// this associated with the given name.
                /// </returns> 
                public abstract ConfigurationCollection GetChildren(string name);
  
                /// <summary>
                /// Gets the Value of the node and converts it 
                /// into specified <see cref="System.Type"/>.
                /// </summary>
                /// <param name="type">The <see cref="System.Type"/></param>
                /// <returns>The Value converted into the specified type.</returns>
                /// <exception cref="System.InvalidCastException">
                /// If the convertion fails, an exception will be thrown.
                /// </exception>
                public object GetValue(Type type)
                {
                        return GetValue(type, null);
                }
                
                /// <summary>
                /// Gets the Value of the node and converts it 
                /// into specified <see cref="System.Type"/>.
                /// </summary>
                /// <param name="type">The <see cref="System.Type"/></param>
                /// <param name="defaultValue">
                /// The defaultValue returned if the convertion fails.
                /// </param>
                /// <returns>The Value converted into the specified type.</returns>
                public object GetValue(Type type, object defaultValue)
                {
  
                        return Converter.ChangeType(Value, type, defaultValue);
                }
                
                /// <summary>
                /// Gets the value of specified attribute and
                /// converts it into specified <see cref="System.Type"/>. 
                /// </summary>
                /// <param name="name">The Name of the attribute you ask the value 
of.</param>
                /// <param name="type">The <see cref="System.Type"/></param>
                /// <returns>The Value converted into the specified type.</returns>
                /// <exception cref="System.InvalidCastException">
                /// If the convertion fails, an exception will be thrown.
                /// </exception>
                public object GetAttribute(string name, Type type)
                {
  
                        return GetAttribute(name, type, null);
                }
  
                /// <summary>
                /// Gets the value of specified attribute and
                /// converts it into specified <see cref="System.Type"/>. 
                /// </summary>
                /// <param name="name">The Name of the attribute you ask the value 
of.</param>
                /// <param name="type">The <see cref="System.Type"/></param>
                /// <param name="defaultValue">
                /// The defaultValue returned if the convertion fails.
                /// </param>
                /// <returns>The Value converted into the specified type.</returns>
                public object GetAttribute(string name, Type type, object defaultValue)
                {
  
                        return Converter.ChangeType(Attributes[name], type, 
defaultValue);
                }
  
                /// <summary>
                /// Make the configuration read only.
                /// </summary>
                public void MakeReadOnly()
                {
                        readOnly = true;
                }
                
                protected void CheckReadOnly()
                {
                        if( IsReadOnly )
                        {
                                throw new ConfigurationException( "Configuration is 
read only and can not be modified." );
                        }
                }
        }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to