// Essentially this method does what MobileControlSectionHandler.Create()
        // does, but use MobileControlsSection for retrieving config data instead
        internal static ControlsConfig CreateControlsConfig(MobileControlsSection controlSection) {
            ControlsConfig config = new ControlsConfig(null);

            config["sessionStateHistorySize"] = controlSection.SessionStateHistorySize.ToString(CultureInfo.InvariantCulture);
            config["cookielessDataDictionaryType"] = controlSection.CookielessDataDictionaryType.AssemblyQualifiedName;
            config["allowCustomAttributes"] = controlSection.AllowCustomAttributes.ToString(CultureInfo.InvariantCulture);

            foreach (DeviceElement device in controlSection.Devices) {
                IndividualDeviceConfig deviceConfig = CreateDeviceConfig(config, device);
                AddControlAdapters(deviceConfig, device);

                if (!config.AddDeviceConfig(device.Name, deviceConfig)) {
                    // Problem is due to a duplicated name
                    throw new ConfigurationErrorsException(
                        SR.GetString(SR.MobileControlsSectionHandler_DuplicatedDeviceName, device.Name));
                }
            }

            // Passing null means no config file and line number info will be
            // shown when error happens.  That is because there is no XmlNode of
            // the config section is available when MobileControlsSection is
            // used.  But the error messages raised should still be good enough.
            config.FixupDeviceConfigInheritance(null);

            return config;
        }
        // Essentially this method does what MobileControlSectionHandler.Create()
        // does, but use MobileControlsSection for retrieving config data instead
        internal static ControlsConfig CreateControlsConfig(MobileControlsSection controlSection)
        {
            ControlsConfig config = new ControlsConfig(null);

            config["sessionStateHistorySize"]      = controlSection.SessionStateHistorySize.ToString(CultureInfo.InvariantCulture);
            config["cookielessDataDictionaryType"] = controlSection.CookielessDataDictionaryType.AssemblyQualifiedName;
            config["allowCustomAttributes"]        = controlSection.AllowCustomAttributes.ToString(CultureInfo.InvariantCulture);

            foreach (DeviceElement device in controlSection.Devices)
            {
                IndividualDeviceConfig deviceConfig = CreateDeviceConfig(config, device);
                AddControlAdapters(deviceConfig, device);

                if (!config.AddDeviceConfig(device.Name, deviceConfig))
                {
                    // Problem is due to a duplicated name
                    throw new ConfigurationErrorsException(
                              SR.GetString(SR.MobileControlsSectionHandler_DuplicatedDeviceName, device.Name));
                }
            }

            // Passing null means no config file and line number info will be
            // shown when error happens.  That is because there is no XmlNode of
            // the config section is available when MobileControlsSection is
            // used.  But the error messages raised should still be good enough.
            config.FixupDeviceConfigInheritance(null);

            return(config);
        }
Exemplo n.º 3
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);
        }
        // IConfigurationSectionHandler methods
        /// <internalonly/>
        protected Object 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 (!String.IsNullOrEmpty(cookielessDataDictionaryType)) {
                Type t = Type.GetType(cookielessDataDictionaryType);
                if (t == null)  
                {
                    throw new ConfigurationErrorsException(
                        SR.GetString(SR.MobileControlsSectionHandler_TypeNotFound,
                                 cookielessDataDictionaryType,
                                 "IDictionary"),
                        input);
                }
                if (!(typeof(IDictionary).IsAssignableFrom(t)))
                {
                    throw new ConfigurationErrorsException(
                        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 ConfigurationErrorsException(
                                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(MobileControl), currentChild),
                                           CheckedGetType(adapterName, "adapter", helper, typeof(IControlAdapter), currentChild));

                        }

                        helper.Node = null;
                    }

                    // Add complete device config to master configs.
                    if (String.IsNullOrEmpty(deviceName)) {
                        deviceName = Guid.NewGuid().ToString();
                    }
                    
                    if (!config.AddDeviceConfig(deviceName, idc))
                    {
                        // Problem is due to a duplicated name
                        throw new ConfigurationErrorsException(
                            SR.GetString(SR.MobileControlsSectionHandler_DuplicatedDeviceName, deviceName),
                            nextNode);
                        
                    }
                    
                    helper.Node = null;
                    break;
                default:
                    throw new ConfigurationErrorsException(
                        SR.GetString(SR.MobileControlsSectionHandler_UnknownElementName, "<device>"),
                        nextNode);
                }
            }

            config.FixupDeviceConfigInheritance(input);

            return config;
            
        }