示例#1
0
 public void NameAlreadyExists() {
     ConfigBase config1 = new ConfigBase("Test", null);
     ConfigBase config2 = new ConfigBase("Test", null);
     ConfigCollection collection = new ConfigCollection(null);
     collection.Add(config1);
     collection.Add(config2); // merges, no exception
 }
示例#2
0
        /// <summary>
        /// Returns an IConfig.  If it does not exist then it is added.
        /// </summary>
        private IConfig GetConfig(string name) {
            IConfig result;

            if(Configs[name] == null) {
                result = new ConfigBase(name, this);
                Configs.Add(result);
            }
            else {
                result = Configs[name];
            }

            return result;
        }
示例#3
0
        public void AddAndRemove() {
            ConfigBase config1 = new ConfigBase("Test", null);
            ConfigBase config2 = new ConfigBase("Another", null);
            ConfigCollection collection = new ConfigCollection(null);
            collection.Add(config1);
            collection.Add(config2);

            Assert.AreEqual(2, collection.Count);
            Assert.IsNotNull(collection["Test"]);
            Assert.IsNotNull(collection["Another"]);

            collection.Remove(config2);
            Assert.AreEqual(1, collection.Count);
            Assert.IsNotNull(collection["Test"]);
            Assert.IsNull(collection["Another"]);
        }
示例#4
0
        public void GetConfig() {
            ConfigBase config1 = new ConfigBase("Test1", null);
            ConfigBase config2 = new ConfigBase("Test2", null);
            ConfigBase config3 = new ConfigBase("Test3", null);
            ConfigCollection collection = new ConfigCollection(null);

            collection.Add(config1);
            Assert.AreEqual(1, collection.Count);
            Assert.AreEqual(config1, collection[0]);

            collection.Add(config2);
            collection.Add(config3);
            Assert.AreEqual(3, collection.Count);

            Assert.AreEqual(config2, collection["Test2"]);
            Assert.AreEqual(config3, collection["Test3"]);
            Assert.AreEqual(config3, collection[2]);
        }
示例#5
0
 public void AlreadyExistsException() {
     ConfigBase config = new ConfigBase("Test", null);
     ConfigCollection collection = new ConfigCollection(null);
     collection.Add(config);
     collection.Add(config); // exception
 }
示例#6
0
        /// <summary>
        /// Merges the IniDocument into the Configs when the document is 
        /// reloaded.  
        /// </summary>
        private void MergeDocumentIntoConfigs() {
            // Remove all missing configs first
            RemoveConfigs();

            IniSection section = null;
            for(int i = 0; i < iniDocument.Sections.Count; i++) {
                section = iniDocument.Sections[i];

                IConfig config = Configs[section.Name];
                if(config == null) {
                    // The section is new so add it
                    config = new ConfigBase(section.Name, this);
                    Configs.Add(config);
                }
                RemoveConfigKeys(config);
            }
        }
示例#7
0
        /// <summary>
        /// Merges the XmlDocument into the Configs when the document is 
        /// reloaded.  
        /// </summary>
        private void MergeDocumentIntoConfigs() {
            // Remove all missing configs first
            RemoveConfigs();

            var sections = GetChildElement("configSections");

            if(sections == null) {
                // There is no configSections node so exit
                return;
            }

            foreach(XmlNode node in sections.ChildNodes) {
                // Find all section nodes
                if(node.NodeType == XmlNodeType.Element && node.Name == "section") {
                    var sectionName = node.Attributes["name"].Value;
                    var config = Configs[sectionName];

                    if(config == null) {
                        // The section is new so add it
                        config = new ConfigBase(sectionName, this);
                        Configs.Add(config);
                    }
                    RemoveConfigKeys(config);
                }
            }
        }
示例#8
0
        /// <summary>
        /// Loads a collection class.
        /// </summary>
        private void LoadCollection(string name, NameValueCollection collection) {
            var config = new ConfigBase(name, this);

            if(collection == null)
                throw new ArgumentException("Section was not found");

            for(var i = 0; i < collection.Count; i++) {
                config.Add(collection.Keys[i], collection[i]);
            }

            Configs.Add(config);
        }
示例#9
0
        /// <summary>
        /// Loads all keys for a config.
        /// </summary>
        private void LoadKeys(XmlNode rootNode, ConfigBase config) {
            var section = GetChildElement(rootNode, config.Name);

            foreach(XmlNode node in section.ChildNodes) {
                if(node.NodeType == XmlNodeType.Element && node.Name == "add") {
                    config.Add(node.Attributes["key"].Value, node.Attributes["value"].Value);
                }
            }
        }
示例#10
0
        /// <summary>
        /// Loads special sections that are not loaded in the configSections
        /// node.  This includes such sections such as appSettings.
        /// </summary>
        private void LoadOtherSection(XmlNode rootNode, string nodeName) {
            var section = GetChildElement(rootNode, nodeName);

            if(section != null) {
                var config = new ConfigBase(section.Name, this);

                Configs.Add(config);
                LoadKeys(rootNode, config);
            }
        }
示例#11
0
        /// <summary>
        /// Loads all configuration sections.
        /// </summary>
        private void LoadSections(XmlNode rootNode) {
            LoadOtherSection(rootNode, "appSettings");

            var sections = GetChildElement(rootNode, "configSections");

            if(sections == null) {
                // There is no configSections node so exit
                return;
            }

            foreach(XmlNode node in sections.ChildNodes) {
                if(node.NodeType == XmlNodeType.Element && node.Name == "section") {
                    var config = new ConfigBase(node.Attributes["name"].Value, this);

                    Configs.Add(config);
                    LoadKeys(rootNode, config);
                }
            }
        }
示例#12
0
        public IConfig Add(string name) {
            ConfigBase result = null;

            if(this[name] == null) {
                result = new ConfigBase(name, owner);
                configList.Add(result);
                OnConfigAdded(new ConfigEventArgs(result));
            }
            else {
                throw new ArgumentException("An IConfig of that name already exists");
            }

            return result;
        }
示例#13
0
        /// <summary>
        /// Merges the XmlDocument into the Configs when the document is 
        /// reloaded.  
        /// </summary>
        private void MergeDocumentIntoConfigs() {
            // Remove all missing configs first
            RemoveConfigs();

            foreach(XmlNode node in configDoc.DocumentElement.ChildNodes) {
                // If node is a section node
                if(node.NodeType == XmlNodeType.Element
                   && node.Name == "Section") {
                    string sectionName = node.Attributes["Name"].Value;
                    IConfig config = Configs[sectionName];
                    if(config == null) {
                        // The section is new so add it
                        config = new ConfigBase(sectionName, this);
                        Configs.Add(config);
                    }
                    RemoveConfigKeys(config);
                }
            }
        }
示例#14
0
 /// <summary>
 /// Loads all keys for a config.
 /// </summary>
 private void LoadKeys(XmlNode node, ConfigBase config) {
     foreach(XmlNode child in node.ChildNodes) {
         if(child.NodeType == XmlNodeType.Element
            && child.Name == "Key") {
             config.Add(child.Attributes["Name"].Value,
                        child.Attributes["Value"].Value);
         }
     }
 }
示例#15
0
        /// <summary>
        /// Loads all configuration sections.
        /// </summary>
        private void LoadSections(XmlNode rootNode) {
            ConfigBase config = null;

            foreach(XmlNode child in rootNode.ChildNodes) {
                if(child.NodeType == XmlNodeType.Element
                   && child.Name == "Section") {
                    config = new ConfigBase(child.Attributes["Name"].Value, this);
                    Configs.Add(config);
                    LoadKeys(child, config);
                }
            }
        }