// 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); }
private static void AddControlAdapters(IndividualDeviceConfig deviceConfig, DeviceElement device) { foreach (ControlElement control in device.Controls) { deviceConfig.AddControl(control.Control, control.Adapter); } }
internal /*public*/ IndividualDeviceConfig GetDeviceConfig(String configName) { IndividualDeviceConfig deviceConfig = (IndividualDeviceConfig)_deviceConfigs[configName]; if (deviceConfig == null && _parent != null) { deviceConfig = _parent.GetDeviceConfig(configName); } return(deviceConfig); }
internal /*public*/ void FixupInheritance(IndividualDeviceConfig referrer, XmlNode configNode) { if (_fixup == FixupState.FixedUp) { return; } if (_fixup == FixupState.FixingUp) { Debug.Assert(referrer != null); // Circular reference throw new Exception(SR.GetString(SR.MobileControlsSectionHandler_CircularReference, referrer.Name)); } _fixup = FixupState.FixingUp; if (ParentConfigName != null) { Debug.Assert(ParentConfigName.Length != 0 && ParentConfig == null); ParentConfig = _controlsConfig.GetDeviceConfig(ParentConfigName); if (ParentConfig == null) { throw new ConfigurationErrorsException( SR.GetString(SR.MobileControlsSectionHandler_DeviceConfigNotFound, ParentConfigName), configNode); } // Make sure parent is fixed up. ParentConfig.FixupInheritance(this, configNode); if (PageAdapterType == null) { PageAdapterType = ParentConfig.PageAdapterType; } if (DeviceQualifiesPredicate == null) { DeviceQualifiesPredicate = ParentConfig.DeviceQualifiesPredicate; } Debug.Assert(PageAdapterType != null); Debug.Assert(DeviceQualifiesPredicate != null); // Reset this since we don't need it any longer. ParentConfigName = null; } _fixup = FixupState.FixedUp; }
// This constructor takes both a delegate that chooses this // device, and a Type to instantiate the appropriate page // adapter with. internal IndividualDeviceConfig(ControlsConfig controlsConfig, String name, DeviceQualifiesDelegate deviceQualifiesDelegate, Type pageAdapterType, String parentConfigName) { _controlsConfig = controlsConfig; _name = name; _deviceQualifiesPredicate = deviceQualifiesDelegate; _parentConfigName = parentConfigName; _parentConfig = null; PageAdapterType = pageAdapterType; }
// Return false if a device of the same name has already been added. internal /*public*/ bool AddDeviceConfig(String configName, IndividualDeviceConfig deviceConfig) { // Note that GetDeviceConfig also walks the parents configs if (GetDeviceConfig(configName) != null) { return false; } else { _deviceConfigs[configName] = deviceConfig; return true; } }
// Return false if a device of the same name has already been added. internal /*public*/ bool AddDeviceConfig(String configName, IndividualDeviceConfig deviceConfig) { // Note that GetDeviceConfig also walks the parents configs if (GetDeviceConfig(configName) != null) { return(false); } else { _deviceConfigs[configName] = deviceConfig; return(true); } }
internal /*public*/ IndividualDeviceConfig GetDeviceConfig(HttpContext context) { IndividualDeviceConfig deviceConfig = null; #if DEBUG if (context.Session != null) { String var = "AdapterOverride"; bool saveInSession = true; String adapterOverride = (String)context.Session[var]; if (adapterOverride == null) { saveInSession = false; adapterOverride = (String)context.Request.QueryString[var]; } if (adapterOverride != null && (deviceConfig = GetDeviceConfig(adapterOverride)) != null) { if (saveInSession) { context.Session[var] = adapterOverride; } return(deviceConfig); } } #endif foreach (IndividualDeviceConfig candidate in _deviceConfigs.Values) { if (candidate.DeviceQualifies(context)) { deviceConfig = candidate; break; } } if (deviceConfig == null && _parent != null) { deviceConfig = _parent.GetDeviceConfig(context); } if (deviceConfig == null) { throw new Exception( SR.GetString(SR.ControlsConfig_NoDeviceConfigRegistered, context.Request.UserAgent)); } return(deviceConfig); }
// 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); }