// 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;
            
        }
        // Helper method to encapsulate type lookup followed by
        // throwing a ConfigurationErrorsException on failure.
        private Type CheckedGetType(String typename,
                                    String whereUsed,
                                    ConfigurationSectionHelper helper,
                                    Type typeImplemented,
                                    XmlNode input)
        {
            Type t = Type.GetType(typename);
            if (t == null) {
                throw new ConfigurationErrorsException(
                    SR.GetString(SR.MobileControlsSectionHandler_TypeNotFound,
                                 typename,
                                 whereUsed),
                    helper.Node);
            }

            if (typeImplemented != null && !typeImplemented.IsAssignableFrom(t)) {
                if (input != null) {
                    throw new ConfigurationErrorsException(
                        SR.GetString(SR.MobileControlsSectionHandler_NotAssignable,
                                     t,
                                     typeImplemented),
                        input);
                }
                else {
                    throw new ConfigurationErrorsException(
                        SR.GetString(SR.MobileControlsSectionHandler_NotAssignable,
                                     t,
                                     typeImplemented),
                        helper.Node);
                }
            }

            return t;
        }
Exemplo n.º 3
0
        // IConfigurationSectionHandler Methods
        Object IConfigurationSectionHandler.Create(Object parent, Object context,
                                                   XmlNode node)
        {
            // see ASURT 123738
            if (context == null || context.GetType() != typeof(System.Web.Configuration.HttpConfigurationContext))
            {
                return(null);
            }

            DeviceFilterDictionary currentFilterDictionary;

            if (parent == null)
            {
                currentFilterDictionary = new DeviceFilterDictionary();
            }
            else
            {
                currentFilterDictionary = new DeviceFilterDictionary(
                    (DeviceFilterDictionary)parent);
            }

            ConfigurationSectionHelper helper = new ConfigurationSectionHelper();

            foreach (XmlNode child in node.ChildNodes)
            {
                helper.Node = child;

                // skip whitespace and comments
                if (helper.IsWhitespaceOrComment())
                {
                    continue;
                }

                // reject nonelements
                helper.RejectNonElement();

                // handle <filter> tags

                if (child.Name.Equals("filter"))
                {
                    String name      = helper.RemoveStringAttribute("name", true);
                    String className = helper.RemoveStringAttribute("type", false);

                    if (className != null)
                    {
                        const String methodAttributeName = "method";
                        String       methodName          = helper.RemoveStringAttribute(methodAttributeName, false);
                        String       capabilityName      = helper.RemoveStringAttribute("compare", false);
                        String       argumentValue       = helper.RemoveStringAttribute("argument", false);

                        helper.CheckForUnrecognizedAttributes();

                        if (className == String.Empty)
                        {
                            throw new
                                  ConfigurationException(SR.GetString(SR.DevCapSect_EmptyClass), child);
                        }

                        if (methodName == null)
                        {
                            throw new
                                  ConfigurationException(SR.GetString(SR.ConfigSect_MissingAttr, methodAttributeName), child);
                        }

                        if (methodName == String.Empty)
                        {
                            throw new
                                  ConfigurationException(SR.GetString(SR.ConfigSect_MissingValue, methodAttributeName), child);
                        }

                        if (capabilityName != null || argumentValue != null)
                        {
                            String msg;
                            if (capabilityName != null)
                            {
                                msg = SR.GetString(SR.DevCapSect_ExtraCompareDelegator);
                            }
                            else
                            {
                                msg = SR.GetString(SR.DevCapSect_ExtraArgumentDelegator);
                            }

                            throw new ConfigurationException(msg, child);
                        }

                        MobileCapabilities.EvaluateCapabilitiesDelegate evaluator;

                        Type evaluatorClass = Type.GetType(className);

                        if (null == evaluatorClass)
                        {
                            String msg =
                                SR.GetString(SR.DevCapSect_NoTypeInfo, className);
                            throw new ConfigurationException(msg, child);
                        }

                        try
                        {
                            evaluator =
                                (MobileCapabilities.EvaluateCapabilitiesDelegate)
                                MobileCapabilities.EvaluateCapabilitiesDelegate.CreateDelegate(
                                    typeof(MobileCapabilities.EvaluateCapabilitiesDelegate),
                                    evaluatorClass, methodName);
                        }
                        catch (Exception e)
                        {
                            String msg =
                                SR.GetString(SR.DevCapSect_NoCapabilityEval,
                                             methodName, e.Message);
                            throw new ConfigurationException(msg, child);
                        }

                        currentFilterDictionary.AddCapabilityDelegate(name, evaluator);
                    }
                    else
                    {
                        String capabilityName = helper.RemoveStringAttribute("compare", false);
                        String argumentValue  = helper.RemoveStringAttribute("argument", false);
                        String methodName     = helper.RemoveStringAttribute("method", false);

                        helper.CheckForUnrecognizedAttributes();

                        if ((capabilityName == null) || (capabilityName == String.Empty))
                        {
                            throw new ConfigurationException(
                                      SR.GetString(SR.DevCapSect_MustSpecify),
                                      child);
                        }

                        if (methodName != null)
                        {
                            throw new ConfigurationException(
                                      SR.GetString(SR.DevCapSect_ComparisonAlreadySpecified),
                                      child);
                        }

                        try
                        {
                            currentFilterDictionary.AddComparisonDelegate(name, capabilityName, argumentValue);
                        }
                        catch (Exception e)
                        {
                            String msg = SR.GetString(SR.DevCapSect_UnableAddDelegate,
                                                      name, e.Message);
                            throw new ConfigurationException(msg, child);
                        }
                    }
                }
                else
                {
                    String msg = SR.GetString(SR.DevCapSect_UnrecognizedTag,
                                              child.Name);
                    throw new ConfigurationException(msg, child);
                }

                helper.Node = null;
            }

            return(currentFilterDictionary);
        }
        // Helper to create a device config given the names of methods
        private IndividualDeviceConfig CreateDeviceConfig(ControlsConfig config,
                                                          ConfigurationSectionHelper helper,
                                                          String deviceName)
        {
            String nameOfDeviceToInheritFrom =
                helper.RemoveStringAttribute("inheritsFrom", false);

            if (nameOfDeviceToInheritFrom != null && nameOfDeviceToInheritFrom.Length == 0) {
                nameOfDeviceToInheritFrom = null;
            }
            
            bool propertiesRequired = nameOfDeviceToInheritFrom == null;

            String predicateClass = helper.RemoveStringAttribute("predicateClass", propertiesRequired);
            // If a predicate class is specified, so must a method.
            String predicateMethod = helper.RemoveStringAttribute("predicateMethod", predicateClass != null);
            String pageAdapterClass = helper.RemoveStringAttribute("pageAdapter", propertiesRequired);

            IndividualDeviceConfig.DeviceQualifiesDelegate predicateDelegate = null;
            if (predicateClass != null || predicateMethod != null)
            {
                Type predicateClassType = CheckedGetType(predicateClass, "PredicateClass", helper, null, null);
                try
                {
                    predicateDelegate =
                        (IndividualDeviceConfig.DeviceQualifiesDelegate)
                        IndividualDeviceConfig.DeviceQualifiesDelegate.CreateDelegate(
                            typeof(IndividualDeviceConfig.DeviceQualifiesDelegate),
                            predicateClassType,
                            predicateMethod);
                }
                catch
                {
                    throw new ConfigurationErrorsException(
                        SR.GetString(SR.MobileControlsSectionHandler_CantCreateMethodOnClass,
                                     predicateMethod, predicateClassType.FullName),
                        helper.Node);
                }
            }
                    
            Type pageAdapterType = null;
            if (pageAdapterClass != null)
            {
                pageAdapterType = CheckedGetType(pageAdapterClass, "PageAdapterClass", helper, typeof(IPageAdapter), null);
            }

            return new IndividualDeviceConfig(config,
                                              deviceName,
                                              predicateDelegate,
                                              pageAdapterType,
                                              nameOfDeviceToInheritFrom);
        }
        protected object Create(Object parent, Object context, XmlNode node) {
            // see ASURT 123738
            if (context == null || context.GetType() != typeof(System.Web.Configuration.HttpConfigurationContext)) {
                return null;
            }
            
            DeviceFilterDictionary currentFilterDictionary;
            
            if(parent == null)
            {
                currentFilterDictionary = new DeviceFilterDictionary();
            }
            else
            {
                currentFilterDictionary = new DeviceFilterDictionary(
                    (DeviceFilterDictionary)parent);
            }

            ConfigurationSectionHelper helper = new ConfigurationSectionHelper();
            foreach(XmlNode child in node.ChildNodes)
            {
                helper.Node = child;

                // skip whitespace and comments
                if(helper.IsWhitespaceOrComment())
                {
                    continue;
                }
                    
                // reject nonelements
                helper.RejectNonElement();

                // handle <filter> tags

                if(child.Name.Equals("filter"))
                {
                    String name = helper.RemoveStringAttribute("name", true);
                    String className = helper.RemoveStringAttribute("type", false);

                    if(className != null)
                    {
                        const String methodAttributeName = "method";
                        String methodName = helper.RemoveStringAttribute(methodAttributeName, false);
                        String capabilityName = helper.RemoveStringAttribute("compare", false);
                        String argumentValue = helper.RemoveStringAttribute("argument", false);

                        helper.CheckForUnrecognizedAttributes();

                        if(className == String.Empty)
                        {
                            throw new
                                ConfigurationErrorsException(SR.GetString(SR.DevCapSect_EmptyClass), child);
                        }

                        if(methodName == null)
                        {
                            throw new
                                ConfigurationErrorsException(SR.GetString(SR.ConfigSect_MissingAttr, methodAttributeName), child);
                        }

                        if(methodName == String.Empty)
                        {
                            throw new
                                ConfigurationErrorsException(SR.GetString(SR.ConfigSect_MissingValue, methodAttributeName), child);
                        }

                        if(capabilityName != null || argumentValue != null)
                        {
                            String msg;
                            if (capabilityName != null)
                            {
                                msg = SR.GetString(SR.DevCapSect_ExtraCompareDelegator);
                            }
                            else
                            {
                                msg = SR.GetString(SR.DevCapSect_ExtraArgumentDelegator);
                            }
                            
                            throw new ConfigurationErrorsException(msg, child);
                        }

                        MobileCapabilities.EvaluateCapabilitiesDelegate evaluator;

                        Type evaluatorClass = Type.GetType(className);

                        if(null == evaluatorClass)
                        {
                            String msg =
                                SR.GetString(SR.DevCapSect_NoTypeInfo, className);
                            throw new ConfigurationErrorsException(msg, child);
                        }

                        try
                        {
                            evaluator =
                                (MobileCapabilities.EvaluateCapabilitiesDelegate)
                                MobileCapabilities.EvaluateCapabilitiesDelegate.CreateDelegate(
                                    typeof(MobileCapabilities.EvaluateCapabilitiesDelegate),
                                    evaluatorClass, methodName);
                        }
                        catch(Exception e)
                        {
                            String msg =
                                SR.GetString(SR.DevCapSect_NoCapabilityEval,
                                             methodName, e.Message);
                            throw new ConfigurationErrorsException(msg, child);
                        }

                        currentFilterDictionary.AddCapabilityDelegate(name, evaluator);
                    }
                    else
                    {
                        String capabilityName = helper.RemoveStringAttribute("compare", false);
                        String argumentValue = helper.RemoveStringAttribute("argument", false);
                        String methodName = helper.RemoveStringAttribute("method", false);                        

                        helper.CheckForUnrecognizedAttributes();

                        if(String.IsNullOrEmpty(capabilityName))
                        {
                            throw new ConfigurationErrorsException(
                                SR.GetString(SR.DevCapSect_MustSpecify),
                                child);
                        }

                        if(methodName != null)
                        {
                            throw new ConfigurationErrorsException(
                                SR.GetString(SR.DevCapSect_ComparisonAlreadySpecified),
                                child);
                        }

                        try
                        {
                            currentFilterDictionary.AddComparisonDelegate(name, capabilityName, argumentValue);
                        }
                        catch(Exception e)
                        {
                            String msg = SR.GetString(SR.DevCapSect_UnableAddDelegate,
                                                      name, e.Message);
                            throw new ConfigurationErrorsException(msg, child);
                        }
                    }                    
                }
                else
                {
                    String msg = SR.GetString(SR.DevCapSect_UnrecognizedTag,
                                              child.Name);
                    throw new ConfigurationErrorsException(msg, child);
                }

                helper.Node = null;
            }
   
            return currentFilterDictionary;
        }