// Copyright 2003-2004 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Apache.Avalon.Framework { using System; using System.Collections; /// /// This is an abstract implementation /// that deals with methods that can be abstracted away /// from underlying implementations. /// /// /// AbstractConfiguration makes easier to implementers /// to create a new version of /// 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(); /// /// Gets a value indicating whether the is read-only. /// /// /// if the is read-only; /// otherwise, . /// public bool IsReadOnly { get { return readOnly; } } /// /// Gets the name of the . /// /// /// The Name of the . /// public string Name { get { return name; } set { CheckReadOnly(); name = value; } } /// /// Gets a string describing location of the . /// /// /// A String describing location of the . /// public string Location { get { return location; } set { CheckReadOnly(); location = value; } } /// /// Gets the value of . /// /// /// The Value of the . /// public string Value { get { return val; } set { CheckReadOnly(); val = value; } } /// /// Gets the namespace of the . /// /// /// The Namespace of the . /// public string Namespace { get { return ns; } set { CheckReadOnly(); ns = value; } } /// /// Gets the prefix of the . /// /// /// The prefix of the . /// public string Prefix { get { return prefix; } set { CheckReadOnly(); prefix = value; } } /// /// Gets all child nodes. /// /// The of child nodes. public ConfigurationCollection Children { get { if (children == null) { children = new ConfigurationCollection(); } return children; } set { CheckReadOnly(); children = value; } } /// /// Gets node attributes. /// /// /// All attributes of the node. /// public IDictionary Attributes { get { if (attributes == null) { attributes = new Hashtable(); } return attributes; } set { CheckReadOnly(); attributes = new Hashtable(value); } } /// /// Gets a instance encapsulating the specified /// child node. /// /// The Name of the child node. /// /// The instance encapsulating the specified /// child node. /// public IConfiguration GetChild(string child) { return GetChild(child, false); } /// /// Gets a instance encapsulating the specified /// child node. /// /// The Name of the child node. /// /// If , a new /// will be created and returned if the specified child does not exist. /// If , will be returned when the specified /// child doesn't exist. /// /// /// The instance encapsulating the specified /// child node. /// public abstract IConfiguration GetChild(string child, bool createNew); /// /// Return an of /// elements containing all node children with the specified name. /// /// The Name of the children to get. /// /// All node children with the specified name /// public abstract ConfigurationCollection GetChildren(string name); /// /// Gets the value of the node and converts it /// into specified . /// /// The /// The Value converted into the specified type. /// /// If the convertion fails, an exception will be thrown. /// public object GetValue(Type type) { return GetValue(type, null); } /// /// Gets the value of the node and converts it /// into specified . /// /// The /// /// The Default value returned if the convertion fails. /// /// The Value converted into the specified type. public object GetValue(Type type, object defaultValue) { return Converter.ChangeType(Value, type, defaultValue); } /// /// Gets the value of specified attribute and /// converts it into specified . /// /// The Name of the attribute you ask the value of. /// The /// The Value converted into the specified type. /// /// If the convertion fails, an exception will be thrown. /// public object GetAttribute(string name, Type type) { return GetAttribute(name, type, null); } /// /// Gets the value of specified attribute /// /// The Name of the attribute you ask the value of. /// /// The Default value returned if the convertion fails. /// /// The Value of the attribute. public object GetAttribute(string name, object defaultValue) { object value = Attributes[name]; if (value == null) { value = defaultValue; } return value; } /// /// Gets the value of specified attribute and /// converts it into specified . /// /// The Name of the attribute you ask the value of. /// The /// /// The Default value returned if the convertion fails. /// /// The Value converted into the specified type. public object GetAttribute(string name, Type type, object defaultValue) { return Converter.ChangeType(Attributes[name], type, defaultValue); } /// /// Make the configuration read only. /// public void MakeReadOnly() { readOnly = true; } /// /// Check whether this node is readonly or not. /// /// /// If this node is readonly then an exception will be thrown. /// protected void CheckReadOnly() { if( IsReadOnly ) { throw new ConfigurationException( "Configuration is read only and can not be modified." ); } } } }