Пример #1
0
        // Create a configuration object for a section.
        public virtual Object Create
            (Object parent, Object configContext, XmlNode section)
        {
            Hashtable coll;
            String    key, value;

            // Create the name/value collection for the result.
            if (parent != null)
            {
                coll = (Hashtable)(((Hashtable)parent).Clone());
            }
            else
            {
                coll = CollectionsUtil.CreateCaseInsensitiveHashtable();
            }

            // Must not be any attributes remaining on the section node.
            if (section.Attributes.Count != 0)
            {
                throw new ConfigurationException
                          (S._("Config_UnrecognizedAttribute"), section);
            }

            // Process the child nodes.
            foreach (XmlNode node in section.ChildNodes)
            {
                // Ignore comments and white space.
                if (node.NodeType == XmlNodeType.Comment ||
                    node.NodeType == XmlNodeType.Whitespace ||
                    node.NodeType == XmlNodeType.SignificantWhitespace)
                {
                    continue;
                }

                // Must be an element node.
                if (node.NodeType != XmlNodeType.Element)
                {
                    throw new ConfigurationException
                              (S._("Config_MustBeElement"), node);
                }

                // Process "add", "remove", and "clear" child tags.
                if (node.Name == "add")
                {
                    key = NameValueSectionHandler.GetRequiredAttribute
                              (node, KeyAttributeName, 1);
                    value = NameValueSectionHandler.GetAttribute
                                (node, ValueAttributeName, 0);
                    coll[key] = value;
                }
                else if (node.Name == "remove")
                {
                    key = NameValueSectionHandler.GetRequiredAttribute
                              (node, KeyAttributeName, 0);
                    coll.Remove(key);
                }
                else if (node.Name == "clear")
                {
                    if (node.Attributes.Count != 0)
                    {
                        throw new ConfigurationException
                                  (S._("Config_UnrecognizedAttribute"), node);
                    }
                    coll.Clear();
                }
                else
                {
                    throw new ConfigurationException
                              (S._("Config_NotRecognized"), node);
                }
            }

            // Return the final collection
            return(coll);
        }