示例#1
0
        // IConfigurationSectionHandler methods
        Object IConfigurationSectionHandler.Create(Object parent, Object context, XmlNode input)
        {
            // see ASURT 123738
            if (context == null || context.GetType() != typeof(System.Web.Configuration.HttpConfigurationContext))
            {
                return(null);
            }

            ControlsConfig config = new ControlsConfig((ControlsConfig)parent);

            // First step through each attribute on the <mobilecontrols> element
            // and update the ControlsConfig dictionary with it.
            XmlAttributeCollection attributes = input.Attributes;

            foreach (XmlNode attribute in attributes)
            {
                config[attribute.Name] = attribute.Value;
            }

            //check validity of cookielessDataDictionary type
            String cookielessDataDictionaryType = config["cookielessDataDictionaryType"];

            if ((cookielessDataDictionaryType != null) &&
                (cookielessDataDictionaryType != String.Empty))
            {
                Type t = Type.GetType(cookielessDataDictionaryType);
                if (t == null)
                {
                    throw new ConfigurationException(
                              SR.GetString(SR.MobileControlsSectionHandler_TypeNotFound,
                                           cookielessDataDictionaryType,
                                           "IDictionary"),
                              input);
                }
                if (!(typeof(IDictionary).IsAssignableFrom(t)))
                {
                    throw new ConfigurationException(
                              SR.GetString(SR.MobileControlsSectionHandler_NotAssignable,
                                           cookielessDataDictionaryType,
                                           "IDictionary"),
                              input);
                }
            }

            // Iterate through each <device> tag within the config section
            ConfigurationSectionHelper helper = new ConfigurationSectionHelper();

            foreach (XmlNode nextNode in input)
            {
                helper.Node = nextNode;

                if (helper.IsWhitespaceOrComment())
                {
                    continue;
                }

                helper.RejectNonElement();

                // handle <device> tags
                switch (nextNode.Name)
                {
                case "device":
                    String deviceName = helper.RemoveStringAttribute("name", false);

                    IndividualDeviceConfig idc = CreateDeviceConfig(config, helper, deviceName);

                    helper.CheckForUnrecognizedAttributes();

                    // Iterate through every control adapter
                    // within the <device>
                    foreach (XmlNode currentChild in nextNode.ChildNodes)
                    {
                        helper.Node = currentChild;

                        if (helper.IsWhitespaceOrComment())
                        {
                            continue;
                        }

                        helper.RejectNonElement();

                        if (!currentChild.Name.Equals("control"))
                        {
                            throw new ConfigurationException(
                                      SR.GetString(SR.MobileControlsSectionHandler_UnknownElementName, "<control>"),
                                      currentChild);
                        }
                        else
                        {
                            String controlName = helper.RemoveStringAttribute("name", true);
                            String adapterName = helper.RemoveStringAttribute("adapter", true);
                            helper.CheckForUnrecognizedAttributes();

                            idc.AddControl(CheckedGetType(controlName, "control", helper, typeof(Control), currentChild),
                                           CheckedGetType(adapterName, "adapter", helper, typeof(IControlAdapter), currentChild));
                        }

                        helper.Node = null;
                    }

                    // Add complete device config to master configs.
                    if (deviceName == String.Empty || deviceName == null)
                    {
                        deviceName = Guid.NewGuid().ToString();
                    }

                    if (!config.AddDeviceConfig(deviceName, idc))
                    {
                        // Problem is due to a duplicated name
                        throw new ConfigurationException(
                                  SR.GetString(SR.MobileControlsSectionHandler_DuplicatedDeviceName, deviceName),
                                  nextNode);
                    }

                    helper.Node = null;
                    break;

                default:
                    throw new ConfigurationException(
                              SR.GetString(SR.MobileControlsSectionHandler_UnknownElementName, "<device>"),
                              nextNode);
                }
            }

            config.FixupDeviceConfigInheritance(input);

            return(config);
        }